ripper_ruby_parser 0.0.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/{README.rdoc → README.md} +17 -17
- data/Rakefile +3 -1
- data/lib/ripper_ruby_parser/{commenting_sexp_builder.rb → commenting_ripper_parser.rb} +22 -6
- data/lib/ripper_ruby_parser/parser.rb +3 -18
- data/lib/ripper_ruby_parser/sexp_handlers/arrays.rb +2 -1
- data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +6 -10
- data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +37 -60
- data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +31 -25
- data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +18 -8
- data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +22 -18
- data/lib/ripper_ruby_parser/sexp_handlers/loops.rb +12 -23
- data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +16 -8
- data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +41 -15
- data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +40 -23
- data/lib/ripper_ruby_parser/sexp_processor.rb +38 -19
- data/lib/ripper_ruby_parser/version.rb +1 -1
- data/test/pt_testcase/pt_test.rb +15 -1
- data/test/test_helper.rb +6 -3
- data/test/unit/commenting_ripper_parser_test.rb +121 -0
- data/test/unit/parser_assignment_test.rb +23 -24
- data/test/unit/parser_blocks_test.rb +207 -35
- data/test/unit/parser_conditionals_test.rb +251 -9
- data/test/unit/parser_literals_test.rb +348 -8
- data/test/unit/parser_loops_test.rb +20 -21
- data/test/unit/parser_method_calls_test.rb +132 -8
- data/test/unit/parser_operators_test.rb +97 -7
- data/test/unit/parser_test.rb +631 -1231
- data/test/unit/sexp_processor_test.rb +26 -28
- metadata +28 -38
- data/test/unit/commenting_sexp_builder_test.rb +0 -113
@@ -6,46 +6,45 @@ describe RipperRubyParser::Parser do
|
|
6
6
|
it "works in the single-line postfix case" do
|
7
7
|
"foo while bar".
|
8
8
|
must_be_parsed_as s(:while,
|
9
|
-
s(:call, nil, :bar
|
10
|
-
s(:call, nil, :foo
|
9
|
+
s(:call, nil, :bar),
|
10
|
+
s(:call, nil, :foo), true)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "works in the block postfix case" do
|
14
14
|
"begin; foo; end while bar".
|
15
15
|
must_be_parsed_as s(:while,
|
16
|
-
s(:call, nil, :bar
|
17
|
-
s(:call, nil, :foo
|
16
|
+
s(:call, nil, :bar),
|
17
|
+
s(:call, nil, :foo), false)
|
18
18
|
end
|
19
19
|
|
20
|
-
it "
|
20
|
+
it "handles a negative condition" do
|
21
21
|
"while not foo; bar; end".
|
22
|
-
must_be_parsed_as s(:
|
23
|
-
s(:call, nil, :foo,
|
24
|
-
s(:call, nil, :bar
|
22
|
+
must_be_parsed_as s(:while,
|
23
|
+
s(:call, s(:call, nil, :foo), :!),
|
24
|
+
s(:call, nil, :bar), true)
|
25
25
|
end
|
26
26
|
|
27
|
-
it "
|
27
|
+
it "handles a negative condition in the postfix case" do
|
28
28
|
"foo while not bar".
|
29
|
-
must_be_parsed_as s(:
|
30
|
-
s(:call, nil, :bar,
|
31
|
-
s(:call, nil, :foo
|
29
|
+
must_be_parsed_as s(:while,
|
30
|
+
s(:call, s(:call, nil, :bar), :!),
|
31
|
+
s(:call, nil, :foo), true)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
describe "for the until statement" do
|
36
|
-
it "
|
36
|
+
it "handles a negative condition" do
|
37
37
|
"until not foo; bar; end".
|
38
|
-
must_be_parsed_as s(:
|
39
|
-
s(:call, nil, :foo,
|
40
|
-
s(:call, nil, :bar
|
41
|
-
|
38
|
+
must_be_parsed_as s(:until,
|
39
|
+
s(:call, s(:call, nil, :foo), :!),
|
40
|
+
s(:call, nil, :bar), true)
|
42
41
|
end
|
43
42
|
|
44
|
-
it "
|
43
|
+
it "handles a negative condition in the postfix case" do
|
45
44
|
"foo until not bar".
|
46
|
-
must_be_parsed_as s(:
|
47
|
-
s(:call, nil, :bar,
|
48
|
-
s(:call, nil, :foo
|
45
|
+
must_be_parsed_as s(:until,
|
46
|
+
s(:call, s(:call, nil, :bar), :!),
|
47
|
+
s(:call, nil, :foo), true)
|
49
48
|
end
|
50
49
|
end
|
51
50
|
end
|
@@ -2,25 +2,149 @@ require File.expand_path('../test_helper.rb', File.dirname(__FILE__))
|
|
2
2
|
|
3
3
|
describe RipperRubyParser::Parser do
|
4
4
|
describe "#parse" do
|
5
|
+
describe "for method calls" do
|
6
|
+
describe "without a receiver" do
|
7
|
+
it "works without brackets" do
|
8
|
+
"foo bar".
|
9
|
+
must_be_parsed_as s(:call, nil, :foo,
|
10
|
+
s(:call, nil, :bar))
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works with brackets" do
|
14
|
+
"foo(bar)".
|
15
|
+
must_be_parsed_as s(:call, nil, :foo,
|
16
|
+
s(:call, nil, :bar))
|
17
|
+
end
|
18
|
+
|
19
|
+
it "works with an empty parameter list and no brackets" do
|
20
|
+
"foo".
|
21
|
+
must_be_parsed_as s(:call, nil, :foo)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "works with brackets around an empty parameter list" do
|
25
|
+
"foo()".
|
26
|
+
must_be_parsed_as s(:call, nil, :foo)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "works for methods ending in a question mark" do
|
30
|
+
"foo?".
|
31
|
+
must_be_parsed_as s(:call, nil, :foo?)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "works with nested calls without brackets" do
|
35
|
+
"foo bar baz".
|
36
|
+
must_be_parsed_as s(:call, nil, :foo,
|
37
|
+
s(:call, nil, :bar,
|
38
|
+
s(:call, nil, :baz)))
|
39
|
+
end
|
40
|
+
|
41
|
+
it "works with a non-final splat argument" do
|
42
|
+
"foo(bar, *baz, qux)".
|
43
|
+
must_be_parsed_as s(:call,
|
44
|
+
nil,
|
45
|
+
:foo,
|
46
|
+
s(:call, nil, :bar),
|
47
|
+
s(:splat, s(:call, nil, :baz)),
|
48
|
+
s(:call, nil, :qux))
|
49
|
+
end
|
50
|
+
|
51
|
+
it "works with a splat argument followed by several regular arguments" do
|
52
|
+
"foo(bar, *baz, qux, quuz)".
|
53
|
+
must_be_parsed_as s(:call,
|
54
|
+
nil,
|
55
|
+
:foo,
|
56
|
+
s(:call, nil, :bar),
|
57
|
+
s(:splat, s(:call, nil, :baz)),
|
58
|
+
s(:call, nil, :qux),
|
59
|
+
s(:call, nil, :quuz))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "with a receiver" do
|
64
|
+
it "works without brackets" do
|
65
|
+
"foo.bar baz".
|
66
|
+
must_be_parsed_as s(:call,
|
67
|
+
s(:call, nil, :foo),
|
68
|
+
:bar,
|
69
|
+
s(:call, nil, :baz))
|
70
|
+
end
|
71
|
+
|
72
|
+
it "works with brackets" do
|
73
|
+
"foo.bar(baz)".
|
74
|
+
must_be_parsed_as s(:call,
|
75
|
+
s(:call, nil, :foo),
|
76
|
+
:bar,
|
77
|
+
s(:call, nil, :baz))
|
78
|
+
end
|
79
|
+
|
80
|
+
it "works with brackets around a call with no brackets" do
|
81
|
+
"foo.bar(baz qux)".
|
82
|
+
must_be_parsed_as s(:call,
|
83
|
+
s(:call, nil, :foo),
|
84
|
+
:bar,
|
85
|
+
s(:call, nil, :baz,
|
86
|
+
s(:call, nil, :qux)))
|
87
|
+
end
|
88
|
+
|
89
|
+
it "works with nested calls without brackets" do
|
90
|
+
"foo.bar baz qux".
|
91
|
+
must_be_parsed_as s(:call,
|
92
|
+
s(:call, nil, :foo),
|
93
|
+
:bar,
|
94
|
+
s(:call, nil, :baz,
|
95
|
+
s(:call, nil, :qux)))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "with blocks" do
|
100
|
+
it "works for a do block" do
|
101
|
+
"foo.bar do baz; end".
|
102
|
+
must_be_parsed_as s(:iter,
|
103
|
+
s(:call,
|
104
|
+
s(:call, nil, :foo),
|
105
|
+
:bar),
|
106
|
+
s(:args),
|
107
|
+
s(:call, nil, :baz))
|
108
|
+
end
|
109
|
+
|
110
|
+
it "works for a do block with several statements" do
|
111
|
+
"foo.bar do baz; qux; end".
|
112
|
+
must_be_parsed_as s(:iter,
|
113
|
+
s(:call,
|
114
|
+
s(:call, nil, :foo),
|
115
|
+
:bar),
|
116
|
+
s(:args),
|
117
|
+
s(:block,
|
118
|
+
s(:call, nil, :baz),
|
119
|
+
s(:call, nil, :qux)))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
5
124
|
describe "for calls to super" do
|
6
125
|
specify { "super".must_be_parsed_as s(:zsuper) }
|
7
126
|
specify { "super foo".must_be_parsed_as s(:super,
|
8
|
-
s(:call, nil, :foo
|
127
|
+
s(:call, nil, :foo)) }
|
9
128
|
specify {
|
10
129
|
"super foo, bar".must_be_parsed_as s(:super,
|
11
|
-
s(:call, nil, :foo
|
12
|
-
s(:call, nil, :bar
|
130
|
+
s(:call, nil, :foo),
|
131
|
+
s(:call, nil, :bar)) }
|
13
132
|
specify {
|
14
133
|
"super foo, *bar".must_be_parsed_as s(:super,
|
15
|
-
s(:call, nil, :foo
|
134
|
+
s(:call, nil, :foo),
|
16
135
|
s(:splat,
|
17
|
-
s(:call, nil, :bar
|
136
|
+
s(:call, nil, :bar))) }
|
18
137
|
specify {
|
19
138
|
"super foo, *bar, &baz".
|
20
139
|
must_be_parsed_as s(:super,
|
21
|
-
s(:call, nil, :foo
|
22
|
-
s(:splat, s(:call, nil, :bar
|
23
|
-
s(:block_pass, s(:call, nil, :baz
|
140
|
+
s(:call, nil, :foo),
|
141
|
+
s(:splat, s(:call, nil, :bar)),
|
142
|
+
s(:block_pass, s(:call, nil, :baz))) }
|
143
|
+
end
|
144
|
+
|
145
|
+
it "handles calling a proc" do
|
146
|
+
"foo.()".
|
147
|
+
must_be_parsed_as s(:call, s(:call, nil, :foo), :call)
|
24
148
|
end
|
25
149
|
end
|
26
150
|
end
|
@@ -6,23 +6,113 @@ describe RipperRubyParser::Parser do
|
|
6
6
|
specify do
|
7
7
|
"foo !~ bar".must_be_parsed_as s(:not,
|
8
8
|
s(:call,
|
9
|
-
s(:call, nil, :foo
|
9
|
+
s(:call, nil, :foo),
|
10
10
|
:=~,
|
11
|
-
s(:
|
12
|
-
s(:call, nil, :bar, s(:arglist)))))
|
11
|
+
s(:call, nil, :bar)))
|
13
12
|
end
|
14
13
|
end
|
15
14
|
|
16
15
|
describe "for boolean operators" do
|
16
|
+
it "handles :and" do
|
17
|
+
"foo and bar".
|
18
|
+
must_be_parsed_as s(:and,
|
19
|
+
s(:call, nil, :foo),
|
20
|
+
s(:call, nil, :bar))
|
21
|
+
end
|
22
|
+
|
23
|
+
it "handles double :and" do
|
24
|
+
"foo and bar and baz".
|
25
|
+
must_be_parsed_as s(:and,
|
26
|
+
s(:call, nil, :foo),
|
27
|
+
s(:and,
|
28
|
+
s(:call, nil, :bar),
|
29
|
+
s(:call, nil, :baz)))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "handles :or" do
|
33
|
+
"foo or bar".
|
34
|
+
must_be_parsed_as s(:or,
|
35
|
+
s(:call, nil, :foo),
|
36
|
+
s(:call, nil, :bar))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "handles double :or" do
|
40
|
+
"foo or bar or baz".
|
41
|
+
must_be_parsed_as s(:or,
|
42
|
+
s(:call, nil, :foo),
|
43
|
+
s(:or,
|
44
|
+
s(:call, nil, :bar),
|
45
|
+
s(:call, nil, :baz)))
|
46
|
+
end
|
47
|
+
|
48
|
+
it "handles :or after :and" do
|
49
|
+
"foo and bar or baz".
|
50
|
+
must_be_parsed_as s(:or,
|
51
|
+
s(:and,
|
52
|
+
s(:call, nil, :foo),
|
53
|
+
s(:call, nil, :bar)),
|
54
|
+
s(:call, nil, :baz))
|
55
|
+
end
|
56
|
+
|
57
|
+
it "handles :and after :or" do
|
58
|
+
"foo or bar and baz".
|
59
|
+
must_be_parsed_as s(:and,
|
60
|
+
s(:or,
|
61
|
+
s(:call, nil, :foo),
|
62
|
+
s(:call, nil, :bar)),
|
63
|
+
s(:call, nil, :baz))
|
64
|
+
end
|
65
|
+
|
66
|
+
it "converts :&& to :and" do
|
67
|
+
"foo && bar".
|
68
|
+
must_be_parsed_as s(:and,
|
69
|
+
s(:call, nil, :foo),
|
70
|
+
s(:call, nil, :bar))
|
71
|
+
end
|
72
|
+
|
73
|
+
it "handles :|| after :&&" do
|
74
|
+
"foo && bar || baz".
|
75
|
+
must_be_parsed_as s(:or,
|
76
|
+
s(:and,
|
77
|
+
s(:call, nil, :foo),
|
78
|
+
s(:call, nil, :bar)),
|
79
|
+
s(:call, nil, :baz))
|
80
|
+
end
|
81
|
+
|
82
|
+
it "handles :&& after :||" do
|
83
|
+
"foo || bar && baz".
|
84
|
+
must_be_parsed_as s(:or,
|
85
|
+
s(:call, nil, :foo),
|
86
|
+
s(:and,
|
87
|
+
s(:call, nil, :bar),
|
88
|
+
s(:call, nil, :baz)))
|
89
|
+
end
|
90
|
+
|
91
|
+
it "handles bracketed :||" do
|
92
|
+
"(foo || bar) || baz".
|
93
|
+
must_be_parsed_as s(:or,
|
94
|
+
s(:or,
|
95
|
+
s(:call, nil, :foo),
|
96
|
+
s(:call, nil, :bar)),
|
97
|
+
s(:call, nil, :baz))
|
98
|
+
end
|
99
|
+
|
100
|
+
it "converts :|| to :or" do
|
101
|
+
"foo || bar".
|
102
|
+
must_be_parsed_as s(:or,
|
103
|
+
s(:call, nil, :foo),
|
104
|
+
s(:call, nil, :bar))
|
105
|
+
end
|
106
|
+
|
17
107
|
it "handles triple :and" do
|
18
108
|
"foo and bar and baz and qux".
|
19
109
|
must_be_parsed_as s(:and,
|
20
|
-
s(:call, nil, :foo
|
110
|
+
s(:call, nil, :foo),
|
21
111
|
s(:and,
|
22
|
-
s(:call, nil, :bar
|
112
|
+
s(:call, nil, :bar),
|
23
113
|
s(:and,
|
24
|
-
s(:call, nil, :baz
|
25
|
-
s(:call, nil, :qux
|
114
|
+
s(:call, nil, :baz),
|
115
|
+
s(:call, nil, :qux))))
|
26
116
|
end
|
27
117
|
end
|
28
118
|
end
|
data/test/unit/parser_test.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
require File.expand_path('../test_helper.rb', File.dirname(__FILE__))
|
2
3
|
|
3
4
|
describe RipperRubyParser::Parser do
|
@@ -29,1127 +30,543 @@ describe RipperRubyParser::Parser do
|
|
29
30
|
|
30
31
|
describe "for a class declaration" do
|
31
32
|
it "works with a namespaced class name" do
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
s(:scope))
|
33
|
+
"class Foo::Bar; end".
|
34
|
+
must_be_parsed_as s(:class,
|
35
|
+
s(:colon2, s(:const, :Foo), :Bar),
|
36
|
+
nil)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "works for singleton classes" do
|
40
|
-
"class << self; end".must_be_parsed_as s(:sclass, s(:self)
|
40
|
+
"class << self; end".must_be_parsed_as s(:sclass, s(:self))
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
describe "for a module declaration" do
|
45
45
|
it "works with a namespaced module name" do
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
s(:scope))
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "for if" do
|
54
|
-
it "works in the postfix case" do
|
55
|
-
result = parser.parse "foo if bar"
|
56
|
-
result.must_equal s(:if,
|
57
|
-
s(:call, nil, :bar, s(:arglist)),
|
58
|
-
s(:call, nil, :foo, s(:arglist)),
|
59
|
-
nil)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "works in the block case" do
|
63
|
-
result = parser.parse "if foo; bar; end"
|
64
|
-
result.must_equal s(:if,
|
65
|
-
s(:call, nil, :foo, s(:arglist)),
|
66
|
-
s(:call, nil, :bar, s(:arglist)),
|
67
|
-
nil)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "works with an else clause" do
|
71
|
-
result = parser.parse "if foo; bar; else; baz; end"
|
72
|
-
result.must_equal s(:if,
|
73
|
-
s(:call, nil, :foo, s(:arglist)),
|
74
|
-
s(:call, nil, :bar, s(:arglist)),
|
75
|
-
s(:call, nil, :baz, s(:arglist)))
|
76
|
-
end
|
77
|
-
|
78
|
-
it "works with an elsif clause" do
|
79
|
-
result = parser.parse "if foo; bar; elsif baz; qux; end"
|
80
|
-
result.must_equal s(:if,
|
81
|
-
s(:call, nil, :foo, s(:arglist)),
|
82
|
-
s(:call, nil, :bar, s(:arglist)),
|
83
|
-
s(:if,
|
84
|
-
s(:call, nil, :baz, s(:arglist)),
|
85
|
-
s(:call, nil, :qux, s(:arglist)),
|
86
|
-
nil))
|
87
|
-
end
|
88
|
-
|
89
|
-
it "handles a negative condition correctly" do
|
90
|
-
result = parser.parse "if not foo; bar; end"
|
91
|
-
result.must_equal s(:if,
|
92
|
-
s(:call, nil, :foo, s(:arglist)),
|
93
|
-
nil,
|
94
|
-
s(:call, nil, :bar, s(:arglist)))
|
95
|
-
end
|
96
|
-
|
97
|
-
it "handles a negative condition in elsif correctly" do
|
98
|
-
result = parser.parse "if foo; bar; elsif not baz; qux; end"
|
99
|
-
result.must_equal s(:if,
|
100
|
-
s(:call, nil, :foo, s(:arglist)),
|
101
|
-
s(:call, nil, :bar, s(:arglist)),
|
102
|
-
s(:if,
|
103
|
-
s(:not, s(:call, nil, :baz, s(:arglist))),
|
104
|
-
s(:call, nil, :qux, s(:arglist)), nil))
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "for unless" do
|
109
|
-
it "works in the postfix case" do
|
110
|
-
result = parser.parse "foo unless bar"
|
111
|
-
result.must_equal s(:if,
|
112
|
-
s(:call, nil, :bar, s(:arglist)),
|
113
|
-
nil,
|
114
|
-
s(:call, nil, :foo, s(:arglist)))
|
115
|
-
end
|
116
|
-
|
117
|
-
it "works in the block case" do
|
118
|
-
result = parser.parse "unless bar; foo; end"
|
119
|
-
result.must_equal s(:if,
|
120
|
-
s(:call, nil, :bar, s(:arglist)),
|
121
|
-
nil,
|
122
|
-
s(:call, nil, :foo, s(:arglist)))
|
123
|
-
end
|
124
|
-
|
125
|
-
it "works with an else clause" do
|
126
|
-
result = parser.parse "unless foo; bar; else; baz; end"
|
127
|
-
result.must_equal s(:if,
|
128
|
-
s(:call, nil, :foo, s(:arglist)),
|
129
|
-
s(:call, nil, :baz, s(:arglist)),
|
130
|
-
s(:call, nil, :bar, s(:arglist)))
|
46
|
+
"module Foo::Bar; end".
|
47
|
+
must_be_parsed_as s(:module,
|
48
|
+
s(:colon2, s(:const, :Foo), :Bar))
|
131
49
|
end
|
132
50
|
end
|
133
51
|
|
134
|
-
describe "for
|
135
|
-
it "works with
|
136
|
-
|
137
|
-
result.must_equal s(:case,
|
138
|
-
s(:call, nil, :foo, s(:arglist)),
|
139
|
-
s(:when,
|
140
|
-
s(:array, s(:call, nil, :bar, s(:arglist))),
|
141
|
-
s(:call, nil, :baz, s(:arglist))),
|
142
|
-
nil)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "works with multiple when clauses" do
|
146
|
-
result = parser.parse "case foo; when bar; baz; when qux; quux; end"
|
147
|
-
result.must_equal s(:case,
|
148
|
-
s(:call, nil, :foo, s(:arglist)),
|
149
|
-
s(:when,
|
150
|
-
s(:array, s(:call, nil, :bar, s(:arglist))),
|
151
|
-
s(:call, nil, :baz, s(:arglist))),
|
152
|
-
s(:when,
|
153
|
-
s(:array, s(:call, nil, :qux, s(:arglist))),
|
154
|
-
s(:call, nil, :quux, s(:arglist))),
|
155
|
-
nil)
|
156
|
-
end
|
157
|
-
|
158
|
-
it "works with multiple statements in the when block" do
|
159
|
-
result = parser.parse "case foo; when bar; baz; qux; end"
|
160
|
-
result.must_equal s(:case,
|
161
|
-
s(:call, nil, :foo, s(:arglist)),
|
162
|
-
s(:when,
|
163
|
-
s(:array, s(:call, nil, :bar, s(:arglist))),
|
164
|
-
s(:block,
|
165
|
-
s(:call, nil, :baz, s(:arglist)),
|
166
|
-
s(:call, nil, :qux, s(:arglist)))),
|
167
|
-
nil)
|
168
|
-
end
|
169
|
-
|
170
|
-
it "works with an else clause" do
|
171
|
-
result = parser.parse "case foo; when bar; baz; else; qux; end"
|
172
|
-
result.must_equal s(:case,
|
173
|
-
s(:call, nil, :foo, s(:arglist)),
|
174
|
-
s(:when,
|
175
|
-
s(:array, s(:call, nil, :bar, s(:arglist))),
|
176
|
-
s(:call, nil, :baz, s(:arglist))),
|
177
|
-
s(:call, nil, :qux, s(:arglist)))
|
52
|
+
describe "for empty brackets" do
|
53
|
+
it "works with lone ()" do
|
54
|
+
"()".must_be_parsed_as s(:nil)
|
178
55
|
end
|
179
56
|
end
|
180
57
|
|
181
58
|
describe "for the return statement" do
|
182
59
|
it "works with no arguments" do
|
183
|
-
|
184
|
-
|
60
|
+
"return".
|
61
|
+
must_be_parsed_as s(:return)
|
185
62
|
end
|
186
63
|
|
187
64
|
it "works with one argument" do
|
188
|
-
|
189
|
-
|
190
|
-
|
65
|
+
"return foo".
|
66
|
+
must_be_parsed_as s(:return,
|
67
|
+
s(:call, nil, :foo))
|
191
68
|
end
|
192
69
|
|
193
70
|
it "works with a splat argument" do
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
71
|
+
"return *foo".
|
72
|
+
must_be_parsed_as s(:return,
|
73
|
+
s(:svalue,
|
74
|
+
s(:splat,
|
75
|
+
s(:call, nil, :foo))))
|
199
76
|
end
|
200
77
|
|
201
78
|
it "works with multiple arguments" do
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
79
|
+
"return foo, bar".
|
80
|
+
must_be_parsed_as s(:return,
|
81
|
+
s(:array,
|
82
|
+
s(:call, nil, :foo),
|
83
|
+
s(:call, nil, :bar)))
|
207
84
|
end
|
208
85
|
|
209
86
|
it "works with a regular argument and a splat argument" do
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
87
|
+
"return foo, *bar".
|
88
|
+
must_be_parsed_as s(:return,
|
89
|
+
s(:array,
|
90
|
+
s(:call, nil, :foo),
|
91
|
+
s(:splat,
|
92
|
+
s(:call, nil, :bar))))
|
216
93
|
end
|
217
94
|
end
|
218
95
|
|
219
96
|
describe "for the until statement" do
|
220
97
|
it "works in the prefix block case with do" do
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
98
|
+
"until foo do; bar; end".
|
99
|
+
must_be_parsed_as s(:until,
|
100
|
+
s(:call, nil, :foo),
|
101
|
+
s(:call, nil, :bar), true)
|
225
102
|
end
|
226
103
|
|
227
104
|
it "works in the prefix block case without do" do
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
105
|
+
"until foo; bar; end".
|
106
|
+
must_be_parsed_as s(:until,
|
107
|
+
s(:call, nil, :foo),
|
108
|
+
s(:call, nil, :bar), true)
|
232
109
|
end
|
233
110
|
|
234
111
|
it "works in the single-line postfix case" do
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
112
|
+
"foo until bar".
|
113
|
+
must_be_parsed_as s(:until,
|
114
|
+
s(:call, nil, :bar),
|
115
|
+
s(:call, nil, :foo), true)
|
239
116
|
end
|
240
117
|
|
241
118
|
it "works in the block postfix case" do
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
119
|
+
"begin; foo; end until bar".
|
120
|
+
must_be_parsed_as s(:until,
|
121
|
+
s(:call, nil, :bar),
|
122
|
+
s(:call, nil, :foo), false)
|
246
123
|
end
|
247
124
|
end
|
248
125
|
|
249
126
|
describe "for the while statement" do
|
250
127
|
it "works with do" do
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
128
|
+
"while foo do; bar; end".
|
129
|
+
must_be_parsed_as s(:while,
|
130
|
+
s(:call, nil, :foo),
|
131
|
+
s(:call, nil, :bar), true)
|
255
132
|
end
|
256
133
|
|
257
134
|
it "works without do" do
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
135
|
+
"while foo; bar; end".
|
136
|
+
must_be_parsed_as s(:while,
|
137
|
+
s(:call, nil, :foo),
|
138
|
+
s(:call, nil, :bar), true)
|
262
139
|
end
|
263
140
|
end
|
264
141
|
|
265
142
|
describe "for the for statement" do
|
266
143
|
it "works with do" do
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
144
|
+
"for foo in bar do; baz; end".
|
145
|
+
must_be_parsed_as s(:for,
|
146
|
+
s(:call, nil, :bar),
|
147
|
+
s(:lasgn, :foo),
|
148
|
+
s(:call, nil, :baz))
|
272
149
|
end
|
273
150
|
|
274
151
|
it "works without do" do
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
152
|
+
"for foo in bar; baz; end".
|
153
|
+
must_be_parsed_as s(:for,
|
154
|
+
s(:call, nil, :bar),
|
155
|
+
s(:lasgn, :foo),
|
156
|
+
s(:call, nil, :baz))
|
157
|
+
end
|
158
|
+
|
159
|
+
it "works with an empty body" do
|
160
|
+
"for foo in bar; end".
|
161
|
+
must_be_parsed_as s(:for,
|
162
|
+
s(:call, nil, :bar),
|
163
|
+
s(:lasgn, :foo))
|
280
164
|
end
|
281
165
|
end
|
282
166
|
|
283
167
|
describe "for a begin..end block" do
|
284
168
|
it "works with no statements" do
|
285
|
-
|
286
|
-
|
169
|
+
"begin; end".
|
170
|
+
must_be_parsed_as s(:nil)
|
287
171
|
end
|
288
172
|
|
289
173
|
it "works with one statement" do
|
290
|
-
|
291
|
-
|
174
|
+
"begin; foo; end".
|
175
|
+
must_be_parsed_as s(:call, nil, :foo)
|
292
176
|
end
|
293
177
|
|
294
178
|
it "works with multiple statements" do
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
describe "for the rescue statement" do
|
303
|
-
it "works with empty main and rescue bodies" do
|
304
|
-
result = parser.parse "begin; rescue; end"
|
305
|
-
result.must_equal s(:rescue,
|
306
|
-
s(:resbody, s(:array), nil))
|
307
|
-
end
|
308
|
-
|
309
|
-
it "works with single statement main and rescue bodies" do
|
310
|
-
result = parser.parse "begin; foo; rescue; bar; end"
|
311
|
-
result.must_equal s(:rescue,
|
312
|
-
s(:call, nil, :foo, s(:arglist)),
|
313
|
-
s(:resbody,
|
314
|
-
s(:array),
|
315
|
-
s(:call, nil, :bar, s(:arglist))))
|
316
|
-
end
|
317
|
-
|
318
|
-
it "works with multi-statement main and rescue bodies" do
|
319
|
-
result = parser.parse "begin; foo; bar; rescue; baz; qux; end"
|
320
|
-
result.must_equal s(:rescue,
|
321
|
-
s(:block,
|
322
|
-
s(:call, nil, :foo, s(:arglist)),
|
323
|
-
s(:call, nil, :bar, s(:arglist))),
|
324
|
-
s(:resbody,
|
325
|
-
s(:array),
|
326
|
-
s(:block,
|
327
|
-
s(:call, nil, :baz, s(:arglist)),
|
328
|
-
s(:call, nil, :qux, s(:arglist)))))
|
329
|
-
end
|
330
|
-
|
331
|
-
it "works with assignment to an error variable" do
|
332
|
-
result = parser.parse "begin; foo; rescue => e; bar; end"
|
333
|
-
result.must_equal s(:rescue,
|
334
|
-
s(:call, nil, :foo, s(:arglist)),
|
335
|
-
s(:resbody,
|
336
|
-
s(:array, s(:lasgn, :e, s(:gvar, :$!))),
|
337
|
-
s(:call, nil, :bar, s(:arglist))))
|
338
|
-
end
|
339
|
-
|
340
|
-
it "works with filtering of the exception type" do
|
341
|
-
result = parser.parse "begin; foo; rescue Bar; baz; end"
|
342
|
-
result.must_equal s(:rescue,
|
343
|
-
s(:call, nil, :foo, s(:arglist)),
|
344
|
-
s(:resbody,
|
345
|
-
s(:array, s(:const, :Bar)),
|
346
|
-
s(:call, nil, :baz, s(:arglist))))
|
347
|
-
end
|
348
|
-
|
349
|
-
it "works with filtering of the exception type and assignment to an error variable" do
|
350
|
-
result = parser.parse "begin; foo; rescue Bar => e; baz; end"
|
351
|
-
result.must_equal s(:rescue,
|
352
|
-
s(:call, nil, :foo, s(:arglist)),
|
353
|
-
s(:resbody,
|
354
|
-
s(:array,
|
355
|
-
s(:const, :Bar),
|
356
|
-
s(:lasgn, :e, s(:gvar, :$!))),
|
357
|
-
s(:call, nil, :baz, s(:arglist))))
|
358
|
-
end
|
359
|
-
|
360
|
-
it "works rescuing multiple exception types" do
|
361
|
-
result = parser.parse "begin; foo; rescue Bar, Baz; qux; end"
|
362
|
-
result.must_equal s(:rescue,
|
363
|
-
s(:call, nil, :foo, s(:arglist)),
|
364
|
-
s(:resbody,
|
365
|
-
s(:array, s(:const, :Bar), s(:const, :Baz)),
|
366
|
-
s(:call, nil, :qux, s(:arglist))))
|
367
|
-
end
|
368
|
-
|
369
|
-
it "works in the postfix case" do
|
370
|
-
result = parser.parse "foo rescue bar"
|
371
|
-
result.must_equal s(:rescue,
|
372
|
-
s(:call, nil, :foo, s(:arglist)),
|
373
|
-
s(:resbody,
|
374
|
-
s(:array),
|
375
|
-
s(:call, nil, :bar, s(:arglist))))
|
376
|
-
end
|
377
|
-
|
378
|
-
it "works in a plain method body" do
|
379
|
-
result = parser.parse "def foo; bar; rescue; baz; end"
|
380
|
-
result.must_equal s(:defn,
|
381
|
-
:foo,
|
382
|
-
s(:args),
|
383
|
-
s(:scope,
|
384
|
-
s(:block,
|
385
|
-
s(:rescue,
|
386
|
-
s(:call, nil, :bar, s(:arglist)),
|
387
|
-
s(:resbody,
|
388
|
-
s(:array),
|
389
|
-
s(:call, nil, :baz, s(:arglist)))))))
|
390
|
-
end
|
391
|
-
end
|
392
|
-
|
393
|
-
describe "for the ensure statement" do
|
394
|
-
it "works with single statement main and ensure bodies" do
|
395
|
-
result = parser.parse "begin; foo; ensure; bar; end"
|
396
|
-
result.must_equal s(:ensure,
|
397
|
-
s(:call, nil, :foo, s(:arglist)),
|
398
|
-
s(:call, nil, :bar, s(:arglist)))
|
399
|
-
end
|
400
|
-
|
401
|
-
it "works with multi-statement main and ensure bodies" do
|
402
|
-
result = parser.parse "begin; foo; bar; ensure; baz; qux; end"
|
403
|
-
result.must_equal s(:ensure,
|
404
|
-
s(:block,
|
405
|
-
s(:call, nil, :foo, s(:arglist)),
|
406
|
-
s(:call, nil, :bar, s(:arglist))),
|
407
|
-
s(:block,
|
408
|
-
s(:call, nil, :baz, s(:arglist)),
|
409
|
-
s(:call, nil, :qux, s(:arglist))))
|
410
|
-
end
|
411
|
-
|
412
|
-
it "works together with rescue" do
|
413
|
-
result = parser.parse "begin; foo; rescue; bar; ensure; baz; end"
|
414
|
-
result.must_equal s(:ensure,
|
415
|
-
s(:rescue,
|
416
|
-
s(:call, nil, :foo, s(:arglist)),
|
417
|
-
s(:resbody,
|
418
|
-
s(:array),
|
419
|
-
s(:call, nil, :bar, s(:arglist)))),
|
420
|
-
s(:call, nil, :baz, s(:arglist)))
|
179
|
+
"begin; foo; bar; end".
|
180
|
+
must_be_parsed_as s(:block,
|
181
|
+
s(:call, nil, :foo),
|
182
|
+
s(:call, nil, :bar))
|
421
183
|
end
|
422
184
|
end
|
423
185
|
|
424
186
|
describe "for the undef statement" do
|
425
187
|
it "works with a single bareword identifier" do
|
426
|
-
|
427
|
-
|
188
|
+
"undef foo".
|
189
|
+
must_be_parsed_as s(:undef, s(:lit, :foo))
|
428
190
|
end
|
429
191
|
|
430
192
|
it "works with a single symbol" do
|
431
|
-
|
432
|
-
|
193
|
+
"undef :foo".
|
194
|
+
must_be_parsed_as s(:undef, s(:lit, :foo))
|
433
195
|
end
|
434
196
|
|
435
197
|
it "works with multiple bareword identifiers" do
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
198
|
+
"undef foo, bar".
|
199
|
+
must_be_parsed_as s(:block,
|
200
|
+
s(:undef, s(:lit, :foo)),
|
201
|
+
s(:undef, s(:lit, :bar)))
|
440
202
|
end
|
441
203
|
|
442
204
|
it "works with multiple bareword symbols" do
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
205
|
+
"undef :foo, :bar".
|
206
|
+
must_be_parsed_as s(:block,
|
207
|
+
s(:undef, s(:lit, :foo)),
|
208
|
+
s(:undef, s(:lit, :bar)))
|
447
209
|
end
|
448
210
|
end
|
449
211
|
|
450
212
|
describe "for the alias statement" do
|
451
213
|
it "works with regular barewords" do
|
452
|
-
|
453
|
-
|
454
|
-
|
214
|
+
"alias foo bar".
|
215
|
+
must_be_parsed_as s(:alias,
|
216
|
+
s(:lit, :foo), s(:lit, :bar))
|
455
217
|
end
|
456
218
|
|
457
219
|
it "works with symbols" do
|
458
|
-
|
459
|
-
|
460
|
-
|
220
|
+
"alias :foo :bar".
|
221
|
+
must_be_parsed_as s(:alias,
|
222
|
+
s(:lit, :foo), s(:lit, :bar))
|
461
223
|
end
|
462
224
|
|
463
225
|
it "works with operator barewords" do
|
464
|
-
|
465
|
-
|
466
|
-
|
226
|
+
"alias + -".
|
227
|
+
must_be_parsed_as s(:alias,
|
228
|
+
s(:lit, :+), s(:lit, :-))
|
229
|
+
end
|
230
|
+
|
231
|
+
it "works with global variables" do
|
232
|
+
"alias $foo $bar".
|
233
|
+
must_be_parsed_as s(:valias, :$foo, :$bar)
|
467
234
|
end
|
468
235
|
end
|
469
236
|
|
470
237
|
describe "for arguments" do
|
471
238
|
it "works for a simple case with splat" do
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
s(:splat, s(:call, nil, :bar, s(:arglist)))))
|
239
|
+
"foo *bar".
|
240
|
+
must_be_parsed_as s(:call,
|
241
|
+
nil,
|
242
|
+
:foo,
|
243
|
+
s(:splat, s(:call, nil, :bar)))
|
478
244
|
end
|
479
245
|
|
480
246
|
it "works for a multi-argument case with splat" do
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
s(:call, nil, :
|
487
|
-
s(:splat, s(:call, nil, :baz, s(:arglist)))))
|
247
|
+
"foo bar, *baz".
|
248
|
+
must_be_parsed_as s(:call,
|
249
|
+
nil,
|
250
|
+
:foo,
|
251
|
+
s(:call, nil, :bar),
|
252
|
+
s(:splat, s(:call, nil, :baz)))
|
488
253
|
end
|
489
254
|
|
490
255
|
it "works for a simple case passing a block" do
|
491
|
-
|
492
|
-
|
493
|
-
s(:arglist,
|
256
|
+
"foo &bar".
|
257
|
+
must_be_parsed_as s(:call, nil, :foo,
|
494
258
|
s(:block_pass,
|
495
|
-
s(:call, nil, :bar
|
259
|
+
s(:call, nil, :bar)))
|
496
260
|
end
|
497
261
|
|
498
262
|
it "works for a bare hash" do
|
499
|
-
|
500
|
-
|
501
|
-
s(:arglist,
|
263
|
+
"foo bar => baz".
|
264
|
+
must_be_parsed_as s(:call, nil, :foo,
|
502
265
|
s(:hash,
|
503
|
-
s(:call, nil, :bar
|
504
|
-
s(:call, nil, :baz
|
505
|
-
end
|
506
|
-
end
|
507
|
-
|
508
|
-
describe "for array literals" do
|
509
|
-
it "works for an empty array" do
|
510
|
-
result = parser.parse "[]"
|
511
|
-
result.must_equal s(:array)
|
512
|
-
end
|
513
|
-
|
514
|
-
it "works for a simple case with splat" do
|
515
|
-
result = parser.parse "[*foo]"
|
516
|
-
result.must_equal s(:array,
|
517
|
-
s(:splat, s(:call, nil, :foo, s(:arglist))))
|
518
|
-
end
|
519
|
-
|
520
|
-
it "works for a multi-element case with splat" do
|
521
|
-
result = parser.parse "[foo, *bar]"
|
522
|
-
result.must_equal s(:array,
|
523
|
-
s(:call, nil, :foo, s(:arglist)),
|
524
|
-
s(:splat, s(:call, nil, :bar, s(:arglist))))
|
525
|
-
end
|
526
|
-
|
527
|
-
it "works for an array created with %W" do
|
528
|
-
result = parser.parse "%W(foo bar)"
|
529
|
-
result.must_equal s(:array, s(:str, "foo"), s(:str, "bar"))
|
530
|
-
end
|
531
|
-
end
|
532
|
-
|
533
|
-
describe "for hash literals" do
|
534
|
-
it "works for an empty hash" do
|
535
|
-
result = parser.parse "{}"
|
536
|
-
result.must_equal s(:hash)
|
537
|
-
end
|
538
|
-
|
539
|
-
it "works for a hash with one pair" do
|
540
|
-
result = parser.parse "{foo => bar}"
|
541
|
-
result.must_equal s(:hash,
|
542
|
-
s(:call, nil, :foo, s(:arglist)),
|
543
|
-
s(:call, nil, :bar, s(:arglist)))
|
544
|
-
end
|
545
|
-
|
546
|
-
it "works for a hash with multiple pairs" do
|
547
|
-
result = parser.parse "{foo => bar, baz => qux}"
|
548
|
-
result.must_equal s(:hash,
|
549
|
-
s(:call, nil, :foo, s(:arglist)),
|
550
|
-
s(:call, nil, :bar, s(:arglist)),
|
551
|
-
s(:call, nil, :baz, s(:arglist)),
|
552
|
-
s(:call, nil, :qux, s(:arglist)))
|
553
|
-
end
|
554
|
-
|
555
|
-
it "works for a hash with label keys (Ruby 1.9 only)" do
|
556
|
-
result = parser.parse "{foo: bar, baz: qux}"
|
557
|
-
result.must_equal s(:hash,
|
558
|
-
s(:lit, :foo),
|
559
|
-
s(:call, nil, :bar, s(:arglist)),
|
560
|
-
s(:lit, :baz),
|
561
|
-
s(:call, nil, :qux, s(:arglist)))
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
describe "for number literals" do
|
566
|
-
it "works for floats" do
|
567
|
-
result = parser.parse "3.14"
|
568
|
-
result.must_equal s(:lit, 3.14)
|
569
|
-
end
|
570
|
-
|
571
|
-
it "works for octal integer literals" do
|
572
|
-
result = parser.parse "0700"
|
573
|
-
result.must_equal s(:lit, 448)
|
266
|
+
s(:call, nil, :bar),
|
267
|
+
s(:call, nil, :baz)))
|
574
268
|
end
|
575
269
|
end
|
576
270
|
|
577
271
|
describe "for collection indexing" do
|
578
272
|
it "works in the simple case" do
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
273
|
+
"foo[bar]".
|
274
|
+
must_be_parsed_as s(:call,
|
275
|
+
s(:call, nil, :foo),
|
276
|
+
:[],
|
277
|
+
s(:call, nil, :bar))
|
584
278
|
end
|
585
279
|
|
586
280
|
it "works without any indexes" do
|
587
|
-
"foo[]".must_be_parsed_as s(:call, s(:call, nil, :foo
|
588
|
-
:[]
|
281
|
+
"foo[]".must_be_parsed_as s(:call, s(:call, nil, :foo),
|
282
|
+
:[])
|
589
283
|
end
|
590
284
|
|
591
285
|
it "drops self from self[]" do
|
592
286
|
"self[foo]".must_be_parsed_as s(:call, nil, :[],
|
593
|
-
s(:
|
594
|
-
s(:call, nil, :foo, s(:arglist))))
|
287
|
+
s(:call, nil, :foo))
|
595
288
|
end
|
596
289
|
end
|
597
290
|
|
598
291
|
describe "for method definitions" do
|
599
292
|
it "works with def with receiver" do
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
293
|
+
"def foo.bar; end".
|
294
|
+
must_be_parsed_as s(:defs,
|
295
|
+
s(:call, nil, :foo),
|
296
|
+
:bar,
|
297
|
+
s(:args))
|
298
|
+
end
|
299
|
+
|
300
|
+
it "works with def with receiver and multiple statements" do
|
301
|
+
"def foo.bar; baz; qux; end".
|
302
|
+
must_be_parsed_as s(:defs,
|
303
|
+
s(:call, nil, :foo),
|
304
|
+
:bar,
|
305
|
+
s(:args),
|
306
|
+
s(:call, nil, :baz),
|
307
|
+
s(:call, nil, :qux))
|
606
308
|
end
|
607
309
|
|
608
310
|
it "works with a method argument with a default value" do
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
311
|
+
"def foo bar=nil; end".
|
312
|
+
must_be_parsed_as s(:defn,
|
313
|
+
:foo,
|
314
|
+
s(:args, s(:lasgn, :bar, s(:nil))),
|
315
|
+
s(:nil))
|
614
316
|
end
|
615
317
|
|
616
318
|
it "works with several method arguments with default values" do
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
:bar, :baz,
|
622
|
-
s(:block,
|
319
|
+
"def foo bar=1, baz=2; end".
|
320
|
+
must_be_parsed_as s(:defn,
|
321
|
+
:foo,
|
322
|
+
s(:args,
|
623
323
|
s(:lasgn, :bar, s(:lit, 1)),
|
624
|
-
s(:lasgn, :baz, s(:lit, 2)))
|
625
|
-
|
324
|
+
s(:lasgn, :baz, s(:lit, 2))),
|
325
|
+
s(:nil))
|
626
326
|
end
|
627
327
|
|
628
328
|
it "works with brackets around the parameter list" do
|
629
|
-
|
630
|
-
|
631
|
-
:foo,
|
632
|
-
s(:args, :bar),
|
633
|
-
s(:scope, s(:block, s(:nil))))
|
329
|
+
"def foo(bar); end".
|
330
|
+
must_be_parsed_as s(:defn, :foo, s(:args, :bar), s(:nil))
|
634
331
|
end
|
635
332
|
|
636
333
|
it "works with a simple splat" do
|
637
|
-
|
638
|
-
|
639
|
-
:foo,
|
640
|
-
s(:args, :"*bar"),
|
641
|
-
s(:scope, s(:block, s(:nil))))
|
334
|
+
"def foo *bar; end".
|
335
|
+
must_be_parsed_as s(:defn, :foo, s(:args, :"*bar"), s(:nil))
|
642
336
|
end
|
643
337
|
|
644
338
|
it "works with a regular argument plus splat" do
|
645
|
-
|
646
|
-
|
647
|
-
:foo,
|
648
|
-
s(:args, :bar, :"*baz"),
|
649
|
-
s(:scope, s(:block, s(:nil))))
|
339
|
+
"def foo bar, *baz; end".
|
340
|
+
must_be_parsed_as s(:defn, :foo, s(:args, :bar, :"*baz"), s(:nil))
|
650
341
|
end
|
651
342
|
|
652
343
|
it "works with a nameless splat" do
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
344
|
+
"def foo *; end".
|
345
|
+
must_be_parsed_as s(:defn,
|
346
|
+
:foo,
|
347
|
+
s(:args, :"*"),
|
348
|
+
s(:nil))
|
658
349
|
end
|
659
350
|
|
660
351
|
it "works for a simple case with explicit block parameter" do
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
352
|
+
"def foo &bar; end".
|
353
|
+
must_be_parsed_as s(:defn,
|
354
|
+
:foo,
|
355
|
+
s(:args, :"&bar"),
|
356
|
+
s(:nil))
|
666
357
|
end
|
667
358
|
|
668
359
|
it "works with a regular argument plus explicit block parameter" do
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
360
|
+
"def foo bar, &baz; end".
|
361
|
+
must_be_parsed_as s(:defn,
|
362
|
+
:foo,
|
363
|
+
s(:args, :bar, :"&baz"),
|
364
|
+
s(:nil))
|
674
365
|
end
|
675
366
|
|
676
367
|
it "works with a argument with default value plus explicit block parameter" do
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
s(:
|
684
|
-
s(:scope, s(:block, s(:nil))))
|
685
|
-
end
|
686
|
-
|
687
|
-
it "works with a splat plus explicit block parameter" do
|
688
|
-
result = parser.parse "def foo *bar, &baz; end"
|
689
|
-
result.must_equal s(:defn,
|
690
|
-
:foo,
|
691
|
-
s(:args, :"*bar", :"&baz"),
|
692
|
-
s(:scope, s(:block, s(:nil))))
|
368
|
+
"def foo bar=1, &baz; end".
|
369
|
+
must_be_parsed_as s(:defn,
|
370
|
+
:foo,
|
371
|
+
s(:args,
|
372
|
+
s(:lasgn, :bar, s(:lit, 1)),
|
373
|
+
:"&baz"),
|
374
|
+
s(:nil))
|
693
375
|
end
|
694
376
|
|
695
|
-
it "works with
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
377
|
+
it "works with a argument with default value followed by a mandatory argument" do
|
378
|
+
"def foo bar=1, baz; end".
|
379
|
+
must_be_parsed_as s(:defn,
|
380
|
+
:foo,
|
381
|
+
s(:args,
|
382
|
+
s(:lasgn, :bar, s(:lit, 1)),
|
383
|
+
:baz),
|
384
|
+
s(:nil))
|
703
385
|
end
|
704
386
|
|
705
|
-
it "works
|
706
|
-
|
707
|
-
|
708
|
-
|
387
|
+
it "works with a splat plus explicit block parameter" do
|
388
|
+
"def foo *bar, &baz; end".
|
389
|
+
must_be_parsed_as s(:defn,
|
390
|
+
:foo,
|
391
|
+
s(:args, :"*bar", :"&baz"),
|
392
|
+
s(:nil))
|
709
393
|
end
|
710
|
-
end
|
711
|
-
|
712
|
-
describe "for method calls" do
|
713
|
-
describe "without a receiver" do
|
714
|
-
it "works without brackets" do
|
715
|
-
result = parser.parse "foo bar"
|
716
|
-
result.must_equal s(:call, nil, :foo,
|
717
|
-
s(:arglist, s(:call, nil, :bar, s(:arglist))))
|
718
|
-
end
|
719
|
-
|
720
|
-
it "works with brackets" do
|
721
|
-
result = parser.parse "foo(bar)"
|
722
|
-
result.must_equal s(:call, nil, :foo,
|
723
|
-
s(:arglist, s(:call, nil, :bar, s(:arglist))))
|
724
|
-
end
|
725
|
-
|
726
|
-
it "works with an empty parameter list and no brackets" do
|
727
|
-
result = parser.parse "foo"
|
728
|
-
result.must_equal s(:call, nil, :foo, s(:arglist))
|
729
|
-
end
|
730
|
-
|
731
|
-
it "works with brackets around an empty parameter list" do
|
732
|
-
result = parser.parse "foo()"
|
733
|
-
result.must_equal s(:call, nil, :foo, s(:arglist))
|
734
|
-
end
|
735
|
-
|
736
|
-
it "works for methods ending in a question mark" do
|
737
|
-
result = parser.parse "foo?"
|
738
|
-
result.must_equal s(:call, nil, :foo?, s(:arglist))
|
739
|
-
end
|
740
394
|
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
395
|
+
it "works with an argument with default value plus splat" do
|
396
|
+
"def foo bar=1, *baz; end".
|
397
|
+
must_be_parsed_as s(:defn,
|
398
|
+
:foo,
|
399
|
+
s(:args,
|
400
|
+
s(:lasgn, :bar, s(:lit, 1)),
|
401
|
+
:"*baz"),
|
402
|
+
s(:nil))
|
749
403
|
end
|
750
404
|
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
s(:
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
it "works with brackets" do
|
761
|
-
result = parser.parse "foo.bar(baz)"
|
762
|
-
result.must_equal s(:call,
|
763
|
-
s(:call, nil, :foo, s(:arglist)),
|
764
|
-
:bar,
|
765
|
-
s(:arglist, s(:call, nil, :baz, s(:arglist))))
|
766
|
-
end
|
767
|
-
|
768
|
-
it "works with brackets around a call with no brackets" do
|
769
|
-
result = parser.parse "foo.bar(baz qux)"
|
770
|
-
result.must_equal s(:call,
|
771
|
-
s(:call, nil, :foo, s(:arglist)),
|
772
|
-
:bar,
|
773
|
-
s(:arglist,
|
774
|
-
s(:call, nil, :baz,
|
775
|
-
s(:arglist,
|
776
|
-
s(:call, nil, :qux, s(:arglist))))))
|
777
|
-
end
|
778
|
-
|
779
|
-
it "works with nested calls without brackets" do
|
780
|
-
result = parser.parse "foo.bar baz qux"
|
781
|
-
result.must_equal s(:call,
|
782
|
-
s(:call, nil, :foo, s(:arglist)),
|
783
|
-
:bar,
|
784
|
-
s(:arglist,
|
785
|
-
s(:call, nil, :baz,
|
786
|
-
s(:arglist,
|
787
|
-
s(:call, nil, :qux, s(:arglist))))))
|
788
|
-
end
|
405
|
+
it "works with an argument with default value plus splat plus final mandatory arguments" do
|
406
|
+
"def foo bar=1, *baz, qux, quuz; end".
|
407
|
+
must_be_parsed_as s(:defn,
|
408
|
+
:foo,
|
409
|
+
s(:args,
|
410
|
+
s(:lasgn, :bar, s(:lit, 1)),
|
411
|
+
:"*baz", :qux, :quuz),
|
412
|
+
s(:nil))
|
789
413
|
end
|
790
414
|
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
s(:call,
|
796
|
-
s(:call, nil, :foo, s(:arglist)),
|
797
|
-
:bar,
|
798
|
-
s(:arglist)),
|
799
|
-
nil,
|
800
|
-
s(:call, nil, :baz, s(:arglist)))
|
801
|
-
end
|
802
|
-
|
803
|
-
it "works for a do block with several statements" do
|
804
|
-
result = parser.parse "foo.bar do baz; qux; end"
|
805
|
-
result.must_equal s(:iter,
|
806
|
-
s(:call,
|
807
|
-
s(:call, nil, :foo, s(:arglist)),
|
808
|
-
:bar,
|
809
|
-
s(:arglist)),
|
810
|
-
nil,
|
811
|
-
s(:block,
|
812
|
-
s(:call, nil, :baz, s(:arglist)),
|
813
|
-
s(:call, nil, :qux, s(:arglist))))
|
814
|
-
end
|
415
|
+
it "works when the method name is an operator" do
|
416
|
+
"def +; end".
|
417
|
+
must_be_parsed_as s(:defn, :+, s(:args),
|
418
|
+
s(:nil))
|
815
419
|
end
|
816
420
|
end
|
817
421
|
|
818
422
|
describe "for blocks" do
|
819
423
|
it "works with no statements in the block body" do
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
424
|
+
"foo do; end".
|
425
|
+
must_be_parsed_as s(:iter,
|
426
|
+
s(:call, nil, :foo),
|
427
|
+
s(:args))
|
824
428
|
end
|
825
429
|
|
826
430
|
it "works with next with no arguments" do
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
431
|
+
"foo do; next; end".
|
432
|
+
must_be_parsed_as s(:iter,
|
433
|
+
s(:call, nil, :foo),
|
434
|
+
s(:args),
|
435
|
+
s(:next))
|
832
436
|
end
|
833
437
|
|
834
438
|
it "works with next with one argument" do
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
439
|
+
"foo do; next bar; end".
|
440
|
+
must_be_parsed_as s(:iter,
|
441
|
+
s(:call, nil, :foo),
|
442
|
+
s(:args),
|
443
|
+
s(:next, s(:call, nil, :bar)))
|
840
444
|
end
|
841
445
|
|
842
446
|
it "works with next with several arguments" do
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
447
|
+
"foo do; next bar, baz; end".
|
448
|
+
must_be_parsed_as s(:iter,
|
449
|
+
s(:call, nil, :foo),
|
450
|
+
s(:args),
|
451
|
+
s(:next,
|
452
|
+
s(:array,
|
453
|
+
s(:call, nil, :bar),
|
454
|
+
s(:call, nil, :baz))))
|
851
455
|
end
|
852
456
|
|
853
457
|
it "works with break with no arguments" do
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
458
|
+
"foo do; break; end".
|
459
|
+
must_be_parsed_as s(:iter,
|
460
|
+
s(:call, nil, :foo),
|
461
|
+
s(:args),
|
462
|
+
s(:break))
|
859
463
|
end
|
860
464
|
|
861
465
|
it "works with break with one argument" do
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
466
|
+
"foo do; break bar; end".
|
467
|
+
must_be_parsed_as s(:iter,
|
468
|
+
s(:call, nil, :foo),
|
469
|
+
s(:args),
|
470
|
+
s(:break, s(:call, nil, :bar)))
|
867
471
|
end
|
868
472
|
|
869
473
|
it "works with break with several arguments" do
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
474
|
+
"foo do; break bar, baz; end".
|
475
|
+
must_be_parsed_as s(:iter,
|
476
|
+
s(:call, nil, :foo),
|
477
|
+
s(:args),
|
478
|
+
s(:break,
|
479
|
+
s(:array,
|
480
|
+
s(:call, nil, :bar),
|
481
|
+
s(:call, nil, :baz))))
|
878
482
|
end
|
879
483
|
|
880
484
|
it "works with redo" do
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
485
|
+
"foo do; redo; end".
|
486
|
+
must_be_parsed_as s(:iter,
|
487
|
+
s(:call, nil, :foo),
|
488
|
+
s(:args),
|
489
|
+
s(:redo))
|
886
490
|
end
|
887
491
|
|
888
492
|
it "works with one argument" do
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
493
|
+
"foo do |bar|; end".
|
494
|
+
must_be_parsed_as s(:iter,
|
495
|
+
s(:call, nil, :foo),
|
496
|
+
s(:args, :bar))
|
893
497
|
end
|
894
498
|
|
895
499
|
it "works with multiple arguments" do
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
s(:array,
|
901
|
-
s(:lasgn, :bar),
|
902
|
-
s(:lasgn, :baz))))
|
500
|
+
"foo do |bar, baz|; end".
|
501
|
+
must_be_parsed_as s(:iter,
|
502
|
+
s(:call, nil, :foo),
|
503
|
+
s(:args, :bar, :baz))
|
903
504
|
end
|
904
505
|
|
905
506
|
it "works with a single splat argument" do
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
s(:array,
|
911
|
-
s(:splat, s(:lasgn, :bar)))))
|
507
|
+
"foo do |*bar|; end".
|
508
|
+
must_be_parsed_as s(:iter,
|
509
|
+
s(:call, nil, :foo),
|
510
|
+
s(:args, :"*bar"))
|
912
511
|
end
|
913
512
|
|
914
513
|
it "works with a combination of regular arguments and a splat argument" do
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
s(:array,
|
920
|
-
s(:lasgn, :bar),
|
921
|
-
s(:splat, s(:lasgn, :baz)))))
|
514
|
+
"foo do |bar, *baz|; end".
|
515
|
+
must_be_parsed_as s(:iter,
|
516
|
+
s(:call, nil, :foo),
|
517
|
+
s(:args, :bar, :"*baz"))
|
922
518
|
end
|
923
519
|
|
924
520
|
end
|
925
521
|
|
926
522
|
describe "for yield" do
|
927
523
|
it "works with no arguments and no brackets" do
|
928
|
-
|
929
|
-
|
524
|
+
"yield".
|
525
|
+
must_be_parsed_as s(:yield)
|
930
526
|
end
|
931
527
|
|
932
528
|
it "works with brackets but no arguments" do
|
933
|
-
|
934
|
-
|
529
|
+
"yield()".
|
530
|
+
must_be_parsed_as s(:yield)
|
935
531
|
end
|
936
532
|
|
937
533
|
it "works with one argument and no brackets" do
|
938
|
-
|
939
|
-
|
534
|
+
"yield foo".
|
535
|
+
must_be_parsed_as s(:yield, s(:call, nil, :foo))
|
940
536
|
end
|
941
537
|
|
942
538
|
it "works with one argument and brackets" do
|
943
|
-
|
944
|
-
|
539
|
+
"yield(foo)".
|
540
|
+
must_be_parsed_as s(:yield, s(:call, nil, :foo))
|
945
541
|
end
|
946
542
|
|
947
543
|
it "works with multiple arguments and no brackets" do
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
544
|
+
"yield foo, bar".
|
545
|
+
must_be_parsed_as s(:yield,
|
546
|
+
s(:call, nil, :foo),
|
547
|
+
s(:call, nil, :bar))
|
952
548
|
end
|
953
549
|
|
954
550
|
it "works with multiple arguments and brackets" do
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
551
|
+
"yield(foo, bar)".
|
552
|
+
must_be_parsed_as s(:yield,
|
553
|
+
s(:call, nil, :foo),
|
554
|
+
s(:call, nil, :bar))
|
959
555
|
end
|
960
556
|
|
961
557
|
it "works with splat" do
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
end
|
967
|
-
end
|
968
|
-
|
969
|
-
describe "for literals" do
|
970
|
-
it "works for symbols" do
|
971
|
-
result = parser.parse ":foo"
|
972
|
-
result.must_equal s(:lit, :foo)
|
973
|
-
end
|
974
|
-
|
975
|
-
it "works for symbols that look like instance variable names" do
|
976
|
-
result = parser.parse ":@foo"
|
977
|
-
result.must_equal s(:lit, :@foo)
|
978
|
-
end
|
979
|
-
|
980
|
-
it "works for empty strings" do
|
981
|
-
result = parser.parse "''"
|
982
|
-
result.must_equal s(:str, "")
|
983
|
-
end
|
984
|
-
|
985
|
-
it "works for strings with escape sequences" do
|
986
|
-
result = parser.parse "\"\\n\""
|
987
|
-
result.must_equal s(:str, "\n")
|
988
|
-
end
|
989
|
-
|
990
|
-
it "works for strings with useless escape sequences" do
|
991
|
-
result = parser.parse "\"F\\OO\""
|
992
|
-
result.must_equal s(:str, "FOO")
|
993
|
-
end
|
994
|
-
|
995
|
-
it "works for strings with escaped backslashes" do
|
996
|
-
result = parser.parse "\"\\\\n\""
|
997
|
-
result.must_equal s(:str, "\\n")
|
998
|
-
end
|
999
|
-
|
1000
|
-
it "works for a double-quoted string representing a regex literal with escaped right bracket" do
|
1001
|
-
result = parser.parse "\"/\\\\)/\""
|
1002
|
-
result.must_equal s(:str, "/\\)/")
|
1003
|
-
end
|
1004
|
-
|
1005
|
-
it "works for a double-quoted string containing a uselessly escaped right bracket" do
|
1006
|
-
result = parser.parse "\"/\\)/\""
|
1007
|
-
result.must_equal s(:str, "/)/")
|
1008
|
-
end
|
1009
|
-
|
1010
|
-
it "works for a string containing escaped quotes" do
|
1011
|
-
result = parser.parse "\"\\\"\""
|
1012
|
-
result.must_equal s(:str, "\"")
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
it "works for trivial interpolated strings" do
|
1016
|
-
result = parser.parse '"#{foo}"'
|
1017
|
-
result.must_equal s(:dstr,
|
1018
|
-
"",
|
1019
|
-
s(:evstr,
|
1020
|
-
s(:call, nil, :foo, s(:arglist))))
|
1021
|
-
end
|
1022
|
-
|
1023
|
-
it "works for basic interpolated strings" do
|
1024
|
-
result = parser.parse '"foo#{bar}"'
|
1025
|
-
result.must_equal s(:dstr,
|
1026
|
-
"foo",
|
1027
|
-
s(:evstr,
|
1028
|
-
s(:call, nil, :bar, s(:arglist))))
|
1029
|
-
end
|
1030
|
-
|
1031
|
-
it "works for strings with several interpolations" do
|
1032
|
-
result = parser.parse '"foo#{bar}baz#{qux}"'
|
1033
|
-
result.must_equal s(:dstr,
|
1034
|
-
"foo",
|
1035
|
-
s(:evstr, s(:call, nil, :bar, s(:arglist))),
|
1036
|
-
s(:str, "baz"),
|
1037
|
-
s(:evstr, s(:call, nil, :qux, s(:arglist))))
|
1038
|
-
end
|
1039
|
-
|
1040
|
-
it "works for strings with interpolations followed by escape sequences" do
|
1041
|
-
result = parser.parse '"#{foo}\\n"'
|
1042
|
-
result.must_equal s(:dstr,
|
1043
|
-
"",
|
1044
|
-
s(:evstr, s(:call, nil, :foo, s(:arglist))),
|
1045
|
-
s(:str, "\n"))
|
1046
|
-
end
|
1047
|
-
|
1048
|
-
it "works for a simple regex literal" do
|
1049
|
-
result = parser.parse "/foo/"
|
1050
|
-
result.must_equal s(:lit, /foo/)
|
1051
|
-
end
|
1052
|
-
|
1053
|
-
it "works for regex literals with escaped right bracket" do
|
1054
|
-
result = parser.parse '/\\)/'
|
1055
|
-
result.must_equal s(:lit, /\)/)
|
1056
|
-
end
|
1057
|
-
|
1058
|
-
it "works for regex literals with escape sequences" do
|
1059
|
-
result = parser.parse '/\\)\\n\\\\/'
|
1060
|
-
result.must_equal s(:lit, /\)\n\\/)
|
1061
|
-
end
|
1062
|
-
|
1063
|
-
it "works for regexes with interpolations" do
|
1064
|
-
result = parser.parse '/foo#{bar}baz/'
|
1065
|
-
result.must_equal s(:dregx,
|
1066
|
-
"foo",
|
1067
|
-
s(:evstr, s(:call, nil, :bar, s(:arglist))),
|
1068
|
-
s(:str, "baz"))
|
1069
|
-
end
|
1070
|
-
|
1071
|
-
it "works for a regex literal with the multiline flag" do
|
1072
|
-
result = parser.parse "/foo/m"
|
1073
|
-
result.must_equal s(:lit, /foo/m)
|
1074
|
-
end
|
1075
|
-
|
1076
|
-
it "works for a regex literal with the extended flag" do
|
1077
|
-
result = parser.parse "/foo/x"
|
1078
|
-
result.must_equal s(:lit, /foo/x)
|
1079
|
-
end
|
1080
|
-
|
1081
|
-
it "works for a regex literal with the ignorecase flag" do
|
1082
|
-
result = parser.parse "/foo/i"
|
1083
|
-
result.must_equal s(:lit, /foo/i)
|
1084
|
-
end
|
1085
|
-
|
1086
|
-
it "works for a regex literal with a combination of flags" do
|
1087
|
-
result = parser.parse "/foo/ixm"
|
1088
|
-
result.must_equal s(:lit, /foo/ixm)
|
1089
|
-
end
|
1090
|
-
|
1091
|
-
it "works for a regex literal with flags and interpolation" do
|
1092
|
-
result = parser.parse '/foo#{bar}/ixm'
|
1093
|
-
result.must_equal s(:dregx,
|
1094
|
-
"foo",
|
1095
|
-
s(:evstr, s(:call, nil, :bar, s(:arglist))),
|
1096
|
-
7)
|
1097
|
-
end
|
1098
|
-
|
1099
|
-
it "works for a regex literal with interpolate-once flag" do
|
1100
|
-
result = parser.parse '/foo#{bar}/o'
|
1101
|
-
result.must_equal s(:dregx_once,
|
1102
|
-
"foo",
|
1103
|
-
s(:evstr, s(:call, nil, :bar, s(:arglist))))
|
1104
|
-
end
|
1105
|
-
|
1106
|
-
it "works for simple dsyms" do
|
1107
|
-
result = parser.parse ':"foo"'
|
1108
|
-
result.must_equal s(:lit, :foo)
|
1109
|
-
end
|
1110
|
-
|
1111
|
-
it "works for dsyms with interpolations" do
|
1112
|
-
result = parser.parse ':"foo#{bar}"'
|
1113
|
-
result.must_equal s(:dsym,
|
1114
|
-
"foo",
|
1115
|
-
s(:evstr, s(:call, nil, :bar, s(:arglist))))
|
1116
|
-
end
|
1117
|
-
|
1118
|
-
it "works for character literals (which are string literals in Ruby 1.9.3)" do
|
1119
|
-
result = parser.parse "?a"
|
1120
|
-
result.must_equal s(:lit, "a")
|
1121
|
-
end
|
1122
|
-
|
1123
|
-
it "works for character literals in extra compatible mode" do
|
1124
|
-
parser.extra_compatible = true
|
1125
|
-
result = parser.parse "?a"
|
1126
|
-
result.must_equal s(:lit, 97)
|
1127
|
-
end
|
1128
|
-
|
1129
|
-
it "works for basic backtick strings" do
|
1130
|
-
result = parser.parse '`foo`'
|
1131
|
-
result.must_equal s(:xstr, "foo")
|
558
|
+
"yield foo, *bar".
|
559
|
+
must_be_parsed_as s(:yield,
|
560
|
+
s(:call, nil, :foo),
|
561
|
+
s(:splat, s(:call, nil, :bar)))
|
1132
562
|
end
|
1133
|
-
|
1134
|
-
it "works for interpolated backtick strings" do
|
1135
|
-
result = parser.parse '`foo#{bar}`'
|
1136
|
-
result.must_equal s(:dxstr,
|
1137
|
-
"foo",
|
1138
|
-
s(:evstr, s(:call, nil, :bar, s(:arglist))))
|
1139
|
-
end
|
1140
|
-
|
1141
|
-
it "works for backtick strings with escape sequences" do
|
1142
|
-
result = parser.parse '`foo\\n`'
|
1143
|
-
result.must_equal s(:xstr, "foo\n")
|
1144
|
-
end
|
1145
|
-
|
1146
563
|
end
|
1147
564
|
|
1148
565
|
describe "for the __FILE__ keyword" do
|
1149
566
|
describe "when not passing a file name" do
|
1150
567
|
it "creates a string sexp with value '(string)'" do
|
1151
|
-
|
1152
|
-
|
568
|
+
"__FILE__".
|
569
|
+
must_be_parsed_as s(:str, "(string)")
|
1153
570
|
end
|
1154
571
|
end
|
1155
572
|
|
@@ -1163,505 +580,466 @@ describe RipperRubyParser::Parser do
|
|
1163
580
|
|
1164
581
|
describe "for the __LINE__ keyword" do
|
1165
582
|
it "creates a literal sexp with value of the line number" do
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
583
|
+
"__LINE__".
|
584
|
+
must_be_parsed_as s(:lit, 1)
|
585
|
+
"\n__LINE__".
|
586
|
+
must_be_parsed_as s(:lit, 2)
|
587
|
+
end
|
588
|
+
end
|
589
|
+
|
590
|
+
describe "for the END keyword" do
|
591
|
+
it "converts to a :postexe iterator" do
|
592
|
+
"END { foo }".
|
593
|
+
must_be_parsed_as s(:iter, s(:postexe), s(:args), s(:call, nil, :foo))
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
describe "for the BEGIN keyword" do
|
598
|
+
it "converts to a :preexe iterator" do
|
599
|
+
"BEGIN { foo }".
|
600
|
+
must_be_parsed_as s(:iter, s(:preexe), s(:args), s(:call, nil, :foo))
|
1170
601
|
end
|
1171
602
|
end
|
1172
603
|
|
1173
604
|
describe "for constant lookups" do
|
1174
605
|
it "works when explicitely starting from the root namespace" do
|
1175
|
-
|
1176
|
-
|
606
|
+
"::Foo".
|
607
|
+
must_be_parsed_as s(:colon3, :Foo)
|
1177
608
|
end
|
1178
609
|
|
1179
610
|
it "works with a three-level constant lookup" do
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
611
|
+
"Foo::Bar::Baz".
|
612
|
+
must_be_parsed_as s(:colon2,
|
613
|
+
s(:colon2, s(:const, :Foo), :Bar),
|
614
|
+
:Baz)
|
1184
615
|
end
|
1185
616
|
|
1186
617
|
it "works looking up a constant in a non-constant" do
|
1187
618
|
"foo::Bar".must_be_parsed_as s(:colon2,
|
1188
|
-
s(:call, nil, :foo
|
619
|
+
s(:call, nil, :foo),
|
1189
620
|
:Bar)
|
1190
621
|
end
|
1191
622
|
end
|
1192
623
|
|
1193
624
|
describe "for variable references" do
|
1194
625
|
it "works for self" do
|
1195
|
-
|
1196
|
-
|
626
|
+
"self".
|
627
|
+
must_be_parsed_as s(:self)
|
1197
628
|
end
|
1198
629
|
|
1199
630
|
it "works for instance variables" do
|
1200
|
-
|
1201
|
-
|
631
|
+
"@foo".
|
632
|
+
must_be_parsed_as s(:ivar, :@foo)
|
1202
633
|
end
|
1203
634
|
|
1204
635
|
it "works for global variables" do
|
1205
|
-
|
1206
|
-
|
636
|
+
"$foo".
|
637
|
+
must_be_parsed_as s(:gvar, :$foo)
|
1207
638
|
end
|
1208
639
|
|
1209
640
|
it "works for regexp match references" do
|
1210
|
-
|
1211
|
-
|
641
|
+
"$1".
|
642
|
+
must_be_parsed_as s(:nth_ref, 1)
|
1212
643
|
end
|
1213
644
|
|
1214
645
|
specify { "$'".must_be_parsed_as s(:back_ref, :"'") }
|
1215
646
|
specify { "$&".must_be_parsed_as s(:back_ref, :"&") }
|
1216
647
|
|
1217
648
|
it "works for class variables" do
|
1218
|
-
|
1219
|
-
|
649
|
+
"@@foo".
|
650
|
+
must_be_parsed_as s(:cvar, :@@foo)
|
1220
651
|
end
|
1221
652
|
end
|
1222
653
|
|
1223
654
|
describe "for single assignment" do
|
1224
655
|
it "works when assigning to an instance variable" do
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
656
|
+
"@foo = bar".
|
657
|
+
must_be_parsed_as s(:iasgn,
|
658
|
+
:@foo,
|
659
|
+
s(:call, nil, :bar))
|
1229
660
|
end
|
1230
661
|
|
1231
662
|
it "works when assigning to a constant" do
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
663
|
+
"FOO = bar".
|
664
|
+
must_be_parsed_as s(:cdecl,
|
665
|
+
:FOO,
|
666
|
+
s(:call, nil, :bar))
|
1236
667
|
end
|
1237
668
|
|
1238
669
|
it "works when assigning to a collection element" do
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
s(:call, nil, :
|
1245
|
-
s(:call, nil, :baz, s(:arglist))))
|
670
|
+
"foo[bar] = baz".
|
671
|
+
must_be_parsed_as s(:attrasgn,
|
672
|
+
s(:call, nil, :foo),
|
673
|
+
:[]=,
|
674
|
+
s(:call, nil, :bar),
|
675
|
+
s(:call, nil, :baz))
|
1246
676
|
end
|
1247
677
|
|
1248
678
|
it "works when assigning to an attribute" do
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
679
|
+
"foo.bar = baz".
|
680
|
+
must_be_parsed_as s(:attrasgn,
|
681
|
+
s(:call, nil, :foo),
|
682
|
+
:bar=,
|
683
|
+
s(:call, nil, :baz))
|
1254
684
|
end
|
1255
685
|
|
1256
686
|
it "works when assigning to a class variable" do
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
687
|
+
"@@foo = bar".
|
688
|
+
must_be_parsed_as s(:cvdecl,
|
689
|
+
:@@foo,
|
690
|
+
s(:call, nil, :bar))
|
1261
691
|
end
|
1262
692
|
|
1263
693
|
it "works when assigning to a class variable inside a method" do
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
s(:block,
|
1269
|
-
s(:cvasgn, :@@bar, s(:call, nil, :baz, s(:arglist))))))
|
694
|
+
"def foo; @@bar = baz; end".
|
695
|
+
must_be_parsed_as s(:defn,
|
696
|
+
:foo, s(:args),
|
697
|
+
s(:cvasgn, :@@bar, s(:call, nil, :baz)))
|
1270
698
|
end
|
1271
699
|
|
1272
700
|
it "works when assigning to a class variable inside a method with a receiver" do
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
s(:block,
|
1279
|
-
s(:cvasgn, :@@bar, s(:call, nil, :baz, s(:arglist))))))
|
701
|
+
"def self.foo; @@bar = baz; end".
|
702
|
+
must_be_parsed_as s(:defs,
|
703
|
+
s(:self),
|
704
|
+
:foo, s(:args),
|
705
|
+
s(:cvasgn, :@@bar, s(:call, nil, :baz)))
|
1280
706
|
end
|
1281
707
|
|
1282
708
|
it "works when assigning to a global variable" do
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
709
|
+
"$foo = bar".
|
710
|
+
must_be_parsed_as s(:gasgn,
|
711
|
+
:$foo,
|
712
|
+
s(:call, nil, :bar))
|
1287
713
|
end
|
1288
714
|
end
|
1289
715
|
|
1290
716
|
describe "for operator assignment" do
|
1291
717
|
it "works with +=" do
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
718
|
+
"foo += bar".
|
719
|
+
must_be_parsed_as s(:lasgn,
|
720
|
+
:foo,
|
721
|
+
s(:call,
|
722
|
+
s(:lvar, :foo),
|
723
|
+
:+,
|
724
|
+
s(:call, nil, :bar)))
|
1299
725
|
end
|
1300
726
|
|
1301
727
|
it "works with -=" do
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
728
|
+
"foo -= bar".
|
729
|
+
must_be_parsed_as s(:lasgn,
|
730
|
+
:foo,
|
731
|
+
s(:call,
|
732
|
+
s(:lvar, :foo),
|
733
|
+
:-,
|
734
|
+
s(:call, nil, :bar)))
|
1309
735
|
end
|
1310
736
|
|
1311
737
|
it "works with ||=" do
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
738
|
+
"foo ||= bar".
|
739
|
+
must_be_parsed_as s(:op_asgn_or,
|
740
|
+
s(:lvar, :foo),
|
741
|
+
s(:lasgn, :foo,
|
742
|
+
s(:call, nil, :bar)))
|
1317
743
|
end
|
1318
744
|
|
1319
745
|
it "works when assigning to an instance variable" do
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
746
|
+
"@foo += bar".
|
747
|
+
must_be_parsed_as s(:iasgn,
|
748
|
+
:@foo,
|
749
|
+
s(:call,
|
750
|
+
s(:ivar, :@foo),
|
751
|
+
:+,
|
752
|
+
s(:call, nil, :bar)))
|
1327
753
|
end
|
1328
754
|
|
1329
755
|
it "works when assigning to a collection element" do
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
756
|
+
"foo[bar] += baz".
|
757
|
+
must_be_parsed_as s(:op_asgn1,
|
758
|
+
s(:call, nil, :foo),
|
759
|
+
s(:arglist, s(:call, nil, :bar)),
|
760
|
+
:+,
|
761
|
+
s(:call, nil, :baz))
|
1336
762
|
end
|
1337
763
|
|
1338
764
|
it "works with ||= when assigning to a collection element" do
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
765
|
+
"foo[bar] ||= baz".
|
766
|
+
must_be_parsed_as s(:op_asgn1,
|
767
|
+
s(:call, nil, :foo),
|
768
|
+
s(:arglist, s(:call, nil, :bar)),
|
769
|
+
:"||",
|
770
|
+
s(:call, nil, :baz))
|
1345
771
|
end
|
1346
772
|
|
1347
773
|
it "works when assigning to an attribute" do
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
774
|
+
"foo.bar += baz".
|
775
|
+
must_be_parsed_as s(:op_asgn2,
|
776
|
+
s(:call, nil, :foo),
|
777
|
+
:bar=,
|
778
|
+
:+,
|
779
|
+
s(:call, nil, :baz))
|
1354
780
|
end
|
1355
781
|
|
1356
782
|
it "works with ||= when assigning to an attribute" do
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
783
|
+
"foo.bar ||= baz".
|
784
|
+
must_be_parsed_as s(:op_asgn2,
|
785
|
+
s(:call, nil, :foo),
|
786
|
+
:bar=,
|
787
|
+
:"||",
|
788
|
+
s(:call, nil, :baz))
|
1363
789
|
end
|
1364
790
|
end
|
1365
791
|
|
1366
792
|
describe "for multiple assignment" do
|
1367
793
|
it "works the same number of items on each side" do
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
794
|
+
"foo, bar = baz, qux".
|
795
|
+
must_be_parsed_as s(:masgn,
|
796
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
797
|
+
s(:array,
|
798
|
+
s(:call, nil, :baz),
|
799
|
+
s(:call, nil, :qux)))
|
1374
800
|
end
|
1375
801
|
|
1376
802
|
it "works with a single item on the right-hand side" do
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
803
|
+
"foo, bar = baz".
|
804
|
+
must_be_parsed_as s(:masgn,
|
805
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
806
|
+
s(:to_ary,
|
807
|
+
s(:call, nil, :baz)))
|
1382
808
|
end
|
1383
809
|
|
1384
810
|
it "works with left-hand splat" do
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
811
|
+
"foo, *bar = baz, qux".
|
812
|
+
must_be_parsed_as s(:masgn,
|
813
|
+
s(:array, s(:lasgn, :foo), s(:splat, s(:lasgn, :bar))),
|
814
|
+
s(:array,
|
815
|
+
s(:call, nil, :baz),
|
816
|
+
s(:call, nil, :qux)))
|
1391
817
|
end
|
1392
818
|
|
1393
819
|
it "works with brackets around the left-hand side" do
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
s(:call, nil, :baz, s(:arglist))))
|
820
|
+
"(foo, bar) = baz".
|
821
|
+
must_be_parsed_as s(:masgn,
|
822
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
823
|
+
s(:to_ary, s(:call, nil, :baz)))
|
1399
824
|
end
|
1400
825
|
|
1401
826
|
it "works with complex destructuring" do
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
827
|
+
"foo, (bar, baz) = qux".
|
828
|
+
must_be_parsed_as s(:masgn,
|
829
|
+
s(:array,
|
830
|
+
s(:lasgn, :foo),
|
831
|
+
s(:masgn,
|
832
|
+
s(:array, s(:lasgn, :bar), s(:lasgn, :baz)))),
|
833
|
+
s(:to_ary, s(:call, nil, :qux)))
|
834
|
+
end
|
835
|
+
|
836
|
+
it "works with complex destructuring of the value" do
|
837
|
+
"foo, (bar, baz) = [qux, [quz, quuz]]".
|
838
|
+
must_be_parsed_as s(:masgn,
|
839
|
+
s(:array,
|
840
|
+
s(:lasgn, :foo),
|
841
|
+
s(:masgn, s(:array, s(:lasgn, :bar), s(:lasgn, :baz)))),
|
842
|
+
s(:to_ary,
|
1407
843
|
s(:array,
|
1408
|
-
s(:
|
1409
|
-
s(:
|
1410
|
-
s(:to_ary,
|
1411
|
-
s(:call, nil, :qux, s(:arglist))))
|
844
|
+
s(:call, nil, :qux),
|
845
|
+
s(:array, s(:call, nil, :quz), s(:call, nil, :quuz)))))
|
1412
846
|
end
|
1413
847
|
|
1414
848
|
it "works with instance variables" do
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
s(:call, nil, :baz, s(:arglist))))
|
849
|
+
"@foo, @bar = baz".
|
850
|
+
must_be_parsed_as s(:masgn,
|
851
|
+
s(:array, s(:iasgn, :@foo), s(:iasgn, :@bar)),
|
852
|
+
s(:to_ary, s(:call, nil, :baz)))
|
1420
853
|
end
|
1421
854
|
|
1422
855
|
it "works with class variables" do
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
s(:call, nil, :baz, s(:arglist))))
|
856
|
+
"@@foo, @@bar = baz".
|
857
|
+
must_be_parsed_as s(:masgn,
|
858
|
+
s(:array, s(:cvdecl, :@@foo), s(:cvdecl, :@@bar)),
|
859
|
+
s(:to_ary, s(:call, nil, :baz)))
|
1428
860
|
end
|
1429
861
|
|
1430
862
|
it "works with attributes" do
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
|
1435
|
-
s(:call, nil, :foo,
|
1436
|
-
|
1437
|
-
s(:arglist)),
|
1438
|
-
s(:attrasgn,
|
1439
|
-
s(:call, nil, :foo, s(:arglist)),
|
1440
|
-
:baz=,
|
1441
|
-
s(:arglist))),
|
1442
|
-
s(:to_ary,
|
1443
|
-
s(:call, nil, :qux, s(:arglist))))
|
863
|
+
"foo.bar, foo.baz = qux".
|
864
|
+
must_be_parsed_as s(:masgn,
|
865
|
+
s(:array,
|
866
|
+
s(:attrasgn, s(:call, nil, :foo), :bar=),
|
867
|
+
s(:attrasgn, s(:call, nil, :foo), :baz=)),
|
868
|
+
s(:to_ary, s(:call, nil, :qux)))
|
1444
869
|
end
|
1445
870
|
|
1446
871
|
it "works with collection elements" do
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
:
|
1453
|
-
|
1454
|
-
s(:
|
1455
|
-
s(:call, nil, :bar, s(:arglist)),
|
1456
|
-
:[]=,
|
1457
|
-
s(:arglist, s(:lit, 2)))),
|
1458
|
-
s(:to_ary, s(:call, nil, :baz, s(:arglist))))
|
872
|
+
"foo[1], bar[2] = baz".
|
873
|
+
must_be_parsed_as s(:masgn,
|
874
|
+
s(:array,
|
875
|
+
s(:attrasgn,
|
876
|
+
s(:call, nil, :foo), :[]=, s(:lit, 1)),
|
877
|
+
s(:attrasgn,
|
878
|
+
s(:call, nil, :bar), :[]=, s(:lit, 2))),
|
879
|
+
s(:to_ary, s(:call, nil, :baz)))
|
1459
880
|
end
|
1460
881
|
|
1461
882
|
it "works with constants" do
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
s(:call, nil, :baz, s(:arglist))))
|
883
|
+
"Foo, Bar = baz".
|
884
|
+
must_be_parsed_as s(:masgn,
|
885
|
+
s(:array, s(:cdecl, :Foo), s(:cdecl, :Bar)),
|
886
|
+
s(:to_ary, s(:call, nil, :baz)))
|
1467
887
|
end
|
1468
888
|
|
1469
889
|
it "works with instance variables and splat" do
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
890
|
+
"@foo, *@bar = baz".
|
891
|
+
must_be_parsed_as s(:masgn,
|
892
|
+
s(:array,
|
893
|
+
s(:iasgn, :@foo),
|
894
|
+
s(:splat, s(:iasgn, :@bar))),
|
895
|
+
s(:to_ary,
|
896
|
+
s(:call, nil, :baz)))
|
1477
897
|
end
|
1478
|
-
|
1479
898
|
end
|
1480
899
|
|
1481
900
|
describe "for operators" do
|
1482
|
-
it "handles :and" do
|
1483
|
-
result = parser.parse "foo and bar"
|
1484
|
-
result.must_equal s(:and,
|
1485
|
-
s(:call, nil, :foo, s(:arglist)),
|
1486
|
-
s(:call, nil, :bar, s(:arglist)))
|
1487
|
-
end
|
1488
|
-
|
1489
|
-
it "handles double :and" do
|
1490
|
-
result = parser.parse "foo and bar and baz"
|
1491
|
-
result.must_equal s(:and,
|
1492
|
-
s(:call, nil, :foo, s(:arglist)),
|
1493
|
-
s(:and,
|
1494
|
-
s(:call, nil, :bar, s(:arglist)),
|
1495
|
-
s(:call, nil, :baz, s(:arglist))))
|
1496
|
-
end
|
1497
|
-
|
1498
|
-
it "handles :or" do
|
1499
|
-
result = parser.parse "foo or bar"
|
1500
|
-
result.must_equal s(:or,
|
1501
|
-
s(:call, nil, :foo, s(:arglist)),
|
1502
|
-
s(:call, nil, :bar, s(:arglist)))
|
1503
|
-
end
|
1504
|
-
|
1505
|
-
it "handles double :or" do
|
1506
|
-
result = parser.parse "foo or bar or baz"
|
1507
|
-
result.must_equal s(:or,
|
1508
|
-
s(:call, nil, :foo, s(:arglist)),
|
1509
|
-
s(:or,
|
1510
|
-
s(:call, nil, :bar, s(:arglist)),
|
1511
|
-
s(:call, nil, :baz, s(:arglist))))
|
1512
|
-
end
|
1513
|
-
|
1514
|
-
it "handles :or after :and" do
|
1515
|
-
result = parser.parse "foo and bar or baz"
|
1516
|
-
result.must_equal s(:or,
|
1517
|
-
s(:and,
|
1518
|
-
s(:call, nil, :foo, s(:arglist)),
|
1519
|
-
s(:call, nil, :bar, s(:arglist))),
|
1520
|
-
s(:call, nil, :baz, s(:arglist)))
|
1521
|
-
end
|
1522
|
-
|
1523
|
-
it "handles :and after :or" do
|
1524
|
-
result = parser.parse "foo or bar and baz"
|
1525
|
-
result.must_equal s(:and,
|
1526
|
-
s(:or,
|
1527
|
-
s(:call, nil, :foo, s(:arglist)),
|
1528
|
-
s(:call, nil, :bar, s(:arglist))),
|
1529
|
-
s(:call, nil, :baz, s(:arglist)))
|
1530
|
-
end
|
1531
|
-
|
1532
|
-
it "converts :&& to :and" do
|
1533
|
-
result = parser.parse "foo && bar"
|
1534
|
-
result.must_equal s(:and,
|
1535
|
-
s(:call, nil, :foo, s(:arglist)),
|
1536
|
-
s(:call, nil, :bar, s(:arglist)))
|
1537
|
-
end
|
1538
|
-
|
1539
|
-
it "converts :|| to :or" do
|
1540
|
-
result = parser.parse "foo || bar"
|
1541
|
-
result.must_equal s(:or,
|
1542
|
-
s(:call, nil, :foo, s(:arglist)),
|
1543
|
-
s(:call, nil, :bar, s(:arglist)))
|
1544
|
-
end
|
1545
|
-
|
1546
901
|
it "handles :!=" do
|
1547
|
-
|
1548
|
-
|
1549
|
-
s(:call,
|
1550
|
-
|
1551
|
-
|
1552
|
-
s(:arglist,
|
1553
|
-
s(:call, nil, :bar, s(:arglist)))))
|
902
|
+
"foo != bar".
|
903
|
+
must_be_parsed_as s(:call,
|
904
|
+
s(:call, nil, :foo),
|
905
|
+
:!=,
|
906
|
+
s(:call, nil, :bar))
|
1554
907
|
end
|
1555
908
|
|
1556
909
|
it "handles :=~ with two non-literals" do
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
910
|
+
"foo =~ bar".
|
911
|
+
must_be_parsed_as s(:call,
|
912
|
+
s(:call, nil, :foo),
|
913
|
+
:=~,
|
914
|
+
s(:call, nil, :bar))
|
1562
915
|
end
|
1563
916
|
|
1564
917
|
it "handles :=~ with literal regexp on the left hand side" do
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
918
|
+
"/foo/ =~ bar".
|
919
|
+
must_be_parsed_as s(:match2,
|
920
|
+
s(:lit, /foo/),
|
921
|
+
s(:call, nil, :bar))
|
1569
922
|
end
|
1570
923
|
|
1571
924
|
it "handles :=~ with literal regexp on the right hand side" do
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
925
|
+
"foo =~ /bar/".
|
926
|
+
must_be_parsed_as s(:match3,
|
927
|
+
s(:lit, /bar/),
|
928
|
+
s(:call, nil, :foo))
|
1576
929
|
end
|
1577
930
|
|
1578
931
|
it "handles unary minus with a number literal" do
|
1579
|
-
|
1580
|
-
|
932
|
+
"-1".
|
933
|
+
must_be_parsed_as s(:lit, -1)
|
1581
934
|
end
|
1582
935
|
|
1583
936
|
it "handles unary minus with a non-literal" do
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
s(:arglist))
|
937
|
+
"-foo".
|
938
|
+
must_be_parsed_as s(:call,
|
939
|
+
s(:call, nil, :foo),
|
940
|
+
:-@)
|
1589
941
|
end
|
1590
942
|
|
1591
943
|
it "handles unary plus with a number literal" do
|
1592
|
-
|
1593
|
-
|
944
|
+
"+ 1".
|
945
|
+
must_be_parsed_as s(:lit, 1)
|
1594
946
|
end
|
1595
947
|
|
1596
948
|
it "handles unary plus with a non-literal" do
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
949
|
+
"+ foo".
|
950
|
+
must_be_parsed_as s(:call,
|
951
|
+
s(:call, nil, :foo),
|
952
|
+
:+@)
|
953
|
+
end
|
954
|
+
|
955
|
+
it "handles unary !" do
|
956
|
+
"!foo".
|
957
|
+
must_be_parsed_as s(:call, s(:call, nil, :foo), :!)
|
1602
958
|
end
|
1603
959
|
|
1604
|
-
it "
|
1605
|
-
|
1606
|
-
|
960
|
+
it "converts :not to :!" do
|
961
|
+
"not foo".
|
962
|
+
must_be_parsed_as s(:call, s(:call, nil, :foo), :!)
|
1607
963
|
end
|
1608
964
|
|
1609
|
-
it "
|
1610
|
-
|
1611
|
-
|
965
|
+
it "handles unary ! with a number literal" do
|
966
|
+
"!1".
|
967
|
+
must_be_parsed_as s(:call, s(:lit, 1), :!)
|
1612
968
|
end
|
1613
969
|
|
1614
970
|
it "handles the range operator with positive number literals" do
|
1615
|
-
|
1616
|
-
|
971
|
+
"1..2".
|
972
|
+
must_be_parsed_as s(:lit, 1..2)
|
1617
973
|
end
|
1618
974
|
|
1619
975
|
it "handles the range operator with negative number literals" do
|
1620
|
-
|
1621
|
-
|
976
|
+
"-1..-2".
|
977
|
+
must_be_parsed_as s(:lit, -1..-2)
|
1622
978
|
end
|
1623
979
|
|
1624
980
|
it "handles the range operator with string literals" do
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
981
|
+
"'a'..'z'".
|
982
|
+
must_be_parsed_as s(:dot2,
|
983
|
+
s(:str, "a"),
|
984
|
+
s(:str, "z"))
|
1629
985
|
end
|
1630
986
|
|
1631
987
|
it "handles the range operator with non-literals" do
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
988
|
+
"foo..bar".
|
989
|
+
must_be_parsed_as s(:dot2,
|
990
|
+
s(:call, nil, :foo),
|
991
|
+
s(:call, nil, :bar))
|
992
|
+
end
|
993
|
+
|
994
|
+
it "handles the exclusive range operator with positive number literals" do
|
995
|
+
"1...2".
|
996
|
+
must_be_parsed_as s(:lit, 1...2)
|
997
|
+
end
|
998
|
+
|
999
|
+
it "handles the exclusive range operator with negative number literals" do
|
1000
|
+
"-1...-2".
|
1001
|
+
must_be_parsed_as s(:lit, -1...-2)
|
1002
|
+
end
|
1003
|
+
|
1004
|
+
it "handles the exclusive range operator with string literals" do
|
1005
|
+
"'a'...'z'".
|
1006
|
+
must_be_parsed_as s(:dot3,
|
1007
|
+
s(:str, "a"),
|
1008
|
+
s(:str, "z"))
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
it "handles the exclusive range operator with non-literals" do
|
1012
|
+
"foo...bar".
|
1013
|
+
must_be_parsed_as s(:dot3,
|
1014
|
+
s(:call, nil, :foo),
|
1015
|
+
s(:call, nil, :bar))
|
1636
1016
|
end
|
1637
1017
|
|
1638
1018
|
it "handles the ternary operator" do
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1019
|
+
"foo ? bar : baz".
|
1020
|
+
must_be_parsed_as s(:if,
|
1021
|
+
s(:call, nil, :foo),
|
1022
|
+
s(:call, nil, :bar),
|
1023
|
+
s(:call, nil, :baz))
|
1644
1024
|
end
|
1645
1025
|
end
|
1646
1026
|
|
1647
1027
|
describe "for expressions" do
|
1648
1028
|
it "handles assignment inside binary operator expressions" do
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
s(:arglist,
|
1029
|
+
"foo + (bar = baz)".
|
1030
|
+
must_be_parsed_as s(:call,
|
1031
|
+
s(:call, nil, :foo),
|
1032
|
+
:+,
|
1654
1033
|
s(:lasgn,
|
1655
1034
|
:bar,
|
1656
|
-
s(:call, nil, :baz
|
1035
|
+
s(:call, nil, :baz)))
|
1657
1036
|
end
|
1658
1037
|
|
1659
1038
|
it "handles assignment inside unary operator expressions" do
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
s(:arglist))
|
1039
|
+
"+(foo = bar)".
|
1040
|
+
must_be_parsed_as s(:call,
|
1041
|
+
s(:lasgn, :foo, s(:call, nil, :bar)),
|
1042
|
+
:+@)
|
1665
1043
|
end
|
1666
1044
|
end
|
1667
1045
|
|
@@ -1672,27 +1050,26 @@ describe RipperRubyParser::Parser do
|
|
1672
1050
|
result = parser.parse "# Foo\ndef foo; end"
|
1673
1051
|
result.must_equal s(:defn,
|
1674
1052
|
:foo,
|
1675
|
-
s(:args), s(:
|
1053
|
+
s(:args), s(:nil))
|
1676
1054
|
result.comments.must_equal "# Foo\n"
|
1677
1055
|
end
|
1678
1056
|
|
1679
1057
|
it "handles comments for methods with explicit receiver" do
|
1680
1058
|
result = parser.parse "# Foo\ndef foo.bar; end"
|
1681
1059
|
result.must_equal s(:defs,
|
1682
|
-
s(:call, nil, :foo
|
1060
|
+
s(:call, nil, :foo),
|
1683
1061
|
:bar,
|
1684
|
-
s(:args)
|
1062
|
+
s(:args))
|
1685
1063
|
result.comments.must_equal "# Foo\n"
|
1686
1064
|
end
|
1687
1065
|
|
1688
1066
|
it "matches comments to the correct entity" do
|
1689
1067
|
result = parser.parse "# Foo\nclass Foo\n# Bar\ndef bar\nend\nend"
|
1690
1068
|
result.must_equal s(:class, :Foo, nil,
|
1691
|
-
s(:
|
1692
|
-
s(:
|
1693
|
-
s(:args), s(:scope, s(:block, s(:nil))))))
|
1069
|
+
s(:defn, :bar,
|
1070
|
+
s(:args), s(:nil)))
|
1694
1071
|
result.comments.must_equal "# Foo\n"
|
1695
|
-
defn = result[3]
|
1072
|
+
defn = result[3]
|
1696
1073
|
defn.sexp_type.must_equal :defn
|
1697
1074
|
defn.comments.must_equal "# Bar\n"
|
1698
1075
|
end
|
@@ -1701,16 +1078,41 @@ describe RipperRubyParser::Parser do
|
|
1701
1078
|
result = parser.parse "# Foo\n# Bar\ndef foo; end"
|
1702
1079
|
result.must_equal s(:defn,
|
1703
1080
|
:foo,
|
1704
|
-
s(:args), s(:
|
1081
|
+
s(:args), s(:nil))
|
1705
1082
|
result.comments.must_equal "# Foo\n# Bar\n"
|
1706
1083
|
end
|
1707
1084
|
|
1085
|
+
it "drops comments inside method bodies" do
|
1086
|
+
result = parser.parse <<-END
|
1087
|
+
# Foo
|
1088
|
+
class Foo
|
1089
|
+
# foo
|
1090
|
+
def foo
|
1091
|
+
bar # this is dropped
|
1092
|
+
end
|
1093
|
+
|
1094
|
+
# bar
|
1095
|
+
def bar
|
1096
|
+
baz
|
1097
|
+
end
|
1098
|
+
end
|
1099
|
+
END
|
1100
|
+
result.must_equal s(:class,
|
1101
|
+
:Foo,
|
1102
|
+
nil,
|
1103
|
+
s(:defn, :foo, s(:args), s(:call, nil, :bar)),
|
1104
|
+
s(:defn, :bar, s(:args), s(:call, nil, :baz)))
|
1105
|
+
result.comments.must_equal "# Foo\n"
|
1106
|
+
result[3].comments.must_equal "# foo\n"
|
1107
|
+
result[4].comments.must_equal "# bar\n"
|
1108
|
+
end
|
1109
|
+
|
1708
1110
|
it "handles the use of symbols that are keywords" do
|
1709
1111
|
result = parser.parse "# Foo\ndef bar\n:class\nend"
|
1710
1112
|
result.must_equal s(:defn,
|
1711
1113
|
:bar,
|
1712
1114
|
s(:args),
|
1713
|
-
s(:
|
1115
|
+
s(:lit, :class))
|
1714
1116
|
result.comments.must_equal "# Foo\n"
|
1715
1117
|
end
|
1716
1118
|
|
@@ -1719,10 +1121,8 @@ describe RipperRubyParser::Parser do
|
|
1719
1121
|
result.must_equal s(:defn,
|
1720
1122
|
:bar,
|
1721
1123
|
s(:args),
|
1722
|
-
s(:
|
1723
|
-
s(:
|
1724
|
-
s(:sclass, s(:self),
|
1725
|
-
s(:scope, s(:call, nil, :baz, s(:arglist)))))))
|
1124
|
+
s(:sclass, s(:self),
|
1125
|
+
s(:call, nil, :baz)))
|
1726
1126
|
result.comments.must_equal "# Foo\n"
|
1727
1127
|
end
|
1728
1128
|
end
|
@@ -1852,23 +1252,23 @@ describe RipperRubyParser::Parser do
|
|
1852
1252
|
end
|
1853
1253
|
|
1854
1254
|
it "assigns line numbers to nested sexps that don't generate their own line numbers" do
|
1855
|
-
result = parser.parse "foo() do\nnext\nend\n"
|
1255
|
+
result = parser.parse "foo(bar) do\nnext baz\nend\n"
|
1856
1256
|
result.must_equal s(:iter,
|
1857
|
-
s(:call, nil, :foo, s(:
|
1858
|
-
|
1859
|
-
s(:next))
|
1257
|
+
s(:call, nil, :foo, s(:call, nil, :bar)),
|
1258
|
+
s(:args),
|
1259
|
+
s(:next, s(:call, nil, :baz)))
|
1860
1260
|
arglist = result[1][3]
|
1861
1261
|
block = result[3]
|
1862
1262
|
nums = [ arglist.line, block.line ]
|
1863
|
-
nums.must_equal [1,
|
1263
|
+
nums.must_equal [1, 2]
|
1864
1264
|
end
|
1865
1265
|
|
1866
1266
|
describe "when a line number is passed" do
|
1867
1267
|
it "shifts all line numbers as appropriate" do
|
1868
1268
|
result = parser.parse "foo\nbar\n", '(string)', 3
|
1869
1269
|
result.must_equal s(:block,
|
1870
|
-
s(:call, nil, :foo
|
1871
|
-
s(:call, nil, :bar
|
1270
|
+
s(:call, nil, :foo),
|
1271
|
+
s(:call, nil, :bar))
|
1872
1272
|
result.line.must_equal 3
|
1873
1273
|
result[1].line.must_equal 3
|
1874
1274
|
result[2].line.must_equal 4
|