Skip to content

Commit

Permalink
Update spec for cyclic references in Set#inspect / Set#to_s
Browse files Browse the repository at this point in the history
The old version did only check a depth of 1. The following pseudocode
would pass that test (if the head and tail of the inspect string were
added to it):

    map { |item| item == self ? "#<Set: {...}>" : item.inspect }

Any deeper level of cyclic references would cause an infinite loop in
this code.
This changes explicitly adds the deeper cycles to the spec.
  • Loading branch information
herwinw committed Aug 27, 2023
1 parent c037246 commit e880895
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions library/set/shared/inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
Set["1", "2"].send(@method).should include('", "')
end

it "correctly handles self-references" do
(set = Set[]) << set
set.send(@method).should be_kind_of(String)
set.send(@method).should include("#<Set: {...}>")
it "correctly handles cyclic-references" do
set1 = Set[]
set2 = Set[set1]
set1 << set2
set1.send(@method).should be_kind_of(String)
set1.send(@method).should include("#<Set: {...}>")
end
end

0 comments on commit e880895

Please sign in to comment.