1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

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)
when :on_label
str.tr(':', '')
when :on_symbeg
_, _, key = tokens.shift
until tokens.empty?
_, type, str = tokens.shift
break if type == :on_op && str == '=>'
end
key
else
raise InternalError.new("Unexpected type: #{type}")
end

View file

@ -8,20 +8,44 @@ describe Hamlit::HashParser do
assert_parse(' hash ', nil)
end
it 'returns nil for partially valid expression' do
assert_parse('hash, foo: bar', nil)
end
it 'parses static hash content' do
assert_parse('', {})
end
it 'parses static hash content' do
assert_parse('_:b,', { '_' => 'b' })
describe 'foo: bar' do
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
it 'parses static hash content' do
assert_parse(' foo: bar ', { 'foo' => 'bar' })
end
describe ':foo => bar' do
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' })
it 'parses static hash content' do
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