dirty_hashy 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,500 @@
1
+ require File.expand_path("../../test_helper", __FILE__)
2
+
3
+ module Unit
4
+ class TestDirtyIndifferentHashy < MiniTest::Unit::TestCase
5
+
6
+ describe DirtyIndifferentHashy do
7
+ it "should behave as expected without method mapping and without restricted keys" do
8
+ hashy = DirtyIndifferentHashy.new
9
+
10
+ assert_equal({}, hashy)
11
+ assert_equal false, hashy.dirty?
12
+ assert_equal false, hashy.changed?
13
+ assert_equal({}, hashy.changes)
14
+
15
+ assert_raises(NoMethodError) do
16
+ hashy.name = "Paul"
17
+ end
18
+
19
+ hashy["name"] = "Paul"
20
+
21
+ assert_equal true, hashy.dirty?
22
+ assert_equal true, hashy.changed?
23
+ assert_equal true, hashy.changed?(:name)
24
+ assert_equal true, hashy.changed?("name")
25
+ assert_equal [nil, "Paul"], hashy.change(:name)
26
+ assert_equal [nil, "Paul"], hashy.change("name")
27
+ assert_equal({"name" => [nil, "Paul"]}, hashy.changes)
28
+
29
+ hashy[:name] = nil
30
+
31
+ assert_equal false, hashy.dirty?
32
+ assert_equal false, hashy.changed?
33
+ assert_equal false, hashy.changed?(:name)
34
+ assert_equal false, hashy.changed?("name")
35
+ assert_equal nil, hashy.change(:name)
36
+ assert_equal nil, hashy.change("name")
37
+ assert_equal({}, hashy.changes)
38
+
39
+ hashy["name"] = "Stephan"
40
+
41
+ assert_equal true, hashy.dirty?
42
+ assert_equal true, hashy.changed?
43
+ assert_equal true, hashy.changed?(:name)
44
+ assert_equal true, hashy.changed?("name")
45
+ assert_equal [nil, "Stephan"], hashy.change(:name)
46
+ assert_equal [nil, "Stephan"], hashy.change("name")
47
+ assert_equal({"name" => [nil, "Stephan"]}, hashy.changes)
48
+
49
+ hashy.clean_up!
50
+
51
+ assert_equal false, hashy.dirty?
52
+ assert_equal false, hashy.changed?
53
+ assert_equal false, hashy.changed?(:name)
54
+ assert_equal false, hashy.changed?("name")
55
+ assert_equal nil, hashy.change(:name)
56
+ assert_equal nil, hashy.change("name")
57
+ assert_equal({}, hashy.changes)
58
+
59
+ hashy["name"] = "Chris"
60
+
61
+ assert_equal true, hashy.dirty?
62
+ assert_equal true, hashy.changed?
63
+ assert_equal true, hashy.changed?(:name)
64
+ assert_equal true, hashy.changed?("name")
65
+ assert_equal ["Stephan", "Chris"], hashy.change(:name)
66
+ assert_equal ["Stephan", "Chris"], hashy.change("name")
67
+ assert_equal({"name" => ["Stephan", "Chris"]}, hashy.changes)
68
+
69
+ hashy["name"] = "Stephan"
70
+
71
+ assert_equal false, hashy.dirty?
72
+ assert_equal false, hashy.changed?
73
+ assert_equal false, hashy.changed?(:name)
74
+ assert_equal false, hashy.changed?("name")
75
+ assert_equal nil, hashy.change(:name)
76
+ assert_equal nil, hashy.change("name")
77
+ assert_equal({}, hashy.changes)
78
+
79
+ hashy["name"] = "Paul"
80
+
81
+ assert_equal true, hashy.dirty?
82
+ assert_equal true, hashy.changed?
83
+ assert_equal true, hashy.changed?(:name)
84
+ assert_equal true, hashy.changed?("name")
85
+ assert_equal ["Stephan", "Paul"], hashy.change(:name)
86
+ assert_equal ["Stephan", "Paul"], hashy.change("name")
87
+ assert_equal({"name" => ["Stephan", "Paul"]}, hashy.changes)
88
+
89
+ hashy["name"] = "Tim"
90
+
91
+ assert_equal true, hashy.dirty?
92
+ assert_equal true, hashy.changed?
93
+ assert_equal true, hashy.changed?(:name)
94
+ assert_equal true, hashy.changed?("name")
95
+ assert_equal ["Stephan", "Tim"], hashy.change(:name)
96
+ assert_equal ["Stephan", "Tim"], hashy.change("name")
97
+ assert_equal({"name" => ["Stephan", "Tim"]}, hashy.changes)
98
+
99
+ hashy["company"] = "Holder"
100
+
101
+ assert_equal true, hashy.dirty?
102
+ assert_equal true, hashy.changed?
103
+ assert_equal true, hashy.changed?(:name)
104
+ assert_equal true, hashy.changed?("name")
105
+ assert_equal true, hashy.changed?(:company)
106
+ assert_equal true, hashy.changed?("company")
107
+ assert_equal ["Stephan", "Tim"], hashy.change(:name)
108
+ assert_equal ["Stephan", "Tim"], hashy.change("name")
109
+ assert_equal [nil, "Holder"], hashy.change(:company)
110
+ assert_equal [nil, "Holder"], hashy.change("company")
111
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Holder"]}, hashy.changes)
112
+
113
+ hashy["city"] = "Amsterdam"
114
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Holder"], "city" => [nil, "Amsterdam"]}, hashy.changes)
115
+
116
+ hashy.delete :city
117
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Holder"]}, hashy.changes)
118
+
119
+ hashy.clean_up!
120
+
121
+ assert_equal false, hashy.dirty?
122
+ assert_equal false, hashy.changed?
123
+ assert_equal false, hashy.changed?(:name)
124
+ assert_equal false, hashy.changed?("name")
125
+ assert_equal false, hashy.changed?(:name)
126
+ assert_equal false, hashy.changed?("name")
127
+ assert_equal nil, hashy.change(:company)
128
+ assert_equal nil, hashy.change("company")
129
+ assert_equal({}, hashy.changes)
130
+
131
+ hashy.delete :company
132
+
133
+ assert_equal true, hashy.dirty?
134
+ assert_equal true, hashy.changed?
135
+ assert_equal true, hashy.changed?(:company)
136
+ assert_equal true, hashy.changed?("company")
137
+ assert_equal "Holder", hashy.was(:company)
138
+ assert_equal "Holder", hashy.was("company")
139
+ assert_equal ["Holder", nil], hashy.change(:company)
140
+ assert_equal ["Holder", nil], hashy.change("company")
141
+
142
+ hashy.merge! :name => "Paul", :company => "Internetbureau Holder B.V."
143
+
144
+ assert_equal true, hashy.dirty?
145
+ assert_equal true, hashy.changed?
146
+ assert_equal true, hashy.changed?(:name)
147
+ assert_equal true, hashy.changed?("name")
148
+ assert_equal true, hashy.changed?(:company)
149
+ assert_equal true, hashy.changed?("company")
150
+ assert_equal ["Tim", "Paul"], hashy.change(:name)
151
+ assert_equal ["Tim", "Paul"], hashy.change("name")
152
+ assert_equal ["Holder", "Internetbureau Holder B.V."], hashy.change(:company)
153
+ assert_equal ["Holder", "Internetbureau Holder B.V."], hashy.change("company")
154
+ assert_equal({"name" => ["Tim", "Paul"], "company" => ["Holder", "Internetbureau Holder B.V."]}, hashy.changes)
155
+ end
156
+
157
+ it "should behave as expected without method mapping, but with restricted keys" do
158
+ hashy = DirtyIndifferentHashy.new({}, false, [:name])
159
+
160
+ assert_equal({"name" => nil}, hashy)
161
+ assert_equal false, hashy.dirty?
162
+ assert_equal false, hashy.changed?
163
+ assert_equal({}, hashy.changes)
164
+
165
+ assert_raises(NoMethodError) do
166
+ hashy.name = "Paul"
167
+ end
168
+
169
+ hashy["name"] = "Paul"
170
+
171
+ assert_equal true, hashy.dirty?
172
+ assert_equal true, hashy.changed?
173
+ assert_equal true, hashy.changed?(:name)
174
+ assert_equal true, hashy.changed?("name")
175
+ assert_equal [nil, "Paul"], hashy.change(:name)
176
+ assert_equal [nil, "Paul"], hashy.change("name")
177
+ assert_equal({"name" => [nil, "Paul"]}, hashy.changes)
178
+
179
+ hashy[:name] = nil
180
+
181
+ assert_equal false, hashy.dirty?
182
+ assert_equal false, hashy.changed?
183
+ assert_equal false, hashy.changed?(:name)
184
+ assert_equal false, hashy.changed?("name")
185
+ assert_equal nil, hashy.change(:name)
186
+ assert_equal nil, hashy.change("name")
187
+ assert_equal({}, hashy.changes)
188
+
189
+ hashy[:name] = "Stephan"
190
+
191
+ assert_equal true, hashy.dirty?
192
+ assert_equal true, hashy.changed?
193
+ assert_equal true, hashy.changed?(:name)
194
+ assert_equal true, hashy.changed?("name")
195
+ assert_equal [nil, "Stephan"], hashy.change(:name)
196
+ assert_equal [nil, "Stephan"], hashy.change("name")
197
+ assert_equal({"name" => [nil, "Stephan"]}, hashy.changes)
198
+
199
+ hashy.clean_up!
200
+
201
+ assert_equal false, hashy.dirty?
202
+ assert_equal false, hashy.changed?
203
+ assert_equal false, hashy.changed?(:name)
204
+ assert_equal false, hashy.changed?("name")
205
+ assert_equal nil, hashy.change(:name)
206
+ assert_equal nil, hashy.change("name")
207
+ assert_equal({}, hashy.changes)
208
+
209
+ hashy["name"] = "Chris"
210
+
211
+ assert_equal true, hashy.dirty?
212
+ assert_equal true, hashy.changed?
213
+ assert_equal true, hashy.changed?(:name)
214
+ assert_equal true, hashy.changed?("name")
215
+ assert_equal "Stephan", hashy.was("name")
216
+ assert_equal ["Stephan", "Chris"], hashy.change(:name)
217
+ assert_equal ["Stephan", "Chris"], hashy.change("name")
218
+ assert_equal({"name" => ["Stephan", "Chris"]}, hashy.changes)
219
+
220
+ hashy["name"] = "Stephan"
221
+
222
+ assert_equal false, hashy.dirty?
223
+ assert_equal false, hashy.changed?
224
+ assert_equal false, hashy.changed?(:name)
225
+ assert_equal false, hashy.changed?("name")
226
+ assert_equal nil, hashy.change(:name)
227
+ assert_equal nil, hashy.change("name")
228
+ assert_equal({}, hashy.changes)
229
+
230
+ hashy["name"] = "Paul"
231
+
232
+ assert_equal true, hashy.dirty?
233
+ assert_equal true, hashy.changed?
234
+ assert_equal true, hashy.changed?(:name)
235
+ assert_equal true, hashy.changed?("name")
236
+ assert_equal ["Stephan", "Paul"], hashy.change(:name)
237
+ assert_equal ["Stephan", "Paul"], hashy.change("name")
238
+ assert_equal({"name" => ["Stephan", "Paul"]}, hashy.changes)
239
+
240
+ hashy["name"] = "Tim"
241
+
242
+ assert_equal true, hashy.dirty?
243
+ assert_equal true, hashy.changed?
244
+ assert_equal true, hashy.changed?(:name)
245
+ assert_equal true, hashy.changed?("name")
246
+ assert_equal ["Stephan", "Tim"], hashy.change(:name)
247
+ assert_equal ["Stephan", "Tim"], hashy.change("name")
248
+ assert_equal({"name" => ["Stephan", "Tim"]}, hashy.changes)
249
+
250
+ hashy.clean_up!
251
+ hashy.merge! :name => "Paul"
252
+
253
+ assert_equal true, hashy.dirty?
254
+ assert_equal true, hashy.changed?
255
+ assert_equal true, hashy.changed?(:name)
256
+ assert_equal true, hashy.changed?("name")
257
+ assert_equal ["Tim", "Paul"], hashy.change(:name)
258
+ assert_equal ["Tim", "Paul"], hashy.change("name")
259
+ assert_equal({"name" => ["Tim", "Paul"]}, hashy.changes)
260
+
261
+ assert_raises(IndexError) do
262
+ hashy[:company]
263
+ end
264
+
265
+ assert_raises(IndexError) do
266
+ hashy["company"] = "Holder"
267
+ end
268
+
269
+ assert_raises(IndexError) do
270
+ hashy.delete :company
271
+ end
272
+
273
+ assert_raises(IndexError) do
274
+ hashy.changed? :company
275
+ end
276
+
277
+ assert_raises(IndexError) do
278
+ hashy.change :company
279
+ end
280
+
281
+ assert_raises(IndexError) do
282
+ hashy.was :company
283
+ end
284
+
285
+ assert_raises(IndexError) do
286
+ hashy.merge! :name => "Paul", :company => "Internetbureau Holder B.V."
287
+ end
288
+ end
289
+
290
+ it "should behave as expected with method mapping and without restricted keys" do
291
+ hashy = DirtyIndifferentHashy.new({}, true)
292
+
293
+ assert_equal({}, hashy)
294
+ assert_equal false, hashy.dirty?
295
+ assert_equal false, hashy.changed?
296
+ assert_equal({}, hashy.changes)
297
+
298
+ assert_raises(NoMethodError) do
299
+ hashy.name
300
+ end
301
+
302
+ hashy.name = "Paul"
303
+
304
+ assert_equal "Paul", hashy.name
305
+ assert_equal true, hashy.dirty?
306
+ assert_equal true, hashy.changed?
307
+ assert_equal true, hashy.name_changed?
308
+ assert_equal true, hashy.changed?(:name)
309
+ assert_equal [nil, "Paul"], hashy.name_change
310
+ assert_equal [nil, "Paul"], hashy.change(:name)
311
+ assert_equal({"name" => [nil, "Paul"]}, hashy.changes)
312
+
313
+ hashy.name = nil
314
+
315
+ assert_equal false, hashy.dirty?
316
+ assert_equal false, hashy.changed?
317
+ assert_equal false, hashy.name_changed?
318
+ assert_equal false, hashy.changed?(:name)
319
+ assert_equal nil, hashy.name_change
320
+ assert_equal nil, hashy.change(:name)
321
+ assert_equal({}, hashy.changes)
322
+
323
+ hashy.name = "Stephan"
324
+
325
+ assert_equal true, hashy.dirty?
326
+ assert_equal true, hashy.changed?
327
+ assert_equal true, hashy.name_changed?
328
+ assert_equal true, hashy.changed?(:name)
329
+ assert_equal [nil, "Stephan"], hashy.name_change
330
+ assert_equal [nil, "Stephan"], hashy.change(:name)
331
+ assert_equal({"name" => [nil, "Stephan"]}, hashy.changes)
332
+
333
+ hashy.clean_up!
334
+
335
+ assert_equal false, hashy.dirty?
336
+ assert_equal false, hashy.changed?
337
+ assert_equal false, hashy.name_changed?
338
+ assert_equal false, hashy.changed?(:name)
339
+ assert_equal nil, hashy.name_change
340
+ assert_equal nil, hashy.change(:name)
341
+ assert_equal({}, hashy.changes)
342
+
343
+ hashy.name = "Chris"
344
+
345
+ assert_equal true, hashy.dirty?
346
+ assert_equal true, hashy.changed?
347
+ assert_equal true, hashy.name_changed?
348
+ assert_equal true, hashy.changed?(:name)
349
+ assert_equal "Stephan", hashy.name_was
350
+ assert_equal "Stephan", hashy.was(:name)
351
+ assert_equal ["Stephan", "Chris"], hashy.name_change
352
+ assert_equal ["Stephan", "Chris"], hashy.change(:name)
353
+ assert_equal({"name" => ["Stephan", "Chris"]}, hashy.changes)
354
+
355
+ hashy.name = "Stephan"
356
+
357
+ assert_equal false, hashy.dirty?
358
+ assert_equal false, hashy.changed?
359
+ assert_equal false, hashy.name_changed?
360
+ assert_equal false, hashy.changed?(:name)
361
+ assert_equal nil, hashy.name_change
362
+ assert_equal nil, hashy.change(:name)
363
+ assert_equal({}, hashy.changes)
364
+
365
+ hashy.name = "Tim"
366
+ hashy.city = "Amsterdam"
367
+
368
+ assert_equal true, hashy.city_changed?
369
+ assert_equal true, hashy.changed?(:city)
370
+ assert_equal nil, hashy.city_was
371
+ assert_equal [nil, "Amsterdam"], hashy.city_change
372
+ assert_equal({"name" => ["Stephan", "Tim"], "city" => [nil, "Amsterdam"]}, hashy.changes)
373
+
374
+ hashy.delete :city
375
+ assert_equal({"name" => ["Stephan", "Tim"]}, hashy.changes)
376
+
377
+ hashy.merge! :company => "Internetbureau Holder B.V."
378
+
379
+ assert_equal true, hashy.dirty?
380
+ assert_equal true, hashy.changed?
381
+ assert_equal true, hashy.changed?(:name)
382
+ assert_equal true, hashy.changed?("name")
383
+ assert_equal true, hashy.changed?(:company)
384
+ assert_equal true, hashy.changed?("company")
385
+ assert_equal ["Stephan", "Tim"], hashy.change(:name)
386
+ assert_equal ["Stephan", "Tim"], hashy.change("name")
387
+ assert_equal [nil, "Internetbureau Holder B.V."], hashy.change(:company)
388
+ assert_equal [nil, "Internetbureau Holder B.V."], hashy.change("company")
389
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Internetbureau Holder B.V."]}, hashy.changes)
390
+
391
+ hashy.clean_up!
392
+
393
+ assert_equal false, hashy.dirty?
394
+ assert_equal false, hashy.changed?
395
+ end
396
+
397
+ it "should behave as expected with method mapping and with restricted keys" do
398
+ hashy = DirtyIndifferentHashy.new({}, true, [:name])
399
+
400
+ assert_equal({"name" => nil}, hashy)
401
+ assert_equal nil, hashy.name
402
+ assert_equal false, hashy.dirty?
403
+ assert_equal false, hashy.changed?
404
+ assert_equal({}, hashy.changes)
405
+
406
+ hashy.name = "Paul"
407
+
408
+ assert_equal "Paul", hashy.name
409
+ assert_equal true, hashy.dirty?
410
+ assert_equal true, hashy.changed?
411
+ assert_equal true, hashy.name_changed?
412
+ assert_equal true, hashy.changed?(:name)
413
+ assert_equal [nil, "Paul"], hashy.name_change
414
+ assert_equal [nil, "Paul"], hashy.change(:name)
415
+ assert_equal({"name" => [nil, "Paul"]}, hashy.changes)
416
+
417
+ hashy.name = nil
418
+
419
+ assert_equal false, hashy.dirty?
420
+ assert_equal false, hashy.changed?
421
+ assert_equal false, hashy.name_changed?
422
+ assert_equal false, hashy.changed?(:name)
423
+ assert_equal nil, hashy.name_change
424
+ assert_equal nil, hashy.change(:name)
425
+ assert_equal({}, hashy.changes)
426
+
427
+ hashy.name = "Stephan"
428
+
429
+ assert_equal true, hashy.dirty?
430
+ assert_equal true, hashy.changed?
431
+ assert_equal true, hashy.name_changed?
432
+ assert_equal true, hashy.changed?(:name)
433
+ assert_equal [nil, "Stephan"], hashy.name_change
434
+ assert_equal [nil, "Stephan"], hashy.change(:name)
435
+ assert_equal({"name" => [nil, "Stephan"]}, hashy.changes)
436
+
437
+ hashy.clean_up!
438
+
439
+ assert_equal false, hashy.dirty?
440
+ assert_equal false, hashy.changed?
441
+ assert_equal false, hashy.name_changed?
442
+ assert_equal false, hashy.changed?(:name)
443
+ assert_equal nil, hashy.name_change
444
+ assert_equal nil, hashy.change(:name)
445
+ assert_equal({}, hashy.changes)
446
+
447
+ hashy.name = "Chris"
448
+
449
+ assert_equal true, hashy.dirty?
450
+ assert_equal true, hashy.changed?
451
+ assert_equal true, hashy.name_changed?
452
+ assert_equal true, hashy.changed?(:name)
453
+ assert_equal "Stephan", hashy.name_was
454
+ assert_equal "Stephan", hashy.was(:name)
455
+ assert_equal ["Stephan", "Chris"], hashy.name_change
456
+ assert_equal ["Stephan", "Chris"], hashy.change(:name)
457
+ assert_equal({"name" => ["Stephan", "Chris"]}, hashy.changes)
458
+
459
+ hashy.name = "Stephan"
460
+
461
+ assert_equal false, hashy.dirty?
462
+ assert_equal false, hashy.changed?
463
+ assert_equal false, hashy.name_changed?
464
+ assert_equal false, hashy.changed?(:name)
465
+ assert_equal nil, hashy.name_change
466
+ assert_equal nil, hashy.change(:name)
467
+ assert_equal({}, hashy.changes)
468
+
469
+ assert_raises(NoMethodError) do
470
+ hashy.company
471
+ end
472
+
473
+ assert_raises(NoMethodError) do
474
+ hashy.company = "Holder"
475
+ end
476
+
477
+ assert_raises(IndexError) do
478
+ hashy.delete :company
479
+ end
480
+
481
+ assert_raises(NoMethodError) do
482
+ hashy.company_changed?
483
+ end
484
+
485
+ assert_raises(NoMethodError) do
486
+ hashy.company_change
487
+ end
488
+
489
+ assert_raises(NoMethodError) do
490
+ hashy.company_was
491
+ end
492
+
493
+ assert_raises(IndexError) do
494
+ hashy.merge! :name => "Paul", :company => "Internetbureau Holder B.V."
495
+ end
496
+ end
497
+ end
498
+
499
+ end
500
+ end