Parse hash rocket

This commit is contained in:
Takashi Kokubun 2015-11-01 20:08:04 +09:00
parent 6a4bb90022
commit 88c698b74c
2 changed files with 38 additions and 7 deletions

View File

@ -28,6 +28,13 @@ module Hamlit
parse_key!(tokens) parse_key!(tokens)
when :on_label when :on_label
str.tr(':', '') str.tr(':', '')
when :on_symbeg
_, _, key = tokens.shift
until tokens.empty?
_, type, str = tokens.shift
break if type == :on_op && str == '=>'
end
key
else else
raise InternalError.new("Unexpected type: #{type}") raise InternalError.new("Unexpected type: #{type}")
end end

View File

@ -8,20 +8,44 @@ describe Hamlit::HashParser do
assert_parse(' hash ', nil) assert_parse(' hash ', nil)
end end
it 'returns nil for partially valid expression' do
assert_parse('hash, foo: bar', nil)
end
it 'parses static hash content' do it 'parses static hash content' do
assert_parse('', {}) assert_parse('', {})
end end
it 'parses static hash content' do describe 'foo: bar' do
assert_parse('_:b,', { '_' => 'b' }) it 'parses static hash content' do
assert_parse('_:1,', { '_' => '1' })
end
it 'parses static hash content' do
assert_parse(' foo: bar ', { 'foo' => 'bar' })
end
it 'parses static hash content' do
assert_parse('a: b, c: :d', { 'a' => 'b', 'c' => ':d' })
end
it 'parses static hash content' do
assert_parse('a: [], c: "d"', { 'a' => '[]', 'c' => '"d"' })
end
end end
it 'parses static hash content' do describe ':foo => bar' do
assert_parse(' foo: bar ', { 'foo' => 'bar' }) it 'parses static hash content' do
end assert_parse(' :foo => :bar ', { 'foo' => ':bar' })
end
it 'parses static hash content' do it 'parses static hash content' do
assert_parse('a: b, c: d', { 'a' => 'b', 'c' => 'd' }) assert_parse(':_=>"foo"', { '_' => '"foo"' })
end
it 'parses static hash content' do
assert_parse(':a => [], c: "", :b => "#{3}"', { 'a' => '[]', 'c' => '""', 'b' => '"#{3}"' })
end
end end
end end
end end