Skip to content

Commit fb7e1eb

Browse files
committed
Reconfigure error tests
1 parent 035890a commit fb7e1eb

File tree

239 files changed

+1299
-2249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+1299
-2249
lines changed

bin/prism

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Prism
1313
when "console" then console
1414
when "dot" then dot(argv)
1515
when "encoding" then encoding(argv)
16+
when "error" then error(argv)
1617
when "lex" then lex(argv)
1718
when "locals" then locals(argv)
1819
when "parse" then parse(argv)
@@ -27,6 +28,7 @@ module Prism
2728
bin/prism console
2829
bin/prism dot [source]
2930
bin/prism encoding [encoding]
31+
bin/prism error [name] [source]
3032
bin/prism lex [source]
3133
bin/prism locals [source]
3234
bin/prism parse [source]
@@ -157,6 +159,29 @@ module Prism
157159
unicode_lists(found) if found == Encoding::UTF_8 || found == Encoding::UTF8_MAC
158160
end
159161

162+
# bin/prism error [name] [source]
163+
def error(argv)
164+
name = argv.shift
165+
source, _ = read_source(argv)
166+
167+
result = Prism.parse(source)
168+
raise "Expected #{source.inspect} to have errors" if result.success?
169+
170+
filepath = File.expand_path("../test/prism/errors/#{name}.txt", __dir__)
171+
172+
if File.file?(filepath)
173+
counter = 1
174+
175+
begin
176+
current = "#{File.dirname(filepath)}/#{File.basename(filepath, ".txt")}_#{counter += 1}.txt"
177+
end while File.file?(current)
178+
179+
filepath = current
180+
end
181+
182+
File.write(filepath, result.errors_format)
183+
end
184+
160185
# bin/prism lex [source]
161186
def lex(argv)
162187
source, filepath = read_source(argv)

lib/prism/parse_result.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ def failure?
575575
# This is a result specific to the `parse` and `parse_file` methods.
576576
class ParseResult < Result
577577
autoload :Comments, "prism/parse_result/comments"
578+
autoload :Errors, "prism/parse_result/errors"
578579
autoload :Newlines, "prism/parse_result/newlines"
579580

580581
private_constant :Comments
@@ -604,6 +605,12 @@ def attach_comments!
604605
def mark_newlines!
605606
value.accept(Newlines.new(source.offsets.size)) # steep:ignore
606607
end
608+
609+
# Returns a string representation of the syntax tree with the errors
610+
# displayed inline.
611+
def errors_format
612+
Errors.new(self).format
613+
end
607614
end
608615

609616
# This is a result specific to the `lex` and `lex_file` methods.

lib/prism/parse_result/errors.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
require "stringio"
4+
5+
module Prism
6+
class ParseResult < Result
7+
class Errors
8+
attr_reader :parse_result
9+
10+
def initialize(parse_result)
11+
@parse_result = parse_result
12+
end
13+
14+
def format
15+
error_lines = {}
16+
parse_result.errors.each do |error|
17+
location = error.location
18+
(location.start_line..location.end_line).each do |line|
19+
error_lines[line] ||= []
20+
error_lines[line] << error
21+
end
22+
end
23+
24+
source_lines = parse_result.source.source.lines
25+
source_lines << "" if error_lines.key?(source_lines.size + 1)
26+
27+
io = StringIO.new
28+
source_lines.each.with_index(1) do |line, line_number|
29+
io.puts(line)
30+
31+
(error_lines.delete(line_number) || []).each do |error|
32+
location = error.location
33+
34+
case line_number
35+
when location.start_line
36+
io.print(" " * location.start_column + "^")
37+
38+
if location.start_line == location.end_line
39+
if location.start_column != location.end_column
40+
io.print("~" * (location.end_column - location.start_column - 1))
41+
end
42+
43+
io.puts(" " + error.message)
44+
else
45+
io.puts("~" * (line.bytesize - location.start_column))
46+
end
47+
when location.end_line
48+
io.puts("~" * location.end_column + " " + error.message)
49+
else
50+
io.puts("~" * line.bytesize)
51+
end
52+
end
53+
end
54+
55+
io.puts
56+
io.string
57+
end
58+
end
59+
end
60+
end

test/prism/errors/1_2_3.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(1, 2, 3)
2+
^ unexpected ',', expecting end-of-input
3+
^ unexpected ',', ignoring it
4+
^ expected a matching `)`
5+
^ unexpected ',', expecting end-of-input
6+
^ unexpected ',', ignoring it
7+
^ unexpected ',', expecting end-of-input
8+
^ unexpected ',', ignoring it
9+
^ unexpected ')', expecting end-of-input
10+
^ unexpected ')', ignoring it
11+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
alias $a $1
2+
^~ invalid argument being passed to `alias`; can't make alias for the number variables
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
alias $a b
2+
^ invalid argument being passed to `alias`; expected a bare word, symbol, constant, or global variable
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
alias a $b
2+
^~ invalid argument being passed to `alias`; expected a bare word, symbol, constant, or global variable
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%qXfooX
2+
^ unknown type of %string
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%QXfooX
2+
^ unknown type of %string
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%wXfooX
2+
^ unknown type of %string
3+

0 commit comments

Comments
 (0)