Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,16 @@ def pretty_print_cycle(q) # :nodoc:
end
class Set # :nodoc:
def pretty_print(pp) # :nodoc:
pp.group(1, '#<Set:', '>') {
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('#<Set: {%s}>', empty? ? '' : '...')
name = self.class.name
pp.text(empty? ? "#{name}[]" : "#{name}[...]")
end
end if set_pp

Expand Down
13 changes: 13 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down