-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathparser_test.rb
More file actions
126 lines (104 loc) · 3.35 KB
/
parser_test.rb
File metadata and controls
126 lines (104 loc) · 3.35 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
require_relative "test_helper"
module SyntaxTree
class ParserTest < Minitest::Test
def test_parses_ripper_methods
# First, get a list of all of the dispatched events from ripper.
events = Ripper::EVENTS
# Next, subtract all of the events that we have explicitly defined.
events -=
Parser.private_instance_methods(false).grep(/^on_(\w+)/) { $1.to_sym }
# Next, subtract the list of events that we purposefully skipped.
events -= %i[
arg_ambiguous
assoclist_from_args
ignored_nl
ignored_sp
magic_comment
nl
nokw_param
operator_ambiguous
semicolon
sp
words_sep
]
# Finally, assert that we have no remaining events.
assert_empty(events)
end
def test_errors_on_missing_token_with_location
error = assert_raises(Parser::ParseError) { SyntaxTree.parse("f+\"foo") }
assert_equal(3, error.column)
end
def test_errors_on_missing_end_with_location
error = assert_raises(Parser::ParseError) { SyntaxTree.parse("foo do 1") }
assert_equal(4, error.column)
end
def test_errors_on_missing_regexp_ending
error =
assert_raises(Parser::ParseError) { SyntaxTree.parse("a =~ /foo") }
assert_equal(6, error.column)
end
def test_errors_on_missing_token_without_location
assert_raises(Parser::ParseError) { SyntaxTree.parse(":\"foo") }
end
def test_handles_strings_with_non_terminated_embedded_expressions
assert_raises(Parser::ParseError) { SyntaxTree.parse('"#{"') }
end
def test_errors_on_else_missing_two_ends
assert_raises(Parser::ParseError) { SyntaxTree.parse(<<~RUBY) }
def foo
if something
else
call do
end
RUBY
end
def test_does_not_choke_on_invalid_characters_in_source_string
SyntaxTree.parse(<<~RUBY)
# comment
# comment
__END__
\xC5
RUBY
end
def test_lambda_vars_with_parameters_location
tree = SyntaxTree.parse(<<~RUBY)
# comment
# comment
->(_i; a) { a }
RUBY
local_location =
tree.statements.body.last.params.contents.locals.first.location
assert_equal(3, local_location.start_line)
assert_equal(3, local_location.end_line)
assert_equal(7, local_location.start_column)
assert_equal(8, local_location.end_column)
end
def test_lambda_vars_location
tree = SyntaxTree.parse(<<~RUBY)
# comment
# comment
->(; a) { a }
RUBY
local_location =
tree.statements.body.last.params.contents.locals.first.location
assert_equal(3, local_location.start_line)
assert_equal(3, local_location.end_line)
assert_equal(5, local_location.start_column)
assert_equal(6, local_location.end_column)
end
def test_multiple_lambda_vars_location
tree = SyntaxTree.parse(<<~RUBY)
# comment
# comment
->(; a, b, c) { a }
RUBY
local_location =
tree.statements.body.last.params.contents.locals.last.location
assert_equal(3, local_location.start_line)
assert_equal(3, local_location.end_line)
assert_equal(11, local_location.start_column)
assert_equal(12, local_location.end_column)
end
end
end