From ed602b9f2b01426f42a18eca6db312e299fdfafc Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 30 Nov 2023 17:03:59 +0100 Subject: [PATCH 1/4] Use a proper feature check to check if Data is defined --- lib/pp.rb | 2 +- test/test_pp.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 1708dee..7c4ba0e 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -438,7 +438,7 @@ def pretty_print(q) # :nodoc: def pretty_print_cycle(q) # :nodoc: q.text sprintf("#", PP.mcall(self, Kernel, :class).name) end -end if "3.2" <= RUBY_VERSION +end if defined?(Data.define) class Range # :nodoc: def pretty_print(q) # :nodoc: diff --git a/test/test_pp.rb b/test/test_pp.rb index bb2299a..bfd5053 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -143,7 +143,7 @@ def test_struct assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) end - if "3.2" <= RUBY_VERSION + if defined?(Data.define) D = Data.define(:aaa, :bbb) def test_data a = D.new("aaa", "bbb") From 6a97d36fbb790d3d7caee10e393fb930ed0dbae5 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 30 Nov 2023 17:04:39 +0100 Subject: [PATCH 2/4] Use .class.members for pretty printing Data * Data#members might not be defined, instead it might be defined on Data subclasses or a module included there. This is notably the case on TruffleRuby which defines it there for optimization purposes. In fact the mere presence of Data#members implies a megamorphic call inside, so it seems best to avoid relying on its existence. --- lib/pp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pp.rb b/lib/pp.rb index 7c4ba0e..5e5fd06 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -423,7 +423,7 @@ def pretty_print_cycle(q) # :nodoc: class Data # :nodoc: def pretty_print(q) # :nodoc: q.group(1, sprintf("#') { - q.seplist(PP.mcall(self, Data, :members), lambda { q.text "," }) {|member| + q.seplist(PP.mcall(self, Kernel, :class).members, lambda { q.text "," }) {|member| q.breakable q.text member.to_s q.text '=' From bed72bfcb8d4819a39cf27911f1cd6b412084dcb Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 30 Nov 2023 17:10:40 +0100 Subject: [PATCH 3/4] Fix pretty printing a Data subclass instance when the subclass is anonymous * It would be "#" (double space) instead of "#" (like #inspect). --- lib/pp.rb | 4 +++- test/test_pp.rb | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pp.rb b/lib/pp.rb index 5e5fd06..fb7aa7b 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -422,7 +422,9 @@ def pretty_print_cycle(q) # :nodoc: class Data # :nodoc: def pretty_print(q) # :nodoc: - q.group(1, sprintf("#') { + class_name = PP.mcall(self, Kernel, :class).name + class_name = " #{class_name}" if class_name + q.group(1, "#') { q.seplist(PP.mcall(self, Kernel, :class).members, lambda { q.text "," }) {|member| q.breakable q.text member.to_s diff --git a/test/test_pp.rb b/test/test_pp.rb index bfd5053..929f933 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -149,6 +149,9 @@ def test_data a = D.new("aaa", "bbb") assert_equal("#\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 From 6e5c7d741e4d95a9924abd31172a87b5878190bb Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 30 Nov 2023 17:19:30 +0100 Subject: [PATCH 4/4] Add TruffleRuby in CI * Only 2 cyclic tests are failing, with the ... in a slightly different place in the output. --- .github/workflows/test.yml | 1 + test/test_pp.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b71d424..77aed90 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,6 +26,7 @@ jobs: - { os: windows-latest, ruby: mingw } - { os: windows-latest, ruby: mswin } - { os: ubuntu-latest, ruby: 'jruby-head', bundle: 'bundle exec' } + - { os: ubuntu-latest, ruby: 'truffleruby-head' } runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 diff --git a/test/test_pp.rb b/test/test_pp.rb index 929f933..473133b 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -140,7 +140,7 @@ def test_struct a = S.new(1,2) a.b = a assert_equal("#>\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) @@ -167,7 +167,7 @@ def test_anonymous end def test_withinspect - omit if RUBY_ENGINE == "jruby" + omit if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby" a = [] a << HasInspect.new(a) assert_equal("[]\n", PP.pp(a, ''.dup))