rspec 0.5.16 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGES +12 -0
  2. data/EXAMPLES.rd +2 -2
  3. data/examples/custom_method_spec.rb +2 -2
  4. data/examples/mocking_spec.rb +2 -2
  5. data/lib/spec/api/helper.rb +0 -6
  6. data/lib/spec/api/helper/diff.rb +1 -0
  7. data/lib/spec/api/helper/have_helper.rb +7 -23
  8. data/lib/spec/api/helper/should_helper.rb +23 -19
  9. data/lib/spec/api/helper/should_negator.rb +10 -18
  10. data/lib/spec/api/mocks/argument_expectation.rb +2 -2
  11. data/lib/spec/api/mocks/message_expectation.rb +28 -53
  12. data/lib/spec/api/mocks/mock.rb +4 -5
  13. data/lib/spec/api/sugar.rb +6 -23
  14. data/lib/spec/runner/instance_exec.rb +11 -5
  15. data/lib/spec/version.rb +2 -2
  16. data/test/spec/api/helper/arbitrary_predicate_test.rb +16 -16
  17. data/test/spec/api/helper/containment_test.rb +16 -16
  18. data/test/spec/api/helper/identity_test.rb +8 -8
  19. data/test/spec/api/helper/object_equality_test.rb +13 -13
  20. data/test/spec/api/helper/raising_test.rb +14 -14
  21. data/test/spec/api/helper/regex_matching_test.rb +4 -4
  22. data/test/spec/api/helper/should_have_test.rb +26 -20
  23. data/test/spec/api/helper/should_satisfy_test.rb +5 -5
  24. data/test/spec/api/helper/throwing_test.rb +7 -7
  25. data/test/spec/api/helper/true_false_special_case_test.rb +12 -12
  26. data/test/spec/api/helper/typing_test.rb +14 -14
  27. data/test/spec/api/mocks/mock_arg_constraints_test.rb +26 -20
  28. data/test/spec/api/mocks/mock_counts_test.rb +431 -0
  29. data/test/spec/api/mocks/mock_ordering_test.rb +61 -42
  30. data/test/spec/api/mocks/mock_test.rb +68 -230
  31. data/test/spec/api/sugar_test.rb +1 -1
  32. data/test/spec/runner/backtrace_tweaker_test.rb +2 -2
  33. data/test/spec/runner/context_runner_test.rb +4 -4
  34. data/test/spec/runner/context_test.rb +22 -22
  35. data/test/spec/runner/execution_context_test.rb +1 -1
  36. data/test/spec/runner/reporter_test.rb +6 -6
  37. data/test/spec/runner/specification_test.rb +30 -30
  38. data/test/test_classes.rb +13 -4
  39. metadata +6 -11
  40. data/lib/spec/api/helper/instance_helper.rb +0 -15
  41. data/lib/spec/api/helper/instance_negator.rb +0 -15
  42. data/lib/spec/api/helper/kind_helper.rb +0 -15
  43. data/lib/spec/api/helper/kind_negator.rb +0 -15
  44. data/lib/spec/api/helper/respond_helper.rb +0 -15
  45. data/lib/spec/api/helper/respond_negator.rb +0 -15
  46. data/test/spec/api/duck_type_test.rb +0 -19
data/CHANGES CHANGED
@@ -1,5 +1,17 @@
1
1
  = RSpec Changelog
2
2
 
3
+ == Version 0.6.0
4
+ This release makes an official commitment to underscore_syntax (with no more support for dot.syntax)
5
+
6
+ * Fixed bug (5292) that caused mock argument matching to fail on classes that overrided ==
7
+ * Converted ALL tests to use underscore syntax
8
+ * Fixed all remaining problems with underscores revealed by converting all the tests to underscores
9
+ * Enhanced sugar to support combinations of methods (i.e. once.and_return)
10
+ * Simplified helper structure taking advantage of dot/underscore combos (i.e. should.be.an_instance_of, which can be expressed as should_be_an_instance_of)
11
+ * Added support for at_most in mocks
12
+ * Added support for should_not_receive(:msg) (will be removing should_receive(:msg).never some time soon)
13
+ * Added support for should_have_exactly(5).items_in_collection
14
+
3
15
  == Version 0.5.16
4
16
  This release improves Rails support and test2spec translation.
5
17
 
@@ -14,8 +14,8 @@
14
14
  # An IoProcessor
15
15
  # * should raise nothing when the file is exactly 32 bytes
16
16
  # * should raise an exception when the file length is less than 32 bytes
17
- # Mocker
18
- # * should be able to call mock()
17
+ # A consumer of a mock
18
+ # * should be able to send messages to the mock
19
19
  # An empty stack
20
20
  # * should accept an item when sent push
21
21
  # * should complain when sent top
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../lib/spec'
3
3
  context "Rspec allow you to define custom methods" do
4
4
  specify "Rspec should allow you to define methods" do
5
5
  a_method
6
- @a_method_called.should.be true
6
+ @a_method_called.should_be true
7
7
  end
8
8
 
9
9
  def a_method
@@ -15,7 +15,7 @@ require File.dirname(__FILE__) + '/../lib/spec'
15
15
  context "Rspec allow you to define custom methods" do
16
16
  specify "Rspec should allow you to define methods" do
17
17
  a_method
18
- @a_method_called.should.be true
18
+ @a_method_called.should_be true
19
19
  end
20
20
 
21
21
  def a_method
@@ -1,8 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/../lib/spec'
2
2
 
3
- context "Mocker" do
3
+ context "A consumer of a mock" do
4
4
 
5
- specify "should be able to call mock()" do
5
+ specify "should be able to send messages to the mock" do
6
6
  mock = mock("poke me")
7
7
  mock.should_receive(:poke)
8
8
  mock.poke
@@ -1,10 +1,4 @@
1
1
  require 'spec/api/helper/should_base'
2
2
  require 'spec/api/helper/have_helper'
3
- require 'spec/api/helper/instance_helper'
4
- require 'spec/api/helper/instance_negator'
5
- require 'spec/api/helper/kind_helper'
6
- require 'spec/api/helper/kind_negator'
7
- require 'spec/api/helper/respond_helper'
8
- require 'spec/api/helper/respond_negator'
9
3
  require 'spec/api/helper/should_helper'
10
4
  require 'spec/api/helper/should_negator'
@@ -1,5 +1,6 @@
1
1
  begin
2
2
  require 'rubygems'
3
+ require 'diff/lcs' #necessary to do loading bug on some machines - not sure why - DaC
3
4
  require 'diff/lcs/hunk'
4
5
  rescue LoadError
5
6
  raise "You must gem install diff-lcs to use this feature"
@@ -2,11 +2,11 @@ module Spec
2
2
 
3
3
  class HaveHelper < ShouldBase
4
4
 
5
- def initialize(target, expected=nil)
5
+ def initialize(target, relativity=:exactly, expected=nil)
6
6
  @target = target
7
7
  @expected = expected == :no ? 0 : expected
8
- @min = false
9
- @max = false
8
+ @at_least = (relativity == :at_least)
9
+ @at_most = (relativity == :at_most)
10
10
  end
11
11
 
12
12
  def method_missing(sym, *args)
@@ -24,33 +24,17 @@ module Spec
24
24
 
25
25
  def build_message(sym)
26
26
  message = "<#{@target.class.to_s}> should have"
27
- message += " at least" if @min
28
- message += " at most" if @max
27
+ message += " at least" if @at_least
28
+ message += " at most" if @at_most
29
29
  message += " #{@expected} #{sym} (has #{actual_size(collection(sym))})"
30
30
  end
31
31
 
32
32
  def as_specified?(sym)
33
- return actual_size(collection(sym)) >= @expected if @min
34
- return actual_size(collection(sym)) <= @expected if @max
33
+ return actual_size(collection(sym)) >= @expected if @at_least
34
+ return actual_size(collection(sym)) <= @expected if @at_most
35
35
  return actual_size(collection(sym)) == @expected
36
36
  end
37
37
 
38
- def at
39
- self
40
- end
41
-
42
- def least(min)
43
- @expected = min
44
- @min = true
45
- self
46
- end
47
-
48
- def most(max)
49
- @expected = max
50
- @max = true
51
- self
52
- end
53
-
54
38
  end
55
39
 
56
40
  end
@@ -11,7 +11,19 @@ module Spec
11
11
  end
12
12
 
13
13
  def have(expected_number=nil)
14
- HaveHelper.new(@target, expected_number)
14
+ HaveHelper.new(@target, :exactly, expected_number)
15
+ end
16
+
17
+ def have_exactly(expected_number=nil)
18
+ HaveHelper.new(@target, :exactly, expected_number)
19
+ end
20
+
21
+ def have_at_least(expected_number=nil)
22
+ HaveHelper.new(@target, :at_least, expected_number)
23
+ end
24
+
25
+ def have_at_most(expected_number=nil)
26
+ HaveHelper.new(@target, :at_most, expected_number)
15
27
  end
16
28
 
17
29
  def satisfy(&block)
@@ -24,48 +36,40 @@ module Spec
24
36
  end
25
37
 
26
38
  def be(expected = :___no_arg)
39
+ @be_seen = true
27
40
  return self if (expected == :___no_arg)
28
41
  return if (expected == false and @target.nil?)
29
42
  return if (expected == true and (!@target.nil?) and (@target != false))
30
43
  fail_with_message(default_message("should be", expected)) unless (@target.equal?(expected))
31
44
  end
32
45
 
33
- def a
34
- self
46
+ def an_instance_of expected_class
47
+ fail_with_message(default_message("should be an instance of", expected_class)) unless @target.instance_of? expected_class
35
48
  end
36
49
 
37
- alias an a
38
-
39
- def instance
40
- InstanceHelper.new(@target)
41
- end
42
-
43
- def kind
44
- KindHelper.new(@target)
50
+ def a_kind_of expected_class
51
+ fail_with_message(default_message("should be a kind of", expected_class)) unless @target.kind_of? expected_class
45
52
  end
46
53
 
47
- def respond
48
- RespondHelper.new(@target)
54
+ def respond_to message
55
+ fail_with_message(default_message("should respond to", message)) unless @target.respond_to? message
49
56
  end
50
57
 
51
58
  def method_missing(sym, *args)
52
59
  return if @target.send("#{sym}?", *args)
53
- fail_with_message(default_message("should be #{sym}" + (args.empty? ? '' : (' ' + args.join(', ')))))
60
+ fail_with_message(default_message("should be #{sym}" + (args.empty? ? '' : (' ' + args.join(', '))))) if @be_seen
61
+ fail_with_message(default_message("should #{sym}" + (args.empty? ? '' : (' ' + args.join(', '))))) unless @be_seen
54
62
  end
55
63
 
56
64
  def match(expected)
57
65
  fail_with_message(default_message("should match", expected)) unless (@target =~ expected)
58
66
  end
59
67
 
60
- def include(sub)
61
- fail_with_message(default_message("should include", sub)) unless (@target.include? sub)
62
- end
63
-
64
68
  def raise(exception=Exception, message=nil)
65
69
  begin
66
70
  @target.call
67
71
  rescue exception => e
68
- e.message.should.equal message unless message.nil?
72
+ e.message.should_equal message unless message.nil?
69
73
  return
70
74
  rescue => e
71
75
  fail_with_message("#{default_message("should raise", exception)} but raised #{e.inspect}")
@@ -15,36 +15,27 @@ module Spec
15
15
  end
16
16
 
17
17
  def be(expected = :no_arg)
18
+ @be_seen = true
18
19
  return self if (expected == :no_arg)
19
20
  fail_with_message(default_message("should not be", expected)) if (@target.equal?(expected))
20
21
  end
21
22
 
22
- def a
23
- self
23
+ def an_instance_of expected_class
24
+ fail_with_message(default_message("should not be an instance of", expected_class)) if @target.instance_of? expected_class
24
25
  end
25
26
 
26
- alias an a
27
-
28
- def instance
29
- InstanceNegator.new(@target)
30
- end
31
-
32
- def kind
33
- KindNegator.new(@target)
27
+ def a_kind_of expected_class
28
+ fail_with_message(default_message("should not be a kind of", expected_class)) if @target.kind_of? expected_class
34
29
  end
35
30
 
36
- def respond
37
- RespondNegator.new(@target)
38
- end
31
+ def respond_to message
32
+ fail_with_message(default_message("should not respond to", message)) if @target.respond_to? message
33
+ end
39
34
 
40
35
  def match(expected)
41
36
  fail_with_message(default_message("should not match", expected)) if (@target =~ expected)
42
37
  end
43
38
 
44
- def include(sub)
45
- fail_with_message(default_message("should not include", sub)) if (@target.include? sub)
46
- end
47
-
48
39
  def raise(exception=Exception, message=nil)
49
40
  begin
50
41
  @target.call
@@ -71,7 +62,8 @@ module Spec
71
62
 
72
63
  def method_missing(sym, *args)
73
64
  return unless @target.send("#{sym}?", *args)
74
- fail_with_message(default_message("should not be #{sym}" + (args.empty? ? '' : (' ' + args.join(', ')))))
65
+ fail_with_message(default_message("should not #{sym}" + (args.empty? ? '' : (' ' + args.join(', '))))) unless @be_seen
66
+ fail_with_message(default_message("should not be #{sym}" + (args.empty? ? '' : (' ' + args.join(', '))))) if @be_seen
75
67
  end
76
68
 
77
69
  end
@@ -68,8 +68,8 @@ module Spec
68
68
  @@constraint_classes[:string] = StringArgConstraint
69
69
 
70
70
  def initialize(args)
71
- if args == [:any_args] then @expected_params = nil
72
- elsif args == [:no_args] then @expected_params = []
71
+ if [:any_args] == args then @expected_params = nil
72
+ elsif [:no_args] == args then @expected_params = []
73
73
  else @expected_params = process_arg_constraints(args)
74
74
  end
75
75
  end
@@ -16,9 +16,6 @@ module Spec
16
16
  @consecutive = false
17
17
  @exception_to_raise = nil
18
18
  @symbol_to_throw = nil
19
- @any_seen = false
20
- @at_seen = false
21
- @and_seen = false
22
19
  @ordering = expectation_ordering
23
20
  @ordered = false
24
21
  end
@@ -45,7 +42,8 @@ module Spec
45
42
  # Error msg should tell exactly what went wrong. (AH).
46
43
 
47
44
  return if @expected_received_count == :any
48
- return if (@expected_received_count < 0) && (@received_count >= @expected_received_count.abs)
45
+ return if (@at_least) && (@received_count >= @expected_received_count)
46
+ return if (@at_most) && (@received_count <= @expected_received_count)
49
47
  return if @expected_received_count == @received_count
50
48
 
51
49
  count_message = make_count_message(@expected_received_count)
@@ -122,49 +120,40 @@ module Spec
122
120
  self
123
121
  end
124
122
 
125
- def at
126
- @at_seen = true
127
- self
128
- end
129
-
130
123
  def exactly(n)
131
- @expected_received_count = n
124
+ set_expected_received_count :exactly, n
132
125
  self
133
126
  end
134
127
 
135
- def least(arg)
136
- if @at_seen
137
- @expected_received_count = -1 if arg == :once
138
- @expected_received_count = -2 if arg == :twice
139
- @expected_received_count = -arg if arg.kind_of? Numeric
140
- end
141
- @at_seen = false
142
- self
143
- end
144
-
145
- def any
146
- @any_seen = true
128
+ def at_least(n)
129
+ set_expected_received_count :at_least, n
147
130
  self
148
131
  end
149
132
 
150
- def number
151
- @number_seen = @any_seen
152
- @any_seen = false
133
+ def at_most(n)
134
+ set_expected_received_count :at_most, n
153
135
  self
154
136
  end
155
137
 
156
- def of
157
- @of_seen = @number_seen
158
- @number_seen = false
159
- self
138
+ def set_expected_received_count relativity, n
139
+ @at_least = (relativity == :at_least)
140
+ @at_most = (relativity == :at_most)
141
+ @expected_received_count = 1 if n == :once
142
+ @expected_received_count = 2 if n == :twice
143
+ @expected_received_count = n if n.kind_of? Numeric
160
144
  end
161
-
145
+
162
146
  def times
163
- @expected_received_count = :any if @of_seen
164
- @of_seen = false
147
+ #pure sugar
165
148
  self
166
149
  end
167
150
 
151
+ def any_number_of_times
152
+ @expected_received_count = :any
153
+ self
154
+ end
155
+
156
+ #TODO - replace this w/ not syntax in mock
168
157
  def never
169
158
  @expected_received_count = 0
170
159
  self
@@ -180,43 +169,29 @@ module Spec
180
169
  self
181
170
  end
182
171
 
183
- def and
184
- @and_seen = true
185
- self
186
- end
187
-
188
- def return(value=nil, &return_block)
172
+ def and_return(value=nil, &return_block)
189
173
  Kernel::raise AmbiguousReturnError unless @method_block.nil?
190
- return self unless @and_seen
191
- @and_seen = false
192
174
  @consecutive = value.instance_of? Array
193
175
  @return_block = block_given? ? return_block : lambda { value }
194
176
  end
195
177
 
196
- def raise(exception=Exception)
197
- return self unless @and_seen
198
- @and_seen = false
178
+ def and_raise(exception=Exception)
199
179
  @exception_to_raise = exception
200
180
  end
201
181
 
202
- def throw(symbol)
203
- return self unless @and_seen
204
- @and_seen = false
182
+ def and_throw(symbol)
205
183
  @symbol_to_throw = symbol
206
184
  end
207
185
 
186
+ def and_yield(*args)
187
+ @args_to_yield = args
188
+ end
189
+
208
190
  def ordered
209
191
  @ordering.register(self)
210
192
  @ordered = true
211
193
  self
212
194
  end
213
-
214
- def yield(*args)
215
- return self unless @and_seen
216
- @and_seen = false
217
- @args_to_yield = args
218
- end
219
-
220
195
  end
221
196
  end
222
197
  end
@@ -16,16 +16,15 @@ module Spec
16
16
  @expectation_ordering = OrderGroup.new
17
17
  end
18
18
 
19
- def should
20
- self
21
- end
22
-
23
- def receive(sym, &block)
19
+ def should_receive(sym, &block)
24
20
  expected_from = caller(1)[0]
25
21
  expectation = MessageExpectation.new(@name, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
26
22
  @expectations << expectation
27
23
  expectation
28
24
  end
25
+
26
+ def should_not_receive(sym, &block)
27
+ end
29
28
 
30
29
  def __verify #:nodoc:
31
30
  @expectations.each do |expectation|
@@ -1,13 +1,13 @@
1
1
  module Spec
2
2
  module Api
3
3
  # This module adds syntactic sugar that allows usage of should_* instead of should.*
4
- module Sugar
4
+ module UnderscoreSugar
5
5
  module SugarizeForRspec; end
6
6
 
7
7
  def sugarize_for_rspec!
8
8
  original_method_missing = instance_method(:method_missing)
9
9
  class_eval do
10
- include SugarizeForRspec # This is meant to add a signiture to the object that sugarization occurred.
10
+ include SugarizeForRspec # This is meant to add a signature to the object that sugarization occurred.
11
11
  def method_missing(sym, *args, &block)
12
12
  _method_missing(sym, args, block)
13
13
  end
@@ -18,9 +18,10 @@ module Spec
18
18
  object = self
19
19
  calls = sym.to_s.split("_")
20
20
  while calls.length > 1
21
+ break if (object.respond_to?calls.join("_"))
21
22
  call = calls.shift
22
23
  object = object.__send__(call)
23
- break if call == "be" unless ["an","a"].include? calls[0]
24
+ break if call == "be"
24
25
  end
25
26
  return object.__send__(calls.join("_"), *args, &block)
26
27
  end
@@ -31,29 +32,11 @@ module Spec
31
32
  end
32
33
  end
33
34
  end
34
-
35
- module MessageExpectationSugar
36
- def __is_sweetened?(sym) #:nodoc:
37
- return true if sym.to_s =~ /^and_|^at_|^any_|^once_/
38
- end
39
- end
40
35
  end
41
36
  end
42
37
 
43
38
  class Module
44
- include Spec::Api::Sugar
45
- end
46
-
47
- Object.sugarize_for_rspec!
48
-
49
- class Spec::Api::Mock #:nodoc:
50
- # NOTE: this resolves a bug caused by a conflict between Sugar#method_missing and Mock#method_missing, specifically
51
- # when the mock is set null_object=>true. It would be nice to get rid of this.
52
- def should_receive(sym, &block)
53
- return receive(sym, &block)
54
- end
39
+ include Spec::Api::UnderscoreSugar
55
40
  end
56
41
 
57
- class Spec::Api::MessageExpectation #:nodoc:
58
- include Spec::Api::MessageExpectationSugar
59
- end
42
+ Object.sugarize_for_rspec!