Skip to content

Commit bc78794

Browse files
committed
Add specs for ObjectSpace#reachable_objects_from
1 parent 48925cf commit bc78794

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require_relative '../../spec_helper'
2+
require 'objspace'
3+
4+
describe "ObjectSpace.reachable_objects_from" do
5+
it "returns nil for true and false" do
6+
ObjectSpace.reachable_objects_from(true).should == nil
7+
ObjectSpace.reachable_objects_from(false).should == nil
8+
end
9+
10+
it "returns nil for nil" do
11+
ObjectSpace.reachable_objects_from(nil).should == nil
12+
end
13+
14+
it "returns nil for small Integers" do
15+
ObjectSpace.reachable_objects_from(42).should == nil
16+
end
17+
18+
it "enumerates objects directly reachable from a given object" do
19+
ObjectSpace.reachable_objects_from(['a', 'b', 'c']).should include(Array, 'a', 'b', 'c')
20+
ObjectSpace.reachable_objects_from(Object.new).should == [Object]
21+
end
22+
23+
it "finds an object stored in an Array" do
24+
obj = Object.new
25+
ary = [obj]
26+
reachable = ObjectSpace.reachable_objects_from(ary)
27+
reachable.should include(obj)
28+
end
29+
30+
it "finds an object stored in a copy-on-write Array" do
31+
removed = Object.new
32+
obj = Object.new
33+
ary = [removed, obj]
34+
ary.shift
35+
reachable = ObjectSpace.reachable_objects_from(ary)
36+
reachable.should include(obj)
37+
reachable.should_not include(removed)
38+
end
39+
40+
it "finds an object stored in a Queue" do
41+
require 'thread'
42+
o = Object.new
43+
q = Queue.new
44+
q << o
45+
46+
reachable = ObjectSpace.reachable_objects_from(q)
47+
reachable = reachable + reachable.flat_map { |r| ObjectSpace.reachable_objects_from(r) }
48+
reachable.should include(o)
49+
end
50+
51+
it "finds an object stored in a SizedQueue" do
52+
require 'thread'
53+
o = Object.new
54+
q = SizedQueue.new(3)
55+
q << o
56+
57+
reachable = ObjectSpace.reachable_objects_from(q)
58+
reachable = reachable + reachable.flat_map { |r| ObjectSpace.reachable_objects_from(r) }
59+
reachable.should include(o)
60+
end
61+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:ObjectSpace.reachable_objects_from returns nil for nil

spec/truffle/objspace/reachable_objects_from_spec.rb

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
describe "ObjectSpace.reachable_objects_from" do
1414

15-
# This is a standard method, but our implementation returns more objects so we keep this spec here
15+
# This is a standard method, but our implementation returns more objects so we add extra specs here
1616

17-
it "enumerates objects directly reachable from a given object" do
17+
it "returns [NilClass] for nil" do
1818
ObjectSpace.reachable_objects_from(nil).should == [NilClass]
19-
ObjectSpace.reachable_objects_from(['a', 'b', 'c']).should include(Array, 'a', 'b', 'c')
20-
ObjectSpace.reachable_objects_from(Object.new).should == [Object]
2119
end
2220

2321
it "finds the superclass and included/prepended modules of a class" do
@@ -46,43 +44,6 @@
4644
reachable.should include(captured)
4745
end
4846

49-
it "finds an object stored in an Array" do
50-
obj = Object.new
51-
ary = [obj]
52-
reachable = ObjectSpace.reachable_objects_from(ary)
53-
reachable.should include(obj)
54-
end
55-
56-
it "finds an object stored in a copy-on-write Array" do
57-
removed = Object.new
58-
obj = Object.new
59-
ary = [removed, obj]
60-
ary.shift
61-
reachable = ObjectSpace.reachable_objects_from(ary)
62-
reachable.should include(obj)
63-
reachable.should_not include(removed)
64-
end
65-
66-
it "finds an object stored in a Queue" do
67-
require 'thread'
68-
o = Object.new
69-
q = Queue.new
70-
q << o
71-
72-
reachable = ObjectSpace.reachable_objects_from(q)
73-
reachable.should include(o)
74-
end
75-
76-
it "finds an object stored in a SizedQueue" do
77-
require 'thread'
78-
o = Object.new
79-
q = SizedQueue.new(3)
80-
q << o
81-
82-
reachable = ObjectSpace.reachable_objects_from(q)
83-
reachable.should include(o)
84-
end
85-
8647
it "finds finalizers" do
8748
object = Object.new
8849
finalizer = proc { }

0 commit comments

Comments
 (0)