data-provider 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/data-provider.gemspec +2 -5
- data/lib/data_provider/base.rb +149 -146
- data/lib/data_provider/container.rb +277 -0
- data/lib/data_provider.rb +1 -0
- data/spec/base_spec.rb +818 -0
- data/spec/container_spec.rb +792 -0
- data/spec/data_provider_spec.rb +85 -349
- metadata +4 -1
@@ -0,0 +1,792 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe DataProvider::Container do
|
4
|
+
# Example implementation of DataProvider::Base
|
5
|
+
let(:container){
|
6
|
+
DataProvider::Container.new.tap do |container|
|
7
|
+
container.provider :sum, :requires => [:array] do
|
8
|
+
sum = 0
|
9
|
+
|
10
|
+
(given(:array) || []).each do |number|
|
11
|
+
sum += number.to_i
|
12
|
+
end
|
13
|
+
|
14
|
+
sum
|
15
|
+
end
|
16
|
+
|
17
|
+
container.provider :static do
|
18
|
+
'StaticValue'
|
19
|
+
end
|
20
|
+
|
21
|
+
container.provider :billy do
|
22
|
+
take([:identification, :fullname])
|
23
|
+
end
|
24
|
+
|
25
|
+
container.provider [:identification, :firstname] do
|
26
|
+
'Billy'
|
27
|
+
end
|
28
|
+
|
29
|
+
container.provider [:identification, :lastname] do
|
30
|
+
'Bragg'
|
31
|
+
end
|
32
|
+
|
33
|
+
container.provider [:identification, :fullname] do
|
34
|
+
"#{scoped_take(:firstname)} #{scoped_take(:lastname)}"
|
35
|
+
end
|
36
|
+
|
37
|
+
container.provider [:identification, :identifier] do
|
38
|
+
take(:firstname)
|
39
|
+
end
|
40
|
+
|
41
|
+
container.provider :fullname do
|
42
|
+
'Stephen William Bragg'
|
43
|
+
end
|
44
|
+
|
45
|
+
container.provider [:identification, :id] do
|
46
|
+
take(:fullname)
|
47
|
+
end
|
48
|
+
|
49
|
+
container.provider [:identification, :ID] do
|
50
|
+
take(:id)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
}
|
54
|
+
|
55
|
+
describe "#has_provider?" do
|
56
|
+
it 'tells if the specified provider exists' do
|
57
|
+
expect(container.respond_to?(:has_provider?)).to eq true
|
58
|
+
expect(container.has_provider?(:sum)).to eq true
|
59
|
+
expect(container.has_provider?(:divid)).to eq false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#has_providers_with_scope?" do
|
64
|
+
let(:container){
|
65
|
+
DataProvider::Container.new.tap do |container|
|
66
|
+
container.provider [:a, :b ,:c]
|
67
|
+
container.provider :unscoped
|
68
|
+
end
|
69
|
+
}
|
70
|
+
|
71
|
+
it "return true if there are providers defined with an array identifier that start with the given prefix" do
|
72
|
+
expect(container.has_providers_with_scope?(:unscoped)).to eq false
|
73
|
+
expect(container.has_providers_with_scope?(:a)).to eq true
|
74
|
+
expect(container.has_providers_with_scope?([:a, :b])).to eq true
|
75
|
+
# scope means prefix, identfier may not be exactly the given array
|
76
|
+
expect(container.has_providers_with_scope?([:a, :b, :c])).to eq false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#provides" do
|
81
|
+
let(:container){
|
82
|
+
DataProvider::Container.new.tap do |container|
|
83
|
+
container.provides({
|
84
|
+
:name => 'Paddy',
|
85
|
+
'instrument' => :bass
|
86
|
+
})
|
87
|
+
end
|
88
|
+
}
|
89
|
+
# class SimpleProviders
|
90
|
+
# include DataProvider::Base
|
91
|
+
# provides({
|
92
|
+
# :name => 'Paddy',
|
93
|
+
# 'instrument' => :bass,
|
94
|
+
# })
|
95
|
+
# end
|
96
|
+
|
97
|
+
it "lets you request all currently available simple providers when called without a parameter" do
|
98
|
+
expect(container.provides).to eq({
|
99
|
+
:name => 'Paddy',
|
100
|
+
'instrument' => :bass
|
101
|
+
})
|
102
|
+
end
|
103
|
+
|
104
|
+
it "lets you define simple providers" do
|
105
|
+
expect(container.take(:name)).to eq 'Paddy'
|
106
|
+
expect(container.take('instrument')).to eq :bass
|
107
|
+
end
|
108
|
+
|
109
|
+
it "works with has_provider?" do
|
110
|
+
expect(container.has_provider?(:name)).to eq true
|
111
|
+
expect(container.has_provider?('name')).to eq false
|
112
|
+
expect(container.has_provider?('instrument')).to eq true
|
113
|
+
expect(container.has_provider?(:instrument)).to eq false
|
114
|
+
end
|
115
|
+
|
116
|
+
it "lets you overwrite existing simple providers" do
|
117
|
+
container.provides({:name => 'Erik'})
|
118
|
+
expect(container.take(:name)).to eq 'Erik'
|
119
|
+
end
|
120
|
+
|
121
|
+
it "lets you write linked notation" do
|
122
|
+
expect(container.provides({:name => 'Lane'}).take(:name)).to eq 'Lane'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "works with lambdas" do
|
126
|
+
expect(container.provides(:name => lambda{ 'Patrick' }).take(:name)).to eq 'Patrick'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "works with Procs" do
|
130
|
+
expect(container.provides(:name => Proc.new{ 'St. Patrick' }).take(:name)).to eq 'St. Patrick'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "#add!" do
|
135
|
+
let(:odd_providers){
|
136
|
+
DataProvider::Container.new.tap do |c|
|
137
|
+
c.provides({1 => 'one'})
|
138
|
+
c.provider :three do 3 end
|
139
|
+
end
|
140
|
+
}
|
141
|
+
|
142
|
+
let(:odd_overwrite_providers){
|
143
|
+
DataProvider::Container.new.tap do |c|
|
144
|
+
c.provides({1 => 'Uno', :five => 555})
|
145
|
+
c.provider :three do :tres end
|
146
|
+
end
|
147
|
+
}
|
148
|
+
|
149
|
+
let(:container){
|
150
|
+
DataProvider::Container.new.tap do |container|
|
151
|
+
container.provides({2 => '1'})
|
152
|
+
container.provider :four do '4' end
|
153
|
+
container.add! odd_providers
|
154
|
+
end
|
155
|
+
}
|
156
|
+
|
157
|
+
it "lets you add providers from another container" do
|
158
|
+
expect(container.has_provider?(1)).to eq true
|
159
|
+
expect(container.has_provider?(2)).to eq true
|
160
|
+
expect(container.has_provider?(:three)).to eq true
|
161
|
+
expect(container.has_provider?(:four)).to eq true
|
162
|
+
# expect(BasicProviders.new.take(1)).to eq 'one'
|
163
|
+
expect(container.take(:three)).to eq 3
|
164
|
+
end
|
165
|
+
|
166
|
+
it "lets you add providers from another container at runtime" do
|
167
|
+
expect(container.has_provider?(:five)).to eq false
|
168
|
+
container.add!(odd_overwrite_providers)
|
169
|
+
expect(container.has_provider?(:five)).to eq true
|
170
|
+
end
|
171
|
+
|
172
|
+
# for the following test the providers of OddOverwriteProviders
|
173
|
+
# have already been added (by the previous test)
|
174
|
+
it "lets you overwrite providers" do
|
175
|
+
container.add!(odd_overwrite_providers)
|
176
|
+
expect(container.take(1)).to eq 'Uno'
|
177
|
+
expect(container.take(:three)).to eq :tres
|
178
|
+
end
|
179
|
+
|
180
|
+
it "includes providers which can be overwritten" do
|
181
|
+
cont = DataProvider::Container.new
|
182
|
+
cont.add! odd_providers
|
183
|
+
cont.provider :three do '33' end
|
184
|
+
expect(cont.take(:three)).to eq '33'
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe "#add" do
|
189
|
+
let(:odd_providers){
|
190
|
+
DataProvider::Container.new.tap do |c|
|
191
|
+
c.provides({1 => 'one'})
|
192
|
+
c.provider :three do 3 end
|
193
|
+
end
|
194
|
+
}
|
195
|
+
|
196
|
+
let(:odd_overwrite_providers){
|
197
|
+
DataProvider::Container.new.tap do |c|
|
198
|
+
c.provides({1 => 'Uno', :five => 555})
|
199
|
+
c.provider :three do :tres end
|
200
|
+
end
|
201
|
+
}
|
202
|
+
|
203
|
+
let(:container){
|
204
|
+
DataProvider::Container.new.tap do |container|
|
205
|
+
container.provides({2 => '1'})
|
206
|
+
container.provider :four do '4' end
|
207
|
+
container.add odd_providers
|
208
|
+
end
|
209
|
+
}
|
210
|
+
|
211
|
+
it "lets you add providers from another container" do
|
212
|
+
expect(container.has_provider?(1)).to eq false
|
213
|
+
expect(container.has_provider?(2)).to eq true
|
214
|
+
expect(container.has_provider?(:three)).to eq false
|
215
|
+
expect(container.has_provider?(:four)).to eq true
|
216
|
+
# expect(BasicProviders.new.take(1)).to eq 'one'
|
217
|
+
# expect(container.take(:three)).to eq 3
|
218
|
+
end
|
219
|
+
|
220
|
+
it "lets you add providers from another container at runtime" do
|
221
|
+
expect(container.has_provider?(:five)).to eq false
|
222
|
+
new_container = container.add(odd_overwrite_providers)
|
223
|
+
expect(container.has_provider?(:five)).to eq false
|
224
|
+
expect(new_container.has_provider?(:five)).to eq true
|
225
|
+
end
|
226
|
+
|
227
|
+
# for the following test the providers of OddOverwriteProviders
|
228
|
+
# have already been added (by the previous test)
|
229
|
+
it "lets you overwrite providers" do
|
230
|
+
new_container = container.add(odd_overwrite_providers)
|
231
|
+
expect(container.has_provider?(1)).to eq false
|
232
|
+
expect(new_container.take(1)).to eq 'Uno'
|
233
|
+
expect(container.has_provider?(:three)).to eq false
|
234
|
+
expect(new_container.take(:three)).to eq :tres
|
235
|
+
end
|
236
|
+
|
237
|
+
it "includes providers which can be overwritten" do
|
238
|
+
cont = DataProvider::Container.new
|
239
|
+
odd = cont.add odd_providers
|
240
|
+
odd.provider :three do '33' end
|
241
|
+
expect(odd.take(:three)).to eq '33'
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "#add_scoped!" do
|
246
|
+
let(:child_providers){
|
247
|
+
DataProvider::Container.new.tap do |c|
|
248
|
+
c.provider :name do "child" end
|
249
|
+
end
|
250
|
+
}
|
251
|
+
|
252
|
+
let(:grandchild_providers){
|
253
|
+
DataProvider::Container.new.tap do |c|
|
254
|
+
c.provider :name do "grandchild" end
|
255
|
+
c.provider [:age] do 1 end
|
256
|
+
c.provides({
|
257
|
+
:mommy => 'Wilma',
|
258
|
+
:daddy => 'Fret'
|
259
|
+
})
|
260
|
+
|
261
|
+
c.provider :mobility do
|
262
|
+
'crawling'
|
263
|
+
end
|
264
|
+
|
265
|
+
c.provider :movement do
|
266
|
+
take(:mobility)
|
267
|
+
end
|
268
|
+
|
269
|
+
c.provider :symbol do
|
270
|
+
'Symbol provider'
|
271
|
+
end
|
272
|
+
|
273
|
+
c.provider :sym do
|
274
|
+
take(:symbol)
|
275
|
+
end
|
276
|
+
|
277
|
+
c.provider ['string'] do
|
278
|
+
'String provider: ' + take(:symbol)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
}
|
282
|
+
|
283
|
+
let(:container){
|
284
|
+
DataProvider::Container.new.tap do |c|
|
285
|
+
c.provider :parent do 'parent' end
|
286
|
+
c.add_scoped! child_providers, :scope => :child
|
287
|
+
c.add_scoped! grandchild_providers, :scope => [:child, :child]
|
288
|
+
end
|
289
|
+
}
|
290
|
+
|
291
|
+
it 'let you array-prefix the providers of an included container' do
|
292
|
+
expect(container.has_provider?(:parent)).to eq true
|
293
|
+
expect(container.has_provider?(:name)).to eq false
|
294
|
+
expect(container.has_provider?([:child, :name])).to eq true
|
295
|
+
expect(container.has_provider?([:child, :age])).to eq false
|
296
|
+
expect(container.has_provider?([:child, :child, :name])).to eq true
|
297
|
+
expect(container.has_provider?([:child, :child, :age])).to eq true
|
298
|
+
expect(container.has_provider?([:child, :child, :mommy])).to eq true
|
299
|
+
expect(container.has_provider?([:child, :child, :daddy])).to eq true
|
300
|
+
expect(container.take([:child, :name])).to eq 'child'
|
301
|
+
expect(container.take([:child, :child, :name])).to eq 'grandchild'
|
302
|
+
expect(container.take([:child, :child, :age])).to eq 1
|
303
|
+
expect(container.take([:child, :child, :mommy])).to eq 'Wilma'
|
304
|
+
expect(container.take([:child, :child, :daddy])).to eq 'Fret'
|
305
|
+
end
|
306
|
+
|
307
|
+
it "#take acts like #scoped_take inside providers works for add_scoped containers as well" do
|
308
|
+
expect( container.take([:child, :child, :mobility]) ).to eq 'crawling'
|
309
|
+
expect( container.take([:child, :child, :movement]) ).to eq 'crawling'
|
310
|
+
end
|
311
|
+
|
312
|
+
it "doesn't act up when mixing symbols and strings in array identifiers" do
|
313
|
+
expect( container.take([:child, :child, 'string']) ).to eq 'String provider: Symbol provider'
|
314
|
+
end
|
315
|
+
|
316
|
+
it "lets #take act like #scoped_take recursively" do
|
317
|
+
expect( container.take([:child, :child, :sym]) ).to eq 'Symbol provider'
|
318
|
+
end
|
319
|
+
|
320
|
+
it 'respect provider order/priority' do
|
321
|
+
c1 = DataProvider::Container.new
|
322
|
+
c1.provider 'version' do 1 end
|
323
|
+
|
324
|
+
c2 = DataProvider::Container.new
|
325
|
+
c2.add! c1
|
326
|
+
c2.provider 'version' do 2 end
|
327
|
+
|
328
|
+
c = DataProvider::Container.new
|
329
|
+
c.provider 'version' do 0 end
|
330
|
+
c.provider ['module', 'version'] do -1 end
|
331
|
+
c.add_scoped! c2, :scope => 'module'
|
332
|
+
|
333
|
+
expect(c.try_take('version')).to eq 0
|
334
|
+
expect(c.try_take(['module', 'version'])).to eq 2
|
335
|
+
end
|
336
|
+
|
337
|
+
it 'works recursively' do
|
338
|
+
c1 = DataProvider::Container.new
|
339
|
+
c1.provider ['name'] do 'Johnny Blaze' end
|
340
|
+
|
341
|
+
expect(c1.provider_identifiers).to eq [['name']]
|
342
|
+
|
343
|
+
c2 = DataProvider::Container.new.add(c1)
|
344
|
+
expect(c2.provider_identifiers).to eq [['name']]
|
345
|
+
|
346
|
+
c3 = DataProvider::Container.new
|
347
|
+
c3.provider ['name'] do 'Mr. Nobody' end
|
348
|
+
# the next line adds the provider ['person', 'name'] to m3
|
349
|
+
c3.add_scoped! c2, :scope => 'person'
|
350
|
+
|
351
|
+
# providers are internally added in reverse order
|
352
|
+
expect(c3.provider_identifiers).to eq [['person', 'name'], ['name']]
|
353
|
+
|
354
|
+
c = DataProvider::Container.new
|
355
|
+
c.add_scoped! c3, :scope => 'creatures'
|
356
|
+
|
357
|
+
expect(c.has_provider?('name')).to eq false
|
358
|
+
expect(c.has_provider?(['name'])).to eq false
|
359
|
+
expect(c.has_provider?(['person', 'name'])).to eq false
|
360
|
+
expect(c.has_provider?(['creatures', 'person', 'name'])).to eq true
|
361
|
+
expect(c.take(['creatures', 'person', 'name'])).to eq 'Johnny Blaze'
|
362
|
+
end
|
363
|
+
|
364
|
+
it "doesn't affect the added container" do
|
365
|
+
c1 = DataProvider::Container.new
|
366
|
+
c1.provider ['name'] do 'Johnny Blaze' end
|
367
|
+
|
368
|
+
expect(c1.provider_identifiers).to eq [['name']]
|
369
|
+
|
370
|
+
c = DataProvider::Container.new
|
371
|
+
c.add_scoped! c1, :scope => 'prefix'
|
372
|
+
|
373
|
+
expect(c1.provider_identifiers).to eq [['name']]
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
describe "#add_scoped" do
|
378
|
+
let(:child_providers){
|
379
|
+
DataProvider::Container.new.tap do |c|
|
380
|
+
c.provider :name do "child" end
|
381
|
+
end
|
382
|
+
}
|
383
|
+
|
384
|
+
let(:grandchild_providers){
|
385
|
+
DataProvider::Container.new.tap do |c|
|
386
|
+
c.provider :name do "grandchild" end
|
387
|
+
c.provider [:age] do 1 end
|
388
|
+
c.provides({
|
389
|
+
:mommy => 'Wilma',
|
390
|
+
:daddy => 'Fret'
|
391
|
+
})
|
392
|
+
|
393
|
+
c.provider :mobility do
|
394
|
+
'crawling'
|
395
|
+
end
|
396
|
+
|
397
|
+
c.provider :movement do
|
398
|
+
take(:mobility)
|
399
|
+
end
|
400
|
+
|
401
|
+
c.provider :symbol do
|
402
|
+
'Symbol provider'
|
403
|
+
end
|
404
|
+
|
405
|
+
c.provider :sym do
|
406
|
+
take(:symbol)
|
407
|
+
end
|
408
|
+
|
409
|
+
c.provider ['string'] do
|
410
|
+
'String provider: ' + take(:symbol)
|
411
|
+
end
|
412
|
+
end
|
413
|
+
}
|
414
|
+
|
415
|
+
let(:container){
|
416
|
+
DataProvider::Container.new.tap do |c|
|
417
|
+
c.provider :parent do 'parent' end
|
418
|
+
c.add_scoped child_providers, :scope => :child
|
419
|
+
c.add_scoped grandchild_providers, :scope => [:child, :child]
|
420
|
+
end
|
421
|
+
}
|
422
|
+
|
423
|
+
it 'let you array-prefix the providers of an included container' do
|
424
|
+
expect(container.has_provider?(:parent)).to eq true
|
425
|
+
expect(container.has_provider?(:name)).to eq false
|
426
|
+
expect(container.has_provider?([:child, :name])).to eq false
|
427
|
+
expect(container.has_provider?([:child, :age])).to eq false
|
428
|
+
expect(container.has_provider?([:child, :child, :name])).to eq false
|
429
|
+
expect(container.has_provider?([:child, :child, :age])).to eq false
|
430
|
+
expect(container.has_provider?([:child, :child, :mommy])).to eq false
|
431
|
+
expect(container.has_provider?([:child, :child, :daddy])).to eq false
|
432
|
+
# expect(container.take([:child, :name])).to eq 'child'
|
433
|
+
# expect(container.take([:child, :child, :name])).to eq 'grandchild'
|
434
|
+
# expect(container.take([:child, :child, :age])).to eq 1
|
435
|
+
# expect(container.take([:child, :child, :mommy])).to eq 'Wilma'
|
436
|
+
# expect(container.take([:child, :child, :daddy])).to eq 'Fret'
|
437
|
+
end
|
438
|
+
|
439
|
+
it "#take acts like #scoped_take inside providers works for add_scoped containers as well" do
|
440
|
+
newcontainer = container.add_scoped(child_providers, :scope => :child).add_scoped(grandchild_providers, :scope => [:child, :child])
|
441
|
+
expect( newcontainer.take([:child, :child, :mobility]) ).to eq 'crawling'
|
442
|
+
expect( newcontainer.take([:child, :child, :movement]) ).to eq 'crawling'
|
443
|
+
end
|
444
|
+
|
445
|
+
it "doesn't act up when mixing symbols and strings in array identifiers" do
|
446
|
+
newcontainer = container.add_scoped(child_providers, :scope => :child).add_scoped(grandchild_providers, :scope => [:child, :child])
|
447
|
+
expect( newcontainer.take([:child, :child, 'string']) ).to eq 'String provider: Symbol provider'
|
448
|
+
end
|
449
|
+
|
450
|
+
it "lets #take act like #scoped_take recursively" do
|
451
|
+
newcontainer = container.add_scoped(child_providers, :scope => :child).add_scoped(grandchild_providers, :scope => [:child, :child])
|
452
|
+
expect( newcontainer.take([:child, :child, :sym]) ).to eq 'Symbol provider'
|
453
|
+
end
|
454
|
+
|
455
|
+
it 'respect provider order/priority' do
|
456
|
+
c1 = DataProvider::Container.new
|
457
|
+
c1.provider 'version' do 1 end
|
458
|
+
|
459
|
+
c2 = DataProvider::Container.new
|
460
|
+
c2.add c1
|
461
|
+
c2.provider 'version' do 2 end
|
462
|
+
|
463
|
+
c = DataProvider::Container.new
|
464
|
+
c.provider 'version' do 0 end
|
465
|
+
c.provider ['module', 'version'] do -1 end
|
466
|
+
cc = c.add_scoped c2, :scope => 'module'
|
467
|
+
|
468
|
+
expect(cc.try_take('version')).to eq 0
|
469
|
+
expect(cc.try_take(['module', 'version'])).to eq 2
|
470
|
+
end
|
471
|
+
|
472
|
+
it 'works recursively' do
|
473
|
+
c1 = DataProvider::Container.new
|
474
|
+
c1.provider ['name'] do 'Johnny Blaze' end
|
475
|
+
|
476
|
+
expect(c1.provider_identifiers).to eq [['name']]
|
477
|
+
|
478
|
+
c2 = DataProvider::Container.new.add(c1)
|
479
|
+
expect(c2.provider_identifiers).to eq [['name']]
|
480
|
+
|
481
|
+
c3 = DataProvider::Container.new
|
482
|
+
c3.provider ['name'] do 'Mr. Nobody' end
|
483
|
+
# the next line adds the provider ['person', 'name'] to m3
|
484
|
+
c4 = c3.add_scoped c2, :scope => 'person'
|
485
|
+
|
486
|
+
# providers are internally added in reverse order
|
487
|
+
expect(c4.provider_identifiers).to eq [['person', 'name'], ['name']]
|
488
|
+
|
489
|
+
c = DataProvider::Container.new
|
490
|
+
cc = c.add_scoped c3, :scope => 'creatures'
|
491
|
+
|
492
|
+
expect(cc.has_provider?('name')).to eq false
|
493
|
+
expect(cc.has_provider?(['name'])).to eq false
|
494
|
+
expect(cc.has_provider?(['person', 'name'])).to eq false
|
495
|
+
expect(cc.has_provider?(['creatures', 'person', 'name'])).to eq false
|
496
|
+
ccc = cc.add_scoped c4, :scope => 'creatures'
|
497
|
+
expect(ccc.has_provider?(['creatures', 'person', 'name'])).to eq true
|
498
|
+
expect(ccc.take(['creatures', 'person', 'name'])).to eq 'Johnny Blaze'
|
499
|
+
end
|
500
|
+
|
501
|
+
it "doesn't affect the added container" do
|
502
|
+
c1 = DataProvider::Container.new
|
503
|
+
c1.provider ['name'] do 'Johnny Blaze' end
|
504
|
+
|
505
|
+
expect(c1.provider_identifiers).to eq [['name']]
|
506
|
+
|
507
|
+
c = DataProvider::Container.new
|
508
|
+
c2 = c.add_scoped c1, :scope => 'prefix'
|
509
|
+
|
510
|
+
expect(c1.provider_identifiers).to eq [['name']]
|
511
|
+
end
|
512
|
+
end
|
513
|
+
|
514
|
+
describe "#provider_missing" do
|
515
|
+
let(:container){
|
516
|
+
DataProvider::Container.new.tap do |c|
|
517
|
+
c.provider_missing do
|
518
|
+
"This provider don't exist!"
|
519
|
+
end
|
520
|
+
end
|
521
|
+
}
|
522
|
+
|
523
|
+
it "lets you define a default fallback provider" do
|
524
|
+
expect(container.has_provider?(:message)).to eq false
|
525
|
+
expect(container.take(:message)).to eq "This provider don't exist!"
|
526
|
+
end
|
527
|
+
|
528
|
+
describe "#missing_provider" do
|
529
|
+
it "provides the missing provider id through the missing_provider method" do
|
530
|
+
c = DataProvider::Container.new
|
531
|
+
c.provider_missing do
|
532
|
+
"Missing #{missing_provider}"
|
533
|
+
end
|
534
|
+
|
535
|
+
expect(c.take(:something)).to eq 'Missing something'
|
536
|
+
end
|
537
|
+
|
538
|
+
it "returns nil when called from anywhere else than the fallback provider" do
|
539
|
+
# expect{c.missing_provider}.to raise_error(NoMethodError)
|
540
|
+
expect(container.missing_provider).to eq nil
|
541
|
+
end
|
542
|
+
end
|
543
|
+
|
544
|
+
it 'calls the fallback provider when using try_take with an unknown provider' do
|
545
|
+
c = DataProvider::Container.new
|
546
|
+
c.provider_missing do
|
547
|
+
"fallback_#{missing_provider}"
|
548
|
+
end
|
549
|
+
|
550
|
+
expect(c.try_take(:cool)).to eq 'fallback_cool'
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
554
|
+
describe "fallback_provider?" do
|
555
|
+
it "lets you know if a fallback provider has been registered" do
|
556
|
+
c = DataProvider::Container.new
|
557
|
+
expect(c.fallback_provider?).to eq false
|
558
|
+
|
559
|
+
c.provider_missing do
|
560
|
+
"New fallback!"
|
561
|
+
end
|
562
|
+
|
563
|
+
expect(c.fallback_provider?).to eq true
|
564
|
+
end
|
565
|
+
end
|
566
|
+
|
567
|
+
describe "#take" do
|
568
|
+
it 'lets you take data from a data provider instance' do
|
569
|
+
expect(container.give(:array => [6,9,2]).take(:sum)).to eq 17
|
570
|
+
expect(container.take(:static)).to eq 'StaticValue'
|
571
|
+
end
|
572
|
+
|
573
|
+
it 'raise a ProviderMissingException when attempting to take from unknown provider' do
|
574
|
+
expect{container.take(:unknown)}.to raise_error(DataProvider::ProviderMissingException)
|
575
|
+
expect{container.take([:identification, :foo])}.to raise_error(DataProvider::ProviderMissingException)
|
576
|
+
end
|
577
|
+
|
578
|
+
it 'works from within a provider block' do
|
579
|
+
expect(container.take(:billy)).to eq 'Billy Bragg'
|
580
|
+
end
|
581
|
+
|
582
|
+
it "acts like #scoped_take when used inside a provider and the specified provider isn't available" do
|
583
|
+
expect(container.take([:identification, :id])).to eq 'Stephen William Bragg'
|
584
|
+
expect(container.take([:identification, :identifier])).to eq 'Billy'
|
585
|
+
end
|
586
|
+
|
587
|
+
it "can act like #scoped_take recursively" do
|
588
|
+
expect(container.take([:identification, :ID])).to eq 'Stephen William Bragg'
|
589
|
+
end
|
590
|
+
end
|
591
|
+
|
592
|
+
describe "#try_take" do
|
593
|
+
it "acts like #take when the specified provider is present" do
|
594
|
+
expect(container.give(:array => [1,2,4]).try_take(:sum)).to eq 7
|
595
|
+
end
|
596
|
+
|
597
|
+
it "returns nil when the specified provider is not found" do
|
598
|
+
expect(container.try_take(:square_root)).to eq nil
|
599
|
+
end
|
600
|
+
end
|
601
|
+
|
602
|
+
describe "#provider_stack" do
|
603
|
+
let(:container){
|
604
|
+
DataProvider::Container.new.tap do |c|
|
605
|
+
c.provider :a do
|
606
|
+
take(:b)
|
607
|
+
end
|
608
|
+
c.provider :b do
|
609
|
+
take(['prefix', :c])
|
610
|
+
end
|
611
|
+
c.provider ['prefix', :c] do
|
612
|
+
take ['prefix', :d]
|
613
|
+
end
|
614
|
+
c.provider ['prefix', :d] do
|
615
|
+
provider_stack
|
616
|
+
end
|
617
|
+
end
|
618
|
+
}
|
619
|
+
|
620
|
+
it "gives providers an array resembling the current provider 'callstack'" do
|
621
|
+
expect(container.take(:a)).to eq [:a, :b, ['prefix', :c], ['prefix', :d]]
|
622
|
+
expect(container.take(:b)).to eq [:b, ['prefix', :c], ['prefix', :d]]
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
describe "#provider_id" do
|
627
|
+
let(:container){
|
628
|
+
DataProvider::Container.new.tap do |c|
|
629
|
+
c.provider :a do
|
630
|
+
take(:b)
|
631
|
+
end
|
632
|
+
c.provider :b do
|
633
|
+
take(['prefix', :c])
|
634
|
+
end
|
635
|
+
c.provider ['prefix', :c] do
|
636
|
+
take ['prefix', :d]
|
637
|
+
end
|
638
|
+
c.provider ['prefix', :d] do
|
639
|
+
provider_id
|
640
|
+
end
|
641
|
+
end
|
642
|
+
}
|
643
|
+
|
644
|
+
it "gives the id of the current provider" do
|
645
|
+
expect(container.take(:a)).to eq ['prefix', :d]
|
646
|
+
expect(container.take(['prefix', :d])).to eq ['prefix', :d]
|
647
|
+
end
|
648
|
+
|
649
|
+
it "gives nil when called fmor outside a provider" do
|
650
|
+
expect(container.provider_id).to eq nil
|
651
|
+
end
|
652
|
+
end
|
653
|
+
|
654
|
+
|
655
|
+
describe "#scopes" do
|
656
|
+
let(:container){
|
657
|
+
DataProvider::Container.new.tap do |c|
|
658
|
+
c.provider :a do
|
659
|
+
take(:b)
|
660
|
+
end
|
661
|
+
c.provider :b do
|
662
|
+
take(['prefix', :c])
|
663
|
+
end
|
664
|
+
c.provider ['prefix', :c] do
|
665
|
+
take :d # take also looks for :d within its own ['prefix'] scope
|
666
|
+
end
|
667
|
+
c.provider ['prefix', :d] do
|
668
|
+
scopes
|
669
|
+
end
|
670
|
+
end
|
671
|
+
}
|
672
|
+
|
673
|
+
it "gives providers an array resembling the current provider 'callstack'" do
|
674
|
+
expect(container.take(:a)).to eq [[], [], ['prefix'], ['prefix']]
|
675
|
+
expect(container.take(:b)).to eq [[], ['prefix'], ['prefix']]
|
676
|
+
end
|
677
|
+
end
|
678
|
+
|
679
|
+
describe "#scope" do
|
680
|
+
let(:container){
|
681
|
+
DataProvider::Container.new.tap do |c|
|
682
|
+
c.provider [:a, :b] do
|
683
|
+
'woeha!'
|
684
|
+
end
|
685
|
+
c.provider [:a, :b ,:c] do
|
686
|
+
scope
|
687
|
+
end
|
688
|
+
c.provider [:a, :b ,:eq] do
|
689
|
+
take scope
|
690
|
+
end
|
691
|
+
end
|
692
|
+
}
|
693
|
+
|
694
|
+
it 'gives providers the current scope' do
|
695
|
+
expect(container.take([:a,:b,:c])).to eq [:a,:b]
|
696
|
+
end
|
697
|
+
|
698
|
+
it "can be used by providers to call the 'parent provider'" do
|
699
|
+
expect(container.take([:a,:b,:eq])).to eq container.take([:a,:b])
|
700
|
+
end
|
701
|
+
end
|
702
|
+
|
703
|
+
describe "#scoped_take" do
|
704
|
+
it 'lets a provider call providers within its own scope' do
|
705
|
+
expect(container.take([:identification, :fullname])).to eq 'Billy Bragg'
|
706
|
+
end
|
707
|
+
end
|
708
|
+
|
709
|
+
describe "#give" do
|
710
|
+
it "lets you give data, creating a new data provider instance" do
|
711
|
+
updated = container.give :array => [1,80]
|
712
|
+
expect(container.take(:sum)).to eq 0
|
713
|
+
expect(updated.take(:sum)).to eq 81
|
714
|
+
end
|
715
|
+
|
716
|
+
it "allows for linked notation" do
|
717
|
+
expect(container.give(:array => [7, -3]).take(:sum)).to eq 4
|
718
|
+
end
|
719
|
+
|
720
|
+
it "has an add_scope alias" do
|
721
|
+
expect(container.add_scope(:array => [400, 20]).take(:sum)).to eq 420
|
722
|
+
end
|
723
|
+
|
724
|
+
it "has an add_data alias" do
|
725
|
+
expect(container.add_data(:array => [400, 20]).take(:sum)).to eq 420
|
726
|
+
end
|
727
|
+
end
|
728
|
+
|
729
|
+
describe "#give!" do
|
730
|
+
it "lets you update the current provider with additional data" do
|
731
|
+
container2 = container.give(:array => [1,1,90])
|
732
|
+
expect(container2.take(:sum)).to eq 92
|
733
|
+
container2.give!(:array => [3,90])
|
734
|
+
expect(container2.take(:sum)).to eq 93
|
735
|
+
end
|
736
|
+
|
737
|
+
it "allows for linked notation" do
|
738
|
+
expect(container.give!(:array => [3]).give!.give!(:array => [-1, -4]).take(:sum)).to eq -5
|
739
|
+
end
|
740
|
+
|
741
|
+
it "has an add_scope! alias" do
|
742
|
+
scoped = container.add_scope.add_scope!(:array => [-1, -4])
|
743
|
+
expect(scoped.given(:array)).to eq [-1,-4]
|
744
|
+
expect(scoped.take(:sum)).to eq -5
|
745
|
+
end
|
746
|
+
|
747
|
+
it "has an add_data! alias" do
|
748
|
+
scoped = container.add_data(:array => []).add_data!(:array => [5, 5])
|
749
|
+
expect(scoped.get_data(:array)).to eq [5,5]
|
750
|
+
expect(scoped.take(:sum)).to eq 10
|
751
|
+
end
|
752
|
+
end
|
753
|
+
|
754
|
+
describe '#got?' do
|
755
|
+
it "returns if the specified piece of data is given" do
|
756
|
+
c = DataProvider::Container.new.give(:name => 'John')
|
757
|
+
expect( c.got?(:name) ).to be true
|
758
|
+
expect( c.got?(:lastname) ).to be false
|
759
|
+
expect( c.give(:lastname => 'Doe').got?(:lastname) ).to be true
|
760
|
+
end
|
761
|
+
|
762
|
+
it "has an 'has_data?' alias" do
|
763
|
+
c = DataProvider::Container.new.give(:name => 'John')
|
764
|
+
expect( c.has_data?(:lastname) ).to be false
|
765
|
+
expect( c.has_data?(:name) ).to be true
|
766
|
+
expect( c.add_data(:lastname => 'Doe').has_data?(:lastname) ).to be true
|
767
|
+
end
|
768
|
+
end
|
769
|
+
|
770
|
+
describe "#given" do
|
771
|
+
it "has a given method to get given data" do
|
772
|
+
expect(DataProvider::Container.new.give(:array => 'array').given(:array)).to eq 'array'
|
773
|
+
end
|
774
|
+
|
775
|
+
it "has a get_data alias" do
|
776
|
+
expect(DataProvider::Container.new.add_data(:foo => :bar).get_data(:foo)).to eq :bar
|
777
|
+
end
|
778
|
+
end
|
779
|
+
|
780
|
+
describe "fallback_provider?" do
|
781
|
+
it "lets you know if a fallback provider has been registered" do
|
782
|
+
c = DataProvider::Container.new
|
783
|
+
expect(c.fallback_provider?).to eq false
|
784
|
+
|
785
|
+
c.provider_missing do
|
786
|
+
"New fallback!"
|
787
|
+
end
|
788
|
+
|
789
|
+
expect(c.fallback_provider?).to eq true
|
790
|
+
end
|
791
|
+
end
|
792
|
+
end
|