summaryrefslogtreecommitdiff
path: root/test/test_pp.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_pp.rb')
-rw-r--r--test/test_pp.rb62
1 files changed, 50 insertions, 12 deletions
diff --git a/test/test_pp.rb b/test/test_pp.rb
index 9cef555d79..2fdd5df114 100644
--- a/test/test_pp.rb
+++ b/test/test_pp.rb
@@ -5,15 +5,6 @@ require 'delegate'
require 'test/unit'
require 'ruby2_keywords'
-# Define bind_call for Ruby 2.6 and earlier, to allow testing on JRuby 9.3
-class UnboundMethod
- unless public_method_defined?(:bind_call)
- def bind_call(obj, *args, &block)
- bind(obj).call(*args, &block)
- end
- end
-end
-
module PPTestModule
class PPTest < Test::Unit::TestCase
@@ -37,6 +28,13 @@ class PPTest < Test::Unit::TestCase
end
assert_equal(%(""\n), PP.pp(o, "".dup))
end
+
+ def test_range
+ assert_equal("0..1\n", PP.pp(0..1, "".dup))
+ assert_equal("0...1\n", PP.pp(0...1, "".dup))
+ assert_equal("0...\n", PP.pp(0..., "".dup))
+ assert_equal("...1\n", PP.pp(...1, "".dup))
+ end
end
class HasInspect
@@ -149,7 +147,19 @@ class PPCycleTest < Test::Unit::TestCase
a = S.new(1,2)
a.b = a
assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''.dup))
- assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
+ assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) unless RUBY_ENGINE == "truffleruby"
+ end
+
+ if defined?(Data.define)
+ D = Data.define(:aaa, :bbb)
+ def test_data
+ a = D.new("aaa", "bbb")
+ assert_equal("#<data PPTestModule::PPCycleTest::D\n aaa=\"aaa\",\n bbb=\"bbb\">\n", PP.pp(a, ''.dup, 20))
+ assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
+
+ b = Data.define(:a).new(42)
+ assert_equal("#{b.inspect}\n", PP.pp(b, ''.dup))
+ end
end
def test_object
@@ -164,11 +174,12 @@ class PPCycleTest < Test::Unit::TestCase
end
def test_withinspect
+ omit if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
a = []
a << HasInspect.new(a)
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''.dup))
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
- end unless RUBY_VERSION < "2.7" # temporary mask to test on JRuby 9.3 (2.6 equivalent)
+ end
def test_share_nil
begin
@@ -188,6 +199,7 @@ class PPSingleLineTest < Test::Unit::TestCase
end
def test_hash_in_array
+ omit if RUBY_ENGINE == "jruby"
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
end
@@ -226,10 +238,36 @@ if defined?(RubyVM)
AST = RubyVM::AbstractSyntaxTree
def test_lasgn_literal
ast = AST.parse("_=1")
- expected = "(SCOPE@1:0-1:3 tbl: [:_] args: nil body: (LASGN@1:0-1:3 :_ (LIT@1:2-1:3 1)))"
+ integer = RUBY_VERSION >= "3.4." ? "INTEGER" : "LIT"
+ expected = "(SCOPE@1:0-1:3 tbl: [:_] args: nil body: (LASGN@1:0-1:3 :_ (#{integer}@1:2-1:3 1)))"
assert_equal(expected, PP.singleline_pp(ast, ''.dup), ast)
end
end
end
+class PPInheritedTest < Test::Unit::TestCase
+ class PPSymbolHash < PP
+ def pp_hash_pair(k, v)
+ case k
+ when Symbol
+ text k.inspect.delete_prefix(":")
+ text ":"
+ group(1) {
+ breakable
+ pp v
+ }
+ else
+ super
+ end
+ end
+ end
+
+ def test_hash_override
+ obj = {k: 1, "": :null, "0": :zero, 100 => :ten}
+ assert_equal <<~EXPECT, PPSymbolHash.pp(obj, "".dup)
+ {k: 1, "": :null, "0": :zero, 100=>:ten}
+ EXPECT
+ end
+end
+
end