micronaut 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/History.txt +15 -0
  2. data/LICENSE +45 -0
  3. data/README.markdown +66 -0
  4. data/RSPEC-LICENSE +23 -0
  5. data/Rakefile +78 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/micronaut +4 -0
  8. data/examples/example_helper.rb +54 -0
  9. data/examples/lib/micronaut/behaviour_example.rb +351 -0
  10. data/examples/lib/micronaut/configuration_example.rb +133 -0
  11. data/examples/lib/micronaut/example_example.rb +67 -0
  12. data/examples/lib/micronaut/expectations/extensions/object_example.rb +146 -0
  13. data/examples/lib/micronaut/expectations/fail_with_example.rb +17 -0
  14. data/examples/lib/micronaut/expectations/wrap_expectation_example.rb +27 -0
  15. data/examples/lib/micronaut/formatters/base_formatter_example.rb +117 -0
  16. data/examples/lib/micronaut/formatters/documentation_formatter_example.rb +5 -0
  17. data/examples/lib/micronaut/formatters/progress_formatter_example.rb +29 -0
  18. data/examples/lib/micronaut/kernel_extensions_example.rb +13 -0
  19. data/examples/lib/micronaut/matchers/be_close_example.rb +52 -0
  20. data/examples/lib/micronaut/matchers/be_example.rb +298 -0
  21. data/examples/lib/micronaut/matchers/change_example.rb +360 -0
  22. data/examples/lib/micronaut/matchers/description_generation_example.rb +175 -0
  23. data/examples/lib/micronaut/matchers/eql_example.rb +35 -0
  24. data/examples/lib/micronaut/matchers/equal_example.rb +35 -0
  25. data/examples/lib/micronaut/matchers/has_example.rb +69 -0
  26. data/examples/lib/micronaut/matchers/have_example.rb +392 -0
  27. data/examples/lib/micronaut/matchers/include_example.rb +103 -0
  28. data/examples/lib/micronaut/matchers/match_example.rb +43 -0
  29. data/examples/lib/micronaut/matchers/matcher_methods_example.rb +78 -0
  30. data/examples/lib/micronaut/matchers/operator_matcher_example.rb +193 -0
  31. data/examples/lib/micronaut/matchers/raise_error_example.rb +348 -0
  32. data/examples/lib/micronaut/matchers/respond_to_example.rb +54 -0
  33. data/examples/lib/micronaut/matchers/satisfy_example.rb +36 -0
  34. data/examples/lib/micronaut/matchers/simple_matcher_example.rb +93 -0
  35. data/examples/lib/micronaut/matchers/throw_symbol_example.rb +125 -0
  36. data/examples/lib/micronaut/mocha_example.rb +29 -0
  37. data/examples/lib/micronaut/runner_example.rb +41 -0
  38. data/examples/lib/micronaut/world_example.rb +98 -0
  39. data/examples/lib/micronaut_example.rb +43 -0
  40. data/examples/resources/example_classes.rb +67 -0
  41. data/lib/micronaut.rb +40 -0
  42. data/lib/micronaut/behaviour.rb +217 -0
  43. data/lib/micronaut/configuration.rb +162 -0
  44. data/lib/micronaut/example.rb +112 -0
  45. data/lib/micronaut/expectations.rb +45 -0
  46. data/lib/micronaut/expectations/extensions/object.rb +92 -0
  47. data/lib/micronaut/expectations/handler.rb +51 -0
  48. data/lib/micronaut/expectations/wrap_expectation.rb +52 -0
  49. data/lib/micronaut/formatters.rb +12 -0
  50. data/lib/micronaut/formatters/base_formatter.rb +127 -0
  51. data/lib/micronaut/formatters/base_text_formatter.rb +139 -0
  52. data/lib/micronaut/formatters/documentation_formatter.rb +78 -0
  53. data/lib/micronaut/formatters/progress_formatter.rb +30 -0
  54. data/lib/micronaut/kernel_extensions.rb +11 -0
  55. data/lib/micronaut/matchers.rb +141 -0
  56. data/lib/micronaut/matchers/be.rb +204 -0
  57. data/lib/micronaut/matchers/be_close.rb +37 -0
  58. data/lib/micronaut/matchers/change.rb +148 -0
  59. data/lib/micronaut/matchers/eql.rb +26 -0
  60. data/lib/micronaut/matchers/equal.rb +26 -0
  61. data/lib/micronaut/matchers/generated_descriptions.rb +36 -0
  62. data/lib/micronaut/matchers/has.rb +19 -0
  63. data/lib/micronaut/matchers/have.rb +153 -0
  64. data/lib/micronaut/matchers/include.rb +80 -0
  65. data/lib/micronaut/matchers/match.rb +22 -0
  66. data/lib/micronaut/matchers/method_missing.rb +9 -0
  67. data/lib/micronaut/matchers/operator_matcher.rb +50 -0
  68. data/lib/micronaut/matchers/raise_error.rb +128 -0
  69. data/lib/micronaut/matchers/respond_to.rb +50 -0
  70. data/lib/micronaut/matchers/satisfy.rb +50 -0
  71. data/lib/micronaut/matchers/simple_matcher.rb +135 -0
  72. data/lib/micronaut/matchers/throw_symbol.rb +108 -0
  73. data/lib/micronaut/mocking/with_absolutely_nothing.rb +11 -0
  74. data/lib/micronaut/mocking/with_mocha.rb +15 -0
  75. data/lib/micronaut/mocking/with_rr.rb +24 -0
  76. data/lib/micronaut/rake_task.rb +84 -0
  77. data/lib/micronaut/runner.rb +60 -0
  78. data/lib/micronaut/world.rb +75 -0
  79. metadata +165 -0
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ describe Micronaut::Formatters::DocumentationFormatter do
4
+
5
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ describe Micronaut::Formatters::ProgressFormatter do
4
+
5
+ before do
6
+ @output = StringIO.new
7
+ @formatter = Micronaut::Formatters::ProgressFormatter.new
8
+ @formatter.start(2)
9
+ @formatter.stubs(:color_enabled?).returns(false)
10
+ @formatter.stubs(:output).returns(@output)
11
+ end
12
+
13
+ it "should produce line break on start dump" do
14
+ @formatter.start_dump(3)
15
+ @output.string.should == "\n"
16
+ end
17
+
18
+ it "should produce standard summary without pending when pending has a 0 count" do
19
+ @formatter.start_dump(3)
20
+ @formatter.dump_summary
21
+ @output.string.should =~ /Finished in 3 seconds\n2 examples/i
22
+ end
23
+
24
+ it "should push nothing on start" do
25
+ @formatter.start(4)
26
+ @output.string.should == ""
27
+ end
28
+
29
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../example_helper")
2
+
3
+ describe Micronaut::KernelExtensions do
4
+
5
+ it "should be included in Object" do
6
+ Object.included_modules.should include(Micronaut::KernelExtensions)
7
+ end
8
+
9
+ it "should add a describe method to Object" do
10
+ Object.should respond_to(:describe)
11
+ end
12
+
13
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ describe Micronaut::Matchers do
4
+
5
+ describe "be_close" do
6
+
7
+ it "should match when value == target" do
8
+ be_close(5.0, 0.5).matches?(5.0).should be_true
9
+ end
10
+
11
+ it "should match when value < (target + delta)" do
12
+ be_close(5.0, 0.5).matches?(5.49).should be_true
13
+ end
14
+
15
+ it "should match when value > (target - delta)" do
16
+ be_close(5.0, 0.5).matches?(4.51).should be_true
17
+ end
18
+
19
+ it "should not match when value == (target - delta)" do
20
+ be_close(5.0, 0.5).matches?(4.5).should be_false
21
+ end
22
+
23
+ it "should not match when value < (target - delta)" do
24
+ be_close(5.0, 0.5).matches?(4.49).should be_false
25
+ end
26
+
27
+ it "should not match when value == (target + delta)" do
28
+ be_close(5.0, 0.5).matches?(5.5).should be_false
29
+ end
30
+
31
+ it "should not match when value > (target + delta)" do
32
+ be_close(5.0, 0.5).matches?(5.51).should be_false
33
+ end
34
+
35
+ it "should provide a useful failure message" do
36
+ #given
37
+ matcher = be_close(5.0, 0.5)
38
+ #when
39
+ matcher.matches?(5.51)
40
+ #then
41
+ matcher.failure_message.should == "expected 5.0 +/- (< 0.5), got 5.51"
42
+ end
43
+
44
+ it "should describe itself" do
45
+ matcher = be_close(5.0, 0.5)
46
+ matcher.matches?(5.1)
47
+ matcher.description.should == "be close to 5.0 (within +- 0.5)"
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,298 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ describe Micronaut::Matchers do
4
+
5
+ describe "should be_predicate" do
6
+
7
+ it "should pass when actual returns true for :predicate?" do
8
+ actual = stub("actual", :happy? => true)
9
+ actual.should be_happy
10
+ end
11
+
12
+ it "should pass when actual returns true for :predicates? (present tense)" do
13
+ actual = stub("actual", :exists? => true, :exist? => true)
14
+ actual.should be_exist
15
+ end
16
+
17
+ it "should fail when actual returns false for :predicate?" do
18
+ actual = stub("actual", :happy? => false)
19
+ lambda do
20
+ actual.should be_happy
21
+ end.should fail_with("expected happy? to return true, got false")
22
+ end
23
+
24
+ it "should fail when actual does not respond to :predicate?" do
25
+ lambda do
26
+ Object.new.should be_happy
27
+ end.should raise_error(NameError, /happy\?/)
28
+ end
29
+
30
+ it "should fail on error other than NameError" do
31
+ actual = stub("actual")
32
+ actual.expects(:foo?).raises("aaaah")
33
+ lambda do
34
+ actual.should be_foo
35
+ end.should raise_error(/aaaah/)
36
+ end
37
+
38
+ it "should fail on error other than NameError (with the present tense predicate)" do
39
+ actual = Object.new
40
+ actual.expects(:foos?).raises("aaaah")
41
+ lambda do
42
+ actual.should be_foo
43
+ end.should raise_error(/aaaah/)
44
+ end
45
+
46
+ end
47
+
48
+ describe "should_not be_predicate" do
49
+
50
+ it "should pass when actual returns false for :sym?" do
51
+ actual = stub("actual", :happy? => false)
52
+ actual.should_not be_happy
53
+ end
54
+
55
+ it "should fail when actual returns true for :sym?" do
56
+ actual = stub("actual", :happy? => true)
57
+ lambda do
58
+ actual.should_not be_happy
59
+ end.should fail_with("expected happy? to return false, got true")
60
+ end
61
+
62
+ it "should fail when actual does not respond to :sym?" do
63
+ lambda do
64
+ Object.new.should_not be_happy
65
+ end.should raise_error(NameError)
66
+ end
67
+
68
+ end
69
+
70
+ describe "should be_predicate(*args)" do
71
+
72
+ it "should pass when actual returns true for :predicate?(*args)" do
73
+ actual = mock("actual")
74
+ actual.expects(:older_than?).with(3).returns(true)
75
+ actual.should be_older_than(3)
76
+ end
77
+
78
+ it "should fail when actual returns false for :predicate?(*args)" do
79
+ actual = mock("actual")
80
+ actual.expects(:older_than?).with(3).returns(false)
81
+ lambda do
82
+ actual.should be_older_than(3)
83
+ end.should fail_with("expected older_than?(3) to return true, got false")
84
+ end
85
+
86
+ it "should fail when actual does not respond to :predicate?" do
87
+ lambda do
88
+ Object.new.should be_older_than(3)
89
+ end.should raise_error(NameError)
90
+ end
91
+
92
+ end
93
+
94
+ describe "should_not be_predicate(*args)" do
95
+
96
+ it "should pass when actual returns false for :predicate?(*args)" do
97
+ actual = mock("actual")
98
+ actual.expects(:older_than?).with(3).returns(false)
99
+ actual.should_not be_older_than(3)
100
+ end
101
+
102
+ it "should fail when actual returns true for :predicate?(*args)" do
103
+ actual = mock("actual")
104
+ actual.expects(:older_than?).with(3).returns(true)
105
+ lambda do
106
+ actual.should_not be_older_than(3)
107
+ end.should fail_with("expected older_than?(3) to return false, got true")
108
+ end
109
+
110
+ it "should fail when actual does not respond to :predicate?" do
111
+ lambda do
112
+ Object.new.should_not be_older_than(3)
113
+ end.should raise_error(NameError)
114
+ end
115
+
116
+ end
117
+
118
+ describe "should be_true" do
119
+
120
+ it "should pass when actual equal(true)" do
121
+ true.should be_true
122
+ end
123
+
124
+ it "should fail when actual equal(false)" do
125
+ lambda do
126
+ false.should be_true
127
+ end.should fail_with("expected true, got false")
128
+ end
129
+
130
+ end
131
+
132
+ describe "should be_false" do
133
+
134
+ it "should pass when actual equal(false)" do
135
+ false.should be_false
136
+ end
137
+
138
+ it "should fail when actual equal(true)" do
139
+ lambda do
140
+ true.should be_false
141
+ end.should fail_with("expected false, got true")
142
+ end
143
+
144
+ end
145
+
146
+ describe "should be_nil" do
147
+
148
+ it "should pass when actual is nil" do
149
+ nil.should be_nil
150
+ end
151
+
152
+ it "should fail when actual is not nil" do
153
+ lambda do
154
+ :not_nil.should be_nil
155
+ end.should fail_with("expected nil? to return true, got false")
156
+ end
157
+
158
+ end
159
+
160
+ describe "should_not be_nil" do
161
+
162
+ it "should pass when actual is not nil" do
163
+ :not_nil.should_not be_nil
164
+ end
165
+
166
+ it "should fail when actual is nil" do
167
+ lambda do
168
+ nil.should_not be_nil
169
+ end.should fail_with("expected nil? to return false, got true")
170
+ end
171
+
172
+ end
173
+
174
+ describe "should be <" do
175
+
176
+ it "should pass when < operator returns true" do
177
+ 3.should be < 4
178
+ end
179
+
180
+ it "should fail when < operator returns false" do
181
+ lambda { 3.should be < 3 }.should fail_with("expected < 3, got 3")
182
+ end
183
+
184
+ end
185
+
186
+ describe "should be <=" do
187
+
188
+ it "should pass when <= operator returns true" do
189
+ 3.should be <= 4
190
+ 4.should be <= 4
191
+ end
192
+
193
+ it "should fail when <= operator returns false" do
194
+ lambda { 3.should be <= 2 }.should fail_with("expected <= 2, got 3")
195
+ end
196
+
197
+ end
198
+
199
+ describe "should be >=" do
200
+
201
+ it "should pass when >= operator returns true" do
202
+ 4.should be >= 4
203
+ 5.should be >= 4
204
+ end
205
+
206
+ it "should fail when >= operator returns false" do
207
+ lambda { 3.should be >= 4 }.should fail_with("expected >= 4, got 3")
208
+ end
209
+
210
+ end
211
+
212
+ describe "should be >" do
213
+
214
+ it "should pass when > operator returns true" do
215
+ 5.should be > 4
216
+ end
217
+
218
+ it "should fail when > operator returns false" do
219
+ lambda { 3.should be > 4 }.should fail_with("expected > 4, got 3")
220
+ end
221
+
222
+ end
223
+
224
+ describe "should be ==" do
225
+
226
+ it "should pass when == operator returns true" do
227
+ 5.should be == 5
228
+ end
229
+
230
+ it "should fail when == operator returns false" do
231
+ lambda { 3.should be == 4 }.should fail_with("expected == 4, got 3")
232
+ end
233
+
234
+ end
235
+
236
+ describe "should be ===" do
237
+
238
+ it "should pass when === operator returns true" do
239
+ Hash.should be === Hash.new
240
+ end
241
+
242
+ it "should fail when === operator returns false" do
243
+ lambda { Hash.should be === "not a hash" }.should fail_with(%[expected === not a hash, got Hash])
244
+ end
245
+
246
+ end
247
+
248
+ describe "should_not with operators" do
249
+
250
+ it "should coach user to stop using operators with should_not" do
251
+ lambda do
252
+ 5.should_not be < 6
253
+ end.should raise_error(/not only FAILED,\nit reads really poorly./m)
254
+ end
255
+
256
+ end
257
+
258
+ describe "should be" do
259
+
260
+ it "should pass if actual is true or a set value" do
261
+ true.should be
262
+ 1.should be
263
+ end
264
+
265
+ it "should fail if actual is false" do
266
+ lambda {false.should be}.should fail_with("expected true, got false")
267
+ end
268
+
269
+ it "should fail if actual is nil" do
270
+ lambda {nil.should be}.should fail_with("expected true, got nil")
271
+ end
272
+
273
+ end
274
+
275
+ describe "should be(value)" do
276
+
277
+ it "should pass if actual.equal?(value)" do
278
+ 5.should be(5)
279
+ end
280
+
281
+ it "should fail if !actual.equal?(value)" do
282
+ lambda { 5.should be(6) }.should fail_with("expected 6, got 5")
283
+ end
284
+
285
+ end
286
+
287
+ describe "'should be' with operator" do
288
+
289
+ it "should include 'be' in the description" do
290
+ (be > 6).description.should =~ /be > 6/
291
+ (be >= 6).description.should =~ /be >= 6/
292
+ (be <= 6).description.should =~ /be <= 6/
293
+ (be < 6).description.should =~ /be < 6/
294
+ end
295
+
296
+ end
297
+
298
+ end
@@ -0,0 +1,360 @@
1
+ #Based on patch from Wilson Bilkovich
2
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
3
+
4
+ class SomethingExpected
5
+ attr_accessor :some_value
6
+ end
7
+
8
+ describe Micronaut::Matchers do
9
+
10
+ describe "should change(actual, message)" do
11
+
12
+ before do
13
+ @instance = SomethingExpected.new
14
+ @instance.some_value = 5
15
+ end
16
+
17
+ it "should pass when actual is modified by the block" do
18
+ lambda {@instance.some_value = 6}.should change(@instance, :some_value)
19
+ end
20
+
21
+ it "should fail when actual is not modified by the block" do
22
+ lambda do
23
+ lambda {}.should change(@instance, :some_value)
24
+ end.should fail_with("some_value should have changed, but is still 5")
25
+ end
26
+
27
+ end
28
+
29
+ describe "should_not change(actual, message)" do
30
+
31
+ before do
32
+ @instance = SomethingExpected.new
33
+ @instance.some_value = 5
34
+ end
35
+
36
+ it "should pass when actual is not modified by the block" do
37
+ lambda { }.should_not change(@instance, :some_value)
38
+ end
39
+
40
+ it "should fail when actual is not modified by the block" do
41
+ lambda do
42
+ lambda {@instance.some_value = 6}.should_not change(@instance, :some_value)
43
+ end.should fail_with("some_value should not have changed, but did change from 5 to 6")
44
+ end
45
+
46
+ end
47
+
48
+ describe "should change { block }" do
49
+
50
+ before do
51
+ @instance = SomethingExpected.new
52
+ @instance.some_value = 5
53
+ end
54
+
55
+ it "should pass when actual is modified by the block" do
56
+ lambda {@instance.some_value = 6}.should change { @instance.some_value }
57
+ end
58
+
59
+ it "should fail when actual is not modified by the block" do
60
+ lambda do
61
+ lambda {}.should change{ @instance.some_value }
62
+ end.should fail_with("result should have changed, but is still 5")
63
+ end
64
+
65
+ it "should warn if passed a block using do/end instead of {}" do
66
+ lambda do
67
+ lambda {}.should change do; end
68
+ end.should raise_error(Micronaut::Matchers::MatcherError, /block passed to should or should_not/)
69
+ end
70
+
71
+ end
72
+
73
+ describe "should_not change { block }" do
74
+
75
+ before do
76
+ @instance = SomethingExpected.new
77
+ @instance.some_value = 5
78
+ end
79
+
80
+ it "should pass when actual is modified by the block" do
81
+ lambda {}.should_not change{ @instance.some_value }
82
+ end
83
+
84
+ it "should fail when actual is not modified by the block" do
85
+ lambda do
86
+ lambda {@instance.some_value = 6}.should_not change { @instance.some_value }
87
+ end.should fail_with("result should not have changed, but did change from 5 to 6")
88
+ end
89
+
90
+ it "should warn if passed a block using do/end instead of {}" do
91
+ lambda do
92
+ lambda {}.should_not change do; end
93
+ end.should raise_error(Micronaut::Matchers::MatcherError, /block passed to should or should_not/)
94
+ end
95
+
96
+ end
97
+
98
+ describe "should change(actual, message).by(expected)" do
99
+
100
+ before do
101
+ @instance = SomethingExpected.new
102
+ @instance.some_value = 5
103
+ end
104
+
105
+ it "should pass when attribute is changed by expected amount" do
106
+ lambda { @instance.some_value += 1 }.should change(@instance, :some_value).by(1)
107
+ end
108
+
109
+ it "should fail when the attribute is changed by unexpected amount" do
110
+ lambda do
111
+ lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by(1)
112
+ end.should fail_with("some_value should have been changed by 1, but was changed by 2")
113
+ end
114
+
115
+ it "should fail when the attribute is changed by unexpected amount in the opposite direction" do
116
+ lambda do
117
+ lambda { @instance.some_value -= 1 }.should change(@instance, :some_value).by(1)
118
+ end.should fail_with("some_value should have been changed by 1, but was changed by -1")
119
+ end
120
+
121
+ end
122
+
123
+ describe "should change{ block }.by(expected)" do
124
+
125
+ before do
126
+ @instance = SomethingExpected.new
127
+ @instance.some_value = 5
128
+ end
129
+
130
+ it "should pass when attribute is changed by expected amount" do
131
+ lambda { @instance.some_value += 1 }.should change{@instance.some_value}.by(1)
132
+ end
133
+
134
+ it "should fail when the attribute is changed by unexpected amount" do
135
+ lambda do
136
+ lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by(1)
137
+ end.should fail_with("result should have been changed by 1, but was changed by 2")
138
+ end
139
+
140
+ it "should fail when the attribute is changed by unexpected amount in the opposite direction" do
141
+ lambda do
142
+ lambda { @instance.some_value -= 1 }.should change{@instance.some_value}.by(1)
143
+ end.should fail_with("result should have been changed by 1, but was changed by -1")
144
+ end
145
+
146
+ end
147
+
148
+ describe "should change(actual, message).by_at_least(expected)" do
149
+
150
+ before do
151
+ @instance = SomethingExpected.new
152
+ @instance.some_value = 5
153
+ end
154
+
155
+ it "should pass when attribute is changed by greater than the expected amount" do
156
+ lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_least(1)
157
+ end
158
+
159
+ it "should pass when attribute is changed by the expected amount" do
160
+ lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_least(2)
161
+ end
162
+
163
+ it "should fail when the attribute is changed by less than the expected amount" do
164
+ lambda do
165
+ lambda { @instance.some_value += 1 }.should change(@instance, :some_value).by_at_least(2)
166
+ end.should fail_with("some_value should have been changed by at least 2, but was changed by 1")
167
+ end
168
+
169
+ end
170
+
171
+ describe "should change{ block }.by_at_least(expected)" do
172
+
173
+ before do
174
+ @instance = SomethingExpected.new
175
+ @instance.some_value = 5
176
+ end
177
+
178
+ it "should pass when attribute is changed by greater than expected amount" do
179
+ lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_least(1)
180
+ end
181
+
182
+ it "should pass when attribute is changed by the expected amount" do
183
+ lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_least(2)
184
+ end
185
+
186
+ it "should fail when the attribute is changed by less than the unexpected amount" do
187
+ lambda do
188
+ lambda { @instance.some_value += 1 }.should change{@instance.some_value}.by_at_least(2)
189
+ end.should fail_with("result should have been changed by at least 2, but was changed by 1")
190
+ end
191
+
192
+ end
193
+
194
+ describe "should change(actual, message).by_at_most(expected)" do
195
+
196
+ before do
197
+ @instance = SomethingExpected.new
198
+ @instance.some_value = 5
199
+ end
200
+
201
+ it "should pass when attribute is changed by less than the expected amount" do
202
+ lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_most(3)
203
+ end
204
+
205
+ it "should pass when attribute is changed by the expected amount" do
206
+ lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_most(2)
207
+ end
208
+
209
+ it "should fail when the attribute is changed by greater than the expected amount" do
210
+ lambda do
211
+ lambda { @instance.some_value += 2 }.should change(@instance, :some_value).by_at_most(1)
212
+ end.should fail_with("some_value should have been changed by at most 1, but was changed by 2")
213
+ end
214
+
215
+ end
216
+
217
+ describe "should change{ block }.by_at_most(expected)" do
218
+
219
+ before do
220
+ @instance = SomethingExpected.new
221
+ @instance.some_value = 5
222
+ end
223
+
224
+ it "should pass when attribute is changed by less than expected amount" do
225
+ lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_most(3)
226
+ end
227
+
228
+ it "should pass when attribute is changed by the expected amount" do
229
+ lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_most(2)
230
+ end
231
+
232
+ it "should fail when the attribute is changed by greater than the unexpected amount" do
233
+ lambda do
234
+ lambda { @instance.some_value += 2 }.should change{@instance.some_value}.by_at_most(1)
235
+ end.should fail_with("result should have been changed by at most 1, but was changed by 2")
236
+ end
237
+
238
+ end
239
+
240
+ describe "should change(actual, message).from(old)" do
241
+
242
+ before do
243
+ @instance = SomethingExpected.new
244
+ @instance.some_value = 'string'
245
+ end
246
+
247
+ it "should pass when attribute is == to expected value before executing block" do
248
+ lambda { @instance.some_value = "astring" }.should change(@instance, :some_value).from("string")
249
+ end
250
+
251
+ it "should fail when attribute is not == to expected value before executing block" do
252
+ lambda do
253
+ lambda { @instance.some_value = "knot" }.should change(@instance, :some_value).from("cat")
254
+ end.should fail_with("some_value should have initially been \"cat\", but was \"string\"")
255
+ end
256
+
257
+ end
258
+
259
+ describe "should change{ block }.from(old)" do
260
+
261
+ before do
262
+ @instance = SomethingExpected.new
263
+ @instance.some_value = 'string'
264
+ end
265
+
266
+ it "should pass when attribute is == to expected value before executing block" do
267
+ lambda { @instance.some_value = "astring" }.should change{@instance.some_value}.from("string")
268
+ end
269
+
270
+ it "should fail when attribute is not == to expected value before executing block" do
271
+ lambda do
272
+ lambda { @instance.some_value = "knot" }.should change{@instance.some_value}.from("cat")
273
+ end.should fail_with("result should have initially been \"cat\", but was \"string\"")
274
+ end
275
+
276
+ end
277
+
278
+ describe "should change(actual, message).to(new)" do
279
+
280
+ before do
281
+ @instance = SomethingExpected.new
282
+ @instance.some_value = 'string'
283
+ end
284
+
285
+ it "should pass when attribute is == to expected value after executing block" do
286
+ lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).to("cat")
287
+ end
288
+
289
+ it "should fail when attribute is not == to expected value after executing block" do
290
+ lambda do
291
+ lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).from("string").to("dog")
292
+ end.should fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
293
+ end
294
+
295
+ end
296
+
297
+ describe "should change{ block }.to(new)" do
298
+
299
+ before do
300
+ @instance = SomethingExpected.new
301
+ @instance.some_value = 'string'
302
+ end
303
+
304
+ it "should pass when attribute is == to expected value after executing block" do
305
+ lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.to("cat")
306
+ end
307
+
308
+ it "should fail when attribute is not == to expected value after executing block" do
309
+ lambda do
310
+ lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.from("string").to("dog")
311
+ end.should fail_with("result should have been changed to \"dog\", but is now \"cat\"")
312
+ end
313
+
314
+ end
315
+
316
+ describe "should change(actual, message).from(old).to(new)" do
317
+
318
+ before do
319
+ @instance = SomethingExpected.new
320
+ @instance.some_value = 'string'
321
+ end
322
+
323
+ it "should pass when #to comes before #from" do
324
+ lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).to("cat").from("string")
325
+ end
326
+
327
+ it "should pass when #from comes before #to" do
328
+ lambda { @instance.some_value = "cat" }.should change(@instance, :some_value).from("string").to("cat")
329
+ end
330
+
331
+ end
332
+
333
+ describe "should change{ block }.from(old).to(new)" do
334
+
335
+ before do
336
+ @instance = SomethingExpected.new
337
+ @instance.some_value = 'string'
338
+ end
339
+
340
+ it "should pass when #to comes before #from" do
341
+ lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.to("cat").from("string")
342
+ end
343
+
344
+ it "should pass when #from comes before #to" do
345
+ lambda { @instance.some_value = "cat" }.should change{@instance.some_value}.from("string").to("cat")
346
+ end
347
+
348
+ end
349
+
350
+ it "should work when the receiver has implemented #send" do
351
+ @instance = SomethingExpected.new
352
+ @instance.some_value = "string"
353
+ def @instance.send(*args); raise "DOH! Library developers shouldn't use #send!" end
354
+
355
+ lambda do
356
+ lambda { @instance.some_value = "cat" }.should change(@instance, :some_value)
357
+ end.should_not raise_error
358
+ end
359
+
360
+ end