Skip to content

Commit 1c4438b

Browse files
committed
Parse individual snippets of test/fixtures/*.txt
* To ensure the parser does not try to read bytes further than the end of each snippet.
1 parent 6b778d7 commit 1c4438b

File tree

5 files changed

+386
-366
lines changed

5 files changed

+386
-366
lines changed

test/fixtures/constants.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ A::>>
5151
A::<<
5252

5353
A::\
54+
#
55+
C
5456

5557
A::alias
5658

@@ -163,6 +165,7 @@ A::**
163165
A::~
164166

165167
A::_::
168+
C
166169

167170
A::_..
168171

test/fixtures/strings.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
%[foo[]]
3838

3939
"foo" +
40-
40+
#
4141
"bar"
4242

4343
%q{abc}

test/parse_test.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ def find_source_file_node(node)
4848
end
4949
end
5050

51-
def test_parse_dollar0
52-
parsed_result = YARP.parse("$0", "-e")
53-
assert_equal 2, parsed_result.value.location.length
54-
end
55-
5651
def test_parse_takes_file_path
5752
filepath = "filepath.rb"
5853
parsed_result = YARP.parse("def foo; __FILE__; end", filepath)
@@ -127,6 +122,26 @@ def test_parse_takes_file_path
127122
end
128123
end
129124

125+
Dir["*.txt", base: base].each do |relative|
126+
# We test every snippet (separated by \n\n) in isolation
127+
# to ensure the parser does not try to read bytes further than the end of each snippet
128+
define_method "test_individual_snippets_#{relative}" do
129+
filepath = File.join(base, relative)
130+
131+
# First, read the source from the filepath. Use binmode to avoid converting CRLF on Windows,
132+
# and explicitly set the external encoding to UTF-8 to override the binmode default.
133+
file_contents = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)
134+
135+
file_contents.split(/(?<=\S)\n\n(?=\S)/).each do |snippet|
136+
snippet = snippet.rstrip
137+
result = YARP.parse(snippet, relative)
138+
assert_empty result.errors
139+
140+
assert_equal_nodes(result.value, YARP.load(snippet, YARP.dump(snippet, relative)))
141+
end
142+
end
143+
end
144+
130145
private
131146

132147
def ignore_warnings

0 commit comments

Comments
 (0)