rspec-expectations 2.0.1 → 2.1.0

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.
data/Gemfile CHANGED
@@ -1,13 +1,14 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ %w[rspec-core rspec-expectations rspec-mocks].each do |lib|
4
+ gem lib, :path => File.expand_path("../../#{lib}", __FILE__)
5
+ end
6
+
3
7
  gem "rake"
4
- gem "cucumber"
5
- gem "aruba", ">= 0.2.0"
8
+ gem "cucumber", "0.8.5"
9
+ gem "aruba", "0.2.2"
6
10
  gem "autotest"
7
11
  gem "diff-lcs"
8
- gem "rspec-expectations", :path => "."
9
- gem "rspec-core", :path => "../rspec-core"
10
- gem "rspec-mocks", :path => "../rspec-mocks"
11
12
  gem "watchr"
12
13
  gem "rcov"
13
14
  gem "relish"
@@ -1,11 +1,21 @@
1
1
  ## rspec-expectations release history (incomplete)
2
2
 
3
- ### 2.0.1 / 2010-10-18
3
+ ### 2.1.0 / 2010-11-07
4
4
 
5
- [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0...v2.0.1)
5
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.1...v2.1.0)
6
6
 
7
7
  * Enhancements
8
- * Make dependencies on other rspec gems consistent across gems
8
+ * be_within(delta).of(expected) matcher (Myron Marston)
9
+ * Lots of new Cucumber features (Myron Marston)
10
+ * Raise error if you try "should != expected" on Ruby-1.9 (Myron Marston)
11
+ * Improved failure messages from throw_symbol (Myron Marston)
12
+
13
+ * Bug fixes
14
+ * Eliminate hard dependency on RSpec::Core (Myron Marston)
15
+ * have_matcher - use pluralize only when ActiveSupport inflections are indeed
16
+ defined (Josep M Bach)
17
+ * throw_symbol matcher no longer swallows exceptions (Myron Marston)
18
+ * fix matcher chaining to avoid name collisions (Myron Marston)
9
19
 
10
20
  ### 2.0.0 / 2010-10-10
11
21
 
@@ -3,6 +3,16 @@
3
3
  rspec-expectations adds `should` and `should_not` to every object and includes
4
4
  RSpec::Matchers, a library of standard matchers.
5
5
 
6
+ ## Documentation
7
+
8
+ * [Cucumber features](http://relishapp.com/rspec/rspec-expectations/v/2-0)
9
+ * [RDoc](http://rubydoc.info/gems/rspec-expectations/2.0.1/frames)
10
+
11
+ ## Install
12
+
13
+ gem install rspec # for rspec-core, rspec-expectations, rspec-mocks
14
+ gem install rspec-expecctations # for rspec-core only
15
+
6
16
  ## Matchers
7
17
 
8
18
  Matchers are objects used to compose expectations:
data/Rakefile CHANGED
@@ -58,7 +58,7 @@ end
58
58
  desc "Push cukes to relishapp using the relish-client-gem"
59
59
  task :relish, :version do |t, args|
60
60
  raise "rake relish[VERSION]" unless args[:version]
61
- sh "bundle exec relish --organization rspec --project rspec-expectations -v #{args[:version]} push"
61
+ sh "bundle exec relish push --organization rspec --project rspec-expectations -v #{args[:version]}"
62
62
  end
63
63
 
64
64
  task :clobber do
@@ -0,0 +1,43 @@
1
+ Feature: be_within matcher
2
+
3
+ Normal equality expectations do not work well for floating point values.
4
+ Consider this irb session:
5
+
6
+ > radius = 3
7
+ => 3
8
+ > area_of_circle = radius * radius * Math::PI
9
+ => 28.2743338823081
10
+ > area_of_circle == 28.2743338823081
11
+ => false
12
+
13
+ Instead, you should use the be_within matcher to check that the value
14
+ is within a delta of your expected value:
15
+
16
+ area_of_circle.should be_within(0.1).of(28.3)
17
+
18
+ Note that the difference between the actual and expected values must be
19
+ smaller than your delta; if it is equal, the matcher will fail.
20
+
21
+ Scenario: basic usage
22
+ Given a file named "be_within_matcher_spec.rb" with:
23
+ """
24
+ describe 27.5 do
25
+ it { should be_within(0.5).of(27.9) }
26
+ it { should be_within(0.5).of(27.1) }
27
+ it { should_not be_within(0.5).of(28) }
28
+ it { should_not be_within(0.5).of(27) }
29
+
30
+ # deliberate failures
31
+ it { should_not be_within(0.5).of(27.9) }
32
+ it { should_not be_within(0.5).of(27.1) }
33
+ it { should be_within(0.5).of(28) }
34
+ it { should be_within(0.5).of(27) }
35
+ end
36
+ """
37
+ When I run "rspec be_within_matcher_spec.rb"
38
+ Then the output should contain all of these:
39
+ | 8 examples, 4 failures |
40
+ | expected 27.5 not to be within 0.5 of 27.9 |
41
+ | expected 27.5 not to be within 0.5 of 27.1 |
42
+ | expected 27.5 to be within 0.5 of 28 |
43
+ | expected 27.5 to be within 0.5 of 27 |
@@ -0,0 +1,44 @@
1
+ Feature: exist matcher
2
+
3
+ The exist matcher is used to specify that something exists
4
+ (as indicated by #exist?):
5
+
6
+ obj.should exist # passes if obj.exist?
7
+
8
+ Scenario: basic usage
9
+ Given a file named "exist_matcher_spec.rb" with:
10
+ """
11
+ class Planet
12
+ attr_reader :name
13
+
14
+ def initialize(name)
15
+ @name = name
16
+ end
17
+
18
+ def inspect
19
+ "<Planet: #{name}>"
20
+ end
21
+
22
+ def exist?
23
+ %w[Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune].include?(name)
24
+ end
25
+ end
26
+
27
+ describe "Earth" do
28
+ subject { Planet.new("Earth") }
29
+ it { should exist }
30
+ it { should_not exist } # deliberate failure
31
+ end
32
+
33
+ describe "Tatooine" do
34
+ subject { Planet.new("Tatooine") }
35
+ it { should_not exist }
36
+ it { should exist } # deliberate failure
37
+ end
38
+ """
39
+ When I run "rspec exist_matcher_spec.rb"
40
+ Then the output should contain all of these:
41
+ | 4 examples, 2 failures |
42
+ | expected <Planet: Earth> not to exist |
43
+ | expected <Planet: Tatooine> to exist |
44
+
@@ -1,6 +1,6 @@
1
1
  Feature: include matcher
2
2
 
3
- Use the include matcher to specify than a collection includes one or more
3
+ Use the include matcher to specify that a collection includes one or more
4
4
  expected objects. This works on any object that responds to #include? (such
5
5
  as a string or array):
6
6
 
@@ -6,7 +6,7 @@ Feature: respond_to matcher
6
6
  obj.should respond_to(:foo) # pass if obj.respond_to?(:foo)
7
7
 
8
8
  You can specify that an object responds to multiple messages in a single
9
- statement with multiple arguments passed to the matcher
9
+ statement with multiple arguments passed to the matcher:
10
10
 
11
11
  obj.should respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar)
12
12
 
@@ -0,0 +1,31 @@
1
+ Feature: Satisfy matcher
2
+
3
+ The satisfy matcher is extremely flexible and can handle almost anything
4
+ you want to specify. It passes if the block you provide returns true:
5
+
6
+ 10.should satisfy { |v| v % 5 == 0 }
7
+ 7.should_not satisfy { |v| v % 5 == 0 }
8
+
9
+ This flexibility comes at a cost, however: the failure message
10
+ ("expected [actual] to satisfy block") is not very descriptive
11
+ or helpful. You will usually be better served by using one of
12
+ the other built-in matchers, or writing a custom matcher.
13
+
14
+ Scenario: basic usage
15
+ Given a file named "satisfy_matcher_spec.rb" with:
16
+ """
17
+ describe 10 do
18
+ it { should satisfy { |v| v > 5 } }
19
+ it { should_not satisfy { |v| v > 15 } }
20
+
21
+ # deliberate failures
22
+ it { should_not satisfy { |v| v > 5 } }
23
+ it { should satisfy { |v| v > 15 } }
24
+ end
25
+ """
26
+ When I run "rspec satisfy_matcher_spec.rb"
27
+ Then the output should contain all of these:
28
+ | 4 examples, 2 failures |
29
+ | expected 10 not to satisfy block |
30
+ | expected 10 to satisfy block |
31
+
@@ -0,0 +1,85 @@
1
+ Feature: Throw symbol matcher
2
+
3
+ The throw_symbol matcher is used to specify that a block of code
4
+ throws a symbol. The most basic form passes if any symbol is thrown:
5
+
6
+ expect { throw :foo }.to throw_symbol
7
+
8
+ You'll often want to specify that a particular symbol is thrown:
9
+
10
+ expect { throw :foo }.to throw_symbol(:foo)
11
+
12
+ If you care about the additional argument given to throw, you can
13
+ specify that as well:
14
+
15
+ expect { throw :foo, 7 }.to throw_symbol(:foo, 7)
16
+
17
+ Scenario: basic usage
18
+ Given a file named "throw_symbol_matcher_spec.rb" with:
19
+ """
20
+ describe "throw" do
21
+ specify { expect { throw :foo }.to throw_symbol }
22
+ specify { expect { throw :bar, 7 }.to throw_symbol }
23
+ specify { expect { 5 + 5 }.to_not throw_symbol }
24
+
25
+ # deliberate failures
26
+ specify { expect { throw :foo }.to_not throw_symbol }
27
+ specify { expect { throw :bar, 7 }.to_not throw_symbol }
28
+ specify { expect { 5 + 5 }.to throw_symbol }
29
+ end
30
+ """
31
+ When I run "rspec throw_symbol_matcher_spec.rb"
32
+ Then the output should contain all of these:
33
+ | 6 examples, 3 failures |
34
+ | expected no Symbol to be thrown, got :foo |
35
+ | expected no Symbol to be thrown, got :bar |
36
+ | expected a Symbol to be thrown, got nothing |
37
+
38
+ Scenario: specify thrown symbol
39
+ Given a file named "throw_symbol_matcher_spec.rb" with:
40
+ """
41
+ describe "throw symbol" do
42
+ specify { expect { throw :foo }.to throw_symbol(:foo) }
43
+ specify { expect { throw :foo, 7 }.to throw_symbol(:foo) }
44
+ specify { expect { 5 + 5 }.to_not throw_symbol(:foo) }
45
+ specify { expect { throw :bar }.to_not throw_symbol(:foo) }
46
+
47
+ # deliberate failures
48
+ specify { expect { throw :foo }.to_not throw_symbol(:foo) }
49
+ specify { expect { throw :foo, 7 }.to_not throw_symbol(:foo) }
50
+ specify { expect { 5 + 5 }.to throw_symbol(:foo) }
51
+ specify { expect { throw :bar }.to throw_symbol(:foo) }
52
+ end
53
+ """
54
+ When I run "rspec throw_symbol_matcher_spec.rb"
55
+ Then the output should contain all of these:
56
+ | 8 examples, 4 failures |
57
+ | expected :foo not to be thrown, got :foo |
58
+ | expected :foo not to be thrown, got :foo with 7 |
59
+ | expected :foo to be thrown, got nothing |
60
+ | expected :foo to be thrown, got :bar |
61
+
62
+ Scenario: specify thrown symbol and argument
63
+ Given a file named "throw_symbol_argument_matcher_spec.rb" with:
64
+ """
65
+ describe "throw symbol with argument" do
66
+ specify { expect { throw :foo, 7 }.to throw_symbol(:foo, 7) }
67
+ specify { expect { throw :foo, 8 }.to_not throw_symbol(:foo, 7) }
68
+ specify { expect { throw :bar, 7 }.to_not throw_symbol(:foo, 7) }
69
+ specify { expect { throw :foo }.to_not throw_symbol(:foo, 7) }
70
+
71
+ # deliberate failures
72
+ specify { expect { throw :foo, 7 }.to_not throw_symbol(:foo, 7) }
73
+ specify { expect { throw :foo, 8 }.to throw_symbol(:foo, 7) }
74
+ specify { expect { throw :bar, 7 }.to throw_symbol(:foo, 7) }
75
+ specify { expect { throw :foo }.to throw_symbol(:foo, 7) }
76
+ end
77
+ """
78
+ When I run "rspec throw_symbol_argument_matcher_spec.rb"
79
+ Then the output should contain all of these:
80
+ | 8 examples, 4 failures |
81
+ | expected :foo with 7 not to be thrown, got :foo with 7 |
82
+ | expected :foo with 7 to be thrown, got :foo with 8 |
83
+ | expected :foo with 7 to be thrown, got :bar |
84
+ | expected :foo with 7 to be thrown, got :foo with no argument |
85
+
@@ -1,6 +1,6 @@
1
- Feature: Type Check matchers
1
+ Feature: specify types of objects
2
2
 
3
- rspec-expectations includes two matchers specify types of objects:
3
+ rspec-expectations includes two matchers to specify types of objects:
4
4
 
5
5
  * obj.should be_kind_of(type): calls obj.kind_of?(type), which returns
6
6
  true if type is in obj's class hierarchy or is a module and is
@@ -31,12 +31,16 @@ Feature: Test::Unit integration
31
31
  array.should be_empty
32
32
  end
33
33
 
34
+ def test_expect_matcher
35
+ expect { @a = 5 }.to change { @a }.from(nil).to(5)
36
+ end
37
+
34
38
  def test_custom_matcher_and_deprecation_warning
35
39
  1.should be_an_int
36
40
  end
37
41
  end
38
42
  """
39
43
  When I run "ruby rspec_expectations_test.rb"
40
- Then the output should contain "3 tests, 0 assertions, 1 failures, 0 errors" or "3 tests, 0 assertions, 0 failures, 1 errors"
44
+ Then the output should contain "4 tests, 0 assertions, 1 failures, 0 errors" or "4 tests, 0 assertions, 0 failures, 1 errors"
41
45
  And the output should contain "expected empty? to return true, got false"
42
46
  And the output should contain "be_an_int is deprecated"
@@ -1,8 +1,8 @@
1
+ require 'rspec/expectations/extensions'
1
2
  require 'rspec/matchers'
2
3
  require 'rspec/expectations/fail_with'
3
4
  require 'rspec/expectations/errors'
4
5
  require 'rspec/expectations/deprecation'
5
- require 'rspec/expectations/extensions'
6
6
  require 'rspec/expectations/handler'
7
7
  require 'rspec/expectations/version'
8
8
  require 'rspec/expectations/backward_compatibility'
@@ -1,3 +1,2 @@
1
1
  require 'rspec/expectations/extensions/kernel'
2
2
  require 'rspec/expectations/extensions/array'
3
- require 'rspec/expectations/extensions/rspec/core/example_group'
@@ -1,7 +1,7 @@
1
1
  module RSpec # :nodoc:
2
2
  module Expectations # :nodoc:
3
3
  module Version # :nodoc:
4
- STRING = '2.0.1'
4
+ STRING = '2.1.0'
5
5
  end
6
6
  end
7
7
  end
@@ -155,10 +155,6 @@ module RSpec
155
155
  # end
156
156
  #
157
157
  module Matchers
158
- if RSpec.respond_to?(:configure)
159
- RSpec.configure {|c| c.include self}
160
- end
161
-
162
158
  # Include Matchers for other test frameworks
163
159
  if defined?(Test::Unit::TestCase)
164
160
  Test::Unit::TestCase.send(:include, self)
@@ -177,6 +173,8 @@ require 'rspec/matchers/be'
177
173
  require 'rspec/matchers/be_close'
178
174
  require 'rspec/matchers/be_instance_of'
179
175
  require 'rspec/matchers/be_kind_of'
176
+ require 'rspec/matchers/be_within'
177
+ require 'rspec/matchers/block_aliases'
180
178
  require 'rspec/matchers/change'
181
179
  require 'rspec/matchers/eq'
182
180
  require 'rspec/matchers/eql'
@@ -10,23 +10,8 @@ module RSpec
10
10
  #
11
11
  # result.should be_close(3.0, 0.5)
12
12
  def be_close(expected, delta)
13
- Matcher.new :be_close, expected, delta do |_expected_, _delta_|
14
- match do |actual|
15
- (actual - _expected_).abs < _delta_
16
- end
17
-
18
- failure_message_for_should do |actual|
19
- "expected #{_expected_} +/- (< #{_delta_}), got #{actual}"
20
- end
21
-
22
- failure_message_for_should_not do |actual|
23
- "expected #{_expected_} +/- (< #{_delta_}), got #{actual}"
24
- end
25
-
26
- description do
27
- "be close to #{_expected_} (within +- #{_delta_})"
28
- end
29
- end
13
+ RSpec.deprecate("be_close(#{expected}, #{delta})", "be_within(#{delta}).of(#{expected})")
14
+ be_within(delta).of(expected)
30
15
  end
31
16
  end
32
17
  end
@@ -0,0 +1,40 @@
1
+ module RSpec
2
+ module Matchers
3
+ # :call-seq:
4
+ # should be_within(delta).of(expected)
5
+ # should_not be_within(delta).of(expected)
6
+ #
7
+ # Passes if actual == expected +/- delta
8
+ #
9
+ # == Example
10
+ #
11
+ # result.should be_within(0.5).of(3.0)
12
+ def be_within(delta)
13
+ Matcher.new :be_within, delta do |_delta_|
14
+ chain :of do |_expected_|
15
+ @_expected = _expected_
16
+ end
17
+
18
+ match do |actual|
19
+ unless @_expected
20
+ raise ArgumentError.new("You must set an expected value using #of: be_within(#{_delta_}).of(expected_value)")
21
+ end
22
+ (actual - @_expected).abs < _delta_
23
+ end
24
+
25
+ failure_message_for_should do |actual|
26
+ "expected #{actual} to #{description}"
27
+ end
28
+
29
+ failure_message_for_should_not do |actual|
30
+ "expected #{actual} not to #{description}"
31
+ end
32
+
33
+ description do
34
+ "be within #{_delta_} of #{@_expected}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,18 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BlockAliases
4
+ alias_method :to, :should
5
+ alias_method :to_not, :should_not
6
+ end
7
+
8
+ # Extends the submitted block with aliases to and to_not
9
+ # for should and should_not. Allows expectations like this:
10
+ #
11
+ # expect { this_block }.to change{this.expression}.from(old_value).to(new_value)
12
+ # expect { this_block }.to raise_error
13
+ def expect(&block)
14
+ block.extend BlockAliases
15
+ end
16
+ end
17
+ end
18
+
@@ -4,7 +4,7 @@ module RSpec
4
4
  def initialize(expected, relativity=:exactly)
5
5
  @expected = (expected == :no ? 0 : expected)
6
6
  @relativity = relativity
7
- @actual = nil
7
+ @actual = @collection_name = @plural_collection_name = nil
8
8
  end
9
9
 
10
10
  def relativities
@@ -75,7 +75,7 @@ EOF
75
75
 
76
76
  def method_missing(sym, *args, &block)
77
77
  @collection_name = sym
78
- if inflector = (defined?(ActiveSupport::Inflector) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
78
+ if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
79
79
  @plural_collection_name = inflector.pluralize(sym.to_s)
80
80
  end
81
81
  @args = args
@@ -11,7 +11,9 @@ module RSpec
11
11
  @expected = expected
12
12
  @actual = nil
13
13
  @diffable = false
14
- @expected_exception, @rescued_exception = nil
14
+ @expected_exception, @rescued_exception = nil, nil
15
+ @match_for_should_not_block = nil
16
+
15
17
  @messages = {
16
18
  :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
17
19
  :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
@@ -97,11 +99,9 @@ module RSpec
97
99
 
98
100
  # See RSpec::Matchers
99
101
  def chain(method, &block)
100
- self.class.class_eval do
101
- define_method method do |*args|
102
- block.call(*args)
103
- self
104
- end
102
+ define_method method do |*args|
103
+ block.call(*args)
104
+ self
105
105
  end
106
106
  end
107
107
 
@@ -29,6 +29,15 @@ module RSpec
29
29
  eval_match(@actual, operator, expected)
30
30
  end
31
31
  end
32
+
33
+ negative_operator = operator.sub(/^=/, '!')
34
+ if negative_operator != operator && respond_to?(negative_operator)
35
+ define_method(negative_operator) do |expected|
36
+ opposite_should = ::RSpec::Matchers.last_should == :should ? :should_not : :should
37
+ raise "RSpec does not support `#{::RSpec::Matchers.last_should} #{negative_operator} expected`. " +
38
+ "Use `#{opposite_should} #{operator} expected` instead."
39
+ end
40
+ end
32
41
  end
33
42
 
34
43
  ['==', '===', '=~', '>', '>=', '<', '<='].each do |operator|
@@ -19,42 +19,45 @@ module RSpec
19
19
  throw :proc_did_not_throw_anything, :nothing_thrown
20
20
  end
21
21
  end
22
- @caught_symbol = @expected_symbol unless @caught_arg == :nothing_thrown
22
+
23
+ if @caught_arg == :nothing_thrown
24
+ @caught_arg = nil
25
+ else
26
+ @caught_symbol = @expected_symbol
27
+ end
23
28
  end
24
29
 
25
30
  # Ruby 1.8 uses NameError with `symbol'
26
31
  # Ruby 1.9 uses ArgumentError with :symbol
27
32
  rescue NameError, ArgumentError => e
28
- raise e unless e.message =~ /uncaught throw (`|\:)([a-zA-Z0-9_]*)(')?/
33
+ unless e.message =~ /uncaught throw (`|\:)([a-zA-Z0-9_]*)(')?/
34
+ other_exception = e
35
+ raise
36
+ end
29
37
  @caught_symbol = $2.to_sym
30
-
38
+ rescue => other_exception
39
+ raise
31
40
  ensure
32
- if @expected_symbol.nil?
33
- return !@caught_symbol.nil?
34
- else
35
- if @expected_arg.nil?
36
- return @caught_symbol == @expected_symbol
41
+ unless other_exception
42
+ if @expected_symbol.nil?
43
+ return !@caught_symbol.nil?
37
44
  else
38
- return (@caught_symbol == @expected_symbol) & (@caught_arg == @expected_arg)
45
+ if @expected_arg.nil?
46
+ return @caught_symbol == @expected_symbol
47
+ else
48
+ return (@caught_symbol == @expected_symbol) & (@caught_arg == @expected_arg)
49
+ end
39
50
  end
40
51
  end
41
52
  end
42
53
  end
43
54
 
44
55
  def failure_message_for_should
45
- if @caught_symbol
46
- "expected #{expected}, got #{@caught_symbol.inspect}"
47
- else
48
- "expected #{expected} but nothing was thrown"
49
- end
56
+ "expected #{expected} to be thrown, got #{caught}"
50
57
  end
51
58
 
52
59
  def failure_message_for_should_not
53
- if @expected_symbol
54
- "expected #{expected} not to be thrown"
55
- else
56
- "expected no Symbol, got :#{@caught_symbol}"
57
- end
60
+ "expected #{expected('no Symbol')}#{' not' if @expected_symbol} to be thrown, got #{caught}"
58
61
  end
59
62
 
60
63
  def description
@@ -62,15 +65,29 @@ module RSpec
62
65
  end
63
66
 
64
67
  private
65
-
66
- def expected
67
- @expected_symbol.nil? ? "a Symbol" : "#{@expected_symbol.inspect}#{args}"
68
+
69
+ def expected(symbol_desc = 'a Symbol')
70
+ throw_description(@expected_symbol || symbol_desc, @expected_arg)
68
71
  end
69
-
70
- def args
71
- @expected_arg.nil? ? "" : " with #{@expected_arg.inspect}"
72
+
73
+ def caught
74
+ throw_description(@caught_symbol || 'nothing', @caught_arg)
72
75
  end
73
-
76
+
77
+ def throw_description(symbol, arg)
78
+ symbol_description = symbol.is_a?(String) ? symbol : symbol.inspect
79
+
80
+ arg_description = if arg
81
+ " with #{arg.inspect}"
82
+ elsif @expected_arg && @caught_symbol == @expected_symbol
83
+ " with no argument"
84
+ else
85
+ ""
86
+ end
87
+
88
+ symbol_description + arg_description
89
+ end
90
+
74
91
  end
75
92
 
76
93
  # :call-seq:
@@ -22,10 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.rdoc_options = ["--charset=UTF-8"]
23
23
  s.require_path = "lib"
24
24
 
25
- s.add_runtime_dependency 'diff-lcs', '>= 1.1.2'
26
- s.add_development_dependency 'cucumber', ">= 0.6.2"
27
- s.add_development_dependency 'aruba', ">= 0.1.1"
28
- s.add_development_dependency 'rspec-core', "~> 2.0.1"
29
- s.add_development_dependency 'rspec-mocks', "~> 2.0.1"
25
+ s.add_runtime_dependency 'diff-lcs', '~> 1.1.2'
30
26
  end
31
27
 
@@ -1,49 +1,21 @@
1
1
  require 'spec_helper'
2
+
2
3
  module RSpec
3
4
  module Matchers
4
5
  describe "[actual.should] be_close(expected, delta)" do
5
- it "matches when actual == expected" do
6
- be_close(5.0, 0.5).matches?(5.0).should be_true
7
- end
8
- it "matches when actual < (expected + delta)" do
9
- be_close(5.0, 0.5).matches?(5.49).should be_true
10
- end
11
- it "matches when actual > (expected - delta)" do
12
- be_close(5.0, 0.5).matches?(4.51).should be_true
13
- end
14
- it "does not match when actual == (expected - delta)" do
15
- be_close(5.0, 0.5).matches?(4.5).should be_false
16
- end
17
- it "does not match when actual < (expected - delta)" do
18
- be_close(5.0, 0.5).matches?(4.49).should be_false
19
- end
20
- it "does not match when actual == (expected + delta)" do
21
- be_close(5.0, 0.5).matches?(5.5).should be_false
22
- end
23
- it "does not match when actual > (expected + delta)" do
24
- be_close(5.0, 0.5).matches?(5.51).should be_false
25
- end
26
- it "provides a failure message for should" do
27
- #given
28
- matcher = be_close(5.0, 0.5)
29
- #when
30
- matcher.matches?(5.51)
31
- #then
32
- matcher.failure_message_for_should.should == "expected 5.0 +/- (< 0.5), got 5.51"
6
+ before(:each) do
7
+ RSpec.stub(:warn)
33
8
  end
34
9
 
35
- it "provides a failure message for should tno" do
36
- #given
37
- matcher = be_close(5.0, 0.5)
38
- #when
39
- matcher.matches?(5.49)
40
- #then
41
- matcher.failure_message_for_should_not.should == "expected 5.0 +/- (< 0.5), got 5.49"
10
+ it "delegates to be_within(delta).of(expected)" do
11
+ should_receive(:be_within).with(0.5).and_return( be_within_matcher = stub )
12
+ be_within_matcher.should_receive(:of).with(3.0)
13
+ be_close(3.0, 0.5)
42
14
  end
43
- it "provides a description" do
44
- matcher = be_close(5.0, 0.5)
45
- matcher.matches?(5.1)
46
- matcher.description.should == "be close to 5.0 (within +- 0.5)"
15
+
16
+ it "prints a deprecation warning" do
17
+ RSpec.should_receive(:warn).with(/please use be_within.*instead/)
18
+ be_close(3.0, 0.5)
47
19
  end
48
20
  end
49
21
  end
@@ -1,6 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "should be_predicate" do
4
+ it "allows other undefined methods to raise errors as normal" do
5
+ expect { some_undefined_method }.to raise_error(NameError)
6
+ end
7
+
4
8
  it "passes when actual returns true for :predicate?" do
5
9
  actual = stub("actual", :happy? => true)
6
10
  actual.should be_happy
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ describe "[actual.should] be_within(delta).of(expected)" do
6
+ it "matches when actual == expected" do
7
+ be_within(0.5).of(5.0).matches?(5.0).should be_true
8
+ end
9
+
10
+ it "matches when actual < (expected + delta)" do
11
+ be_within(0.5).of(5.0).matches?(5.49).should be_true
12
+ end
13
+
14
+ it "matches when actual > (expected - delta)" do
15
+ be_within(0.5).of(5.0).matches?(4.51).should be_true
16
+ end
17
+
18
+ it "does not match when actual == (expected - delta)" do
19
+ be_within(0.5).of(5.0).matches?(4.5).should be_false
20
+ end
21
+
22
+ it "does not match when actual < (expected - delta)" do
23
+ be_within(0.5).of(5.0).matches?(4.49).should be_false
24
+ end
25
+
26
+ it "does not match when actual == (expected + delta)" do
27
+ be_within(0.5).of(5.0).matches?(5.5).should be_false
28
+ end
29
+
30
+ it "does not match when actual > (expected + delta)" do
31
+ be_within(0.5).of(5.0).matches?(5.51).should be_false
32
+ end
33
+
34
+ it "provides a failure message for should" do
35
+ #given
36
+ matcher = be_within(0.5).of(5.0)
37
+ #when
38
+ matcher.matches?(5.51)
39
+ #then
40
+ matcher.failure_message_for_should.should == "expected 5.51 to be within 0.5 of 5.0"
41
+ end
42
+
43
+ it "provides a failure message for should not" do
44
+ #given
45
+ matcher = be_within(0.5).of(5.0)
46
+ #when
47
+ matcher.matches?(5.49)
48
+ #then
49
+ matcher.failure_message_for_should_not.should == "expected 5.49 not to be within 0.5 of 5.0"
50
+ end
51
+
52
+ it "provides a description" do
53
+ matcher = be_within(0.5).of(5.0)
54
+ matcher.matches?(5.1)
55
+ matcher.description.should == "be within 0.5 of 5.0"
56
+ end
57
+
58
+ it "raises an error if no expected value is given" do
59
+ matcher = be_within(0.5)
60
+ expect { matcher.matches?(5.1) }.to raise_error(ArgumentError, /must set an expected value using #of/)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -4,7 +4,7 @@ require 'stringio'
4
4
  describe "have matcher" do
5
5
 
6
6
  before(:each) do
7
- if defined?(::ActiveSupport::Inflector)
7
+ if defined?(::ActiveSupport::Inflector) && ::ActiveSupport::Inflector.respond_to?(:pluralize)
8
8
  @active_support_was_defined = true
9
9
  else
10
10
  @active_support_was_defined = false
@@ -68,6 +68,16 @@ describe 'should have(1).item when ActiveSupport::Inflector is defined' do
68
68
  owner = create_collection_owner_with(1)
69
69
  owner.should have(1).item
70
70
  end
71
+
72
+ context "when ActiveSupport::Inflector is partially loaded without its inflectors" do
73
+
74
+ it "does not pluralize the collection name" do
75
+ (class << ::ActiveSupport::Inflector; self; end).send :undef_method, :pluralize
76
+ owner = create_collection_owner_with(1)
77
+ expect { owner.should have(1).item }.to raise_error(NoMethodError)
78
+ end
79
+
80
+ end
71
81
 
72
82
  after(:each) do
73
83
  unless @active_support_was_defined
@@ -334,6 +334,14 @@ module RSpec
334
334
  matcher.expecting('value').matches?('other value').should be_false
335
335
  end
336
336
 
337
+ it "prevents name collisions on chainable methods from different matchers" do
338
+ m1 = RSpec::Matchers::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }
339
+ m2 = RSpec::Matchers::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }
340
+
341
+ expect { m1.foo }.to raise_error("foo in m1")
342
+ expect { m2.foo }.to raise_error("foo in m2")
343
+ end
344
+
337
345
  context "defined using the dsl" do
338
346
  def a_method_in_the_example
339
347
  "method defined in the example"
@@ -21,6 +21,32 @@ describe "should ==" do
21
21
 
22
22
  end
23
23
 
24
+ describe "unsupported operators", :ruby => '1.9' do
25
+ it "raises an appropriate error for should != expected" do
26
+ expect {
27
+ "apple".should != "pear"
28
+ }.to raise_error(/does not support `should != expected`. Use `should_not == expected`/)
29
+ end
30
+
31
+ it "raises an appropriate error for should_not != expected" do
32
+ expect {
33
+ "apple".should_not != "pear"
34
+ }.to raise_error(/does not support `should_not != expected`. Use `should == expected`/)
35
+ end
36
+
37
+ it "raises an appropriate error for should !~ expected" do
38
+ expect {
39
+ "apple".should !~ /regex/
40
+ }.to raise_error(/does not support `should !~ expected`. Use `should_not =~ expected`/)
41
+ end
42
+
43
+ it "raises an appropriate error for should_not !~ expected" do
44
+ expect {
45
+ "apple".should_not !~ /regex/
46
+ }.to raise_error(/does not support `should_not !~ expected`. Use `should =~ expected`/)
47
+ end
48
+ end
49
+
24
50
  describe "should_not ==" do
25
51
 
26
52
  it "delegates message to target" do
@@ -17,11 +17,11 @@ module RSpec
17
17
  end
18
18
  it "provides a failure message" do
19
19
  @matcher.matches?(lambda{})
20
- @matcher.failure_message_for_should.should == "expected a Symbol but nothing was thrown"
20
+ @matcher.failure_message_for_should.should == "expected a Symbol to be thrown, got nothing"
21
21
  end
22
22
  it "provides a negative failure message" do
23
23
  @matcher.matches?(lambda{ throw :sym})
24
- @matcher.failure_message_for_should_not.should == "expected no Symbol, got :sym"
24
+ @matcher.failure_message_for_should_not.should == "expected no Symbol to be thrown, got :sym"
25
25
  end
26
26
  end
27
27
 
@@ -42,18 +42,20 @@ module RSpec
42
42
  end
43
43
  it "provides a failure message when no Symbol is thrown" do
44
44
  @matcher.matches?(lambda{})
45
- @matcher.failure_message_for_should.should == "expected :sym but nothing was thrown"
45
+ @matcher.failure_message_for_should.should == "expected :sym to be thrown, got nothing"
46
46
  end
47
47
  it "provides a failure message when wrong Symbol is thrown" do
48
48
  @matcher.matches?(lambda{ throw :other_sym })
49
- @matcher.failure_message_for_should.should == "expected :sym, got :other_sym"
49
+ @matcher.failure_message_for_should.should == "expected :sym to be thrown, got :other_sym"
50
50
  end
51
51
  it "provides a negative failure message" do
52
52
  @matcher.matches?(lambda{ throw :sym })
53
- @matcher.failure_message_for_should_not.should == "expected :sym not to be thrown"
53
+ @matcher.failure_message_for_should_not.should == "expected :sym not to be thrown, got :sym"
54
54
  end
55
55
  it "only matches NameErrors raised by uncaught throws" do
56
- @matcher.matches?(lambda{ sym }).should be_false
56
+ expect {
57
+ @matcher.matches?(lambda{ sym }).should be_false
58
+ }.to raise_error(NameError)
57
59
  end
58
60
  end
59
61
 
@@ -77,18 +79,33 @@ module RSpec
77
79
  end
78
80
  it "provides a failure message when no Symbol is thrown" do
79
81
  @matcher.matches?(lambda{})
80
- @matcher.failure_message_for_should.should == %q[expected :sym with "a" but nothing was thrown]
82
+ @matcher.failure_message_for_should.should == %q[expected :sym with "a" to be thrown, got nothing]
81
83
  end
82
84
  it "provides a failure message when wrong Symbol is thrown" do
83
85
  @matcher.matches?(lambda{ throw :other_sym })
84
- @matcher.failure_message_for_should.should == %q[expected :sym with "a", got :other_sym]
86
+ @matcher.failure_message_for_should.should == %q[expected :sym with "a" to be thrown, got :other_sym]
87
+ end
88
+ it "provides a failure message when wrong arg is thrown" do
89
+ @matcher.matches?(lambda{ throw :sym, "b" })
90
+ @matcher.failure_message_for_should.should == %q[expected :sym with "a" to be thrown, got :sym with "b"]
91
+ end
92
+ it "provides a failure message when no arg is thrown" do
93
+ @matcher.matches?(lambda{ throw :sym })
94
+ @matcher.failure_message_for_should.should == %q[expected :sym with "a" to be thrown, got :sym with no argument]
85
95
  end
86
96
  it "provides a negative failure message" do
87
97
  @matcher.matches?(lambda{ throw :sym })
88
- @matcher.failure_message_for_should_not.should == %q[expected :sym with "a" not to be thrown]
98
+ @matcher.failure_message_for_should_not.should == %q[expected :sym with "a" not to be thrown, got :sym with no argument]
89
99
  end
90
100
  it "only matches NameErrors raised by uncaught throws" do
91
- @matcher.matches?(lambda{ sym }).should be_false
101
+ expect {
102
+ @matcher.matches?(lambda{ sym }).should be_false
103
+ }.to raise_error(NameError)
104
+ end
105
+ it "raises other errors" do
106
+ expect {
107
+ @matcher.matches?(lambda { raise "Boom" })
108
+ }.to raise_error(/Boom/)
92
109
  end
93
110
  end
94
111
  end
@@ -1,5 +1,5 @@
1
1
  def add_to_load_path(path, prepend=false)
2
- path = File.expand_path("../#{path}", __FILE__)
2
+ path = File.expand_path("../../#{path}/lib", __FILE__)
3
3
  if prepend
4
4
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
5
5
  else
@@ -7,10 +7,11 @@ def add_to_load_path(path, prepend=false)
7
7
  end
8
8
  end
9
9
 
10
- add_to_load_path("../lib", :prepend)
11
- add_to_load_path("../../rspec-core/lib")
12
- add_to_load_path("../../rspec-mocks/lib")
10
+ add_to_load_path("rspec-expectations", :prepend)
11
+ add_to_load_path("rspec-core")
12
+ add_to_load_path("rspec-mocks")
13
13
 
14
+ require 'rspec/expectations'
14
15
  require 'rspec/core'
15
16
  require 'rspec/mocks'
16
17
 
@@ -49,5 +50,17 @@ RSpec::configure do |config|
49
50
  config.include RSpec::Mocks::Methods
50
51
  config.color_enabled = true
51
52
  config.filter_run :focused => true
53
+
54
+ config.filter_run_excluding :ruby => lambda {|version|
55
+ case version.to_s
56
+ when "!jruby"
57
+ RUBY_ENGINE != "jruby"
58
+ when /^> (.*)/
59
+ !(RUBY_VERSION.to_s > $1)
60
+ else
61
+ !(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
62
+ end
63
+ }
64
+
52
65
  config.run_all_when_everything_filtered = true
53
66
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-expectations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
5
4
  prerelease: false
6
5
  segments:
7
6
  - 2
8
- - 0
9
7
  - 1
10
- version: 2.0.1
8
+ - 0
9
+ version: 2.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - David Chelimsky
@@ -16,89 +15,24 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-10-18 00:00:00 -05:00
18
+ date: 2010-11-07 01:00:00 -05:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ name: diff-lcs
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
- hash: 23
29
28
  segments:
30
29
  - 1
31
30
  - 1
32
31
  - 2
33
32
  version: 1.1.2
34
- requirement: *id001
35
33
  type: :runtime
36
- name: diff-lcs
37
- prerelease: false
38
- - !ruby/object:Gem::Dependency
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- - 6
48
- - 2
49
- version: 0.6.2
50
- requirement: *id002
51
- type: :development
52
- name: cucumber
53
- prerelease: false
54
- - !ruby/object:Gem::Dependency
55
- version_requirements: &id003 !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 25
61
- segments:
62
- - 0
63
- - 1
64
- - 1
65
- version: 0.1.1
66
- requirement: *id003
67
- type: :development
68
- name: aruba
69
- prerelease: false
70
- - !ruby/object:Gem::Dependency
71
- version_requirements: &id004 !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ~>
75
- - !ruby/object:Gem::Version
76
- hash: 13
77
- segments:
78
- - 2
79
- - 0
80
- - 1
81
- version: 2.0.1
82
- requirement: *id004
83
- type: :development
84
- name: rspec-core
85
- prerelease: false
86
- - !ruby/object:Gem::Dependency
87
- version_requirements: &id005 !ruby/object:Gem::Requirement
88
- none: false
89
- requirements:
90
- - - ~>
91
- - !ruby/object:Gem::Version
92
- hash: 13
93
- segments:
94
- - 2
95
- - 0
96
- - 1
97
- version: 2.0.1
98
- requirement: *id005
99
- type: :development
100
- name: rspec-mocks
101
34
  prerelease: false
35
+ version_requirements: *id001
102
36
  description: rspec expectations (should[_not] and matchers)
103
37
  email: dchelimsky@gmail.com;chad.humphries@gmail.com
104
38
  executables: []
@@ -123,11 +57,13 @@ files:
123
57
  - features/expectations/diffing.feature
124
58
  - features/expectations/implicit_docstrings.feature
125
59
  - features/matchers/access_running_example.feature
60
+ - features/matchers/be_within.feature
126
61
  - features/matchers/define_diffable_matcher.feature
127
62
  - features/matchers/define_matcher.feature
128
63
  - features/matchers/define_matcher_outside_rspec.feature
129
64
  - features/matchers/define_matcher_with_fluent_interface.feature
130
65
  - features/matchers/equality.feature
66
+ - features/matchers/exist.feature
131
67
  - features/matchers/expect_change.feature
132
68
  - features/matchers/expect_error.feature
133
69
  - features/matchers/have.feature
@@ -135,6 +71,8 @@ files:
135
71
  - features/matchers/operators.feature
136
72
  - features/matchers/predicates.feature
137
73
  - features/matchers/respond_to.feature
74
+ - features/matchers/satisfy.feature
75
+ - features/matchers/throw_symbol.feature
138
76
  - features/matchers/types.feature
139
77
  - features/step_definitions/additional_cli_steps.rb
140
78
  - features/support/env.rb
@@ -148,7 +86,6 @@ files:
148
86
  - lib/rspec/expectations/extensions.rb
149
87
  - lib/rspec/expectations/extensions/array.rb
150
88
  - lib/rspec/expectations/extensions/kernel.rb
151
- - lib/rspec/expectations/extensions/rspec/core/example_group.rb
152
89
  - lib/rspec/expectations/fail_with.rb
153
90
  - lib/rspec/expectations/handler.rb
154
91
  - lib/rspec/expectations/version.rb
@@ -157,6 +94,8 @@ files:
157
94
  - lib/rspec/matchers/be_close.rb
158
95
  - lib/rspec/matchers/be_instance_of.rb
159
96
  - lib/rspec/matchers/be_kind_of.rb
97
+ - lib/rspec/matchers/be_within.rb
98
+ - lib/rspec/matchers/block_aliases.rb
160
99
  - lib/rspec/matchers/change.rb
161
100
  - lib/rspec/matchers/compatibility.rb
162
101
  - lib/rspec/matchers/dsl.rb
@@ -189,6 +128,7 @@ files:
189
128
  - spec/rspec/matchers/be_instance_of_spec.rb
190
129
  - spec/rspec/matchers/be_kind_of_spec.rb
191
130
  - spec/rspec/matchers/be_spec.rb
131
+ - spec/rspec/matchers/be_within_spec.rb
192
132
  - spec/rspec/matchers/change_spec.rb
193
133
  - spec/rspec/matchers/compatibility_spec.rb
194
134
  - spec/rspec/matchers/description_generation_spec.rb
@@ -227,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
227
167
  requirements:
228
168
  - - ">="
229
169
  - !ruby/object:Gem::Version
230
- hash: 3
170
+ hash: -1883027973330642761
231
171
  segments:
232
172
  - 0
233
173
  version: "0"
@@ -236,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
176
  requirements:
237
177
  - - ">="
238
178
  - !ruby/object:Gem::Version
239
- hash: 3
179
+ hash: -1883027973330642761
240
180
  segments:
241
181
  - 0
242
182
  version: "0"
@@ -246,7 +186,7 @@ rubyforge_project: rspec
246
186
  rubygems_version: 1.3.7
247
187
  signing_key:
248
188
  specification_version: 3
249
- summary: rspec-expectations-2.0.1
189
+ summary: rspec-expectations-2.1.0
250
190
  test_files:
251
191
  - features/README.markdown
252
192
  - features/expectations/attribute_of_subject.feature
@@ -254,11 +194,13 @@ test_files:
254
194
  - features/expectations/diffing.feature
255
195
  - features/expectations/implicit_docstrings.feature
256
196
  - features/matchers/access_running_example.feature
197
+ - features/matchers/be_within.feature
257
198
  - features/matchers/define_diffable_matcher.feature
258
199
  - features/matchers/define_matcher.feature
259
200
  - features/matchers/define_matcher_outside_rspec.feature
260
201
  - features/matchers/define_matcher_with_fluent_interface.feature
261
202
  - features/matchers/equality.feature
203
+ - features/matchers/exist.feature
262
204
  - features/matchers/expect_change.feature
263
205
  - features/matchers/expect_error.feature
264
206
  - features/matchers/have.feature
@@ -266,6 +208,8 @@ test_files:
266
208
  - features/matchers/operators.feature
267
209
  - features/matchers/predicates.feature
268
210
  - features/matchers/respond_to.feature
211
+ - features/matchers/satisfy.feature
212
+ - features/matchers/throw_symbol.feature
269
213
  - features/matchers/types.feature
270
214
  - features/step_definitions/additional_cli_steps.rb
271
215
  - features/support/env.rb
@@ -278,6 +222,7 @@ test_files:
278
222
  - spec/rspec/matchers/be_instance_of_spec.rb
279
223
  - spec/rspec/matchers/be_kind_of_spec.rb
280
224
  - spec/rspec/matchers/be_spec.rb
225
+ - spec/rspec/matchers/be_within_spec.rb
281
226
  - spec/rspec/matchers/change_spec.rb
282
227
  - spec/rspec/matchers/compatibility_spec.rb
283
228
  - spec/rspec/matchers/description_generation_spec.rb
@@ -1,19 +0,0 @@
1
- module RSpec
2
- module Core
3
- class ExampleGroup
4
- module BlockAliases
5
- alias_method :to, :should
6
- alias_method :to_not, :should_not
7
- end
8
-
9
- # Extends the submitted block with aliases to and to_not
10
- # for should and should_not. Allows expectations like this:
11
- #
12
- # expect { this_block }.to change{this.expression}.from(old_value).to(new_value)
13
- # expect { this_block }.to raise_error
14
- def expect(&block)
15
- block.extend BlockAliases
16
- end
17
- end
18
- end
19
- end