objectified_sessions 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +21 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +391 -0
  7. data/Rakefile +6 -0
  8. data/lib/objectified_session_generator.rb +139 -0
  9. data/lib/objectified_sessions/base.rb +299 -0
  10. data/lib/objectified_sessions/errors.rb +55 -0
  11. data/lib/objectified_sessions/field_definition.rb +105 -0
  12. data/lib/objectified_sessions/version.rb +3 -0
  13. data/lib/objectified_sessions.rb +157 -0
  14. data/objectified_sessions.gemspec +43 -0
  15. data/spec/objectified_sessions/helpers/controller_helper.rb +55 -0
  16. data/spec/objectified_sessions/helpers/exception_helpers.rb +20 -0
  17. data/spec/objectified_sessions/system/basic_system_spec.rb +135 -0
  18. data/spec/objectified_sessions/system/error_handling_system_spec.rb +217 -0
  19. data/spec/objectified_sessions/system/prefix_system_spec.rb +62 -0
  20. data/spec/objectified_sessions/system/retired_inactive_system_spec.rb +188 -0
  21. data/spec/objectified_sessions/system/setup_system_spec.rb +65 -0
  22. data/spec/objectified_sessions/system/strings_symbols_system_spec.rb +73 -0
  23. data/spec/objectified_sessions/system/unknown_data_system_spec.rb +65 -0
  24. data/spec/objectified_sessions/system/visibility_system_spec.rb +61 -0
  25. data/spec/objectified_sessions/unit/objectified_session_generator_spec.rb +121 -0
  26. data/spec/objectified_sessions/unit/objectified_sessions/base_spec.rb +484 -0
  27. data/spec/objectified_sessions/unit/objectified_sessions/errors_spec.rb +75 -0
  28. data/spec/objectified_sessions/unit/objectified_sessions/field_definition_spec.rb +138 -0
  29. data/spec/objectified_sessions/unit/objectified_sessions_spec.rb +149 -0
  30. metadata +120 -0
@@ -0,0 +1,484 @@
1
+ require 'objectified_sessions'
2
+ require 'objectified_sessions/helpers/exception_helpers'
3
+
4
+ describe ObjectifiedSessions::Base do
5
+ include ObjectifiedSessions::Helpers::ExceptionHelpers
6
+
7
+ before :each do
8
+ @class = Class.new(::ObjectifiedSessions::Base)
9
+ @underlying_session = double("underlying_session")
10
+ end
11
+
12
+ def allow_prefix!(prefix)
13
+ @prefixed_underlying_session = double("prefixed_underlying_session")
14
+ allow(@underlying_session).to receive(:[]).with(prefix.to_s).and_return(@prefixed_underlying_session)
15
+ end
16
+
17
+ def new_instance!
18
+ @instance = @class.new(@underlying_session)
19
+ end
20
+
21
+ def expect_and_create_field!(name, storage_name, allow_access_to_data, delete_data_with_storage_name, options)
22
+ out = double("field_#{name}")
23
+
24
+ allow(out).to receive(:name).with().and_return(name.to_sym)
25
+ allow(out).to receive(:storage_name).with().and_return(storage_name.to_s)
26
+ allow(out).to receive(:allow_access_to_data?).with().and_return(allow_access_to_data)
27
+ allow(out).to receive(:delete_data_with_storage_name?).with().and_return(delete_data_with_storage_name)
28
+ expect(ObjectifiedSessions::FieldDefinition).to receive(:new).once.with(@class, name, options).and_return(out)
29
+
30
+ out
31
+ end
32
+
33
+ describe "deletion of unknown fields" do
34
+ before :each do
35
+ allow(@underlying_session).to receive(:keys).with().and_return([ :foo, :bar, :baz ])
36
+ end
37
+
38
+ def expect_deletion(expected_deleted_keys)
39
+ expect(@underlying_session).to receive(:delete).once do |keys|
40
+ unless keys.sort_by(&:to_s) == expected_deleted_keys.sort_by(&:to_s)
41
+ raise "Unexpected keys passed: #{keys.inspect}; expected: #{expected_deleted_keys.inspect}"
42
+ end
43
+ end
44
+
45
+ new_instance!
46
+ end
47
+
48
+ it "should delete unknown fields on startup if unknown_fields == :delete" do
49
+ @class.unknown_fields :delete
50
+
51
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
52
+ @class.field :foo
53
+
54
+ expect_deletion([ :bar, :baz ])
55
+ end
56
+
57
+ it "should also delete fields that have delete_data_with_storage_name == true" do
58
+ @class.unknown_fields :delete
59
+
60
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, true, { :type => :normal, :visibility => :public })
61
+ @class.field :foo
62
+
63
+ expect_deletion([ :foo, :bar, :baz ])
64
+ end
65
+
66
+ it "should delete fields based on storage name, not name" do
67
+ @class.unknown_fields :delete
68
+
69
+ @field_foo = expect_and_create_field!(:foo, 'bar', true, false, { :type => :normal, :visibility => :public })
70
+ @class.field :foo
71
+
72
+ expect_deletion([ :foo, :baz ])
73
+ end
74
+
75
+ it "should not delete fields if unknown_fields is not set" do
76
+ @field_foo = expect_and_create_field!(:foo, 'bar', true, false, { :type => :normal, :visibility => :public })
77
+ @class.field :foo
78
+
79
+ new_instance!
80
+ end
81
+
82
+ it "should not delete fields if unknown_fields is set to :preserve" do
83
+ @class.unknown_fields :preserve
84
+
85
+ @field_foo = expect_and_create_field!(:foo, 'bar', true, false, { :type => :normal, :visibility => :public })
86
+ @class.field :foo
87
+
88
+ new_instance!
89
+ end
90
+
91
+ it "should only delete data within the prefix, if one is set" do
92
+ @class.prefix :prf
93
+ @class.unknown_fields :delete
94
+
95
+ allow_prefix!(:prf)
96
+
97
+ allow(@underlying_session).to receive(:keys).with().and_return([ :foo, :bar, :baz, :prf ])
98
+ allow(@prefixed_underlying_session).to receive(:keys).with().and_return([ :foo, :quux, :marph ])
99
+
100
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
101
+ @class.field :foo
102
+
103
+ expect(@prefixed_underlying_session).to receive(:delete).once do |keys|
104
+ unless keys.sort_by(&:to_s) == [ :quux, :marph ].sort_by(&:to_s)
105
+ raise "Unexpected keys passed: #{keys.inspect}"
106
+ end
107
+ end
108
+
109
+ new_instance!
110
+ end
111
+ end
112
+
113
+ describe "retrieving field names" do
114
+
115
+ end
116
+
117
+ describe "reading fields" do
118
+ before :each do
119
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
120
+ @class.field :foo
121
+
122
+ new_instance!
123
+ end
124
+
125
+ it "should raise if asked for a field that doesn't exist" do
126
+ e = capture_exception(ObjectifiedSessions::Errors::NoSuchFieldError) { @instance.send(:[], :bar) }
127
+ e.session_class.should be(@class)
128
+ e.field_name.should == :bar
129
+ e.message.should match(/bar/i)
130
+ e.message.should match(/foo/i)
131
+ end
132
+
133
+ it "should return data if present" do
134
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(123)
135
+ @instance.send(:[], 'foo').should == 123
136
+ end
137
+
138
+ it "should return data via the prefix, if one is set" do
139
+ @class.prefix :prf
140
+ allow_prefix!(:prf)
141
+
142
+ allow(@prefixed_underlying_session).to receive(:[]).with('foo').and_return(234)
143
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(345)
144
+
145
+ @instance.send(:[], 'foo').should == 234
146
+ end
147
+
148
+ it "should not bind a hash to the prefix, if none is there" do
149
+ @class.prefix :prf
150
+
151
+ allow(@underlying_session).to receive(:[]).with('prf').and_return(nil)
152
+
153
+ @instance.send(:[], 'foo').should == nil
154
+ end
155
+ end
156
+
157
+ describe "writing fields" do
158
+ before :each do
159
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
160
+ @class.field :foo
161
+
162
+ new_instance!
163
+ end
164
+
165
+ it "should raise if asked for a field that doesn't exist" do
166
+ e = capture_exception(ObjectifiedSessions::Errors::NoSuchFieldError) { @instance.send(:[]=, :bar, 123) }
167
+ e.session_class.should be(@class)
168
+ e.field_name.should == :bar
169
+ e.message.should match(/bar/i)
170
+ e.message.should match(/foo/i)
171
+ end
172
+
173
+ it "should assign data" do
174
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
175
+ @instance.send(:[]=, :foo, 123).should == 123
176
+ end
177
+
178
+ it "should assign data via the prefix, if one is set" do
179
+ @class.prefix :prf
180
+ allow_prefix!(:prf)
181
+
182
+ expect(@prefixed_underlying_session).to receive(:[]=).once.with('foo', 123)
183
+
184
+ @instance.send(:[]=, 'foo', 123).should == 123
185
+ end
186
+
187
+ it "should bind a hash to the prefix, if none is there" do
188
+ @class.prefix :prf
189
+
190
+
191
+ @prefixed_underlying_session = double("prefixed_underlying_session")
192
+ expect(@underlying_session).to receive(:[]).once.ordered.with('prf').and_return(nil)
193
+ expect(@underlying_session).to receive(:[]=).once.ordered.with('prf', { })
194
+ expect(@underlying_session).to receive(:[]).once.ordered.with('prf').and_return(@prefixed_underlying_session)
195
+
196
+ expect(@prefixed_underlying_session).to receive(:[]=).once.with('foo', 123)
197
+ @instance.send(:[]=, 'foo', 123).should == 123
198
+ end
199
+ end
200
+
201
+ describe "defining fields" do
202
+ it "should define a simple field, and make it retrievable" do
203
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
204
+ @class.field :foo
205
+
206
+ @class._field_named(:foo).should be(@field_foo)
207
+ @class._field_named('foo').should be(@field_foo)
208
+ @class._field_with_storage_name(:foo).should be(@field_foo)
209
+ @class._field_with_storage_name('foo').should be(@field_foo)
210
+ end
211
+
212
+ it "should allow you to change the visibility" do
213
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :private })
214
+ @class.field :foo, :visibility => :private
215
+
216
+ @class._field_named(:foo).should be(@field_foo)
217
+ @class._field_named('foo').should be(@field_foo)
218
+ @class._field_with_storage_name(:foo).should be(@field_foo)
219
+ @class._field_with_storage_name('foo').should be(@field_foo)
220
+ end
221
+
222
+ it "should respect the default visibility" do
223
+ @class.default_visibility :private
224
+
225
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :private })
226
+ @class.field :foo
227
+
228
+ @field_bar = expect_and_create_field!(:bar, 'bar', true, false, { :type => :normal, :visibility => :public })
229
+ @class.field :bar, :visibility => :public
230
+
231
+ @class.default_visibility :public
232
+
233
+ @field_baz = expect_and_create_field!(:baz, 'baz', true, false, { :type => :normal, :visibility => :public })
234
+ @class.field :baz
235
+ end
236
+
237
+ it "should allow you to change the storage name" do
238
+ @field_foo = expect_and_create_field!(:foo, 'bar', true, false, { :type => :normal, :visibility => :public, :storage => :bar })
239
+ @class.field :foo, :storage => :bar
240
+
241
+ @class._field_named(:foo).should be(@field_foo)
242
+ @class._field_named('foo').should be(@field_foo)
243
+ @class._field_with_storage_name('foo').should == nil
244
+ @class._field_with_storage_name(:foo).should == nil
245
+ @class._field_with_storage_name(:bar).should be(@field_foo)
246
+ @class._field_with_storage_name('bar').should be(@field_foo)
247
+ end
248
+
249
+ it "should raise if you try to define a field with the same name" do
250
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
251
+ @class.field :foo
252
+
253
+ @field_foo = expect_and_create_field!('foo', 'bar', true, false, { :type => :normal, :visibility => :public, :storage => :bar })
254
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
255
+ @class.field 'foo', :storage => :bar
256
+ end
257
+ e.session_class.should be(@class)
258
+ e.field_name.should == :foo
259
+ e.message.should match(/foo/i)
260
+ end
261
+
262
+ it "should raise if you try to define a field with the same storage name" do
263
+ @field_foo = expect_and_create_field!(:foo, 'foo', true, false, { :type => :normal, :visibility => :public })
264
+ @class.field :foo
265
+
266
+ @field_foo = expect_and_create_field!('bar', 'foo', true, false, { :type => :normal, :visibility => :public, :storage => :foo })
267
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
268
+ @class.field 'bar', :storage => :foo
269
+ end
270
+ e.session_class.should be(@class)
271
+ e.original_field_name.should == :foo
272
+ e.new_field_name.should == :bar
273
+ e.storage_name.should == 'foo'
274
+ e.message.should match(/foo/i)
275
+ e.message.should match(/bar/i)
276
+ end
277
+
278
+ describe "inactive fields" do
279
+ it "should allow defining an inactive field" do
280
+ @field_foo = expect_and_create_field!(:foo, 'foo', false, false, { :type => :inactive, :visibility => :public })
281
+ @class.inactive :foo
282
+
283
+ @class._field_named(:foo).should be(@field_foo)
284
+ @class._field_named('foo').should be(@field_foo)
285
+ @class._field_with_storage_name(:foo).should be(@field_foo)
286
+ @class._field_with_storage_name('foo').should be(@field_foo)
287
+ end
288
+
289
+ it "should conflict with a normal field" do
290
+ @field_foo = expect_and_create_field!(:foo, 'foo', false, false, { :type => :inactive, :visibility => :public })
291
+ @class.inactive :foo
292
+
293
+ @field_foo = expect_and_create_field!('foo', 'bar', true, false, { :type => :normal, :visibility => :public, :storage => :bar })
294
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
295
+ @class.field 'foo', :storage => :bar
296
+ end
297
+ e.session_class.should be(@class)
298
+ e.field_name.should == :foo
299
+ e.message.should match(/foo/i)
300
+ end
301
+
302
+ it "should raise if you try to define a field with the same storage name" do
303
+ @field_foo = expect_and_create_field!(:foo, 'foo', false, false, { :type => :inactive, :visibility => :public })
304
+ @class.inactive :foo
305
+
306
+ @field_foo = expect_and_create_field!('bar', 'foo', true, false, { :type => :normal, :visibility => :public, :storage => :foo })
307
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
308
+ @class.field 'bar', :storage => :foo
309
+ end
310
+ e.session_class.should be(@class)
311
+ e.original_field_name.should == :foo
312
+ e.new_field_name.should == :bar
313
+ e.storage_name.should == 'foo'
314
+ e.message.should match(/foo/i)
315
+ e.message.should match(/bar/i)
316
+ end
317
+ end
318
+
319
+ describe "retired fields" do
320
+ it "should allow defining a retired field" do
321
+ @field_foo = expect_and_create_field!(:foo, 'foo', false, true, { :type => :retired, :visibility => :public })
322
+ @class.retired :foo
323
+
324
+ @class._field_named(:foo).should be(@field_foo)
325
+ @class._field_named('foo').should be(@field_foo)
326
+ @class._field_with_storage_name(:foo).should be(@field_foo)
327
+ @class._field_with_storage_name('foo').should be(@field_foo)
328
+ end
329
+
330
+ it "should conflict with a normal field" do
331
+ @field_foo = expect_and_create_field!(:foo, 'foo', false, true, { :type => :retired, :visibility => :public })
332
+ @class.retired :foo
333
+
334
+ @field_foo = expect_and_create_field!('foo', 'bar', true, false, { :type => :normal, :visibility => :public, :storage => :bar })
335
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
336
+ @class.field 'foo', :storage => :bar
337
+ end
338
+ e.session_class.should be(@class)
339
+ e.field_name.should == :foo
340
+ e.message.should match(/foo/i)
341
+ end
342
+
343
+ it "should raise if you try to define a field with the same storage name" do
344
+ @field_foo = expect_and_create_field!(:foo, 'foo', false, true, { :type => :retired, :visibility => :public })
345
+ @class.retired :foo
346
+
347
+ @field_foo = expect_and_create_field!('bar', 'foo', true, false, { :type => :normal, :visibility => :public, :storage => :foo })
348
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
349
+ @class.field 'bar', :storage => :foo
350
+ end
351
+ e.session_class.should be(@class)
352
+ e.original_field_name.should == :foo
353
+ e.new_field_name.should == :bar
354
+ e.storage_name.should == 'foo'
355
+ e.message.should match(/foo/i)
356
+ e.message.should match(/bar/i)
357
+ end
358
+ end
359
+ end
360
+
361
+ it "should set and return the default visibility properly" do
362
+ @class.default_visibility.should == :public
363
+ @class.default_visibility :private
364
+ @class.default_visibility.should == :private
365
+ lambda { @class.default_visibility :foobar }.should raise_error(ArgumentError)
366
+ lambda { @class.default_visibility 12345 }.should raise_error(ArgumentError)
367
+ @class.default_visibility.should == :private
368
+ @class.default_visibility :public
369
+ @class.default_visibility.should == :public
370
+ end
371
+
372
+ it "should set and return the prefix properly" do
373
+ @class.prefix.should == nil
374
+ @class.prefix :prf
375
+ @class.prefix.should == 'prf'
376
+ @class.prefix 'BaZ'
377
+ @class.prefix.should == 'BaZ'
378
+ lambda { @class.prefix 12345 }.should raise_error(ArgumentError)
379
+ lambda { @class.prefix 4.3 }.should raise_error(ArgumentError)
380
+ @class.prefix.should == 'BaZ'
381
+ @class.prefix nil
382
+ @class.prefix.should == nil
383
+ end
384
+
385
+ it "should set and return #unknown_fields properly" do
386
+ @class.unknown_fields.should == :preserve
387
+ @class.unknown_fields :delete
388
+ @class.unknown_fields.should == :delete
389
+ lambda { @class.unknown_fields :foo }.should raise_error(ArgumentError)
390
+ lambda { @class.unknown_fields 12345 }.should raise_error(ArgumentError)
391
+ @class.unknown_fields.should == :delete
392
+ @class.unknown_fields :preserve
393
+ @class.unknown_fields.should == :preserve
394
+ end
395
+
396
+ it "should return only fields in #accessible_field_names, return fields by name or storage name appropriately, and raise NoSuchFieldError when appropriate" do
397
+ @field_foo = expect_and_create_field!(:foo, 'stg1', true, false, { :type => :normal, :visibility => :public, :storage => :stg1 })
398
+ @class.field :foo, :storage => :stg1
399
+
400
+ @field_bar = expect_and_create_field!(:bar, 'stg2', false, false, { :type => :inactive, :visibility => :public, :storage => :stg2 })
401
+ @class.inactive :bar, :storage => :stg2
402
+
403
+ @field_baz = expect_and_create_field!(:baz, 'stg3', false, true, { :type => :retired, :visibility => :public, :storage => :stg3 })
404
+ @class.retired :baz, :storage => :stg3
405
+
406
+ @field_quux = expect_and_create_field!(:quux, 'stg4', true, false, { :type => :normal, :visibility => :public, :storage => :stg4 })
407
+ @class.field :quux, :storage => :stg4
408
+
409
+ @class.accessible_field_names.sort_by(&:to_s).should == [ :foo, :quux ].sort_by(&:to_s)
410
+
411
+ instance = @class.new(@underlying_session)
412
+ instance.field_names.sort_by(&:to_s).should == [ :foo, :quux ].sort_by(&:to_s)
413
+
414
+ expect(@underlying_session).to receive(:[]).once.with('stg1').and_return(nil)
415
+ expect(@underlying_session).to receive(:[]).once.with('stg4').and_return(123)
416
+ instance.keys.should == [ :quux ]
417
+
418
+ @class._field_named(:foo).should be(@field_foo)
419
+ @class._field_named(:bar).should be(@field_bar)
420
+ @class._field_named(:baz).should be(@field_baz)
421
+ @class._field_named(:quux).should be(@field_quux)
422
+ @class._field_named('foo').should be(@field_foo)
423
+ @class._field_named('bar').should be(@field_bar)
424
+ @class._field_named('baz').should be(@field_baz)
425
+ @class._field_named('quux').should be(@field_quux)
426
+
427
+ @class._field_with_storage_name(:foo).should == nil
428
+ @class._field_with_storage_name(:bar).should == nil
429
+ @class._field_with_storage_name(:baz).should == nil
430
+ @class._field_with_storage_name(:quux).should == nil
431
+ @class._field_with_storage_name('foo').should == nil
432
+ @class._field_with_storage_name('bar').should == nil
433
+ @class._field_with_storage_name('baz').should == nil
434
+ @class._field_with_storage_name('quux').should == nil
435
+
436
+ @class._field_with_storage_name(:stg1).should be(@field_foo)
437
+ @class._field_with_storage_name(:stg2).should be(@field_bar)
438
+ @class._field_with_storage_name(:stg3).should be(@field_baz)
439
+ @class._field_with_storage_name(:stg4).should be(@field_quux)
440
+ @class._field_with_storage_name('stg1').should be(@field_foo)
441
+ @class._field_with_storage_name('stg2').should be(@field_bar)
442
+ @class._field_with_storage_name('stg3').should be(@field_baz)
443
+ @class._field_with_storage_name('stg4').should be(@field_quux)
444
+
445
+ @class._ensure_has_field_named(:foo)
446
+ @class._ensure_has_field_named(:quux)
447
+ @class._ensure_has_field_named('foo')
448
+ @class._ensure_has_field_named('quux')
449
+
450
+ lambda { @class._ensure_has_field_named(:bar) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
451
+ lambda { @class._ensure_has_field_named(:baz) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
452
+ lambda { @class._ensure_has_field_named('bar') }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
453
+ lambda { @class._ensure_has_field_named('baz') }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
454
+
455
+ lambda { @class._ensure_has_field_named(:stg1) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
456
+ lambda { @class._ensure_has_field_named(:stg2) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
457
+ lambda { @class._ensure_has_field_named(:stg3) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
458
+ lambda { @class._ensure_has_field_named(:stg4) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
459
+ lambda { @class._ensure_has_field_named('stg1') }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
460
+ lambda { @class._ensure_has_field_named('stg2') }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
461
+ lambda { @class._ensure_has_field_named('stg3') }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
462
+ lambda { @class._ensure_has_field_named('stg4') }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
463
+ end
464
+
465
+ it "should return a dynamic-methods module that's configured correctly" do
466
+ ::Object.const_set(:ObjectifiedSessionsSpecClassDmm, @class)
467
+
468
+ mod = @class._dynamic_methods_module
469
+ @class.included_modules.include?(mod).should be
470
+ mod.name.should == "ObjectifiedSessionsSpecClassDmm::ObjectifiedSessionsDynamicMethods"
471
+
472
+ mod.define_method("foo") { "foo!" }
473
+ instance = @class.new(@underlying_session)
474
+ instance.foo.should == "foo!"
475
+
476
+ mod.define_method("bar") { "bar!" }
477
+ mod.private :bar
478
+ instance = @class.new(@underlying_session)
479
+ instance.respond_to?(:bar).should_not be
480
+ instance.send(:bar).should == "bar!"
481
+
482
+ @class._dynamic_methods_module.should be(mod)
483
+ end
484
+ end
@@ -0,0 +1,75 @@
1
+ require 'objectified_sessions'
2
+
3
+ describe ObjectifiedSessions::Errors do
4
+ before :each do
5
+ @session_class = double("session_class")
6
+ allow(@session_class).to receive(:name).with().and_return("scname")
7
+ allow(@session_class).to receive(:to_s).with().and_return("scto_s")
8
+ allow(@session_class).to receive(:inspect).with().and_return("scinspect")
9
+ allow(@session_class).to receive(:accessible_field_names).with().and_return([ :foo, :bar, :baz ])
10
+
11
+ @field_name = double("field_name")
12
+ allow(@field_name).to receive(:to_s).with().and_return("fnto_s")
13
+ allow(@field_name).to receive(:inspect).with().and_return("fninspect")
14
+
15
+ @field_name_2 = double("field_name_2")
16
+ allow(@field_name_2).to receive(:to_s).with().and_return("fn2to_s")
17
+ allow(@field_name_2).to receive(:inspect).with().and_return("fn2inspect")
18
+
19
+ @storage_name = double("storage_name")
20
+ allow(@storage_name).to receive(:to_s).with().and_return("snto_s")
21
+ allow(@storage_name).to receive(:inspect).with().and_return("sninspect")
22
+ end
23
+
24
+ it "should inherit Base from StandardError" do
25
+ ObjectifiedSessions::Errors::Base.new("foo").kind_of?(StandardError).should be
26
+ end
27
+
28
+ it "should inherit CannotCreateSessionError from Base" do
29
+ ObjectifiedSessions::Errors::CannotCreateSessionError.new("foo").kind_of?(ObjectifiedSessions::Errors::Base).should be
30
+ end
31
+
32
+ it "should inherit NoSuchFieldError from Base, and give a good message" do
33
+ instance = ObjectifiedSessions::Errors::NoSuchFieldError.new(@session_class, @field_name)
34
+
35
+ instance.kind_of?(ObjectifiedSessions::Errors::Base).should be
36
+
37
+ instance.session_class.should be(@session_class)
38
+ instance.field_name.should be(@field_name)
39
+ instance.accessible_field_names.should == [ :foo, :bar, :baz ]
40
+
41
+ instance.message.should match(/scname/i)
42
+ instance.message.should match(/fninspect/i)
43
+ instance.message.should match(/foo/i)
44
+ instance.message.should match(/bar/i)
45
+ instance.message.should match(/baz/i)
46
+ end
47
+
48
+ it "should inherit DuplicateFieldNameError from Base, and give a good message" do
49
+ instance = ObjectifiedSessions::Errors::DuplicateFieldNameError.new(@session_class, @field_name)
50
+
51
+ instance.kind_of?(ObjectifiedSessions::Errors::Base).should be
52
+
53
+ instance.session_class.should be(@session_class)
54
+ instance.field_name.should be(@field_name)
55
+
56
+ instance.message.should match(/scname/i)
57
+ instance.message.should match(/fninspect/i)
58
+ end
59
+
60
+ it "should inherit DuplicateFieldStorageNameError from Base, and give a good message" do
61
+ instance = ObjectifiedSessions::Errors::DuplicateFieldStorageNameError.new(@session_class, @field_name, @field_name_2, @storage_name)
62
+
63
+ instance.kind_of?(ObjectifiedSessions::Errors::Base).should be
64
+
65
+ instance.session_class.should be(@session_class)
66
+ instance.original_field_name.should be(@field_name)
67
+ instance.new_field_name.should be(@field_name_2)
68
+ instance.storage_name.should be(@storage_name)
69
+
70
+ instance.message.should match(/scname/i)
71
+ instance.message.should match(/fninspect/i)
72
+ instance.message.should match(/fn2inspect/i)
73
+ instance.message.should match(/sninspect/i)
74
+ end
75
+ end