woyo-world 0.0.6 → 0.0.7

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.
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'woyo/world/actions'
3
+
4
+ describe Woyo::Actions do
5
+
6
+ context "may be listed" do
7
+
8
+ let( :at ) { class ActionTest ; include Woyo::Actions ; end.new }
9
+
10
+ it "intially empty" do
11
+ expect(at.actions).to be_instance_of Woyo::Attributes::AttributesHash
12
+ expect(at.actions).to be_empty
13
+ end
14
+
15
+ it "single action" do
16
+ at.action( :act1 ) { Time.now }
17
+ expect(at.actions.count).to eq 1
18
+ expect(at.actions[:act1]). to be_instance_of Proc
19
+ end
20
+
21
+ it "multiple actions" do
22
+ at.action( :act1 ) { Time.now }
23
+ at.action( :act2 ) { Time.now }
24
+ expect(at.actions.count).to eq 2
25
+ expect(at.actions[:act2]). to be_instance_of Proc
26
+ end
27
+
28
+ end
29
+
30
+ context "may be defined" do
31
+
32
+ let( :at ) { class ActionTest ; include Woyo::Actions ; end.new }
33
+
34
+ it "via a block" do
35
+ at.action( :sum ) { 1 + 2 }
36
+ expect(at.actions).to include :sum
37
+ expect(at.actions[:sum]).to be_instance_of Proc
38
+ end
39
+
40
+ it "via a proc" do
41
+ at.action sum: proc { 1 + 2 }
42
+ expect(at.actions).to include :sum
43
+ expect(at.actions[:sum]).to be_instance_of Proc
44
+ end
45
+
46
+ it "via a lambda" do
47
+ at.action sum: lambda { |this| 1 + 2 }
48
+ expect(at.actions).to include :sum
49
+ expect(at.actions[:sum]).to be_instance_of Proc
50
+ end
51
+
52
+ end
53
+
54
+ context "may be invoked" do
55
+
56
+ let( :at ) { class ActionTest ; include Woyo::Actions ; end.new }
57
+
58
+ it "by name" do
59
+ at.action( :sum ) { 1 + 2 }
60
+ expect(at.sum).to eq 3
61
+ end
62
+
63
+ it "via #do :action" do
64
+ at.action( :sum ) { 1 + 2 }
65
+ expect(at.do :sum).to eq 3
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
@@ -1,3 +1,4 @@
1
+ require 'spec_helper'
1
2
  require 'woyo/world/attributes'
2
3
 
3
4
  describe Woyo::Attributes do
@@ -5,494 +6,411 @@ describe Woyo::Attributes do
5
6
  before :all do
6
7
  class AttrTest
7
8
  include Woyo::Attributes
8
- attributes :attr1, :attr2, :attr3
9
9
  end
10
10
  end
11
-
12
- it 'names can be listed for class' do
13
- attrs = AttrTest.attributes
14
- attrs.should be_instance_of Array
15
- attrs.count.should eq 3
16
- attrs.all? { |a| a.is_a? Symbol }.should be_true
17
- end
18
-
19
- it 'hash of names and values can be retrieved for instance' do
20
- attr_test = AttrTest.new
21
- attr_test.attributes.should be_instance_of Woyo::Attributes::AttributesHash
22
- AttrTest.attributes.each do |attr|
23
- attr_test.send(attr, attr.to_s.upcase)
24
- end
25
- attr_test.attributes.keys.should eq [ :attr1, :attr2, :attr3 ]
26
- attr_test.attributes.values.should eq [ 'ATTR1', 'ATTR2', 'ATTR3' ]
27
- end
28
-
29
- it 'hash of names and nil values can be retrieved for instance before populating' do
30
- attr_test = AttrTest.new
31
- attr_test.attributes.should be_instance_of Woyo::Attributes::AttributesHash
32
- attr_test.attributes.keys.should eq [ :attr1, :attr2, :attr3 ]
33
- attr_test.attributes.values.should eq [ nil, nil, nil ]
34
- end
35
11
 
36
- it 'hash of names and default values can be retrieved for instance before populating' do
37
- expect {
38
- class DefTest
39
- include Woyo::Attributes
40
- attributes one: 1, two: 2, three: proc { 3 }
41
- end
42
- }.to_not raise_error
43
- def_test = DefTest.new
44
- def_test.attributes.keys.should eq [ :one, :two, :three ]
45
- def_test.attributes.values.should eq [ 1, 2, 3 ]
46
- end
12
+ context '#attributes' do
47
13
 
48
- it 'have convenience accessor :names for :keys' do
49
- attr_test = AttrTest.new
50
- attr_test.attributes.names.should eq [ :attr1, :attr2, :attr3 ]
51
- end
52
-
53
- it 'can be written via method with =' do
54
- attr_test = AttrTest.new
55
- AttrTest.attributes.each do |attr|
56
- attr_test.send("#{attr}=", attr.to_s.upcase)
57
- end
58
- attr_test.attributes.count.should eq AttrTest.attributes.count
59
- attr_test.attributes.each do |name,value|
60
- value.should eq name.to_s.upcase
14
+ it 'returns empty AttributesHash for instance with no attributes' do
15
+ attr_test = AttrTest.new
16
+ expect(attr_test.attributes).to be_instance_of Woyo::Attributes::AttributesHash
17
+ expect(attr_test.attributes.count).to eq 0
61
18
  end
62
- end
63
19
 
64
- it 'can be written via method without =' do
65
- attr_test = AttrTest.new
66
- AttrTest.attributes.each do |attr|
67
- attr_test.send(attr, attr.to_s.upcase)
20
+ it 'returns AttributesHash with names and nil values for instance with unpopulated attributes' do
21
+ attr_test = AttrTest.new
22
+ attr_test.attributes :attr1, :attr2, :attr3
23
+ expect(attr_test.attributes).to be_instance_of Woyo::Attributes::AttributesHash
24
+ expect(attr_test.attributes.keys).to eq [ :attr1, :attr2, :attr3 ]
25
+ expect(attr_test.attributes.values).to eq [ nil, nil, nil ]
68
26
  end
69
- attr_test.attributes.count.should eq AttrTest.attributes.count
70
- attr_test.attributes.each do |name,value|
71
- value.should eq name.to_s.upcase
27
+
28
+ it 'returns AttributesHash with names and values for instance with populated attributes' do
29
+ attr_test = AttrTest.new
30
+ attr_test.attributes :attr1, :attr2, :attr3
31
+ expect(attr_test.attributes).to be_instance_of Woyo::Attributes::AttributesHash
32
+ expect(attr_test.attributes.keys).to eq [ :attr1, :attr2, :attr3 ]
33
+ attr_test.attributes.keys.each do |attr|
34
+ attr_test.send(attr, attr.to_s.upcase)
35
+ end
36
+ expect(attr_test.attributes.values).to eq [ 'ATTR1', 'ATTR2', 'ATTR3' ]
72
37
  end
73
- end
74
38
 
75
- it 'can be read via method' do
76
- attr_test = AttrTest.new
77
- AttrTest.attributes.each do |attr|
78
- attr_test.send(attr, attr.to_s.upcase)
39
+ it 'returns AttributesHash with names and default values for instance with unpopulated attributes' do
40
+ attr_test = AttrTest.new
41
+ attr_test.attributes one: 1, two: 2, three: 3
42
+ expect(attr_test.attributes).to be_instance_of Woyo::Attributes::AttributesHash
43
+ expect(attr_test.attributes.keys).to eq [ :one, :two, :three ]
44
+ expect(attr_test.attributes.values).to eq [ 1, 2, 3 ]
79
45
  end
80
- attr_test.attributes.count.should eq AttrTest.attributes.count
81
- attr_test.attributes.each do |name,value|
82
- eval("attr_test.#{name}").should eq value
46
+
47
+ it 'provides convenience accessor :names for :keys' do
48
+ attr_test = AttrTest.new
49
+ attr_test.attributes :attr1, :attr2, :attr3
50
+ expect(attr_test.attributes.names).to eq [ :attr1, :attr2, :attr3 ]
83
51
  end
52
+
84
53
  end
85
54
 
86
- it 'list can be added to' do
87
- expect {
88
- class AttrTest
89
- attributes :attr4, :attr5, :attr6
55
+ context 'can be accessed' do
56
+
57
+ it 'to write via method with =' do
58
+ attr_test = AttrTest.new
59
+ attr_test.attributes :attr1, :attr2, :attr3
60
+ attr_test.attributes.names.each do |attr|
61
+ attr_test.send("#{attr}=", attr.to_s.upcase)
62
+ end
63
+ attr_test.attributes.each do |name,value|
64
+ expect(value).to eq name.to_s.upcase
90
65
  end
91
- }.to_not raise_error
92
- AttrTest.attributes.count.should eq 6
93
- attr_test = AttrTest.new
94
- AttrTest.attributes.each do |attr|
95
- attr_test.send(attr, attr.to_s.upcase)
96
66
  end
97
- attr_test.attributes.count.should eq 6
98
- end
99
67
 
100
- it 'can be defined with "attribute"' do
101
- expect {
102
- class AttrTest
103
- attribute :open
68
+ it 'to write via method without =' do
69
+ attr_test = AttrTest.new
70
+ attr_test.attributes :attr1, :attr2, :attr3
71
+ attr_test.attributes.names.each do |attr|
72
+ attr_test.send(attr, attr.to_s.upcase)
104
73
  end
105
- }.to_not raise_error
106
- attr_test = AttrTest.new
107
- attr_test.open.should be_nil
108
- attr_test.open = true
109
- attr_test.open.should be_true
110
- end
111
-
112
- it 'can have a default value' do
113
- expect {
114
- class AttrTest
115
- attributes attr_with_array___default: [ 1, 2, 3 ]
116
- attributes attr_with_hash____default: { a: 1, b: 2, c: 3 }
117
- attributes attr_with_number__default: 12345
118
- attributes attr_with_string__default: "abcde"
119
- attributes attr_with_boolean_default: true
74
+ attr_test.attributes.each do |name,value|
75
+ expect(value).to eq name.to_s.upcase
120
76
  end
121
- }.to_not raise_error
122
- attr_test = AttrTest.new
123
- attr_test.attr_with_array___default.should eq [ 1, 2, 3 ]
124
- attr_test.attr_with_array___default = :array
125
- attr_test.attr_with_array___default.should eq :array
126
- attr_test.attr_with_hash____default.should eq ( { a: 1, b: 2, c: 3 } )
127
- attr_test.attr_with_hash____default = :hash
128
- attr_test.attr_with_hash____default.should eq :hash
129
- attr_test.attr_with_number__default.should eq 12345
130
- attr_test.attr_with_number__default = :number
131
- attr_test.attr_with_number__default.should eq :number
132
- attr_test.attr_with_string__default.should eq "abcde"
133
- attr_test.attr_with_string__default = :string
134
- attr_test.attr_with_string__default.should eq :string
135
- attr_test.attr_with_boolean_default.should eq true
136
- attr_test.attr_with_boolean_default = :boolean
137
- attr_test.attr_with_boolean_default.should eq :boolean
138
- end
77
+ end
139
78
 
140
- it 'can have a default proc' do
141
- expect {
142
- class AttrTest
143
- attributes attr_with_proc_default: proc { Time.now }
79
+ it 'to read via method' do
80
+ attr_test = AttrTest.new
81
+ attr_test.attributes :attr1, :attr2, :attr3
82
+ attr_test.attributes.names.each do |attr|
83
+ attr_test.send(attr, attr.to_s.upcase)
84
+ end
85
+ attr_test.attributes.each do |name,value|
86
+ expect(eval("attr_test.#{name}")).to eq value
144
87
  end
145
- }.to_not raise_error
146
- attr_test = AttrTest.new
147
- attr_test.attr_with_proc_default.should be < Time.now
88
+ end
89
+
148
90
  end
149
91
 
150
- it 'default proc runs in instance scope' do
151
- expect {
152
- class AttrTest
153
- attributes attr_with_proc_default: proc { |this| this.my_method }
154
- def my_method
155
- "okay"
92
+ context 'can be defined' do
93
+
94
+ it 'via "attribute"' do
95
+ attr_test = AttrTest.new
96
+ attr_test.attribute :open
97
+ expect(attr_test.open).to be_nil
98
+ attr_test.open = true
99
+ expect(attr_test.open).to be true
100
+ end
101
+
102
+ it 'attribute list can be added to' do
103
+ attr_test = AttrTest.new
104
+ attr_test.attributes :attr1, :attr2, :attr3
105
+ attr_test.attributes :attr4, :attr5, :attr6
106
+ expect(attr_test.attributes.count).to eq 6
107
+ expect(attr_test.attributes.names).to eq [ :attr1, :attr2, :attr3, :attr4, :attr5, :attr6 ]
108
+ end
109
+
110
+ it 'attribute list can be added to without duplication' do
111
+ attr_test = AttrTest.new
112
+ attr_test.attributes :attr1, :attr2, :attr3
113
+ attr_test.attributes :attr2, :attr3, :attr4
114
+ expect(attr_test.attributes.count).to eq 4
115
+ expect(attr_test.attributes.names).to eq [ :attr1, :attr2, :attr3, :attr4 ]
116
+ end
117
+
118
+ it 'with a default value' do
119
+ attr_test = AttrTest.new
120
+ attr_test.attributes attr_with_array___default: [ 1, 2, 3 ]
121
+ attr_test.attributes attr_with_hash____default: { a: 1, b: 2, c: 3 }
122
+ attr_test.attributes attr_with_number__default: 12345
123
+ attr_test.attributes attr_with_string__default: "abcde"
124
+ attr_test.attributes attr_with_boolean_default: true
125
+ expect(attr_test.attr_with_array___default).to eq [ 1, 2, 3 ]
126
+ expect(attr_test.attr_with_hash____default).to eq ( { a: 1, b: 2, c: 3 } )
127
+ expect(attr_test.attr_with_number__default).to eq 12345
128
+ expect(attr_test.attr_with_string__default).to eq "abcde"
129
+ expect(attr_test.attr_with_boolean_default).to eq true
130
+ end
131
+
132
+ context 'wth a dynamic value' do
133
+
134
+ context 'a proc' do
135
+
136
+ it 'runs' do
137
+ attr_test = AttrTest.new
138
+ attr_test.attributes attr_with_proc_default: proc { Time.now }
139
+ expect(attr_test.attr_with_proc_default).to be < Time.now
156
140
  end
157
- end
158
- }.to_not raise_error
159
- attr_test = AttrTest.new
160
- attr_test.attr_with_proc_default.should eq "okay"
161
- end
162
141
 
163
- it 'can have a default lambda' do
164
- expect {
165
- class AttrTest
166
- attributes attr_with_lambda_default: lambda { Time.now }
167
- end
168
- }.to_not raise_error
169
- attr_test = AttrTest.new
170
- attr_test.attr_with_lambda_default.should be < Time.now
171
- end
142
+ it 'runs in instance scope' do
143
+ attr_test = AttrTest.new
144
+ attr_test.define_singleton_method(:my_method) { "okay" }
145
+ attr_test.attributes attr_with_proc_default: proc { |this| this.my_method }
146
+ expect(attr_test.attr_with_proc_default).to eq "okay"
147
+ end
148
+
149
+ it 'runs on each access via method' do
150
+ attr_test = AttrTest.new
151
+ attr_test.attributes time_proc: proc { Time.now }
152
+ old_time = attr_test.time_proc
153
+ expect(attr_test.time_proc).to be > old_time
154
+ end
172
155
 
173
- it 'default lambda runs in instance scope' do
174
- expect {
175
- class AttrTest
176
- attributes attr_with_lambda_default: lambda { |this| this.my_method }
177
- def my_method
178
- "okay"
156
+ it 'is returned on direct access to attribute' do
157
+ attr_test = AttrTest.new
158
+ attr_test.attributes time_proc: proc { Time.now }
159
+ expect(attr_test.attributes[:time_proc]).to respond_to :call
179
160
  end
161
+
180
162
  end
181
- }.to_not raise_error
182
- attr_test = AttrTest.new
183
- attr_test.attr_with_lambda_default.should eq "okay"
184
- end
185
163
 
186
- context 'that are boolean' do
164
+ context 'a lambda' do
165
+
166
+ it 'runs' do
167
+ attr_test = AttrTest.new
168
+ attr_test.attributes attr_with_lambda_default: lambda { Time.now }
169
+ expect(attr_test.attr_with_lambda_default).to be < Time.now
170
+ end
187
171
 
188
- context 'have convenient instance accessors' do
172
+ it 'runs in instance scope' do
173
+ attr_test = AttrTest.new
174
+ attr_test.define_singleton_method(:my_method) { "okay" }
175
+ attr_test.attributes attr_with_lambda_default: lambda { |this| this.my_method }
176
+ expect(attr_test.attr_with_lambda_default).to eq "okay"
177
+ end
189
178
 
190
- before :all do
191
- expect {
192
- class BooleanAttrTest
193
- include Woyo::Attributes
194
- attribute open: true
195
- attribute light: false
196
- end
197
- }.to_not raise_error
198
- end
179
+ it 'runs on each access via method' do
180
+ attr_test = AttrTest.new
181
+ attr_test.attributes time_lambda: lambda { Time.now }
182
+ old_time = attr_test.time_lambda
183
+ expect(attr_test.time_lambda).to be > old_time
184
+ end
199
185
 
200
- it '#attr?' do
201
- attr_test = BooleanAttrTest.new
202
- attr_test.open.should eq true
203
- attr_test.open?.should eq true
204
- end
186
+ it 'is returned on direct access to attribute' do
187
+ attr_test = AttrTest.new
188
+ attr_test.attributes time_lambda: lambda { Time.now }
189
+ expect(attr_test.attributes[:time_lambda]).to respond_to :call
190
+ end
205
191
 
206
- it '#attr!' do
207
- attr_test = BooleanAttrTest.new
208
- attr_test.open = false
209
- attr_test.open.should eq false
210
- attr_test.open!.should eq true
211
- attr_test.open.should eq true
212
192
  end
193
+
194
+ context 'a block' do
213
195
 
214
- it '#is? :attr' do
215
- attr_test = BooleanAttrTest.new
216
- attr_test.is?(:open).should eq true
217
- attr_test.open = false
218
- attr_test.is?(:open).should eq false
219
- end
196
+ it 'runs' do
197
+ attr_test = AttrTest.new
198
+ attr_test.attribute( :attr_with_block_default ) { Time.now }
199
+ expect(attr_test.attr_with_block_default).to be < Time.now
200
+ end
201
+
202
+ it 'runs in instance scope' do
203
+ attr_test = AttrTest.new
204
+ attr_test.define_singleton_method(:my_method) { "okay" }
205
+ attr_test.attribute( :attr_with_block_default ) { |this| this.my_method }
206
+ expect(attr_test.attr_with_block_default).to eq "okay"
207
+ end
208
+
209
+ it 'runs on each access via method' do
210
+ attr_test = AttrTest.new
211
+ attr_test.attribute( :time_block ) { Time.now }
212
+ old_time = attr_test.time_block
213
+ expect(attr_test.time_block).to be > old_time
214
+ end
215
+
216
+ it 'is returned on direct access to attribute' do
217
+ attr_test = AttrTest.new
218
+ attr_test.attribute( :time_block ) { Time.now }
219
+ expect(attr_test.attributes[:time_block]).to respond_to :call
220
+ end
220
221
 
221
- it '#is :attr' do
222
- attr_test = BooleanAttrTest.new
223
- attr_test.light.should eq false
224
- attr_test.is(:light)
225
- attr_test.light.should eq true
226
222
  end
223
+
227
224
  end
228
225
 
229
- context 'have class accessor ::is' do
226
+ end
230
227
 
231
- # I was confused here, this may not be needed as a class method at all!!!
232
-
233
- it 'that defines new attribute with true default' do
234
- expect {
235
- class BooleanIsTest1
236
- include Woyo::Attributes
237
- is :attr1
238
- end
239
- }.to_not raise_error
240
- attr_test = BooleanIsTest1.new
241
- attr_test.attr1.should eq true
242
- end
228
+ context 'that are boolean have convenient instance accessors' do
243
229
 
244
- it 'that sets true default for existing attribute' do
245
- expect {
246
- class BooleanIsTest2
247
- include Woyo::Attributes
248
- attribute :attr2
249
- is :attr2
250
- end
251
- }.to_not raise_error
252
- attr_test = BooleanIsTest2.new
253
- attr_test.attr2.should eq true
254
- end
230
+ let(:bat) { class BooleanAttrTest; include Woyo::Attributes; end.new }
255
231
 
256
- it 'that changes default to true for existing attribute' do
257
- expect {
258
- class BooleanIsTest3
259
- include Woyo::Attributes
260
- attribute :attr3 => false
261
- is :attr3
262
- end
263
- }.to_not raise_error
264
- attr_test = BooleanIsTest3.new
265
- attr_test.attr3.should eq true
266
- end
232
+ before :each do
233
+ bat.attribute open: true
234
+ end
267
235
 
268
- it 'that works for attribute in BooleanGroup' do
269
- expect {
270
- class BooleanIsTest4
271
- include Woyo::Attributes
272
- group! :temp, :hot, :warm, :cool, :cold
273
- # :hot is true by default
274
- is :cold
275
- # :cold is now true
276
- end
277
- }.to_not raise_error
278
- attr_test = BooleanIsTest4.new
279
- attr_test.hot.should eq false
280
- attr_test.warm.should eq false
281
- attr_test.cool.should eq false
282
- attr_test.cold.should eq true
283
- end
236
+ it '#attr?' do
237
+ expect(bat.open).to eq true
238
+ expect(bat.open?).to eq true
239
+ end
240
+
241
+ it '#attr!' do
242
+ bat.open = false
243
+ expect(bat.open).to eq false
244
+ expect(bat.open!).to eq true
245
+ expect(bat.open).to eq true
246
+ end
284
247
 
248
+ it '#is? :attr' do
249
+ expect(bat.is?(:open)).to eq true
250
+ bat.open = false
251
+ expect(bat.is?(:open)).to eq false
252
+ end
253
+
254
+ it '#is :attr' do
255
+ bat.open = false
256
+ expect(bat.open).to eq false
257
+ bat.is(:open)
258
+ expect(bat.open).to eq true
285
259
  end
286
260
 
287
261
  end
288
262
 
289
263
  context 'can be re-defined' do
290
264
 
265
+ let(:ard) { AttrTest.new }
266
+
291
267
  it 'without duplication' do
292
- expect {
293
- class AttrReDef1
294
- include Woyo::Attributes
295
- attribute :attr1
296
- end
297
- class AttrReDef1
298
- attribute :attr1
299
- end
300
- }.to_not raise_error
301
- AttrReDef1.attributes.count.should eq 1
302
- AttrReDef1.attributes.should eq [:attr1]
303
- attr_rd1 = AttrReDef1.new
304
- attr_rd1.attr1.should be_nil
268
+ ard.attribute :attr
269
+ ard.attribute :attr
270
+ expect(ard.attributes.count).to eq 1
271
+ expect(ard.attributes.names).to eq [:attr]
272
+ expect(ard.attr).to eq nil
305
273
  end
306
274
 
307
275
  it 'to set default' do
308
- expect {
309
- class AttrReDef2
310
- include Woyo::Attributes
311
- attribute :attr2
312
- end
313
- class AttrReDef2
314
- attribute attr2: 'two'
315
- end
316
- }.to_not raise_error
317
- AttrReDef2.attributes.count.should eq 1
318
- AttrReDef2.attributes.should eq [:attr2]
319
- attr_rd2 = AttrReDef2.new
320
- attr_rd2.attr2.should eq 'two'
276
+ ard.attribute :attr
277
+ ard.attribute :attr => 'default'
278
+ expect(ard.attributes.count).to eq 1
279
+ expect(ard.attributes.names).to eq [:attr]
280
+ expect(ard.attr).to eq 'default'
321
281
  end
322
282
 
323
283
  it 'to change default' do
324
- expect {
325
- class AttrReDef3
326
- include Woyo::Attributes
327
- attribute attr3: '333'
328
- end
329
- class AttrReDef3
330
- attribute attr3: 'three'
331
- end
332
- }.to_not raise_error
333
- AttrReDef3.attributes.count.should eq 1
334
- AttrReDef3.attributes.should eq [:attr3]
335
- attr_rd3 = AttrReDef3.new
336
- attr_rd3.attr3.should eq 'three'
284
+ ard.attribute :attr => 'old_default'
285
+ ard.attribute :attr => 'new_default'
286
+ expect(ard.attributes.count).to eq 1
287
+ expect(ard.attributes.names).to eq [:attr]
288
+ expect(ard.attr).to eq 'new_default'
337
289
  end
338
290
 
339
291
  end
340
292
 
341
293
  context 'groups' do
342
294
 
343
- before :all do
344
- class AT
345
- include Woyo::Attributes
346
- group :stooges, :larry, :curly, :moe
347
- group :cars, :mustang, :ferarri, :mini
348
- end
349
- @at = AT.new
350
- end
295
+ let(:gat) { AttrTest.new }
351
296
 
352
- it 'can be listed for class' do
353
- groups = AT.groups
354
- groups.should be_instance_of Hash
355
- groups.count.should eq 2
356
- groups.keys.should eq [ :stooges, :cars ]
357
- groups[:stooges].should eq [ :larry, :curly, :moe ]
358
- groups[:cars].should eq [ :mustang, :ferarri, :mini ]
297
+ before :each do
298
+ gat.group :stooges, :larry, :curly, :moe
359
299
  end
360
300
 
361
- it 'have convenience accessor :names for :keys' do
362
- @at.stooges.names.should eq [ :larry, :curly, :moe ]
301
+ it 'can be accessed by named instance methods' do
302
+ expect(gat.stooges).to be_instance_of Woyo::Attributes::Group
363
303
  end
364
304
 
365
- it 'names and nil values can be retrieved from instance without populating' do
366
- @at.stooges.should be_instance_of Woyo::Attributes::Group
367
- @at.stooges.count.should eq 3
368
- @at.stooges.names.should eq [ :larry, :curly, :moe ]
369
- @at.stooges.values.should eq [ nil, nil, nil ]
305
+ it 'names and nil values can be retrieved' do
306
+ expect(gat.stooges).to be_instance_of Woyo::Attributes::Group
307
+ expect(gat.stooges.count).to eq 3
308
+ expect(gat.stooges.names).to eq [ :larry, :curly, :moe ]
309
+ expect(gat.stooges.values).to eq [ nil, nil, nil ]
370
310
  end
371
311
 
372
- it 'names and default values can be retrieved from instance without populating' do
373
- expect {
374
- class GroupDefTest
375
- include Woyo::Attributes
376
- group :numbers, one: 1, two: 2, three: proc { 3 }
377
- end
378
- }.to_not raise_error
379
- def_test = GroupDefTest.new
380
- groups = def_test.groups
381
- groups.should be_instance_of Hash
382
- groups.count.should eq 1
383
- groups.names.should eq [ :numbers ]
384
- groups[:numbers].should be_instance_of Woyo::Attributes::Group
385
- groups[:numbers].names.should eq [ :one, :two, :three ]
386
- groups[:numbers].values.should eq [ 1, 2, 3 ]
387
- def_test.numbers.names.should eq [ :one, :two, :three ]
388
- def_test.numbers.values.should eq [ 1, 2, 3 ]
312
+ it 'names and default values can be retrieved' do
313
+ gat.group :numbers, one: 1, two: 2, three: 3
314
+ expect(gat.numbers).to be_instance_of Woyo::Attributes::Group
315
+ expect(gat.numbers.count).to eq 3
316
+ expect(gat.numbers.names).to eq [ :one, :two, :three ]
317
+ expect(gat.numbers.values).to eq [ 1, 2, 3 ]
389
318
  end
390
319
 
391
320
  it 'members can be accessed via group' do
392
- @at.stooges[:curly].should eq nil
393
- @at.stooges[:curly] = 'bald'
394
- @at.stooges[:curly].should eq 'bald'
395
- end
396
-
397
- it 'members are also attributes' do
398
- all_attrs = [ :larry, :curly, :moe, :mustang, :ferarri, :mini ]
399
- @at.attributes.keys.should eq all_attrs
400
- all_attrs.each do |attr|
401
- @at.should respond_to attr
402
- end
321
+ expect(gat.stooges[:curly]).to eq nil
322
+ gat.stooges[:curly] = 'bald'
323
+ expect(gat.stooges[:curly]).to eq 'bald'
403
324
  end
404
325
 
405
326
  it 'members are attributes' do
406
- @at.stooges[:moe] = 'knucklehead'
407
- @at.stooges[:moe].should eq 'knucklehead'
408
- @at.moe.should eq 'knucklehead'
327
+ expect(gat.attributes.keys).to eq [ :larry, :curly, :moe ]
328
+ gat.stooges[:larry] = 'knucklehead'
329
+ expect(gat.larry).to eq 'knucklehead'
409
330
  end
410
331
 
411
332
  it 'attributes are members' do
412
- @at.ferarri = 'fast'
413
- @at.ferarri.should eq 'fast'
414
- @at.cars[:ferarri].should eq 'fast'
333
+ gat.moe = 'smart'
334
+ expect(gat.stooges[:moe]).to eq 'smart'
335
+ end
336
+
337
+ it '#groups returns hash with names and groups' do
338
+ gat.group :numbers, one: 1, two: 2, three: proc { 3 }
339
+ expect(gat.groups).to eq( { stooges: gat.stooges, numbers: gat.numbers } )
415
340
  end
416
341
 
417
342
  end
418
343
 
419
- context 'boolean groups' do
344
+ context 'exclusions' do
420
345
 
421
- before :all do
422
- class ExGroupTest
423
- include Woyo::Attributes
424
- group! :temp, :hot, :warm, :cool, :cold
425
- group! :light, :dark, :dim, :bright
426
- end
427
- end
346
+ let(:xat) { AttrTest.new }
428
347
 
429
- it 'are listed for a class' do
430
- groups = ExGroupTest.boolean_groups
431
- groups.should be_instance_of Hash
432
- groups.count.should eq 2
433
- groups.keys.should eq [ :temp, :light ]
434
- groups[:temp].should eq [ :hot, :warm, :cool, :cold ]
435
- groups[:light].should eq [ :dark, :dim, :bright ]
348
+ before :each do
349
+ xat.exclusion :temp, :warm, :cool, :cold
436
350
  end
437
351
 
438
- it 'accessor returns BooleanGroup instance' do
439
- egt = ExGroupTest.new
440
- egt.temp.should be_instance_of Woyo::Attributes::BooleanGroup
352
+ it 'can be accessed by named instance methods' do
353
+ expect(xat.temp).to be_instance_of Woyo::Attributes::Exclusion
441
354
  end
442
355
 
443
356
  it 'first member is true, rest are false' do
444
- egt = ExGroupTest.new
445
- egt.light[:dark].should eq true
446
- egt.light[:dim].should eq false
447
- egt.light[:bright].should eq false
357
+ expect(xat.temp[:warm]).to eq true
358
+ expect(xat.temp[:cool]).to eq false
359
+ expect(xat.temp[:cold]).to eq false
360
+ end
361
+
362
+ it '#value returns true member' do
363
+ expect(xat.temp.value).to eq :warm
364
+ xat.temp[:cold] = true
365
+ expect(xat.temp.value).to eq :cold
448
366
  end
449
367
 
450
368
  it 'making group member true affects member attributes' do
451
- egt = ExGroupTest.new
452
- egt.temp[:cold] = true
453
- egt.cold.should be true
454
- egt.cool.should be false
455
- egt.warm.should be false
456
- egt.hot.should be false
369
+ xat.temp[:cold] = true
370
+ expect(xat.cold).to be true
371
+ expect(xat.cool).to be false
372
+ expect(xat.warm).to be false
457
373
  end
458
374
 
459
375
  it 'making attribute true affects group members' do
460
- egt = ExGroupTest.new
461
- egt.cold = true
462
- egt.light[:cold].should be true
463
- egt.light[:cool].should be false
464
- egt.light[:warm].should be false
465
- egt.light[:hot].should be false
376
+ xat.cool = true
377
+ expect(xat.temp[:cold]).to be false
378
+ expect(xat.temp[:cool]).to be true
379
+ expect(xat.temp[:warm]).to be false
380
+ end
381
+
382
+ it '#exclusions returns hash with names and exlcusions' do
383
+ xat.exclusion :light, :dark, :dim, :bright
384
+ expect(xat.exclusions).to eq( { temp: xat.temp, light: xat.light } )
466
385
  end
467
386
 
468
387
  end
469
388
 
470
- context 'assigned a Hash as a value' do
389
+ context 'with Hash value' do
471
390
 
472
- before :all do
473
- class AttrHashTest
474
- include Woyo::Attributes
475
- attributes :reaction, :hot, :cold
476
- end
477
- @aht = AttrHashTest.new
391
+ let(:hat) { AttrTest.new }
392
+
393
+ before :each do
394
+ hat.attributes :reaction, :hot, :cold
478
395
  end
479
396
 
480
- it 'accepts the hash as a value' do
481
- expect { @aht.reaction hot: 'Sweat', cold: 'Shiver' }.to_not raise_error
397
+ it 'accept a hash as value' do
398
+ expect { hat.reaction hot: 'Sweat', cold: 'Shiver' }.to_not raise_error
482
399
  end
483
400
 
484
- it 'returns the value of the first key that evaluates as a true attribute' do
485
- @aht.cold = true
486
- @aht.reaction.should eq 'Shiver'
487
- @aht.hot = true
488
- @aht.reaction.should eq 'Sweat'
401
+ it 'return the value of the first key that evaluates as a true attribute' do
402
+ hat.reaction hot: 'Sweat', cold: 'Shiver'
403
+ hat.cold = true
404
+ expect(hat.reaction).to eq 'Shiver'
405
+ hat.hot = true
406
+ expect(hat.reaction).to eq 'Sweat'
489
407
  end
490
408
 
491
- it 'otherwise it returns the hash' do
492
- reactions = { :hot => 'Sweat', :cold => 'Shiver' }
493
- @aht.cold = false
494
- @aht.hot = false
495
- @aht.reaction.should eq reactions
409
+ it 'otherwise return the hash' do
410
+ hat.reaction hot: 'Sweat', cold: 'Shiver'
411
+ hat.cold = false
412
+ hat.hot = false
413
+ expect(hat.reaction).to eq ( { :hot => 'Sweat', :cold => 'Shiver' } )
496
414
  end
497
415
 
498
416
  end