edave-enumerated_attribute 0.2.18

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 (107) hide show
  1. data/.gitignore +15 -0
  2. data/CHANGELOG.rdoc +49 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +558 -0
  5. data/Rakefile +30 -0
  6. data/init.rb +1 -0
  7. data/lib/enumerated_attribute.rb +24 -0
  8. data/lib/enumerated_attribute/attribute.rb +79 -0
  9. data/lib/enumerated_attribute/attribute/arguments.rb +48 -0
  10. data/lib/enumerated_attribute/attribute/attribute_descriptor.rb +51 -0
  11. data/lib/enumerated_attribute/attribute/class_methods.rb +45 -0
  12. data/lib/enumerated_attribute/attribute/instance_methods.rb +96 -0
  13. data/lib/enumerated_attribute/integrations.rb +33 -0
  14. data/lib/enumerated_attribute/integrations/active_record.rb +114 -0
  15. data/lib/enumerated_attribute/integrations/datamapper.rb +6 -0
  16. data/lib/enumerated_attribute/integrations/default.rb +25 -0
  17. data/lib/enumerated_attribute/integrations/object.rb +47 -0
  18. data/lib/enumerated_attribute/method_definition_dsl.rb +142 -0
  19. data/lib/enumerated_attribute/rails_helpers.rb +99 -0
  20. data/spec/active_record/active_record_spec.rb +384 -0
  21. data/spec/active_record/association_test_classes.rb +40 -0
  22. data/spec/active_record/associations_spec.rb +130 -0
  23. data/spec/active_record/cfg.rb +6 -0
  24. data/spec/active_record/df.rb +12 -0
  25. data/spec/active_record/inheritance_classes.rb +19 -0
  26. data/spec/active_record/inheritance_spec.rb +60 -0
  27. data/spec/active_record/race_car.rb +15 -0
  28. data/spec/active_record/single_table_inheritance_spec.rb +0 -0
  29. data/spec/active_record/sti_classes.rb +10 -0
  30. data/spec/active_record/sti_spec.rb +41 -0
  31. data/spec/active_record/test_in_memory.rb +71 -0
  32. data/spec/car.rb +67 -0
  33. data/spec/cfg.rb +8 -0
  34. data/spec/inheritance_classes.rb +16 -0
  35. data/spec/inheritance_spec.rb +80 -0
  36. data/spec/new_and_method_missing_spec.rb +73 -0
  37. data/spec/plural.rb +8 -0
  38. data/spec/poro_spec.rb +420 -0
  39. data/spec/rails/README +243 -0
  40. data/spec/rails/Rakefile +10 -0
  41. data/spec/rails/app/controllers/application_controller.rb +10 -0
  42. data/spec/rails/app/controllers/form_test_controller.rb +38 -0
  43. data/spec/rails/app/helpers/application_helper.rb +3 -0
  44. data/spec/rails/app/helpers/form_test_helper.rb +2 -0
  45. data/spec/rails/app/models/user.rb +9 -0
  46. data/spec/rails/app/views/form_test/form.html.erb +1 -0
  47. data/spec/rails/app/views/form_test/form_for.html.erb +10 -0
  48. data/spec/rails/app/views/form_test/form_tag.html.erb +9 -0
  49. data/spec/rails/app/views/form_test/index.html.erb +6 -0
  50. data/spec/rails/app/views/layouts/application.html.erb +11 -0
  51. data/spec/rails/config/boot.rb +110 -0
  52. data/spec/rails/config/database.yml +22 -0
  53. data/spec/rails/config/environment.rb +45 -0
  54. data/spec/rails/config/environments/development.rb +17 -0
  55. data/spec/rails/config/environments/production.rb +28 -0
  56. data/spec/rails/config/environments/test.rb +28 -0
  57. data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/rails/config/initializers/inflections.rb +10 -0
  59. data/spec/rails/config/initializers/mime_types.rb +5 -0
  60. data/spec/rails/config/initializers/new_rails_defaults.rb +19 -0
  61. data/spec/rails/config/initializers/session_store.rb +15 -0
  62. data/spec/rails/config/locales/en.yml +5 -0
  63. data/spec/rails/config/routes.rb +43 -0
  64. data/spec/rails/db/development.sqlite3 +0 -0
  65. data/spec/rails/db/migrate/20090804230221_create_sessions.rb +16 -0
  66. data/spec/rails/db/migrate/20090804230546_create_users.rb +21 -0
  67. data/spec/rails/db/schema.rb +35 -0
  68. data/spec/rails/db/test.sqlite3 +0 -0
  69. data/spec/rails/public/404.html +30 -0
  70. data/spec/rails/public/422.html +30 -0
  71. data/spec/rails/public/500.html +30 -0
  72. data/spec/rails/public/favicon.ico +0 -0
  73. data/spec/rails/public/images/rails.png +0 -0
  74. data/spec/rails/public/index.html +275 -0
  75. data/spec/rails/public/javascripts/application.js +2 -0
  76. data/spec/rails/public/javascripts/controls.js +963 -0
  77. data/spec/rails/public/javascripts/dragdrop.js +973 -0
  78. data/spec/rails/public/javascripts/effects.js +1128 -0
  79. data/spec/rails/public/javascripts/prototype.js +4320 -0
  80. data/spec/rails/public/robots.txt +5 -0
  81. data/spec/rails/public/stylesheets/scaffold.css +54 -0
  82. data/spec/rails/script/about +4 -0
  83. data/spec/rails/script/autospec +6 -0
  84. data/spec/rails/script/console +3 -0
  85. data/spec/rails/script/dbconsole +3 -0
  86. data/spec/rails/script/destroy +3 -0
  87. data/spec/rails/script/generate +3 -0
  88. data/spec/rails/script/performance/benchmarker +3 -0
  89. data/spec/rails/script/performance/profiler +3 -0
  90. data/spec/rails/script/plugin +3 -0
  91. data/spec/rails/script/runner +3 -0
  92. data/spec/rails/script/server +3 -0
  93. data/spec/rails/script/spec +10 -0
  94. data/spec/rails/script/spec_server +9 -0
  95. data/spec/rails/spec/controllers/form_test_controller_spec.rb +41 -0
  96. data/spec/rails/spec/integrations/enum_select_spec.rb +75 -0
  97. data/spec/rails/spec/matchers.rb +12 -0
  98. data/spec/rails/spec/rcov.opts +2 -0
  99. data/spec/rails/spec/spec.opts +4 -0
  100. data/spec/rails/spec/spec_helper.rb +40 -0
  101. data/spec/rails/spec/views/form_test/form.html.erb_spec.rb +12 -0
  102. data/spec/rails/spec/views/form_test/form_for.html.erb_spec.rb +12 -0
  103. data/spec/rails/spec/views/form_test/form_tag.html.erb_spec.rb +12 -0
  104. data/spec/rcov.opts +2 -0
  105. data/spec/spec.opts +4 -0
  106. data/spec/tractor.rb +48 -0
  107. metadata +190 -0
@@ -0,0 +1,8 @@
1
+ #irb -r cfg ==> to work configure for working in IRB
2
+ $:.unshift '../lib'
3
+ require 'enumerated_attribute'
4
+ require 'tractor'
5
+ require 'car'
6
+ require 'plural'
7
+ require 'inheritance_classes'
8
+
@@ -0,0 +1,16 @@
1
+ require 'enumerated_attribute'
2
+
3
+ class Base
4
+ enum_attr :base1, %w(^one two three four)
5
+ enum_attr :inherited1, %w(^one two three four)
6
+ enum_attr :inherited2, %w(^one two three four)
7
+ end
8
+
9
+ class Sub < Base
10
+ enum_attr :sub1, %w(one ^two three four)
11
+ enum_attr :inherited1, %w(one ^two three four)
12
+ enum_attr :inherited2, %w(^five six seven eight)
13
+ end
14
+
15
+ class Sub2 < Base
16
+ end
@@ -0,0 +1,80 @@
1
+ require 'inheritance_classes'
2
+
3
+ def one_to_four; [:one, :two, :three, :four]; end
4
+ def five_to_eight; [:five, :six, :seven, :eight]; end
5
+
6
+ describe "Base" do
7
+ it "should have :base1 init to :one" do
8
+ o=Base.new
9
+ o.base1.should == :one
10
+ o.enums(:base1).should == one_to_four
11
+ o.base1_previous.should == :four
12
+ end
13
+ it "should have :inherited1 init to :one" do
14
+ o=Base.new
15
+ o.inherited1.should == :one
16
+ o.enums(:inherited1).should == one_to_four
17
+ o.inherited1_previous.should == :four
18
+ end
19
+ it "should have :inherited2 init to :one" do
20
+ o=Base.new
21
+ o.inherited2.should == :one
22
+ o.enums(:inherited2).should == one_to_four
23
+ o.inherited2_previous.should == :four
24
+ end
25
+ end
26
+
27
+ describe "Sub<Base" do
28
+ it "should instantiate an object" do
29
+ lambda { s=Sub.new }.should_not raise_error
30
+ end
31
+ it "should have :sub1 init to :two" do
32
+ o=Sub.new
33
+ o.sub1.should == :two
34
+ o.enums(:sub1).should == one_to_four
35
+ o.sub1_previous.should == :one
36
+ end
37
+ it "should have :base1 init to :one" do
38
+ o=Sub.new
39
+ o.base1.should == :one
40
+ o.enums(:base1).should == one_to_four
41
+ o.base1_previous.should == :four
42
+ end
43
+ it "should have :inherited1 init to :two" do
44
+ o=Sub.new
45
+ o.inherited1.should == :two
46
+ o.enums(:inherited1).should == one_to_four
47
+ o.inherited1_previous.should == :one
48
+ end
49
+ it "should have :inherited2 init to :five" do
50
+ o=Sub.new
51
+ o.inherited2.should == :five
52
+ o.enums(:inherited2).should == five_to_eight
53
+ o.inherited2_previous.should == :eight
54
+ end
55
+ end
56
+
57
+ describe "Sub2<Base" do
58
+ it "should instantiate an object" do
59
+ lambda {s=Sub2.new}.should_not raise_error
60
+ end
61
+ it "should have :base1 init to :one" do
62
+ o=Sub2.new
63
+ o.base1.should == :one
64
+ o.enums(:base1).should == one_to_four
65
+ o.base1_previous.should == :four
66
+ end
67
+ it "should have :inherited1 init to :one" do
68
+ o=Sub2.new
69
+ o.inherited1.should == :one
70
+ o.enums(:inherited1).should == one_to_four
71
+ o.inherited1_previous.should == :four
72
+ end
73
+ it "should have :inherited2 init to :one" do
74
+ o=Sub2.new
75
+ o.inherited2.should == :one
76
+ o.enums(:inherited2).should == one_to_four
77
+ o.inherited2_previous.should == :four
78
+ end
79
+ end
80
+
@@ -0,0 +1,73 @@
1
+ require 'car'
2
+
3
+ #used to test that method_missing chaining plays nice in inheritance situation
4
+
5
+ describe "CarWithMethods" do
6
+
7
+ it "should initialize Car and Vehicle _method_missing_called? to false" do
8
+ c= CarWithMethods.new
9
+ c.car_method_missing_called?.should be_false
10
+ c.vehicle_method_missing_called?.should be_false
11
+ end
12
+
13
+ it "should initialize Car and Vehicle _new_called? to false" do
14
+ CarWithMethods.reset
15
+ CarWithMethods.car_new_called?.should be_false
16
+ CarWithMethods.vehicle_new_called?.should be_false
17
+ end
18
+
19
+ it "should initialize :gear to :neutral" do
20
+ c = CarWithMethods.new
21
+ c.gear.should == :neutral
22
+ end
23
+
24
+ it "should hit both new methods for Car and Vehicle on instantiation" do
25
+ CarWithMethods.reset
26
+ CarWithMethods.new
27
+ CarWithMethods.car_new_called?.should be_true
28
+ CarWithMethods.vehicle_new_called?.should be_true
29
+ end
30
+
31
+ it "should not hit method_missing when calling dynamic predicate method :gear_is_in_reverse?" do
32
+ c = CarWithMethods.new
33
+ c.gear_is_in_reverse?
34
+ c.car_method_missing_called?.should be_false
35
+ c.vehicle_method_missing_called?.should be_false
36
+ end
37
+
38
+ it "should hit Car and Vehicle method_missing when calling unsupported dynamic predicate method" do
39
+ c = CarWithMethods.new
40
+ c.parking_break_is_on?
41
+ c.car_method_missing_called?.should be_true
42
+ c.vehicle_method_missing_called?.should be_true
43
+ end
44
+ end
45
+
46
+ describe "CarWithoutMethods" do
47
+
48
+ it "should set Car and Vehicle _new_called? to false when calling :reset" do
49
+ CarWithoutMethods.reset
50
+ CarWithoutMethods.car_new_called?.should be_false
51
+ CarWithoutMethods.vehicle_new_called?.should be_false
52
+ end
53
+
54
+ it "should hit only Vehicle _new_called? on instantiation" do
55
+ CarWithoutMethods.reset
56
+ CarWithoutMethods.new
57
+ CarWithoutMethods.car_new_called?.should be_false
58
+ CarWithoutMethods.vehicle_new_called?.should be_true
59
+ end
60
+
61
+ it "should not hit Vehicle method_missing when calling dynamic predicate method :gear_is_in_first?" do
62
+ c = CarWithoutMethods.new
63
+ c.gear_is_in_drive?
64
+ c.vehicle_method_missing_called?.should be_false
65
+ end
66
+
67
+ it "should hit Vehicle method_missing when calling unsupported predicate method" do
68
+ c = CarWithoutMethods.new
69
+ c.parking_break2_is_on?
70
+ c.vehicle_method_missing_called?.should be_true
71
+ end
72
+
73
+ end
@@ -0,0 +1,8 @@
1
+ require 'enumerated_attribute'
2
+
3
+ class Plural
4
+ enum_attr :box, %w(small medium large)
5
+ enum_attr :batch, %w(none daily weekly)
6
+ enum_attr :cherry, %w(red green yellow)
7
+ enum_attr :guy, %w(handsome funny cool)
8
+ end
@@ -0,0 +1,420 @@
1
+ require 'rubygems'
2
+ require 'tractor'
3
+ require 'plural'
4
+
5
+ Spec::Matchers.define :define_instance_method do |method_name|
6
+ match do |klass_or_object|
7
+ if klass_or_object.is_a?(Class)
8
+ klass_or_object.method_defined?(method_name.to_sym)
9
+ else
10
+ klass_or_object.class.method_defined?(method_name.to_sym)
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "enumerated_attributes caching" do
16
+ class Impl1
17
+ enum_attr :enum1, %w(e1 e2)
18
+ enumerated_attributes
19
+ enum_attr :enum2, %w(e3 e4)
20
+ end
21
+ it "should have enum2 described even though enumerated_attributes cached state" do
22
+ obj = Impl1.new
23
+ lambda { obj.enum2 = :e3 }.should_not raise_exception
24
+ end
25
+ end
26
+ describe "Plural" do
27
+ it "should have plural accessor :boxes for :box" do
28
+ p=Plural.new
29
+ p.should define_instance_method(:boxes) #methods.should include("boxes")
30
+ p.boxes.should == [:small, :medium, :large]
31
+ end
32
+ it "should have plural accessor :batches for :batch" do
33
+ p=Plural.new
34
+ p.should define_instance_method(:batches) #methods.should include("batches")
35
+ p.batches.should == [:none, :daily, :weekly]
36
+ end
37
+ it "should have plural accessor :cherries for :cherry" do
38
+ p=Plural.new
39
+ p.should define_instance_method(:cherries) #methods.should include("cherries")
40
+ p.cherries.should == [:red, :green, :yellow]
41
+ end
42
+ it "should have plural accessor :guys for :guy" do
43
+ p=Plural.new
44
+ p.should define_instance_method(:guys) #methods.should include("guys")
45
+ p.guys.should == [:handsome, :funny, :cool]
46
+ end
47
+
48
+ end
49
+
50
+ describe "Tractor" do
51
+
52
+ it "should have default labels for :gear attribute" do
53
+ labels_hash = {:reverse=>'Reverse', :neutral=>'Neutral', :first=>'First', :second=>'Second', :over_drive=>'Over drive'}
54
+ labels = ['Reverse', 'Neutral', 'First', 'Second', 'Over drive']
55
+ select_options = [['Reverse', 'reverse'], ['Neutral', 'neutral'], ['First', 'first'], ['Second', 'second'], ['Over drive', 'over_drive']]
56
+ t=Tractor.new
57
+ t.gears.labels.should == labels
58
+ labels_hash.each do |k,v|
59
+ t.gears.label(k).should == v
60
+ end
61
+ t.gears.hash.should == labels_hash
62
+ t.gears.select_options.should == select_options
63
+ end
64
+
65
+ it "should retrieve :gear enums through enums method" do
66
+ t=Tractor.new
67
+ t.enums(:gear).should == t.gears
68
+ end
69
+
70
+ it "should retrieve custom labels for :side_light attribute" do
71
+ labels_hash = {:off=>'OFF', :low=>'LOW DIM', :high=>'HIGH BEAM', :super_high=>'SUPER BEAM'}
72
+ t=Tractor.new
73
+ enum = t.enums(:side_light)
74
+ t.enums(:side_light).hash.each do |k,v|
75
+ enum.label(k).should == labels_hash[k]
76
+ end
77
+ end
78
+
79
+ it "should return a Symbol type from reader methods" do
80
+ t=Tractor.new
81
+ t.gear.should be_an_instance_of(Symbol)
82
+ end
83
+
84
+ it "should not raise errors for dynamic predicate methods missing attribute name" do
85
+ t=Tractor.new
86
+ lambda{ t.neutral?.should be_true }.should_not raise_error
87
+ lambda{ t.is_neutral?.should be_true }.should_not raise_error
88
+ lambda{ t.not_neutral?.should be_false}.should_not raise_error
89
+ t.gear = :first
90
+ t.neutral?.should be_false
91
+ t.not_neutral?.should be_true
92
+ end
93
+
94
+ it "should raise AmbiguousMethod when calling :off?" do
95
+ t=Tractor.new
96
+ lambda { t.off? }.should raise_error(EnumeratedAttribute::AmbiguousMethod)
97
+ end
98
+
99
+ it "should raise AmbiguousMethod when calling :in_reverse?" do
100
+ t=Tractor.new
101
+ lambda {t.in_reverse?}.should raise_error(EnumeratedAttribute::AmbiguousMethod)
102
+ end
103
+
104
+ it "should raise AmbiguousMethod when calling :not_reverse?" do
105
+ t=Tractor.new
106
+ lambda {t.not_reverse?}.should raise_error(EnumeratedAttribute::AmbiguousMethod)
107
+ end
108
+
109
+ it "should initialize :gear for two instances of the same class" do
110
+ t=Tractor.new
111
+ t.gear.should == :neutral
112
+ s=Tractor.new
113
+ s.gear.should == :neutral
114
+ end
115
+
116
+ it "should dynamically create :plow_nil? and :plow_not_nil?" do
117
+ t=Tractor.new
118
+ t.plow_nil?.should be_false
119
+ t.plow_not_nil?.should be_true
120
+ t.plow = nil
121
+ t.plow_not_nil?.should be_false
122
+ t.plow_nil?.should be_true
123
+ Tractor.should define_instance_method(:plow_nil?)
124
+ Tractor.should define_instance_method(:plow_not_nil?)
125
+ end
126
+
127
+ it "should dynamically create :plow_is_nil? and :plow_is_not_nil?" do
128
+ t=Tractor.new
129
+ t.plow_is_nil?.should be_false
130
+ t.plow_is_not_nil?.should be_true
131
+ t.plow = nil
132
+ t.plow_is_not_nil?.should be_false
133
+ t.plow_is_nil?.should be_true
134
+ Tractor.should define_instance_method(:plow_is_nil?)
135
+ Tractor.should define_instance_method(:plow_is_not_nil?)
136
+ end
137
+
138
+ it "should negate result for not_parked? defined with is_not" do
139
+ t=Tractor.new
140
+ t.gear = :neutral
141
+ t.not_parked?.should be_false
142
+ end
143
+
144
+ it "should negate result for not_driving? defined with is_not" do
145
+ t=Tractor.new
146
+ t.gear = :neutral
147
+ t.not_driving?.should be_true
148
+ end
149
+
150
+ =begin
151
+ it "should have getter but no setter for :temperature" do
152
+ Tractor.instance_methods.should_not include('temperature=')
153
+ Tractor.instance_methods.should include('temperature')
154
+ end
155
+
156
+ it "should have setter but no getter for :ignition" do
157
+ Tractor.instance_methods.should_not include('ignition')
158
+ Tractor.instance_methods.should include('ignition=')
159
+ end
160
+ =end
161
+
162
+ it "should be able to set :plow to nil" do
163
+ t=Tractor.new
164
+ lambda { t.plow = nil }.should_not raise_error(EnumeratedAttribute::InvalidEnumeration)
165
+ end
166
+
167
+ it "should have method :plow_nil? that operates correctly" do
168
+ t=Tractor.new
169
+ t.plow.should_not be_nil
170
+ t.plow_nil?.should be_false
171
+ t.plow = nil
172
+ t.plow.should be_nil
173
+ t.plow_nil?.should be_true
174
+ end
175
+
176
+ it "should raise EnumeratedAttribute::InvalidEnumeration when setting :gear to nil" do
177
+ t=Tractor.new
178
+ lambda{ t.gear = nil }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
179
+ end
180
+
181
+ it "should have respond_to? method for :gear_is_in_neutral?" do
182
+ t=Tractor.new
183
+ t.respond_to?('gear_is_in_neutral?').should be_true
184
+ end
185
+
186
+ it "should have respond_to? method for :side_light_is_super_high?" do
187
+ t=Tractor.new
188
+ t.respond_to?(:side_light_is_super_high?).should be_true
189
+ end
190
+
191
+ it "should not have respond_to? method for :gear_is_in_high?" do
192
+ t=Tractor.new
193
+ t.respond_to?(:gear_is_in_high?).should be_false
194
+ end
195
+
196
+ it "should return true when calling respond_to? for :gear_is_in_neutral? with an optional argument" do
197
+ t=Tractor.new
198
+ t.respond_to?(:gear_is_in_neutral?, true).should be_true
199
+ t.respond_to?(:gear_is_in_neutral?, false).should be_true
200
+ end
201
+
202
+ it "should return true when called respond_to? for :gear_is_in_high? with an optional argument" do
203
+ t=Tractor.new
204
+ t.respond_to?(:gear_is_in_high?, true).should be_false
205
+ t.respond_to?(:gear_is_in_high?, false).should be_false
206
+ end
207
+
208
+ it "should initially set :plow to :up" do
209
+ t=Tractor.new
210
+ t.plow.should == :up
211
+ end
212
+
213
+ it "should have plowing? state method" do
214
+ t=Tractor.new
215
+ t.plowing?.should be_false
216
+ t.plow=:down
217
+ t.plowing?.should be_false
218
+ t.gear= :first
219
+ t.plowing?.should be_true
220
+ t.plow=:up
221
+ t.plowing?.should be_false
222
+ end
223
+
224
+ it "should have :side_light_up incrementor" do
225
+ t=Tractor.new
226
+ t.side_light = :off
227
+ t.side_light_up.should == :low
228
+ t.side_light_up.should == :high
229
+ t.side_light_up.should == :super_high
230
+ t.side_light_up.should == :off
231
+ end
232
+
233
+ it "should have :side_light_down incrementor" do
234
+ t=Tractor.new
235
+ t.side_light_down.should == :super_high
236
+ t.side_light_down.should == :high
237
+ t.side_light_down.should == :low
238
+ t.side_light_down.should == :off
239
+ end
240
+
241
+ it "should have :turn_lights_up incrementor" do
242
+ t=Tractor.new
243
+ t.lights = :off
244
+ t.turn_lights_up.should == :low
245
+ t.turn_lights_up.should == :high
246
+ end
247
+
248
+ it "should have :turn_lights_down decrementor" do
249
+ t=Tractor.new
250
+ t.lights=:high
251
+ t.turn_lights_down.should == :low
252
+ t.turn_lights_down.should == :off
253
+ end
254
+
255
+ it "should have :gear_previous which wraps from :neutral to :over_drive" do
256
+ t=Tractor.new
257
+ t.gear_previous.should == :reverse
258
+ t.gear.should == :reverse
259
+ t.gear_previous.should == :over_drive
260
+ t.gear.should == :over_drive
261
+ end
262
+
263
+ it "should have :gear_next which wraps from :second to :reverse" do
264
+ t=Tractor.new
265
+ t.gear = :second
266
+ t.gear_next.should == :over_drive
267
+ t.gear.should == :over_drive
268
+ t.gear_next.should == :reverse
269
+ t.gear.should == :reverse
270
+ end
271
+
272
+ it "should have :upshift which increments :gear from :neutral to :over_drive without wrapping" do
273
+ t=Tractor.new
274
+ t.upshift.should == :first
275
+ t.upshift.should == :second
276
+ t.upshift.should == :over_drive
277
+ t.upshift.should == :over_drive
278
+ end
279
+
280
+ it "should have :downshift which decrements :gear from :over_drive to :neutral without wrapping" do
281
+ t=Tractor.new
282
+ t.gear = :over_drive
283
+ t.downshift.should == :second
284
+ t.downshift.should == :first
285
+ t.downshift.should == :neutral
286
+ t.downshift.should == :neutral
287
+ end
288
+
289
+ it "should have parked? method" do
290
+ t=Tractor.new
291
+ t.parked?.should be_true
292
+ t.gear = :reverse
293
+ t.parked?.should be_false
294
+ end
295
+
296
+ it "should have driving? method" do
297
+ t=Tractor.new
298
+ t.driving?.should be_false
299
+ [:first, :second, :over_drive].each do |g|
300
+ t.gear=g
301
+ t.driving?.should be_true
302
+ end
303
+ end
304
+
305
+ it "should initially set side_light to :off" do
306
+ t=Tractor.new
307
+ t.side_light.should == :off
308
+ end
309
+
310
+ it "should have side_light_enums method" do
311
+ t = Tractor.new
312
+ t.side_light_enums.should == Tractor::SIDE_LIGHT_ENUM_VALUES
313
+ end
314
+
315
+ it "should have state method side_light_is_off?" do
316
+ t=Tractor.new
317
+ t.side_light_is_off?.should be_true
318
+ end
319
+
320
+ it "should have state method side_light_is_super_high?" do
321
+ t=Tractor.new
322
+ t.side_light_is_super_high?.should be_false
323
+ end
324
+
325
+ it "should initially set :gear to :neutral" do
326
+ t=Tractor.new
327
+ t.gear.should == :neutral
328
+ end
329
+
330
+ it "should set lights initially to :off" do
331
+ t=Tractor.new
332
+ t.lights.should == :off
333
+ end
334
+
335
+ it "should create a lights_enums method for all light enumerated values" do
336
+ t=Tractor.new
337
+ t.lights_enums.should == Tractor::LIGHTS_ENUM_VALUES
338
+ end
339
+
340
+ it "should initially set lights to :off" do
341
+ t=Tractor.new
342
+ t.lights.should equal(:off)
343
+ end
344
+
345
+ it "should have dynamic state methods for :reverse and :neutral" do
346
+ t = Tractor.new
347
+ t.gear_is_in_reverse?.should be_false
348
+ t.gear_is_in_neutral?.should be_true
349
+ end
350
+
351
+ it "should have negative dynamic state methods for :reverses and :neutral" do
352
+ t = Tractor.new
353
+ t.gear_is_not_in_reverse?.should be_true
354
+ t.gear_is_not_in_neutral?.should be_false
355
+ end
356
+
357
+ it "should have negative and positive dynamic state methods for :over_drive" do
358
+ t = Tractor.new
359
+ t.gear_is_in_over_drive?.should be_false
360
+ t.gear_is_not_in_over_drive?.should be_true
361
+ end
362
+
363
+ it "should have created instance methods for :reverse" do
364
+ Tractor.should define_instance_method(:gear_is_in_reverse?)
365
+ Tractor.should define_instance_method(:gear_is_not_in_reverse?)
366
+ end
367
+
368
+ it "should have created instance methods for :neutral" do
369
+ Tractor.should define_instance_method(:gear_is_in_neutral?)
370
+ Tractor.should define_instance_method(:gear_is_not_in_neutral?)
371
+ end
372
+
373
+ it "should have created instance methods for :over_drive" do
374
+ Tractor.should define_instance_method :gear_is_in_over_drive?
375
+ Tractor.should define_instance_method :gear_is_not_in_over_drive?
376
+ end
377
+
378
+ it "should raise NoMethodError for dynamic state methods not querying valid enumeration values" do
379
+ t = Tractor.new
380
+ lambda { t.gear_is_in_high? }.should raise_error(NoMethodError)
381
+ end
382
+
383
+ it "should convert string values to symbols for attr setters" do
384
+ t = Tractor.new
385
+ t.gear= 'reverse'
386
+ t.gear.should == :reverse
387
+ end
388
+
389
+ it "should have instance method gears equal to enumeration array" do
390
+ Tractor.new.gears.should == Tractor::GEAR_ENUM_VALUES
391
+ end
392
+
393
+ it "should have gear attribute initialized to :neutral" do
394
+ t = Tractor.new
395
+ t.gear.should == :neutral
396
+ end
397
+
398
+ it "should set gear attribute to :first" do
399
+ t = Tractor.new
400
+ t.gear = :first
401
+ t.gear.should == :first
402
+ end
403
+
404
+ it "should raise error when set gear attribute to :broken" do
405
+ t = Tractor.new
406
+ lambda { t.gear= :broken }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
407
+ end
408
+
409
+ it "should have name attribute initially set to 'old faithful'" do
410
+ t = Tractor.new
411
+ t.name.should == 'old faithful'
412
+ end
413
+
414
+ it "should set name attribute to 'broke n busted'" do
415
+ t = Tractor.new
416
+ t.name = 'broke n busted'
417
+ t.name.should == 'broke n busted'
418
+ end
419
+
420
+ end