summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/monitor/test_monitor.rb2
-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/ripper_test.rb14
-rw-r--r--test/prism/ruby/source_test.rb60
-rw-r--r--test/ruby/test_thread.rb33
-rw-r--r--test/ruby/test_zjit.rb40
11 files changed, 110 insertions, 81 deletions
diff --git a/test/monitor/test_monitor.rb b/test/monitor/test_monitor.rb
index 4c55afca6c..7a26831baf 100644
--- a/test/monitor/test_monitor.rb
+++ b/test/monitor/test_monitor.rb
@@ -274,7 +274,7 @@ class TestMonitor < Test::Unit::TestCase
@monitor.synchronize do
queue2.enq(nil)
assert_equal("foo", b)
- result2 = cond.wait(0.1)
+ result2 = cond.wait(10)
assert_equal(true, result2)
assert_equal("bar", b)
end
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/ripper_test.rb b/test/prism/ruby/ripper_test.rb
index c8d259135f..a89a9503b9 100644
--- a/test/prism/ruby/ripper_test.rb
+++ b/test/prism/ruby/ripper_test.rb
@@ -136,7 +136,7 @@ module Prism
assert_equal(expected, lexer.parse[0].to_a)
assert_equal(lexer.parse[0].to_a, lexer.scan[0].to_a)
- assert_equal(%i[on_int on_sp on_op], Translation::Ripper::Lexer.new("1 +").lex.map(&:event))
+ assert_equal(%i[on_int on_sp on_op], Translation::Ripper::Lexer.new("1 +").lex.map { |token| token[1] })
assert_raise(SyntaxError) { Translation::Ripper::Lexer.new("1 +").lex(raise_errors: true) }
end
@@ -169,13 +169,13 @@ module Prism
# Prism emits tokens by their order in the code, not in parse order
ripper.sort_by! { |elem| elem[0] }
- [prism.size, ripper.size].max.times do |i|
- expected = ripper[i]
- actual = prism[i]
+ [prism.size, ripper.size].max.times do |index|
+ expected = ripper[index]
+ actual = prism[index]
- # Since tokens related to heredocs are not emitted in the same order,
- # the state also doesn't line up.
- if expected && actual && expected[1] == :on_heredoc_end && actual[1] == :on_heredoc_end
+ # There are some tokens that have slightly different state that do not
+ # effect the parse tree, so they may not match.
+ if expected && actual && expected[1] == actual[1] && %i[on_comment on_heredoc_end on_embexpr_end on_sp].include?(expected[1])
expected[3] = actual[3] = nil
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
diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb
index 60e3aa772a..b2d8e73693 100644
--- a/test/ruby/test_thread.rb
+++ b/test/ruby/test_thread.rb
@@ -1664,37 +1664,4 @@ q.pop
assert_operator elapsed, :>=, 0.1, "sub-millisecond sleeps should not return immediately"
end;
end
-
- # [Bug #21840]
- def test_mutex_owner_doesnt_starve_waiters
- assert_ruby_status([], "#{<<~"begin;"}\n#{<<~'end;'}")
- begin;
- m = Mutex.new
-
- fib = lambda { |n|
- return n if n <= 1
- fib(n - 1) + fib(n - 2)
- }
-
- t1_running = false
- t1 = Thread.new do
- t1_running = true
- loop do
- fib(20)
- m.synchronize do
- File.open(__FILE__) { } # reset timeslice due to blocking operation
- end
- end
- end
-
- loop until t1_running
-
- 3.times.map do
- Thread.new do
- m.synchronize do
- end
- end
- end.each(&:join)
- end;
- end
end
diff --git a/test/ruby/test_zjit.rb b/test/ruby/test_zjit.rb
index 2066610cb2..6ad06f9453 100644
--- a/test/ruby/test_zjit.rb
+++ b/test/ruby/test_zjit.rb
@@ -1496,6 +1496,46 @@ class TestZJIT < Test::Unit::TestCase
}, call_threshold: 2
end
+ def test_invokesuperforward
+ assert_compiles '[1, 2, 3]', %q{
+ class A
+ def foo(a,b,c) = [a,b,c]
+ end
+
+ class B < A
+ def foo(...) = super
+ end
+
+ def test
+ B.new.foo(1, 2, 3)
+ end
+
+ test
+ test
+ }, call_threshold: 2
+ end
+
+ def test_invokesuperforward_with_args_kwargs_and_block
+ assert_compiles '[[1, 2], {x: 3}, 4]', %q{
+ class A
+ def foo(*args, **kwargs, &block)
+ [args, kwargs, block&.call]
+ end
+ end
+
+ class B < A
+ def foo(...) = super
+ end
+
+ def test
+ B.new.foo(1, 2, x: 3) { 4 }
+ end
+
+ test
+ test
+ }, call_threshold: 2
+ end
+
def test_send_with_non_constant_keyword_default
assert_compiles '[[2, 4, 16], [10, 4, 16], [2, 20, 16], [2, 4, 30], [10, 20, 30]]', %q{
def dbl(x = 1) = x * 2