From 439f069b4b421964dab39b0a01e0afdae89ea26c Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 25 Aug 2023 09:23:46 -0400 Subject: [PATCH] [ruby/yarp] Move tests from test/* to test/yarp/* to match CRuby This should make it easier on the sync to determine what changed and hopefully result in fewer merge conflicts that have to be manually resolved. https://github.com/ruby/yarp/commit/17d82afbfc --- test/yarp/bom_test.rb | 2 +- test/yarp/comments_test.rb | 2 +- test/{ => yarp}/desugar_visitor_test.rb | 2 +- test/yarp/encoding_test.rb | 2 +- test/yarp/errors_test.rb | 2 +- test/yarp/heredoc_dedent_test.rb | 2 +- test/yarp/library_symbols_test.rb | 2 +- test/yarp/locals_test.rb | 2 +- test/yarp/location_test.rb | 2 +- test/yarp/memsize_test.rb | 2 +- test/yarp/newline_test.rb | 2 +- test/yarp/parse_serialize_test.rb | 2 +- test/yarp/parse_test.rb | 2 +- test/yarp/regexp_test.rb | 2 +- test/yarp/ripper_compat_test.rb | 2 +- test/yarp/ruby_api_test.rb | 2 +- test/yarp/test_helper.rb | 100 ++++++++++++++++++++++++ test/yarp/unescape_test.rb | 2 +- test/yarp/version_test.rb | 2 +- 19 files changed, 118 insertions(+), 18 deletions(-) rename test/{ => yarp}/desugar_visitor_test.rb (99%) create mode 100644 test/yarp/test_helper.rb diff --git a/test/yarp/bom_test.rb b/test/yarp/bom_test.rb index 7dc7eabe92..b386a5d9a3 100644 --- a/test/yarp/bom_test.rb +++ b/test/yarp/bom_test.rb @@ -4,7 +4,7 @@ # test. return if RUBY_ENGINE == "jruby" || RUBY_ENGINE == "truffleruby" -require "yarp_test_helper" +require_relative "test_helper" class BOMTest < Test::Unit::TestCase def test_ident diff --git a/test/yarp/comments_test.rb b/test/yarp/comments_test.rb index fdb70045ca..13bf819a4e 100644 --- a/test/yarp/comments_test.rb +++ b/test/yarp/comments_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class CommentsTest < Test::Unit::TestCase include ::YARP::DSL diff --git a/test/desugar_visitor_test.rb b/test/yarp/desugar_visitor_test.rb similarity index 99% rename from test/desugar_visitor_test.rb rename to test/yarp/desugar_visitor_test.rb index 3af3d9deb4..a893422a7c 100644 --- a/test/desugar_visitor_test.rb +++ b/test/yarp/desugar_visitor_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class DesugarVisitorTest < Test::Unit::TestCase def test_and_write diff --git a/test/yarp/encoding_test.rb b/test/yarp/encoding_test.rb index 7bf99ece92..2ee084cd38 100644 --- a/test/yarp/encoding_test.rb +++ b/test/yarp/encoding_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class EncodingTest < Test::Unit::TestCase %w[ diff --git a/test/yarp/errors_test.rb b/test/yarp/errors_test.rb index 7b20ceadab..074dd7129e 100644 --- a/test/yarp/errors_test.rb +++ b/test/yarp/errors_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class ErrorsTest < Test::Unit::TestCase include ::YARP::DSL diff --git a/test/yarp/heredoc_dedent_test.rb b/test/yarp/heredoc_dedent_test.rb index 64627c564f..2744b930ed 100644 --- a/test/yarp/heredoc_dedent_test.rb +++ b/test/yarp/heredoc_dedent_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" module YARP class HeredocDedentTest < Test::Unit::TestCase diff --git a/test/yarp/library_symbols_test.rb b/test/yarp/library_symbols_test.rb index 766adf16b7..c5927c2a89 100644 --- a/test/yarp/library_symbols_test.rb +++ b/test/yarp/library_symbols_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" if RUBY_PLATFORM =~ /linux/ # diff --git a/test/yarp/locals_test.rb b/test/yarp/locals_test.rb index 4d9cea94a8..42fb72df78 100644 --- a/test/yarp/locals_test.rb +++ b/test/yarp/locals_test.rb @@ -13,7 +13,7 @@ return if !defined?(RubyVM::InstructionSequence) || RUBY_VERSION < "3.2" # Ruby is handling large ISeqs on 32-bit machines return if RUBY_PLATFORM =~ /i686/ -require "yarp_test_helper" +require_relative "test_helper" class LocalsTest < Test::Unit::TestCase invalid = [] diff --git a/test/yarp/location_test.rb b/test/yarp/location_test.rb index 424c9778f8..8e357fa193 100644 --- a/test/yarp/location_test.rb +++ b/test/yarp/location_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" module YARP class LocationTest < Test::Unit::TestCase diff --git a/test/yarp/memsize_test.rb b/test/yarp/memsize_test.rb index 30de1085cf..9ff670c118 100644 --- a/test/yarp/memsize_test.rb +++ b/test/yarp/memsize_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" return if YARP::BACKEND == :FFI diff --git a/test/yarp/newline_test.rb b/test/yarp/newline_test.rb index 80f6329d94..2eaaefc61e 100644 --- a/test/yarp/newline_test.rb +++ b/test/yarp/newline_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" return unless defined?(RubyVM::InstructionSequence) diff --git a/test/yarp/parse_serialize_test.rb b/test/yarp/parse_serialize_test.rb index 6789ba9c43..daecbe1488 100644 --- a/test/yarp/parse_serialize_test.rb +++ b/test/yarp/parse_serialize_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" return if YARP::BACKEND == :FFI diff --git a/test/yarp/parse_test.rb b/test/yarp/parse_test.rb index 1f8b1374f6..b9f852abf9 100644 --- a/test/yarp/parse_test.rb +++ b/test/yarp/parse_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class ParseTest < Test::Unit::TestCase # When we pretty-print the trees to compare against the snapshots, we want to diff --git a/test/yarp/regexp_test.rb b/test/yarp/regexp_test.rb index bb236e2f1f..241fcc862f 100644 --- a/test/yarp/regexp_test.rb +++ b/test/yarp/regexp_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" return if YARP::BACKEND == :FFI diff --git a/test/yarp/ripper_compat_test.rb b/test/yarp/ripper_compat_test.rb index 4350ba7f81..e13cef08b1 100644 --- a/test/yarp/ripper_compat_test.rb +++ b/test/yarp/ripper_compat_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" module YARP class RipperCompatTest < Test::Unit::TestCase diff --git a/test/yarp/ruby_api_test.rb b/test/yarp/ruby_api_test.rb index 1f66084ad2..f02026541d 100644 --- a/test/yarp/ruby_api_test.rb +++ b/test/yarp/ruby_api_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class YARPRubyAPITest < Test::Unit::TestCase def test_ruby_api diff --git a/test/yarp/test_helper.rb b/test/yarp/test_helper.rb new file mode 100644 index 0000000000..0be0f51651 --- /dev/null +++ b/test/yarp/test_helper.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +require "yarp" +require "ripper" +require "pp" +require "test/unit" +require "tempfile" + +puts "Using YARP backend: #{YARP::BACKEND}" if ENV["YARP_FFI_BACKEND"] + +module YARP + module Assertions + private + + def assert_equal_nodes(expected, actual, compare_location: true, parent: nil) + assert_equal expected.class, actual.class + + case expected + when Array + assert_equal( + expected.size, + actual.size, + -> { "Arrays were different sizes. Parent: #{parent.pretty_inspect}" } + ) + + expected.zip(actual).each do |(expected_element, actual_element)| + assert_equal_nodes( + expected_element, + actual_element, + compare_location: compare_location, + parent: actual + ) + end + when YARP::SourceFileNode + deconstructed_expected = expected.deconstruct_keys(nil) + deconstructed_actual = actual.deconstruct_keys(nil) + assert_equal deconstructed_expected.keys, deconstructed_actual.keys + + # Filepaths can be different if test suites were run + # on different machines. + # We accommodate for this by comparing the basenames, + # and not the absolute filepaths + assert_equal deconstructed_expected.except(:filepath), deconstructed_actual.except(:filepath) + assert_equal File.basename(deconstructed_expected[:filepath]), File.basename(deconstructed_actual[:filepath]) + when YARP::Node + deconstructed_expected = expected.deconstruct_keys(nil) + deconstructed_actual = actual.deconstruct_keys(nil) + assert_equal deconstructed_expected.keys, deconstructed_actual.keys + + deconstructed_expected.each_key do |key| + assert_equal_nodes( + deconstructed_expected[key], + deconstructed_actual[key], + compare_location: compare_location, + parent: actual + ) + end + when YARP::Location + assert_operator actual.start_offset, :<=, actual.end_offset, -> { + "start_offset > end_offset for #{actual.inspect}, parent is #{parent.pretty_inspect}" + } + if compare_location + assert_equal( + expected.start_offset, + actual.start_offset, + -> { "Start locations were different. Parent: #{parent.pretty_inspect}" } + ) + + assert_equal( + expected.end_offset, + actual.end_offset, + -> { "End locations were different. Parent: #{parent.pretty_inspect}" } + ) + + end + else + assert_equal expected, actual + end + end + + def assert_valid_locations(value, parent: nil) + case value + when Array + value.each do |element| + assert_valid_locations(element, parent: value) + end + when YARP::Node + value.deconstruct_keys(nil).each_value do |field| + assert_valid_locations(field, parent: value) + end + when YARP::Location + assert_operator value.start_offset, :<=, value.end_offset, -> { + "start_offset > end_offset for #{value.inspect}, parent is #{parent.pretty_inspect}" + } + end + end + end +end + +Test::Unit::TestCase.include(YARP::Assertions) diff --git a/test/yarp/unescape_test.rb b/test/yarp/unescape_test.rb index 3b9e665248..eef989ad23 100644 --- a/test/yarp/unescape_test.rb +++ b/test/yarp/unescape_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" return if YARP::BACKEND == :FFI diff --git a/test/yarp/version_test.rb b/test/yarp/version_test.rb index f431157ae9..aaace0aa89 100644 --- a/test/yarp/version_test.rb +++ b/test/yarp/version_test.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "yarp_test_helper" +require_relative "test_helper" class VersionTest < Test::Unit::TestCase def test_version_is_set