File: attribute_parser_test.rb

package info (click to toggle)
ruby-haml 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,004 kB
  • sloc: ruby: 9,908; sh: 23; makefile: 11
file content (101 lines) | stat: -rw-r--r-- 4,967 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
describe Haml::AttributeParser do
  describe '.parse' do
    def assert_parse(expected, haml)
      actual = Haml::AttributeParser.parse(haml)
      if expected.nil?
        assert_nil actual
      else
        assert_equal expected, actual
      end
    end

    it { assert_parse({}, '') }
    it { assert_parse({}, '{}') }

    describe 'invalid hash' do
      it { assert_parse(nil, ' hash ') }
      it { assert_parse(nil, 'hash, foo: bar') }
      it { assert_parse(nil, ' {hash} ') }
      it { assert_parse(nil, ' { hash, foo: bar } ') }
    end

    describe 'dynamic key' do
      it { assert_parse(nil, 'foo => bar') }
      it { assert_parse(nil, '[] => bar') }
      it { assert_parse(nil, '[1,2,3] => bar') }
    end

    describe 'foo: bar' do
      it { assert_parse({ '_' => '1' }, '_:1,') }
      it { assert_parse({ 'foo' => 'bar' }, ' foo:  bar ') }
      it { assert_parse({ 'a' => 'b', 'c' => ':d' }, 'a: b, c: :d') }
      it { assert_parse({ 'a' => '[]', 'c' => '"d"' }, 'a: [], c: "d"') }
      it { assert_parse({ '_' => '1' }, ' { _:1, } ') }
      it { assert_parse({ 'foo' => 'bar' }, ' {  foo:  bar } ') }
      it { assert_parse({ 'a' => 'b', 'c' => ':d' }, ' { a: b, c: :d } ') }
      it { assert_parse({ 'a' => '[]', 'c' => '"d"' }, ' { a: [], c: "d" } ') }
    end

    describe ':foo => bar' do
      it { assert_parse({ 'foo' => ':bar' }, '  :foo   =>  :bar  ') }
      it { assert_parse({ '_' => '"foo"' }, ':_=>"foo"') }
      it { assert_parse({ 'a' => '[]', 'c' => '""', 'b' => '"#{3}"' }, ':a => [], c: "", :b => "#{3}"') }
      it { assert_parse({ 'foo' => ':bar' }, ' {   :foo   =>  :bar } ') }
      it { assert_parse({ '_' => '"foo"' }, ' { :_=>"foo" } ') }
      it { assert_parse({ 'a' => '[]', 'c' => '""', 'b' => '"#{3}"' }, ' { :a => [], c: "", :b => "#{3}" } ') }
      it { assert_parse(nil, ':"f#{o}o" => bar') }
      it { assert_parse(nil, ':"#{f}oo" => bar') }
      it { assert_parse(nil, ':"#{foo}" => bar') }
    end

    describe '"foo" => bar' do
      it { assert_parse({ 'foo' => '[1]' }, '"foo"=>[1]') }
      it { assert_parse({ 'foo' => 'nya' }, " 'foo' => nya ") }
      it { assert_parse({ 'foo' => 'bar' }, '%q[foo] => bar ') }
      it { assert_parse({ 'foo' => '[1]' }, ' { "foo"=>[1] } ') }
      it { assert_parse({ 'foo' => 'nya' }, " {  'foo' => nya } ") }
      it { assert_parse({ 'foo' => 'bar' }, ' { %q[foo] => bar } ') }
      it { assert_parse(nil, '"f#{o}o" => bar') }
      it { assert_parse(nil, '"#{f}oo" => bar') }
      it { assert_parse(nil, '"#{foo}" => bar') }
      it { assert_parse({ 'f#{o}o' => 'bar' }, '%q[f#{o}o] => bar ') }
      it { assert_parse({ 'f#{o}o' => 'bar' }, ' { %q[f#{o}o] => bar,  } ') }
      it { assert_parse(nil, '%Q[f#{o}o] => bar ') }
    end

    if RUBY_VERSION >= '2.2.0'
      describe '"foo": bar' do
        it { assert_parse({ 'foo' => '()' }, '"foo":()') }
        it { assert_parse({ 'foo' => 'nya' }, " 'foo': nya ") }
        it { assert_parse({ 'foo' => '()' }, ' { "foo":() , }') }
        it { assert_parse({ 'foo' => 'nya' }, " {  'foo': nya , }") }
        it { assert_parse(nil, '"f#{o}o": bar') }
        it { assert_parse(nil, '"#{f}oo": bar') }
        it { assert_parse(nil, '"#{foo}": bar') }
      end
    end

    describe 'nested array' do
      it { assert_parse({ 'foo' => '[1,2,]' }, 'foo: [1,2,],') }
      it { assert_parse({ 'foo' => '[1,2,[3,4],5]' }, 'foo: [1,2,[3,4],5],') }
      it { assert_parse({ 'foo' => '[1,2,[3,4],5]', 'bar' => '[[1,2],]'}, 'foo: [1,2,[3,4],5],bar: [[1,2],],') }
      it { assert_parse({ 'foo' => '[1,2,]' }, ' { foo: [1,2,], } ') }
      it { assert_parse({ 'foo' => '[1,2,[3,4],5]' }, ' { foo: [1,2,[3,4],5], } ') }
      it { assert_parse({ 'foo' => '[1,2,[3,4],5]', 'bar' => '[[1,2],]'}, ' { foo: [1,2,[3,4],5],bar: [[1,2],], } ') }
    end

    describe 'nested hash' do
      it { assert_parse({ 'foo' => '{ }', 'bar' => '{}' }, 'foo: { }, bar: {}') }
      it { assert_parse({ 'foo' => '{ bar: baz, hoge: fuga, }' }, 'foo: { bar: baz, hoge: fuga, }, ') }
      it { assert_parse({ 'data' => '{ confirm: true, disable: false }', 'hello' => '{ world: foo, }' }, 'data: { confirm: true, disable: false }, :hello => { world: foo, },') }
      it { assert_parse({ 'foo' => '{ }', 'bar' => '{}' }, ' { foo: { }, bar: {} } ') }
      it { assert_parse({ 'foo' => '{ bar: baz, hoge: fuga, }' }, ' { foo: { bar: baz, hoge: fuga, }, } ') }
      it { assert_parse({ 'data' => '{ confirm: true, disable: false }', 'hello' => '{ world: foo, }' }, ' { data: { confirm: true, disable: false }, :hello => { world: foo, }, } ') }
    end

    describe 'nested method' do
      it { assert_parse({ 'foo' => 'bar(a, b)', 'hoge' => 'piyo(a, b,)' }, 'foo: bar(a, b), hoge: piyo(a, b,),') }
      it { assert_parse({ 'foo' => 'bar(a, b)', 'hoge' => 'piyo(a, b,)' }, ' { foo: bar(a, b), hoge: piyo(a, b,), } ') }
    end
  end if RUBY_ENGINE != 'truffleruby' # truffleruby doesn't have Ripper.lex
end