rspec 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,7 +48,6 @@ private
48
48
 
49
49
  def find_matching_expectation(sym, *args)
50
50
  expectation = @expectations.find {|expectation| expectation.matches(sym, args)}
51
- expectation
52
51
  end
53
52
 
54
53
  end
@@ -0,0 +1,15 @@
1
+ module Spec
2
+
3
+ class RespondHelper < ShouldBase
4
+
5
+ def initialize(target)
6
+ @target = target
7
+ end
8
+
9
+ def to(expected)
10
+ fail_with_message(default_message("should respond to", expected)) unless @target.respond_to? expected
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ module Spec
2
+
3
+ class RespondNegator < ShouldBase
4
+
5
+ def initialize(target)
6
+ @target = target
7
+ end
8
+
9
+ def to(expected)
10
+ fail_with_message(default_message("should not respond to", expected)) if @target.respond_to? expected
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,36 @@
1
+ module Spec
2
+
3
+ class ShouldBase
4
+
5
+ def default_message(expectation, expected=:no_expectation_specified)
6
+ message = wrap(@target)
7
+ message += " #{expectation}"
8
+ if (expected != :no_expectation_specified)
9
+ message += " " + wrap(expected)
10
+ end
11
+ message
12
+ end
13
+
14
+ def wrap(obj)
15
+ if obj == true
16
+ "<true>"
17
+ elsif obj == false
18
+ "<false>"
19
+ elsif obj.nil?
20
+ "nil"
21
+ elsif obj.kind_of? Proc
22
+ "<Proc>"
23
+ elsif (obj.kind_of? Array) || (obj.kind_of? String)
24
+ "<#{obj.inspect}>"
25
+ else
26
+ "<#{obj.inspect}:#{obj.class}>"
27
+ end
28
+ end
29
+
30
+ def fail_with_message(message)
31
+ Kernel::raise(Spec::Exceptions::ExpectationNotMetError.new(message))
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,74 @@
1
+ module Spec
2
+
3
+ class ShouldHelper < ShouldBase
4
+
5
+ def initialize(target)
6
+ @target = target
7
+ end
8
+
9
+ def not
10
+ ShouldNegator.new(@target)
11
+ end
12
+
13
+ def have(expected_number=nil)
14
+ HaveHelper.new(@target, expected_number)
15
+ end
16
+
17
+ def satisfy
18
+ fail_with_message "Supplied expectation was not satisfied" if (!yield @target)
19
+ end
20
+
21
+ def equal(expected)
22
+ fail_with_message(default_message("should equal", expected)) unless (@target == expected)
23
+ end
24
+
25
+ def be(expected = :no_arg)
26
+ return self if (expected == :no_arg)
27
+ return if (expected == false and @target.nil?)
28
+ fail_with_message(default_message("should be", expected)) unless (@target.equal?(expected))
29
+ end
30
+
31
+ def a
32
+ self
33
+ end
34
+
35
+ alias an a
36
+
37
+ def instance
38
+ InstanceHelper.new(@target)
39
+ end
40
+
41
+ def kind
42
+ KindHelper.new(@target)
43
+ end
44
+
45
+ def respond
46
+ RespondHelper.new(@target)
47
+ end
48
+
49
+ def method_missing(sym, *args)
50
+ return if @target.send("#{sym}?", *args)
51
+ fail_with_message(default_message("should be #{sym}" + (args.empty? ? '' : (' ' + args.join(', ')))))
52
+ end
53
+
54
+ def match(expected)
55
+ fail_with_message(default_message("should match", expected)) unless (@target =~ expected)
56
+ end
57
+
58
+ def include(sub)
59
+ fail_with_message(default_message("should include", sub)) unless (@target.include? sub)
60
+ end
61
+
62
+ def raise(exception=Exception)
63
+ begin
64
+ @target.call
65
+ fail_with_message(default_message("should raise", exception.inspect))
66
+ rescue exception
67
+ rescue
68
+ fail_with_message(default_message("should raise", exception.inspect))
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,64 @@
1
+ module Spec
2
+
3
+ class ShouldNegator < ShouldBase
4
+
5
+ def initialize(target)
6
+ @target = target
7
+ end
8
+
9
+ def satisfy
10
+ fail_with_message "Supplied expectation was satisfied, but should not have been" if (yield @target)
11
+ end
12
+
13
+ def equal(expected)
14
+ fail_with_message(default_message("should not equal", expected)) if (@target == expected)
15
+ end
16
+
17
+ def be(expected = :no_arg)
18
+ return self if (expected == :no_arg)
19
+ fail_with_message(default_message("should not be", expected)) if (@target.equal?(expected))
20
+ end
21
+
22
+ def a
23
+ self
24
+ end
25
+
26
+ alias an a
27
+
28
+ def instance
29
+ InstanceNegator.new(@target)
30
+ end
31
+
32
+ def kind
33
+ KindNegator.new(@target)
34
+ end
35
+
36
+ def respond
37
+ RespondNegator.new(@target)
38
+ end
39
+
40
+ def match(expected)
41
+ fail_with_message(default_message("should not match", expected)) if (@target =~ expected)
42
+ end
43
+
44
+ def include(sub)
45
+ fail_with_message(default_message("should not include", sub)) if (@target.include? sub)
46
+ end
47
+
48
+ def raise(exception=Exception)
49
+ begin
50
+ @target.call
51
+ rescue exception
52
+ fail_with_message(default_message("should not raise", exception.inspect))
53
+ rescue
54
+ end
55
+ end
56
+
57
+ def method_missing(sym, *args)
58
+ return unless @target.send("#{sym}?", *args)
59
+ fail_with_message(default_message("should not be #{sym}" + (args.empty? ? '' : (' ' + args.join(', ')))))
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,48 @@
1
+ class CollectionWithSizeMethod
2
+
3
+ def initialize
4
+ @list = []
5
+ end
6
+
7
+ def size
8
+ @list.size
9
+ end
10
+
11
+ def push(item)
12
+ @list.push(item)
13
+ end
14
+
15
+ end
16
+
17
+ class CollectionWithLengthMethod
18
+
19
+ def initialize
20
+ @list = []
21
+ end
22
+
23
+ def length
24
+ @list.length
25
+ end
26
+
27
+ def push(item)
28
+ @list.push(item)
29
+ end
30
+
31
+ end
32
+
33
+ class CollectionOwner
34
+ attr_reader :items_in_collection_with_size_method, :items_in_collection_with_length_method
35
+
36
+ def initialize
37
+ @items_in_collection_with_size_method = CollectionWithSizeMethod.new
38
+ @items_in_collection_with_length_method = CollectionWithLengthMethod.new
39
+ end
40
+
41
+ def add_to_collection_with_size_method(item)
42
+ @items_in_collection_with_size_method.push(item)
43
+ end
44
+
45
+ def add_to_collection_with_length_method(item)
46
+ @items_in_collection_with_length_method.push(item)
47
+ end
48
+ end
@@ -9,32 +9,32 @@ class TestCon < Spec::Context
9
9
  end
10
10
 
11
11
  def passing_once_specification
12
- true.should_equal(true)
12
+ true.should.equal(true)
13
13
  end
14
14
 
15
15
  def failing_once_specification
16
- false.should_equal(true)
16
+ false.should.equal(true)
17
17
  end
18
18
 
19
19
  def erroring_once_specification
20
- undefined_method.should_equal(true)
20
+ undefined_method.should.equal(true)
21
21
  end
22
22
 
23
23
  def passing_multi_specification
24
- true.should_equal(true)
25
- false.should_equal(false)
26
- Object.should_equal(Object)
24
+ true.should.equal(true)
25
+ false.should.equal(false)
26
+ Object.should.equal(Object)
27
27
  end
28
28
 
29
29
  def failing_multi_specification
30
- false.should_equal(true)
31
- true.should_equal(false)
32
- false.should_equal(nil)
30
+ false.should.equal(true)
31
+ true.should.equal(false)
32
+ false.should.equal(nil)
33
33
  end
34
34
 
35
35
  def erroring_multi_specification
36
- undefined_method.should_equal(false)
37
- undefined_method.should_not_equal(true)
36
+ undefined_method.should.equal(false)
37
+ undefined_method.should.not.equal(true)
38
38
  end
39
39
 
40
40
  end
@@ -1,123 +1,103 @@
1
1
  require 'test/unit'
2
2
 
3
+ require 'collection_owner'
3
4
  require 'spec'
4
5
 
5
-
6
6
  class ErrorReportingContext < Spec::Context
7
7
 
8
- # should
9
-
10
- def use_the_standard_message_for_should
11
- Object.should { false == true }
8
+ def should_satisfy
9
+ 5.should.satisfy { |x| x == 3 }
12
10
  end
13
11
 
14
- def use_the_provided_message_for_should
15
- Object.should("provided message") { false == true }
12
+ def should_not_satisfy
13
+ 3.should.not.satisfy { |x| x == 3 }
16
14
  end
17
-
18
- # should_equal
19
15
 
20
- def use_the_standard_message_for_should_equal
21
- Object.should_equal Class
16
+ def should_equal
17
+ Object.should.equal Class
22
18
  end
23
19
 
24
- def use_the_provided_message_for_should_equal
25
- Object.should_equal(Class, "provided for should_equal")
20
+ def should_not_equal
21
+ Object.should.not.equal Object
26
22
  end
27
23
 
28
- # should_not_equal
29
-
30
- def use_the_standard_message_for_should_not_equal
31
- Object.should_not_equal Object
24
+ def should_be_nil
25
+ Object.should.be nil
32
26
  end
33
27
 
34
- def use_the_provided_message_for_should_not_equal
35
- Object.should_not_equal(Object, "provided for should_not_equal")
28
+ def should_not_be_nil
29
+ nil.should.not.be nil
36
30
  end
37
31
 
38
- # should_be_nil
32
+ def should_be_empty
33
+ ['foo', 'bar'].should.be.empty
34
+ end
39
35
 
40
- def use_the_standard_message_for_should_be_nil
41
- Object.should_be_nil
36
+ def should_not_be_empty
37
+ [].should.not.be.empty
42
38
  end
43
39
 
44
- def use_the_provided_message_for_should_be_nil
45
- Object.should_be_nil("provided for should_be_nil")
40
+ def should_include
41
+ ['foo'].should.include('bar')
46
42
  end
47
43
 
48
- # should_not_be_nil
49
-
50
- def use_the_standard_message_for_should_not_be_nil
51
- nil.should_not_be_nil
44
+ def should_not_include
45
+ ['foo'].should.not.include('foo')
52
46
  end
53
47
 
54
- def use_the_provided_message_for_should_not_be_nil
55
- nil.should_not_be_nil("provided for should_not_be_nil")
48
+ def should_be_true
49
+ false.should.be true
56
50
  end
57
51
 
58
- # should_be_empty
59
-
60
- def use_the_standard_message_for_should_be_empty
61
- ['foo', 'bar'].should_be_empty
52
+ def should_be_false
53
+ true.should.be false
62
54
  end
63
55
 
64
- def use_the_provided_message_for_should_be_empty
65
- ['foo', 'bar'].should_be_empty("provided for should_be_empty")
56
+ def should_raise_with_no_error
57
+ proc { ''.to_s }.should.raise NoMethodError
66
58
  end
67
-
68
- # should_not_be_empty
69
59
 
70
- def use_the_standard_message_for_should_not_be_empty
71
- [].should_not_be_empty
72
- end
73
-
74
- def use_the_provided_message_for_should_not_be_empty
75
- [].should_not_be_empty("provided for should_not_be_empty")
60
+ def should_raise_with_wrong_error
61
+ proc { ''.no_method }.should.raise SyntaxError
76
62
  end
77
-
78
- # should_include
79
63
 
80
- def use_the_standard_message_for_should_include
81
- ['foo'].should_include('bar')
82
- end
64
+ def should_not_raise
65
+ proc { ''.no_method }.should.not.raise NoMethodError
66
+ end
83
67
 
84
- def use_the_provided_message_for_should_include
85
- ['foo'].should_include('bar', "provided for should_include")
68
+ def should_have_with_length
69
+ owner = CollectionOwner.new
70
+ owner.should.have(3).items_in_collection_with_length_method
86
71
  end
87
72
 
88
- # should_not_include
89
-
90
- def use_the_standard_message_for_should_not_include
91
- ['foo'].should_not_include('foo')
73
+ def should_have_at_least_with_length
74
+ owner = CollectionOwner.new
75
+ owner.should.have.at.least(3).items_in_collection_with_length_method
92
76
  end
93
77
 
94
- def use_the_provided_message_for_should_not_include
95
- ['foo'].should_not_include('foo', "provided for should_not_include")
78
+ def should_have_at_most_with_length
79
+ owner = CollectionOwner.new
80
+ (1..4).each { |n| owner.add_to_collection_with_length_method n }
81
+ owner.should.have.at.most(3).items_in_collection_with_length_method
96
82
  end
97
-
98
- # should_be_true
99
83
 
100
- def use_the_standard_message_for_should_be_true
101
- false.should_be_true
84
+ def should_have_with_size
85
+ owner = CollectionOwner.new
86
+ owner.should.have(3).items_in_collection_with_size_method
102
87
  end
103
88
 
104
- def use_the_provided_message_for_should_be_true
105
- false.should_be_true("provided for should_be_true")
89
+ def should_have_at_least_with_size
90
+ owner = CollectionOwner.new
91
+ owner.should.have.at.least(3).items_in_collection_with_size_method
106
92
  end
107
93
 
108
- # should_be_false
109
-
110
- def use_the_standard_message_for_should_be_false
111
- true.should_be_false
94
+ def should_have_at_most_with_size
95
+ owner = CollectionOwner.new
96
+ (1..4).each { |n| owner.add_to_collection_with_size_method n }
97
+ owner.should.have.at.most(3).items_in_collection_with_size_method
112
98
  end
113
-
114
- def use_the_provided_message_for_should_be_false
115
- true.should_be_false("provided for should_be_false")
116
- end
117
-
118
99
  end
119
100
 
120
-
121
101
  class ErrorReportingRunner
122
102
 
123
103
  def initialize
@@ -158,114 +138,88 @@ class ErrorReportingTest < Test::Unit::TestCase
158
138
  @runner.run(ErrorReportingContext)
159
139
  end
160
140
 
161
- # should
162
-
163
- def test_should_report_standard_message_for_should
164
- assert @runner.dump_failures.include?("Expectation not met.")
141
+ def test_should_report_message_for_should_satisfy
142
+ assert @runner.dump_failures.include?("Supplied expectation was not satisfied")
165
143
  end
166
144
 
167
- def test_should_report_provided_message_for_should
168
- assert @runner.dump_failures.include?("provided message")
169
- end
145
+ def test_should_report_message_for_should_equal
146
+ assert @runner.dump_failures.include?("<Object:Class> should equal <Class:Class>"), @runner.dump_failures
147
+ end
170
148
 
171
- # should_equal
149
+ def test_should_report_message_for_should_not_equal
150
+ assert @runner.dump_failures.include?("<Object:Class> should not equal <Object:Class>"), @runner.dump_failures
151
+ end
172
152
 
173
- def test_should_report_standard_message_for_should_equal
174
- assert @runner.dump_failures.include?("<Object:Class>\nshould be equal to:\n<Class:Class>"), @runner.dump_failures
153
+ def test_should_report_message_for_should_be_nil
154
+ assert @runner.dump_failures.include?("<Object:Class> should be nil")
175
155
  end
176
156
 
177
- def test_should_report_provided_message_for_should_equal
178
- assert @runner.dump_failures.include?("provided for should_equal"), @runner.dump_failures
157
+ def test_should_report_message_for_should_not_be_nil
158
+ assert @runner.dump_failures.include?("nil should not be nil")
179
159
  end
180
-
181
- # should_not_equal
182
160
 
183
- def test_should_report_standard_message_for_should_not_equal
184
- assert @runner.dump_failures.include?("<Object:Class>\nshould not be equal to:\n<Object:Class>"), @runner.dump_failures
161
+ def test_should_report_message_for_should_be_empty
162
+ assert @runner.dump_failures.include?('<["foo", "bar"]> should be empty')
185
163
  end
186
-
187
- def test_should_report_provided_message_for_should_not_equal
188
- assert @runner.dump_failures.include?("provided for should_not_equal"), @runner.dump_failures
164
+
165
+ def test_should_report_message_for_should_not_be_empty
166
+ assert @runner.dump_failures.include?("<[]> should not be empty")
189
167
  end
190
-
191
- # should_be_nil
192
-
193
- def test_should_report_standard_message_for_should_be_nil
194
- assert @runner.dump_failures.include?("<Object> should be nil")
168
+
169
+ def test_should_report_message_for_should_include
170
+ assert @runner.dump_failures.include?('<["foo"]> should include <"bar">')
195
171
  end
196
172
 
197
- def test_should_report_provided_message_fro_should_be_nil
198
- assert @runner.dump_failures.include?("provided for should_be_nil")
173
+ def test_should_report_message_for_should_not_include
174
+ assert @runner.dump_failures.include?('<["foo"]> should not include <"foo">')
199
175
  end
200
176
 
201
- # should_not_be_nil
177
+ def test_should_report_message_for_should_be_true
178
+ assert @runner.dump_failures.include?("<false> should be <true>")
179
+ end
202
180
 
203
- def test_should_report_standard_message_for_should_not_be_nil
204
- assert @runner.dump_failures.include?("<> should not be nil")
181
+ def test_should_report_message_for_should_be_false
182
+ assert @runner.dump_failures.include?("<true> should be <false>")
205
183
  end
206
184
 
207
- def test_should_report_provided_message_for_should_not_be_nil
208
- assert @runner.dump_failures.include?("provided for should_not_be_nil")
185
+ def test_should_report_message_for_should_raise_with_no_error
186
+ assert @runner.dump_failures.include?("<Proc> should raise <\"NoMethodError\">")
209
187
  end
210
-
211
- # should_be_empty
212
188
 
213
- def test_should_report_standard_message_for_should_be_empty
214
- assert @runner.dump_failures.include?('<["foo", "bar"]> should be empty')
189
+ def test_should_report_message_for_should_raise_with_wrong_error
190
+ assert @runner.dump_failures.include?("<Proc> should raise <\"SyntaxError\">")
215
191
  end
216
192
 
217
- def test_should_report_provided_message_for_should_be_empty
218
- assert @runner.dump_failures.include?("provided for should_be_empty")
193
+ def test_should_report_message_for_should_not_raise
194
+ assert @runner.dump_failures.include?("<Proc> should not raise <\"NoMethodError\">")
219
195
  end
220
196
 
221
- # should_not_be_empty
222
-
223
- def test_should_report_standard_message_for_should_not_be_empty
224
- assert @runner.dump_failures.include?("<[]> should not be empty")
197
+ def test_should_report_message_for_should_not_raise
198
+ assert @runner.dump_failures.include?("<Proc> should not raise <\"NoMethodError\">")
225
199
  end
226
200
 
227
- def test_should_report_provided_message_for_should_not_be_empty
228
- assert @runner.dump_failures.include?("provided for should_not_be_empty")
201
+ def test_should_report_message_for_should_have_with_length
202
+ assert @runner.dump_failures.include?('<CollectionOwner> should have 3 items_in_collection_with_length_method (has 0)')
229
203
  end
230
-
231
- # should_include
232
204
 
233
- def test_should_report_standard_message_for_should_include
234
- assert @runner.dump_failures.include?('<["foo"]> should include <"bar">')
205
+ def test_should_report_message_for_should_have_at_least_with_length
206
+ assert @runner.dump_failures.include?('<CollectionOwner> should have at least 3 items_in_collection_with_length_method (has 0)')
235
207
  end
236
208
 
237
- def test_should_report_provided_message_for_should_include
238
- assert @runner.dump_failures.include?("provided for should_include")
209
+ def test_should_report_message_for_should_have_at_most_with_length
210
+ assert @runner.dump_failures.include?('<CollectionOwner> should have at most 3 items_in_collection_with_length_method (has 4)')
239
211
  end
240
212
 
241
- # should_not_include
242
-
243
- def test_should_report_standard_message_for_should_not_include
244
- assert @runner.dump_failures.include?('<["foo"]> should not include <"foo">')
245
- end
246
-
247
- def test_should_report_provided_message_for_should_not_include
248
- assert @runner.dump_failures.include?("provided for should_not_include")
213
+ def test_should_report_message_for_should_have_with_size
214
+ assert @runner.dump_failures.include?('<CollectionOwner> should have 3 items_in_collection_with_size_method (has 0)')
249
215
  end
250
216
 
251
- # should_be_true
252
-
253
- def test_should_report_standard_message_for_should_be_true
254
- assert @runner.dump_failures.include?("<false> should be true")
255
- end
256
-
257
- def test_should_report_provided_message_for_should_be_true
258
- assert @runner.dump_failures.include?("provided for should_be_true")
217
+ def test_should_report_message_for_should_have_at_least_with_size
218
+ assert @runner.dump_failures.include?('<CollectionOwner> should have at least 3 items_in_collection_with_size_method (has 0)')
259
219
  end
260
220
 
261
- # should_be_false
262
-
263
- def test_should_report_standard_message_for_should_be_false
264
- assert @runner.dump_failures.include?("<true> should be false")
265
- end
266
-
267
- def test_should_report_provided_message_for_should_be_false
268
- assert @runner.dump_failures.include?("provided for should_be_false")
221
+ def test_should_report_message_for_should_have_at_most_with_size
222
+ assert @runner.dump_failures.include?('<CollectionOwner> should have at most 3 items_in_collection_with_size_method (has 4)')
269
223
  end
270
-
224
+
271
225
  end