rspec-expectations 2.13.0 → 2.14.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/Changelog.md +34 -0
  2. data/README.md +43 -87
  3. data/features/README.md +8 -9
  4. data/features/built_in_matchers/README.md +41 -41
  5. data/features/built_in_matchers/be_within.feature +3 -3
  6. data/features/built_in_matchers/expect_change.feature +6 -6
  7. data/features/built_in_matchers/expect_error.feature +2 -2
  8. data/features/built_in_matchers/start_with.feature +1 -1
  9. data/features/built_in_matchers/throw_symbol.feature +11 -11
  10. data/features/built_in_matchers/yield.feature +18 -3
  11. data/features/custom_matchers/define_diffable_matcher.feature +1 -1
  12. data/features/custom_matchers/define_matcher_outside_rspec.feature +6 -6
  13. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
  14. data/features/customized_message.feature +1 -1
  15. data/features/support/env.rb +10 -1
  16. data/features/syntax_configuration.feature +3 -0
  17. data/features/test_frameworks/test_unit.feature +15 -17
  18. data/lib/rspec/expectations.rb +1 -1
  19. data/lib/rspec/expectations/deprecation.rb +12 -33
  20. data/lib/rspec/expectations/differ.rb +25 -7
  21. data/lib/rspec/expectations/expectation_target.rb +7 -8
  22. data/lib/rspec/expectations/extensions/object.rb +2 -12
  23. data/lib/rspec/expectations/fail_with.rb +11 -1
  24. data/lib/rspec/expectations/handler.rb +11 -6
  25. data/lib/rspec/expectations/syntax.rb +2 -2
  26. data/lib/rspec/expectations/version.rb +1 -1
  27. data/lib/rspec/matchers.rb +134 -144
  28. data/lib/rspec/matchers/be_close.rb +1 -1
  29. data/lib/rspec/matchers/built_in/be_within.rb +1 -1
  30. data/lib/rspec/matchers/built_in/have.rb +20 -4
  31. data/lib/rspec/matchers/built_in/raise_error.rb +23 -7
  32. data/lib/rspec/matchers/built_in/yield.rb +78 -3
  33. data/lib/rspec/matchers/operator_matcher.rb +1 -1
  34. data/lib/rspec/matchers/test_unit_integration.rb +11 -0
  35. data/spec/rspec/expectations/differ_spec.rb +27 -5
  36. data/spec/rspec/expectations/expectation_target_spec.rb +10 -3
  37. data/spec/rspec/expectations/extensions/kernel_spec.rb +5 -5
  38. data/spec/rspec/expectations/fail_with_spec.rb +19 -0
  39. data/spec/rspec/expectations/handler_spec.rb +42 -21
  40. data/spec/rspec/expectations/syntax_spec.rb +45 -3
  41. data/spec/rspec/matchers/be_close_spec.rb +6 -6
  42. data/spec/rspec/matchers/be_spec.rb +36 -36
  43. data/spec/rspec/matchers/be_within_spec.rb +4 -0
  44. data/spec/rspec/matchers/change_spec.rb +6 -6
  45. data/spec/rspec/matchers/configuration_spec.rb +57 -89
  46. data/spec/rspec/matchers/description_generation_spec.rb +1 -1
  47. data/spec/rspec/matchers/exist_spec.rb +9 -9
  48. data/spec/rspec/matchers/has_spec.rb +1 -1
  49. data/spec/rspec/matchers/have_spec.rb +12 -2
  50. data/spec/rspec/matchers/include_matcher_integration_spec.rb +2 -2
  51. data/spec/rspec/matchers/include_spec.rb +4 -4
  52. data/spec/rspec/matchers/match_array_spec.rb +1 -1
  53. data/spec/rspec/matchers/match_spec.rb +1 -1
  54. data/spec/rspec/matchers/raise_error_spec.rb +189 -99
  55. data/spec/rspec/matchers/respond_to_spec.rb +4 -4
  56. data/spec/rspec/matchers/satisfy_spec.rb +1 -1
  57. data/spec/rspec/matchers/start_with_end_with_spec.rb +2 -2
  58. data/spec/rspec/matchers/yield_spec.rb +81 -4
  59. data/spec/spec_helper.rb +1 -1
  60. metadata +10 -12
@@ -124,7 +124,7 @@ Feature: raise_error matcher
124
124
  describe Object, "#public_instance_methods" do
125
125
  it "does not raise" do
126
126
  expect { Object.public_instance_methods }.
127
- to_not raise_error(NameError)
127
+ not_to raise_error(NameError)
128
128
  end
129
129
  end
130
130
  """
@@ -136,7 +136,7 @@ Feature: raise_error matcher
136
136
  """
137
137
  describe "#to_s" do
138
138
  it "does not raise" do
139
- expect { Object.new.to_s }.to_not raise_error
139
+ expect { Object.new.to_s }.not_to raise_error
140
140
  end
141
141
  end
142
142
  """
@@ -1,6 +1,6 @@
1
1
  Feature: start_with matcher
2
2
 
3
- Use the `start_with` matcher to specify that a string or array starts with
3
+ Use the `start_with` matcher to specify that a string or array starts with
4
4
  the expected characters or elements.
5
5
 
6
6
  ```ruby
@@ -26,11 +26,11 @@ Feature: throw_symbol matcher
26
26
  describe "throw" do
27
27
  specify { expect { throw :foo }.to throw_symbol }
28
28
  specify { expect { throw :bar, 7 }.to throw_symbol }
29
- specify { expect { 5 + 5 }.to_not throw_symbol }
29
+ specify { expect { 5 + 5 }.not_to throw_symbol }
30
30
 
31
31
  # deliberate failures
32
- specify { expect { throw :foo }.to_not throw_symbol }
33
- specify { expect { throw :bar, 7 }.to_not throw_symbol }
32
+ specify { expect { throw :foo }.not_to throw_symbol }
33
+ specify { expect { throw :bar, 7 }.not_to throw_symbol }
34
34
  specify { expect { 5 + 5 }.to throw_symbol }
35
35
  end
36
36
  """
@@ -47,12 +47,12 @@ Feature: throw_symbol matcher
47
47
  describe "throw symbol" do
48
48
  specify { expect { throw :foo }.to throw_symbol(:foo) }
49
49
  specify { expect { throw :foo, 7 }.to throw_symbol(:foo) }
50
- specify { expect { 5 + 5 }.to_not throw_symbol(:foo) }
51
- specify { expect { throw :bar }.to_not throw_symbol(:foo) }
50
+ specify { expect { 5 + 5 }.not_to throw_symbol(:foo) }
51
+ specify { expect { throw :bar }.not_to throw_symbol(:foo) }
52
52
 
53
53
  # deliberate failures
54
- specify { expect { throw :foo }.to_not throw_symbol(:foo) }
55
- specify { expect { throw :foo, 7 }.to_not throw_symbol(:foo) }
54
+ specify { expect { throw :foo }.not_to throw_symbol(:foo) }
55
+ specify { expect { throw :foo, 7 }.not_to throw_symbol(:foo) }
56
56
  specify { expect { 5 + 5 }.to throw_symbol(:foo) }
57
57
  specify { expect { throw :bar }.to throw_symbol(:foo) }
58
58
  end
@@ -70,12 +70,12 @@ Feature: throw_symbol matcher
70
70
  """ruby
71
71
  describe "throw symbol with argument" do
72
72
  specify { expect { throw :foo, 7 }.to throw_symbol(:foo, 7) }
73
- specify { expect { throw :foo, 8 }.to_not throw_symbol(:foo, 7) }
74
- specify { expect { throw :bar, 7 }.to_not throw_symbol(:foo, 7) }
75
- specify { expect { throw :foo }.to_not throw_symbol(:foo, 7) }
73
+ specify { expect { throw :foo, 8 }.not_to throw_symbol(:foo, 7) }
74
+ specify { expect { throw :bar, 7 }.not_to throw_symbol(:foo, 7) }
75
+ specify { expect { throw :foo }.not_to throw_symbol(:foo, 7) }
76
76
 
77
77
  # deliberate failures
78
- specify { expect { throw :foo, 7 }.to_not throw_symbol(:foo, 7) }
78
+ specify { expect { throw :foo, 7 }.not_to throw_symbol(:foo, 7) }
79
79
  specify { expect { throw :foo, 8 }.to throw_symbol(:foo, 7) }
80
80
  specify { expect { throw :bar, 7 }.to throw_symbol(:foo, 7) }
81
81
  specify { expect { throw :foo }.to throw_symbol(:foo, 7) }
@@ -30,6 +30,10 @@ Feature: yield matchers
30
30
  yield *args
31
31
  end
32
32
 
33
+ def self.yield_twice_with(*args)
34
+ 2.times { yield *args }
35
+ end
36
+
33
37
  def self.raw_yield
34
38
  yield
35
39
  end
@@ -47,17 +51,28 @@ Feature: yield matchers
47
51
  describe "yield_control matcher" do
48
52
  specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control }
49
53
  specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_control }
54
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.twice }
55
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.exactly(2).times }
56
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.at_least(1) }
57
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.at_most(3).times }
50
58
 
51
59
  # deliberate failures
52
60
  specify { expect { |b| MyClass.yield_once_with(1, &b) }.not_to yield_control }
53
61
  specify { expect { |b| MyClass.dont_yield(&b) }.to yield_control }
62
+ specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control.at_least(2).times }
63
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.twice }
64
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_least(2).times }
65
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_least(1) }
66
+ specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_most(3).times }
54
67
  end
55
68
  """
56
69
  When I run `rspec yield_control_spec.rb`
57
70
  Then the output should contain all of these:
58
- | 4 examples, 2 failures |
59
- | expected given block to yield control |
60
- | expected given block not to yield control |
71
+ | 13 examples, 7 failures |
72
+ | expected given block to yield control |
73
+ | expected given block not to yield control |
74
+ | expected given block not to yield control at least twice |
75
+ | expected given block not to yield control at most 3 times |
61
76
 
62
77
  Scenario: yield_with_args matcher
63
78
  Given a file named "yield_with_args_spec.rb" with:
@@ -12,7 +12,7 @@ Feature: define diffable matcher
12
12
  match do |actual|
13
13
  actual == expected
14
14
  end
15
-
15
+
16
16
  diffable
17
17
  end
18
18
 
@@ -9,19 +9,19 @@ Feature: define matcher outside rspec
9
9
  """ruby
10
10
  require "rspec/expectations"
11
11
  require "test/unit"
12
-
12
+
13
13
  RSpec::Matchers.define :be_a_multiple_of do |expected|
14
14
  match do |actual|
15
15
  actual % expected == 0
16
16
  end
17
17
  end
18
-
18
+
19
19
  class Test::Unit::TestCase
20
20
  include RSpec::Matchers
21
21
  end
22
-
22
+
23
23
  class TestMultiples < Test::Unit::TestCase
24
-
24
+
25
25
  def test_9_should_be_a_multiple_of_3
26
26
  9.should be_a_multiple_of(3)
27
27
  end
@@ -29,10 +29,10 @@ Feature: define matcher outside rspec
29
29
  def test_9_should_be_a_multiple_of_4
30
30
  9.should be_a_multiple_of(4)
31
31
  end
32
-
32
+
33
33
  end
34
34
  """
35
35
  When I run `ruby test_multiples.rb`
36
- Then the exit status should not be 0
36
+ Then the exit status should not be 0
37
37
  And the output should contain "expected 9 to be a multiple of 4"
38
38
  And the output should contain "2 tests, 0 assertions, 0 failures, 1 errors"
@@ -1,7 +1,7 @@
1
1
  Feature: define matcher with fluent interface
2
2
 
3
3
  Use the chain() method to define matchers with a fluent interface.
4
-
4
+
5
5
  Scenario: chained method with argumetn
6
6
  Given a file named "between_spec.rb" with:
7
7
  """ruby
@@ -3,7 +3,7 @@ Feature: customized message
3
3
  RSpec tries to provide useful failure messages, but for cases in which you
4
4
  want more specific information, you can define your own message right in the
5
5
  example. This works for any matcher _other than the operator matchers_.
6
-
6
+
7
7
  Scenario: customize failure message
8
8
  Given a file named "example_spec.rb" with:
9
9
  """ruby
@@ -1,5 +1,14 @@
1
1
  require 'aruba/cucumber'
2
2
 
3
+ timeouts = { 'java' => 60 }
4
+
3
5
  Before do
4
- @aruba_timeout_seconds = 15
6
+ @aruba_timeout_seconds = timeouts.fetch(RUBY_PLATFORM) { 15 }
5
7
  end
8
+
9
+ Aruba.configure do |config|
10
+ config.before_cmd do |cmd|
11
+ set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
12
+ end
13
+ end if RUBY_PLATFORM == 'java'
14
+
@@ -47,6 +47,9 @@ Feature: Syntax Configuration
47
47
  config.expect_with :rspec do |c|
48
48
  c.syntax = :should
49
49
  end
50
+ config.mock_with :rspec do |c|
51
+ c.syntax = :should
52
+ end
50
53
  end
51
54
  """
52
55
  When I run `rspec disable_expect_syntax.rb syntaxes_spec.rb`
@@ -1,11 +1,12 @@
1
1
  Feature: Test::Unit integration
2
2
 
3
- RSpec-expectations is a stand-alone gem that can be used without
4
- the rest of RSpec. It can easily be used with another test
5
- framework such as Test::Unit if you like RSpec's should/should_not
6
- syntax but prefer the test organization of another framework.
3
+ RSpec-expectations is a stand-alone gem that can be used without the rest of
4
+ RSpec. If you like the way Test::Unit (or MiniTest) organizes tests, but
5
+ prefer RSpec's approach to expressing expectations, you can have both.
7
6
 
8
- Scenario: Basic Test::Unit usage
7
+ The one downside is that failures are reported as errors with MiniTest.
8
+
9
+ Scenario: use rspec/expectations with Test::Unit
9
10
  Given a file named "rspec_expectations_test.rb" with:
10
11
  """ruby
11
12
  require 'test/unit'
@@ -17,30 +18,27 @@ Feature: Test::Unit integration
17
18
  end
18
19
 
19
20
  def be_an_int
20
- RSpec.deprecate(:be_an_int, :be_an_integer)
21
+ # This is actually an internal rspec-expectations API, but is used
22
+ # here to demonstrate that deprecation warnings from within
23
+ # rspec-expectations work correcty without depending on rspec-core
24
+ RSpec.deprecate(:be_an_int, :replacement => :be_an_integer)
21
25
  be_an_integer
22
26
  end
23
27
 
24
28
  def test_passing_expectation
25
- x = 1 + 3
26
- x.should == 4
29
+ expect(1 + 3).to eq 4
27
30
  end
28
31
 
29
32
  def test_failing_expectation
30
- array = [1, 2]
31
- array.should be_empty
32
- end
33
-
34
- def test_expect_matcher
35
- expect { @a = 5 }.to change { @a }.from(nil).to(5)
33
+ expect([1,2]).to be_empty
36
34
  end
37
35
 
38
- def test_custom_matcher_and_deprecation_warning
39
- 1.should be_an_int
36
+ def test_custom_matcher_with_deprecation_warning
37
+ expect(1).to be_an_int
40
38
  end
41
39
  end
42
40
  """
43
41
  When I run `ruby rspec_expectations_test.rb`
44
- Then the output should contain "4 tests, 0 assertions, 1 failures, 0 errors" or "4 tests, 0 assertions, 0 failures, 1 errors"
42
+ Then the output should contain "3 tests, 0 assertions, 0 failures, 1 errors" or "3 tests, 0 assertions, 1 failures, 0 errors"
45
43
  And the output should contain "expected empty? to return true, got false"
46
44
  And the output should contain "be_an_int is deprecated"
@@ -37,7 +37,7 @@ module RSpec
37
37
  # `eq.failure_message_for_should_not`.
38
38
  #
39
39
  # rspec-expectations ships with a standard set of useful matchers, and writing
40
- # your own matchers is quite simple.
40
+ # your own matchers is quite simple.
41
41
  #
42
42
  # See [RSpec::Matchers](../RSpec/Matchers) for more information about the
43
43
  # built-in matchers that ship with rspec-expectations, and how to write your
@@ -1,38 +1,17 @@
1
1
  module RSpec
2
- unless respond_to?(:deprecate)
3
- class << self
4
- # Used internally by RSpec to display standard deprecation warnings.
5
- # This is also defined in rspec-core, but we can't assume it's loaded
6
- # since rspec-expectations should be usable w/o rspec-core.
7
- def deprecate(method, alternate_method=nil, version=nil)
8
- version_string = version ? "rspec-#{version}" : "a future version of RSpec"
9
-
10
- message = <<-NOTICE
11
-
12
- *****************************************************************
13
- DEPRECATION WARNING: you are using deprecated behaviour that will
14
- be removed from #{version_string}.
15
-
16
- #{caller(0)[2]}
17
-
18
- * #{method} is deprecated.
19
- NOTICE
20
- if alternate_method
21
- message << <<-ADDITIONAL
22
- * please use #{alternate_method} instead.
23
- ADDITIONAL
24
- end
25
-
26
- message << "*****************************************************************"
27
- warn_deprecation(message)
28
- end
29
-
30
- # Used internally by RSpec to display custom deprecation warnings. This
31
- # is also defined in rspec-core, but we can't assume it's loaded since
32
- # rspec-expectations should be usable w/o rspec-core.
33
- def warn_deprecation(message)
34
- warn(message)
2
+ module Expectations
3
+ module Deprecation
4
+ # @private
5
+ #
6
+ # Used internally to print deprecation warnings
7
+ def deprecate(deprecated, options={})
8
+ message = "DEPRECATION: #{deprecated} is deprecated."
9
+ message << " Use #{options[:replacement]} instead." if options[:replacement]
10
+ message << " Called from #{caller(0)[2]}."
11
+ warn message
35
12
  end
36
13
  end
37
14
  end
15
+
16
+ extend(Expectations::Deprecation) unless respond_to?(:deprecate)
38
17
  end
@@ -6,11 +6,11 @@ module RSpec
6
6
  module Expectations
7
7
  class Differ
8
8
  # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
9
- def diff_as_string(data_new, data_old)
10
- data_old = data_old.split(/\n/).map! { |e| e.chomp }
11
- data_new = data_new.split(/\n/).map! { |e| e.chomp }
9
+ def diff_as_string(input_data_new, input_data_old)
10
+ output = matching_encoding("", input_data_old)
11
+ data_old = input_data_old.split(matching_encoding("\n", input_data_old)).map! { |e| e.chomp }
12
+ data_new = input_data_new.split(matching_encoding("\n", input_data_new)).map! { |e| e.chomp }
12
13
  diffs = Diff::LCS.diff(data_old, data_new)
13
- output = ""
14
14
  return output if diffs.empty?
15
15
  oldhunk = hunk = nil
16
16
  file_length_difference = 0
@@ -33,16 +33,24 @@ module RSpec
33
33
  hunk.unshift(oldhunk)
34
34
  end
35
35
  else
36
- output << oldhunk.diff(format)
36
+ output << matching_encoding(oldhunk.diff(format).to_s, output)
37
37
  end
38
38
  ensure
39
39
  oldhunk = hunk
40
- output << "\n"
40
+ output << matching_encoding("\n", output)
41
41
  end
42
42
  end
43
43
  #Handle the last remaining hunk
44
- output << oldhunk.diff(format) << "\n"
44
+ output << matching_encoding(oldhunk.diff(format).to_s,output)
45
+ output << matching_encoding("\n",output)
45
46
  color_diff output
47
+ rescue Encoding::CompatibilityError
48
+ if input_data_new.encoding != input_data_old.encoding
49
+ "Could not produce a diff because the encoding of the actual string (#{input_data_old.encoding}) "+
50
+ "differs from the encoding of the expected string (#{input_data_new.encoding})"
51
+ else
52
+ "Could not produce a diff because of the encoding of the string (#{input_data_old.encoding})"
53
+ end
46
54
  end
47
55
 
48
56
  def diff_as_object(actual, expected)
@@ -108,6 +116,16 @@ module RSpec
108
116
  PP.pp(object,"")
109
117
  end
110
118
  end
119
+
120
+ if String.method_defined?(:encoding)
121
+ def matching_encoding(string, source)
122
+ string.encode(source.encoding)
123
+ end
124
+ else
125
+ def matching_encoding(string, source)
126
+ string
127
+ end
128
+ end
111
129
  end
112
130
 
113
131
  end
@@ -7,8 +7,8 @@ module RSpec
7
7
  # # used with `to`
8
8
  # expect(actual).to eq(3)
9
9
  #
10
- # # with `to_not`
11
- # expect(actual).to_not eq(3)
10
+ # # with `not_to`
11
+ # expect(actual).not_to eq(3)
12
12
  class ExpectationTarget
13
13
  class << self
14
14
  attr_accessor :deprecated_should_enabled
@@ -36,29 +36,28 @@ module RSpec
36
36
 
37
37
  # Runs the given expectation, passing if `matcher` returns false.
38
38
  # @example
39
- # expect(value).to_not eq(5)
40
39
  # expect(value).not_to eq(5)
41
40
  # @param [Matcher]
42
41
  # matcher
43
42
  # @param [String] message optional message to display when the expectation fails
44
43
  # @return [Boolean] false if the negative expectation succeeds (else raises)
45
44
  # @see RSpec::Matchers
46
- def to_not(matcher=nil, message=nil, &block)
47
- prevent_operator_matchers(:to_not, matcher)
45
+ def not_to(matcher=nil, message=nil, &block)
46
+ prevent_operator_matchers(:not_to, matcher)
48
47
  RSpec::Expectations::NegativeExpectationHandler.handle_matcher(@target, matcher, message, &block)
49
48
  end
50
- alias not_to to_not
49
+ alias to_not not_to
51
50
 
52
51
  def self.enable_deprecated_should
53
52
  return if deprecated_should_enabled?
54
53
 
55
54
  def should(*args)
56
- RSpec.deprecate "`expect { }.should`", "`expect { }.to`", 3
55
+ RSpec.deprecate "`expect { }.should`", :replacement => "`expect { }.to`"
57
56
  @target.should(*args)
58
57
  end
59
58
 
60
59
  def should_not(*args)
61
- RSpec.deprecate "`expect { }.should_not`", "`expect { }.to_not`", 3
60
+ RSpec.deprecate "`expect { }.should_not`", :replacement => "`expect { }.not_to`"
62
61
  @target.should_not(*args)
63
62
  end
64
63
 
@@ -6,17 +6,7 @@ module RSpec
6
6
  def const_missing(name)
7
7
  case name
8
8
  when :Rspec, :Spec
9
- RSpec.warn_deprecation <<-WARNING
10
- *****************************************************************
11
- DEPRECATION WARNING: you are using a deprecated constant that will
12
- be removed from a future version of RSpec.
13
-
14
- #{caller(0)[2]}
15
-
16
- * #{name} is deprecated.
17
- * RSpec is the new top-level module in RSpec-2
18
- ***************************************************************
19
- WARNING
9
+ RSpec.deprecate(name.to_s, :replacement => "RSpec")
20
10
  RSpec
21
11
  else
22
12
  begin
@@ -31,7 +21,7 @@ WARNING
31
21
 
32
22
  # @deprecated (no replacement)
33
23
  def differ=(ignore)
34
- RSpec.deprecate("RSpec::Expectations.differ=(differ)", "nothing at all (diffing is now automatic and no longer configurable)")
24
+ RSpec.deprecate("RSpec::Expectations.differ=(differ)")
35
25
  end
36
26
  end
37
27
  end