Skip to content

Commit

Permalink
We can support Ruby 1.9/Javascript style hashes, as well as ruby ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyatom committed Nov 24, 2009
1 parent e86acbd commit 42f3809
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -25,7 +25,7 @@ if Object.const_defined?(:Gem)

# Change these as appropriate
s.name = "vanilla"
s.version = "1.9.15"
s.version = "1.9.15.1"
s.summary = "A bliki-type web content thing."
s.author = "James Adam"
s.email = "james@lazyatom.com.com"
Expand Down
9 changes: 7 additions & 2 deletions lib/vanilla/snip_reference.treetop
Expand Up @@ -23,9 +23,14 @@ grammar SnipReference
end

rule hash_argument
word_or_symbol optional_spaces "=>" optional_spaces word
word_or_symbol optional_spaces hash_argument_separator optional_spaces word
end


rule hash_argument_separator
"=>" /
":"
end

rule word_or_symbol
[:]? word
end
Expand Down
53 changes: 53 additions & 0 deletions test/snip_parser_test.rb
@@ -0,0 +1,53 @@
require File.join(File.dirname(__FILE__), *%w[test_helper])
Treetop.load File.join(File.dirname(__FILE__), *%w[.. lib vanilla snip_reference])

class SnipReferenceParserTest < Test::Unit::TestCase
examples = {
%|{snip}| => {:snip => 'snip', :attribute => nil, :arguments => []},
%|{snip argument}| => {:snip => 'snip', :attribute => nil, :arguments => ["argument"]},
%|{"snip with spaces"}| => {:snip => 'snip with spaces', :attribute => nil, :arguments => []},
%|{snip-with-dashes}| => {:snip => 'snip-with-dashes', :attribute => nil, :arguments => []},
%|{snip_with_underscores}| => {:snip => 'snip_with_underscores', :attribute => nil, :arguments => []},
%|{"snip with spaces" argument}| => {:snip => 'snip with spaces', :attribute => nil, :arguments => ['argument']},
%|{'snip with spaces' argument}| => {:snip => 'snip with spaces', :attribute => nil, :arguments => ['argument']},
%|{snip "argument with spaces"}| => {:snip => 'snip', :attribute => nil, :arguments => ['argument with spaces']},
%|{snip 'argument with spaces'}| => {:snip => 'snip', :attribute => nil, :arguments => ['argument with spaces']},
%|{snip arg1,arg2}| => {:snip => 'snip', :attribute => nil, :arguments => ['arg1', 'arg2']},
%|{snip arg1, arg2}| => {:snip => 'snip', :attribute => nil, :arguments => ['arg1', 'arg2']},
%|{snip "argument with spaces", arg2}| => {:snip => 'snip', :attribute => nil, :arguments => ['argument with spaces', 'arg2']},
%|{"snip with spaces" arg1, arg2}| => {:snip => 'snip with spaces', :attribute => nil, :arguments => ['arg1', 'arg2']},
%|{snip.snip_attribute}| => {:snip => 'snip', :attribute => 'snip_attribute', :arguments => []},
%|{snip."spaced attribute"}| => {:snip => 'snip', :attribute => 'spaced attribute', :arguments => []},
%|{"snip with spaces".attribute}| => {:snip => 'snip with spaces', :attribute => 'attribute', :arguments => []},
%|{snip.snip_attribute arg}| => {:snip => 'snip', :attribute => 'snip_attribute', :arguments => ['arg']},
%|{snip arg with spaces}| => {:snip => 'snip', :attribute => nil, :arguments => ['arg with spaces']},
%|{snip arg with spaces, another arg}| => {:snip => 'snip', :attribute => nil, :arguments => ['arg with spaces', 'another arg']},
%|{snip key1=>value1, key2 => value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
%|{snip.attribute key1=>value1}| => {:snip => 'snip', :attribute => 'attribute', :arguments => {:key1 => 'value1'}},
%|{snip key1 => value1, key2 => value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
%|{snip :key1 => value1, :key2 => value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
%|{snip key1 => "value with spaces"}| => {:snip => 'snip', :arguments => {:key1 => "value with spaces"}},
# %|{snip "key with spaces" => value} | => {:snip => 'snip', :arguments => {:"key with spaces" => "value"}},
%|{snip key1:value1,key2:value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
%|{snip key1:value1, key2:value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
%|{snip key1: value1, key2: value2}| => {:snip => 'snip', :arguments => {:key1 => 'value1', :key2 => 'value2'}},
%|{snip key1:"value with spaces"}| => {:snip => 'snip', :arguments => {:key1 => 'value with spaces'}}
}

def setup
@parser = SnipReferenceParser.new
end

examples.each do |example, expected|
define_method :"test_parsing_#{example}" do
reference = @parser.parse(example)
if reference
assert_equal expected[:snip], reference.snip
assert_equal expected[:attribute], reference.attribute
assert_equal expected[:arguments], reference.arguments
else
flunk "failed to parse: #{example}"
end
end
end
end
2 changes: 1 addition & 1 deletion vanilla.gemspec
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = %q{vanilla}
s.version = "1.9.15"
s.version = "1.9.15.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["James Adam"]
Expand Down

0 comments on commit 42f3809

Please sign in to comment.