Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e68f225

Browse files
committed
More test cases, more tests
1 parent 1735dd6 commit e68f225

File tree

12 files changed

+167
-18
lines changed

12 files changed

+167
-18
lines changed

lib/syntax_tree/node.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,12 +5484,14 @@ def format_flat(q)
54845484
q.format(predicate)
54855485
q.text(" ?")
54865486

5487-
q.breakable
5488-
q.format(truthy)
5489-
q.text(" :")
5487+
q.indent do
5488+
q.breakable
5489+
q.format(truthy)
5490+
q.text(" :")
54905491

5491-
q.breakable
5492-
q.format(falsy)
5492+
q.breakable
5493+
q.format(falsy)
5494+
end
54935495
end
54945496
end
54955497

lib/syntax_tree/parser.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,6 @@ def on_aryptn(constant, requireds, rest, posts)
548548
parts[0].location.to(parts[-1].location)
549549
end
550550

551-
# If there's the optional then keyword, then we'll delete that and use it
552-
# as the end bounds of the location.
553-
if (token = find_token(Kw, "then", consume: false))
554-
tokens.delete(token)
555-
location = location.to(token.location)
556-
end
557-
558551
# If there is a plain *, then we're going to fix up the location of it
559552
# here because it currently doesn't have anything to use for its precise
560553
# location. If we hit a comma, then we've gone too far.
@@ -1698,12 +1691,6 @@ def on_hshptn(constant, keywords, keyword_rest)
16981691
end
16991692
end
17001693

1701-
# Delete the optional then keyword
1702-
if (token = find_token(Kw, "then", consume: false))
1703-
parts << token
1704-
tokens.delete(token)
1705-
end
1706-
17071694
HshPtn.new(
17081695
constant: constant,
17091696
keywords: keywords || [],
@@ -3013,6 +3000,11 @@ def on_stmts_new
30133000
# (StringEmbExpr | StringDVar | TStringContent) part
30143001
# ) -> StringContent
30153002
def on_string_add(string, part)
3003+
# Due to some eccentricities in how ripper works, you need this here in
3004+
# case you have a syntax error with an embedded expression that doesn't
3005+
# finish, as in: "#{"
3006+
return string if part.is_a?(String)
3007+
30163008
location =
30173009
string.parts.any? ? string.location.to(part.location) : part.location
30183010

test/fixtures/aryptn.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
end
55
%
66
case foo
7+
in [] then
8+
end
9+
-
10+
case foo
11+
in []
12+
end
13+
%
14+
case foo
15+
in * then
16+
end
17+
-
18+
case foo
19+
in [*]
20+
end
21+
%
22+
case foo
723
in _, _
824
end
925
-

test/fixtures/call.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
%
22
foo.bar
33
%
4+
foo.bar(baz)
5+
%
46
foo.()
57
%
68
foo::()
@@ -21,3 +23,40 @@
2123
.barrrrrrrrrrrrrrrrrrr {}
2224
.bazzzzzzzzzzzzzzzzzzzzzzzzzz
2325
.quxxxxxxxxx
26+
%
27+
foo. # comment
28+
bar
29+
%
30+
foo
31+
.bar
32+
.baz # comment
33+
.qux
34+
.quux
35+
%
36+
foo
37+
.bar
38+
.baz.
39+
# comment
40+
qux
41+
.quux
42+
%
43+
{ a: 1, b: 2 }.fooooooooooooooooo.barrrrrrrrrrrrrrrrrrr.bazzzzzzzzzzzz.quxxxxxxxxxxxx
44+
-
45+
{ a: 1, b: 2 }.fooooooooooooooooo
46+
.barrrrrrrrrrrrrrrrrrr
47+
.bazzzzzzzzzzzz
48+
.quxxxxxxxxxxxx
49+
%
50+
fooooooooooooooooo.barrrrrrrrrrrrrrrrrrr.bazzzzzzzzzzzz.quxxxxxxxxxxxx.each { block }
51+
-
52+
fooooooooooooooooo.barrrrrrrrrrrrrrrrrrr.bazzzzzzzzzzzz.quxxxxxxxxxxxx.each do
53+
block
54+
end
55+
%
56+
foo.bar.baz.each do
57+
block1
58+
block2
59+
end
60+
%
61+
a b do
62+
end.c d

test/fixtures/command_call.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@
3232
foo.
3333
# comment
3434
bar baz
35+
%
36+
foo.bar baz ? qux : qaz

test/fixtures/do_block.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@
1414
foo :bar do
1515
baz
1616
end
17+
%
18+
sig do
19+
override.params(contacts: Contact::ActiveRecord_Relation).returns(
20+
Customer::ActiveRecord_Relation
21+
)
22+
end
23+
-
24+
sig do
25+
override
26+
.params(contacts: Contact::ActiveRecord_Relation)
27+
.returns(Customer::ActiveRecord_Relation)
28+
end

test/fixtures/hshptn.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
end
6565
%
6666
case foo
67+
in {} then
68+
end
69+
-
70+
case foo
71+
in {}
72+
end
73+
%
74+
case foo
6775
in **nil
6876
end
6977
%

test/fixtures/if.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@
4949
end
5050
-
5151
not(a) ? b : c
52+
%
53+
(if foo then bar else baz end)
54+
-
55+
(
56+
if foo
57+
bar
58+
else
59+
baz
60+
end
61+
)

test/fixtures/ifop.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
end
1111
%
1212
foo bar ? 1 : 2
13+
%
14+
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ? break : baz
15+
-
16+
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ?
17+
break :
18+
baz

test/location_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
5+
module SyntaxTree
6+
class LocationTest < Minitest::Test
7+
def test_lines
8+
location = Location.fixed(line: 1, char: 0, column: 0)
9+
location = location.to(Location.fixed(line: 3, char: 3, column: 3))
10+
11+
assert_equal(1..3, location.lines)
12+
end
13+
14+
def test_deconstruct
15+
location = Location.fixed(line: 1, char: 0, column: 0)
16+
17+
case location
18+
in [1, 0, 0, *]
19+
end
20+
end
21+
22+
def test_deconstruct_keys
23+
location = Location.fixed(line: 1, char: 0, column: 0)
24+
25+
case location
26+
in { start_line: 1 }
27+
end
28+
end
29+
end
30+
end

test/node_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,20 @@ def test_multibyte_column_positions
10321032
assert_node(Command, source, at: at)
10331033
end
10341034

1035+
def test_root_class_raises_not_implemented_errors
1036+
{
1037+
accept: [nil],
1038+
child_nodes: [],
1039+
deconstruct: [],
1040+
deconstruct_keys: [[]],
1041+
format: [nil]
1042+
}.each do |method, arguments|
1043+
assert_raises(NotImplementedError) do
1044+
Node.new.public_send(method, *arguments)
1045+
end
1046+
end
1047+
end
1048+
10351049
private
10361050

10371051
def location(lines: 1..1, chars: 0..0, columns: 0..0)

test/parser_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,23 @@ def test_parses_ripper_methods
3030
# Finally, assert that we have no remaining events.
3131
assert_empty(events)
3232
end
33+
34+
def test_errors_on_missing_token_with_location
35+
assert_raises(Parser::ParseError) do
36+
SyntaxTree.parse("\"foo")
37+
end
38+
end
39+
40+
def test_errors_on_missing_token_without_location
41+
assert_raises(Parser::ParseError) do
42+
SyntaxTree.parse(":\"foo")
43+
end
44+
end
45+
46+
def test_handles_strings_with_non_terminated_embedded_expressions
47+
assert_raises(Parser::ParseError) do
48+
SyntaxTree.parse('"#{"')
49+
end
50+
end
3351
end
3452
end

0 commit comments

Comments
 (0)