diff --git a/lib/pp.rb b/lib/pp.rb index ed801f0..6060282 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -452,18 +452,16 @@ def pretty_print_cycle(q) # :nodoc: end class Set # :nodoc: def pretty_print(pp) # :nodoc: - pp.group(1, '#') { - pp.breakable - pp.group(1, '{', '}') { - pp.seplist(self) { |o| - pp.pp o - } + pp.group(1, "#{self.class.name}[", ']') { + pp.seplist(self) { |o| + pp.pp o } } end def pretty_print_cycle(pp) # :nodoc: - pp.text sprintf('#', empty? ? '' : '...') + name = self.class.name + pp.text(empty? ? "#{name}[]" : "#{name}[...]") end end if set_pp diff --git a/test/test_pp.rb b/test/test_pp.rb index 28da00e..c5340a3 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -2,11 +2,14 @@ require 'pp' require 'delegate' +require 'set' require 'test/unit' require 'ruby2_keywords' module PPTestModule +SetPP = Set.instance_method(:pretty_print).source_location[0].end_with?("/pp.rb") + class PPTest < Test::Unit::TestCase def test_list0123_12 assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12)) @@ -16,6 +19,10 @@ def test_list0123_11 assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11)) end + def test_set + assert_equal("Set[0, 1, 2, 3]\n", PP.pp(Set[0,1,2,3], ''.dup, 16)) + end if SetPP + OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) def test_struct_override_members # [ruby-core:7865] a = OverriddenStruct.new(1,2) @@ -164,6 +171,12 @@ def test_hash assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) end + def test_set + s = Set[] + s.add s + assert_equal("Set[Set[...]]\n", PP.pp(s, ''.dup)) + end if SetPP + S = Struct.new("S", :a, :b) def test_struct a = S.new(1,2)