summaryrefslogtreecommitdiff
path: root/test/prism
diff options
context:
space:
mode:
Diffstat (limited to 'test/prism')
-rw-r--r--test/prism/bom_test.rb3
-rw-r--r--test/prism/errors_test.rb8
-rw-r--r--test/prism/lex_test.rb3
-rw-r--r--test/prism/result/breadth_first_search_test.rb11
-rw-r--r--test/prism/result/overlap_test.rb9
-rw-r--r--test/prism/result/source_location_test.rb8
-rw-r--r--test/prism/ruby/source_test.rb60
7 files changed, 62 insertions, 40 deletions
diff --git a/test/prism/bom_test.rb b/test/prism/bom_test.rb
index 890bc4b36c..0fa00ae4e8 100644
--- a/test/prism/bom_test.rb
+++ b/test/prism/bom_test.rb
@@ -5,6 +5,7 @@
return if RUBY_ENGINE != "ruby"
require_relative "test_helper"
+require "ripper"
module Prism
class BOMTest < TestCase
@@ -53,7 +54,7 @@ module Prism
def assert_bom(source)
bommed = "\xEF\xBB\xBF#{source}"
- assert_equal Prism.lex_ripper(bommed), Prism.lex_compat(bommed).value
+ assert_equal Ripper.lex(bommed), Prism.lex_compat(bommed).value
end
end
end
diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb
index aa264ae5b7..cbe8b06ad6 100644
--- a/test/prism/errors_test.rb
+++ b/test/prism/errors_test.rb
@@ -45,19 +45,19 @@ module Prism
def test_unterminated_string_closing
statement = Prism.parse_statement("'hello")
assert_equal statement.unescaped, "hello"
- assert_empty statement.closing
+ assert_nil statement.closing
end
def test_unterminated_interpolated_string_closing
statement = Prism.parse_statement('"hello')
assert_equal statement.unescaped, "hello"
- assert_empty statement.closing
+ assert_nil statement.closing
end
def test_unterminated_empty_string_closing
statement = Prism.parse_statement('"')
assert_empty statement.unescaped
- assert_empty statement.closing
+ assert_nil statement.closing
end
def test_invalid_message_name
@@ -84,7 +84,7 @@ module Prism
def test_incomplete_def_closing_loc
statement = Prism.parse_statement("def f; 123")
- assert_empty(statement.end_keyword)
+ assert_nil(statement.end_keyword)
end
private
diff --git a/test/prism/lex_test.rb b/test/prism/lex_test.rb
index ea4606d2fb..9a9f203c28 100644
--- a/test/prism/lex_test.rb
+++ b/test/prism/lex_test.rb
@@ -3,6 +3,7 @@
return if !(RUBY_ENGINE == "ruby" && RUBY_VERSION >= "3.2.0")
require_relative "test_helper"
+require "ripper"
module Prism
class LexTest < TestCase
@@ -49,7 +50,7 @@ module Prism
if RUBY_VERSION >= "3.3"
def test_lex_compare
prism = Prism.lex_compat(File.read(__FILE__), version: "current").value
- ripper = Prism.lex_ripper(File.read(__FILE__))
+ ripper = Ripper.lex(File.read(__FILE__))
assert_equal(ripper, prism)
end
end
diff --git a/test/prism/result/breadth_first_search_test.rb b/test/prism/result/breadth_first_search_test.rb
index e2e043a902..7e7962f172 100644
--- a/test/prism/result/breadth_first_search_test.rb
+++ b/test/prism/result/breadth_first_search_test.rb
@@ -14,5 +14,16 @@ module Prism
refute_nil found
assert_equal 8, found.start_offset
end
+
+ def test_breadth_first_search_all
+ result = Prism.parse("[1 + 2, 2]")
+ found_nodes =
+ result.value.breadth_first_search_all do |node|
+ node.is_a?(IntegerNode)
+ end
+
+ assert_equal 3, found_nodes.size
+ assert_equal 8, found_nodes[0].start_offset
+ end
end
end
diff --git a/test/prism/result/overlap_test.rb b/test/prism/result/overlap_test.rb
index 155bc870d3..d605eeca44 100644
--- a/test/prism/result/overlap_test.rb
+++ b/test/prism/result/overlap_test.rb
@@ -33,8 +33,13 @@ module Prism
queue << child
if compare
- assert_operator current.location.start_offset, :<=, child.location.start_offset
- assert_operator current.location.end_offset, :>=, child.location.end_offset
+ assert_operator current.location.start_offset, :<=, child.location.start_offset, -> {
+ "[#{fixture.full_path}] Parent node #{current.class} at #{current.location} does not start before child node #{child.class} at #{child.location}"
+ }
+
+ assert_operator current.location.end_offset, :>=, child.location.end_offset, -> {
+ "[#{fixture.full_path}] Parent node #{current.class} at #{current.location} does not end after child node #{child.class} at #{child.location}"
+ }
end
end
end
diff --git a/test/prism/result/source_location_test.rb b/test/prism/result/source_location_test.rb
index 38b971d02b..993150f581 100644
--- a/test/prism/result/source_location_test.rb
+++ b/test/prism/result/source_location_test.rb
@@ -935,16 +935,16 @@ module Prism
node = yield node if block_given?
if expected.begin == 0
- assert_equal 0, node.location.start_column
+ assert_equal 0, node.location.start_column, "#{kind} start_column"
end
if expected.end == source.length
- assert_equal source.split("\n").last.length, node.location.end_column
+ assert_equal source.split("\n").last.length, node.location.end_column, "#{kind} end_column"
end
assert_kind_of kind, node
- assert_equal expected.begin, node.location.start_offset
- assert_equal expected.end, node.location.end_offset
+ assert_equal expected.begin, node.location.start_offset, "#{kind} start_offset"
+ assert_equal expected.end, node.location.end_offset, "#{kind} end_offset"
end
end
end
diff --git a/test/prism/ruby/source_test.rb b/test/prism/ruby/source_test.rb
index afd2825765..f7cf4fe83a 100644
--- a/test/prism/ruby/source_test.rb
+++ b/test/prism/ruby/source_test.rb
@@ -4,44 +4,48 @@ require_relative "../test_helper"
module Prism
class SourceTest < TestCase
- def test_line_to_byte_offset
- parse_result = Prism.parse(<<~SRC)
+ def test_byte_offset
+ source = Prism.parse(<<~SRC).source
abcd
efgh
ijkl
SRC
- source = parse_result.source
-
- assert_equal 0, source.line_to_byte_offset(1)
- assert_equal 5, source.line_to_byte_offset(2)
- assert_equal 10, source.line_to_byte_offset(3)
- assert_equal 15, source.line_to_byte_offset(4)
- e = assert_raise(ArgumentError) { source.line_to_byte_offset(5) }
- assert_equal "line 5 is out of range", e.message
- e = assert_raise(ArgumentError) { source.line_to_byte_offset(0) }
- assert_equal "line 0 is out of range", e.message
- e = assert_raise(ArgumentError) { source.line_to_byte_offset(-1) }
- assert_equal "line -1 is out of range", e.message
+
+ assert_equal 0, source.byte_offset(1, 0)
+ assert_equal 5, source.byte_offset(2, 0)
+ assert_equal 10, source.byte_offset(3, 0)
+ assert_equal 15, source.byte_offset(4, 0)
+
+ error = assert_raise(ArgumentError) { source.byte_offset(5, 0) }
+ assert_equal "line 5 is out of range", error.message
+
+ error = assert_raise(ArgumentError) { source.byte_offset(0, 0) }
+ assert_equal "line 0 is out of range", error.message
+
+ error = assert_raise(ArgumentError) { source.byte_offset(-1, 0) }
+ assert_equal "line -1 is out of range", error.message
end
- def test_line_to_byte_offset_with_start_line
- parse_result = Prism.parse(<<~SRC, line: 11)
+ def test_byte_offset_with_start_line
+ source = Prism.parse(<<~SRC, line: 11).source
abcd
efgh
ijkl
SRC
- source = parse_result.source
-
- assert_equal 0, source.line_to_byte_offset(11)
- assert_equal 5, source.line_to_byte_offset(12)
- assert_equal 10, source.line_to_byte_offset(13)
- assert_equal 15, source.line_to_byte_offset(14)
- e = assert_raise(ArgumentError) { source.line_to_byte_offset(15) }
- assert_equal "line 15 is out of range", e.message
- e = assert_raise(ArgumentError) { source.line_to_byte_offset(10) }
- assert_equal "line 10 is out of range", e.message
- e = assert_raise(ArgumentError) { source.line_to_byte_offset(9) }
- assert_equal "line 9 is out of range", e.message
+
+ assert_equal 0, source.byte_offset(11, 0)
+ assert_equal 5, source.byte_offset(12, 0)
+ assert_equal 10, source.byte_offset(13, 0)
+ assert_equal 15, source.byte_offset(14, 0)
+
+ error = assert_raise(ArgumentError) { source.byte_offset(15, 0) }
+ assert_equal "line 15 is out of range", error.message
+
+ error = assert_raise(ArgumentError) { source.byte_offset(10, 0) }
+ assert_equal "line 10 is out of range", error.message
+
+ error = assert_raise(ArgumentError) { source.byte_offset(9, 0) }
+ assert_equal "line 9 is out of range", error.message
end
end
end