/
test_mass.rb
221 lines (178 loc) · 7.22 KB
/
test_mass.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require File.expand_path("../../test_helper", __FILE__)
module Unit
class TestMass < MiniTest::Unit::TestCase
describe Mass do
before do
class Foo
attr_accessor :foo
class Bar
attr_accessor :fool
end
end
class Thing
attr_accessor :food
end
class OneMoreThing
attr_accessor :thing
end
end
after do
GC.start
end
it "should be able to return the object corresponding to the passed object_id" do
f = Foo.new
o = Mass[f.object_id]
assert_equal true, f.object_id == o.object_id
f = nil
o = nil
end
it "should be able to index objects" do
assert_equal({}, Mass.index(Foo))
f = Foo.new
b1 = Foo::Bar.new
b2 = Foo::Bar.new
t = Thing.new
assert_equal({"Unit::TestMass::Foo" => [f.object_id], "Unit::TestMass::Foo::Bar" => [b1.object_id, b2.object_id].sort}, Mass.index(Foo))
assert_equal({"Unit::TestMass::Foo::Bar" => [b1.object_id, b2.object_id].sort}, Mass.index(Foo::Bar))
assert_equal({"Unit::TestMass::Thing" => [t.object_id]}, Mass.index(Thing))
assert_equal({"Unit::TestMass::Foo::Bar" => [b1.object_id, b2.object_id].sort, "Unit::TestMass::Thing" => [t.object_id]}, Mass.index(Foo::Bar, Thing))
assert_equal({}, Mass.index(OneMoreThing))
index = Mass.index
assert_equal true, index.keys.include?("String")
assert_equal true, index.keys.include?("Array")
assert_equal true, index.keys.include?("Hash")
assert_equal true, index["String"].size > 1000
f = nil
b1 = nil
b2 = nil
t = nil
end
it "should be able to count objects" do
assert_equal({}, Mass.count(Foo))
f = Foo.new
b1 = Foo::Bar.new
b2 = Foo::Bar.new
t = Thing.new
assert_equal({"Unit::TestMass::Foo" => 1, "Unit::TestMass::Foo::Bar" => 2}, Mass.count(Foo))
assert_equal({"Unit::TestMass::Foo::Bar" => 2}, Mass.count(Foo::Bar))
assert_equal({"Unit::TestMass::Thing" => 1}, Mass.count(Thing))
assert_equal({"Unit::TestMass::Foo::Bar" => 2, "Unit::TestMass::Thing" => 1}, Mass.count(Foo::Bar, Thing))
assert_equal({}, Mass.count(OneMoreThing))
count = Mass.count
assert_equal true, count.keys.include?("String")
assert_equal true, count.keys.include?("Array")
assert_equal true, count.keys.include?("Hash")
assert_equal true, count["String"] > 1000
f = nil
b1 = nil
b2 = nil
t = nil
end
it "should tackle nil objects" do
assert_equal({}, Mass.references(nil))
assert_equal(false, Mass.detach(nil))
assert_equal(false, Mass.detach!(nil))
end
# NOTE: The first assertion fails sometimes (maybe multiple test runs related?). If so, retry running the tests.
it "should be able to locate object references" do
f1 = Foo.new
assert_equal({}, Mass.references(f1))
f2 = Foo.new
f2.foo = f1
assert_equal({"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"]}, Mass.references(f1))
assert_equal({"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"]}, Mass.references(f1, Foo))
assert_equal({}, Mass.references(f1, Foo::Bar))
assert_equal({}, Mass.references(f1, Foo::Bar, OneMoreThing))
b = Foo::Bar.new
b.fool = f1
t = Thing.new
t.food = f1
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"],
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"],
"Unit::TestMass::Thing##{t.object_id}" => ["@food"]
}, Mass.references(f1))
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"],
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"]
}, Mass.references(f1, Foo))
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"],
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"]
}, f1._references(Foo, Foo::Bar))
assert_equal({
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"]
}, Mass.references(f1, Foo::Bar))
assert_equal({
"Unit::TestMass::Thing##{t.object_id}" => ["@food"]
}, Mass.references(f1, Thing))
assert_equal({}, Mass.references(f1, Hash, OneMoreThing))
f1 = nil
f2 = nil
b = nil
t = nil
end
describe "using simple objects" do
# NOTE: I don't know why the last assertion fails (maybe test environment related?) as the same steps to succeed within script/console (see contents of the file)
it "should be able to detach objects" do
f1 = Foo.new
object_id = f1.object_id
assert_equal({}, Mass.references(f1))
f2 = Foo.new
f2.foo = f1
b = Foo::Bar.new
b.fool = f1
t = Thing.new
t.food = f1
assert_equal({
"Unit::TestMass::Foo" => [object_id, f2.object_id].sort,
"Unit::TestMass::Foo::Bar" => [b.object_id]
}, Mass.index(Foo))
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"],
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"],
"Unit::TestMass::Thing##{t.object_id}" => ["@food"]
}, Mass.references(f1))
assert_equal(false, Mass.detach(f1, OneMoreThing){f1 = nil})
assert_equal({
"Unit::TestMass::Foo" => [object_id, f2.object_id].sort,
"Unit::TestMass::Foo::Bar" => [b.object_id]
}, Mass.index(Foo))
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"],
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"],
"Unit::TestMass::Thing##{t.object_id}" => ["@food"]
}, Mass.references(f1))
assert_equal(false, Mass.detach(f1, OneMoreThing, Thing){f1 = nil})
assert_equal({
"Unit::TestMass::Foo" => [object_id, f2.object_id].sort,
"Unit::TestMass::Foo::Bar" => [b.object_id]
}, Mass.index(Foo))
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"],
"Unit::TestMass::Foo::Bar##{b.object_id}" => ["@fool"]
}, Mass.references(f1))
assert_equal(false, f1.detach(Foo::Bar){f1 = nil})
assert_equal({
"Unit::TestMass::Foo" => [object_id, f2.object_id].sort,
"Unit::TestMass::Foo::Bar" => [b.object_id]
}, Mass.index(Foo))
assert_equal({
"Unit::TestMass::Foo##{f2.object_id}" => ["@foo"]
}, Mass.references(f1))
assert_equal(true, Mass.detach(f1){f1 = nil})
Mass.gc!(f1)
assert_equal({
"Unit::TestMass::Foo" => [f2.object_id],
"Unit::TestMass::Foo::Bar" => [b.object_id]
}, Mass.index(Foo))
end
end
describe "using complex objects" do
it "should be able to detach objects" do
# TODO: although RubyMass is used within a very complex project using DataMapper write tests within a similar situation
end
end
end
end
end