rspec-expectations 2.11.3 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/Changelog.md +25 -2
  2. data/README.md +5 -1
  3. data/features/built_in_matchers/be.feature +4 -4
  4. data/features/built_in_matchers/be_within.feature +1 -1
  5. data/features/built_in_matchers/cover.feature +1 -1
  6. data/features/built_in_matchers/end_with.feature +2 -2
  7. data/features/built_in_matchers/equality.feature +5 -5
  8. data/features/built_in_matchers/exist.feature +1 -1
  9. data/features/built_in_matchers/expect_change.feature +3 -3
  10. data/features/built_in_matchers/expect_error.feature +4 -4
  11. data/features/built_in_matchers/have.feature +2 -2
  12. data/features/built_in_matchers/include.feature +3 -3
  13. data/features/built_in_matchers/match.feature +2 -2
  14. data/features/built_in_matchers/operators.feature +3 -3
  15. data/features/built_in_matchers/predicates.feature +9 -8
  16. data/features/built_in_matchers/respond_to.feature +2 -2
  17. data/features/built_in_matchers/satisfy.feature +1 -1
  18. data/features/built_in_matchers/start_with.feature +2 -2
  19. data/features/built_in_matchers/throw_symbol.feature +3 -3
  20. data/features/built_in_matchers/types.feature +2 -2
  21. data/features/built_in_matchers/yield.feature +5 -5
  22. data/features/custom_matchers/access_running_example.feature +3 -3
  23. data/features/custom_matchers/define_diffable_matcher.feature +1 -1
  24. data/features/custom_matchers/define_matcher.feature +12 -12
  25. data/features/custom_matchers/define_matcher_outside_rspec.feature +1 -1
  26. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
  27. data/features/customized_message.feature +1 -1
  28. data/features/diffing.feature +4 -4
  29. data/features/implicit_docstrings.feature +2 -2
  30. data/features/test_frameworks/test_unit.feature +1 -1
  31. data/lib/rspec/expectations/differ.rb +35 -1
  32. data/lib/rspec/expectations/handler.rb +21 -6
  33. data/lib/rspec/expectations/version.rb +1 -1
  34. data/lib/rspec/matchers/built_in/be_instance_of.rb +4 -0
  35. data/lib/rspec/matchers/built_in/include.rb +3 -1
  36. data/lib/rspec/matchers/built_in/match_array.rb +12 -6
  37. data/lib/rspec/matchers/built_in/raise_error.rb +17 -7
  38. data/lib/rspec/matchers/built_in/yield.rb +5 -5
  39. data/lib/rspec/matchers/configuration.rb +42 -0
  40. data/lib/rspec/matchers/generated_descriptions.rb +2 -3
  41. data/spec/rspec/expectations/differ_spec.rb +24 -0
  42. data/spec/rspec/expectations/handler_spec.rb +40 -40
  43. data/spec/rspec/expectations/syntax_spec.rb +35 -0
  44. data/spec/rspec/matchers/be_instance_of_spec.rb +19 -2
  45. data/spec/rspec/matchers/be_spec.rb +10 -10
  46. data/spec/rspec/matchers/configuration_spec.rb +155 -123
  47. data/spec/rspec/matchers/dsl_spec.rb +8 -8
  48. data/spec/rspec/matchers/have_spec.rb +8 -38
  49. data/spec/rspec/matchers/include_spec.rb +6 -0
  50. data/spec/rspec/matchers/match_array_spec.rb +14 -0
  51. data/spec/rspec/matchers/raise_error_spec.rb +89 -38
  52. data/spec/rspec/matchers/start_with_end_with_spec.rb +1 -1
  53. data/spec/rspec/matchers/yield_spec.rb +35 -0
  54. metadata +70 -78
@@ -16,7 +16,7 @@ Feature: throw_symbol matcher
16
16
 
17
17
  Scenario: basic usage
18
18
  Given a file named "throw_symbol_matcher_spec.rb" with:
19
- """
19
+ """ruby
20
20
  describe "throw" do
21
21
  specify { expect { throw :foo }.to throw_symbol }
22
22
  specify { expect { throw :bar, 7 }.to throw_symbol }
@@ -37,7 +37,7 @@ Feature: throw_symbol matcher
37
37
 
38
38
  Scenario: specify thrown symbol
39
39
  Given a file named "throw_symbol_matcher_spec.rb" with:
40
- """
40
+ """ruby
41
41
  describe "throw symbol" do
42
42
  specify { expect { throw :foo }.to throw_symbol(:foo) }
43
43
  specify { expect { throw :foo, 7 }.to throw_symbol(:foo) }
@@ -61,7 +61,7 @@ Feature: throw_symbol matcher
61
61
 
62
62
  Scenario: specify thrown symbol and argument
63
63
  Given a file named "throw_symbol_argument_matcher_spec.rb" with:
64
- """
64
+ """ruby
65
65
  describe "throw symbol with argument" do
66
66
  specify { expect { throw :foo, 7 }.to throw_symbol(:foo, 7) }
67
67
  specify { expect { throw :foo, 8 }.to_not throw_symbol(:foo, 7) }
@@ -17,7 +17,7 @@ Feature: specify types of objects
17
17
 
18
18
  Scenario: be_(a_)kind_of matcher
19
19
  Given a file named "be_kind_of_matcher_spec.rb" with:
20
- """
20
+ """ruby
21
21
  module MyModule; end
22
22
 
23
23
  class Fixnum
@@ -70,7 +70,7 @@ Feature: specify types of objects
70
70
 
71
71
  Scenario: be_(an_)instance_of matcher
72
72
  Given a file named "be_instance_of_matcher_spec.rb" with:
73
- """
73
+ """ruby
74
74
  module MyModule; end
75
75
 
76
76
  class Fixnum
@@ -24,7 +24,7 @@ Feature: yield matchers
24
24
 
25
25
  Background:
26
26
  Given a file named "my_class.rb" with:
27
- """
27
+ """ruby
28
28
  class MyClass
29
29
  def self.yield_once_with(*args)
30
30
  yield *args
@@ -41,7 +41,7 @@ Feature: yield matchers
41
41
 
42
42
  Scenario: yield_control matcher
43
43
  Given a file named "yield_control_spec.rb" with:
44
- """
44
+ """ruby
45
45
  require './my_class'
46
46
 
47
47
  describe "yield_control matcher" do
@@ -61,7 +61,7 @@ Feature: yield matchers
61
61
 
62
62
  Scenario: yield_with_args matcher
63
63
  Given a file named "yield_with_args_spec.rb" with:
64
- """
64
+ """ruby
65
65
  require './my_class'
66
66
 
67
67
  describe "yield_with_args matcher" do
@@ -94,7 +94,7 @@ Feature: yield matchers
94
94
 
95
95
  Scenario: yield_with_no_args matcher
96
96
  Given a file named "yield_with_no_args_spec.rb" with:
97
- """
97
+ """ruby
98
98
  require './my_class'
99
99
 
100
100
  describe "yield_with_no_args matcher" do
@@ -117,7 +117,7 @@ Feature: yield matchers
117
117
 
118
118
  Scenario: yield_successive_args matcher
119
119
  Given a file named "yield_successive_args_spec.rb" with:
120
- """
120
+ """ruby
121
121
  def array
122
122
  [1, 2, 3]
123
123
  end
@@ -11,7 +11,7 @@ Feature: access running example
11
11
 
12
12
  Scenario: call method defined on example from matcher
13
13
  Given a file named "example_spec.rb" with:
14
- """
14
+ """ruby
15
15
  RSpec::Matchers.define :bar do
16
16
  match do |_|
17
17
  foo == "foo"
@@ -33,7 +33,7 @@ Feature: access running example
33
33
 
34
34
  Scenario: call method _not_ defined on example from matcher
35
35
  Given a file named "example_spec.rb" with:
36
- """
36
+ """ruby
37
37
  RSpec::Matchers.define :bar do
38
38
  match do |_|
39
39
  foo == "foo"
@@ -48,6 +48,6 @@ Feature: access running example
48
48
  """
49
49
  When I run `rspec ./example_spec.rb`
50
50
  Then the output should contain "1 example, 1 failure"
51
- And the output should contain "undefined local variable"
51
+ And the output should match /undefined.*method/
52
52
  And the output should contain "RSpec::Matchers::DSL::Matcher"
53
53
  And the output should not contain "ExampleGroup"
@@ -7,7 +7,7 @@ Feature: define diffable matcher
7
7
  @wip
8
8
  Scenario: define a diffable matcher
9
9
  Given a file named "diffable_matcher_spec.rb" with:
10
- """
10
+ """ruby
11
11
  RSpec::Matchers.define :be_just_like do |expected|
12
12
  match do |actual|
13
13
  actual == expected
@@ -6,7 +6,7 @@ Feature: define matcher
6
6
 
7
7
  Scenario: define a matcher with default messages
8
8
  Given a file named "matcher_with_default_message_spec.rb" with:
9
- """
9
+ """ruby
10
10
  require 'rspec/expectations'
11
11
 
12
12
  RSpec::Matchers.define :be_a_multiple_of do |expected|
@@ -48,7 +48,7 @@ Feature: define matcher
48
48
 
49
49
  Scenario: overriding the failure_message_for_should
50
50
  Given a file named "matcher_with_failure_message_spec.rb" with:
51
- """
51
+ """ruby
52
52
  require 'rspec/expectations'
53
53
 
54
54
  RSpec::Matchers.define :be_a_multiple_of do |expected|
@@ -72,7 +72,7 @@ Feature: define matcher
72
72
 
73
73
  Scenario: overriding the failure_message_for_should_not
74
74
  Given a file named "matcher_with_failure_for_message_spec.rb" with:
75
- """
75
+ """ruby
76
76
  require 'rspec/expectations'
77
77
 
78
78
  RSpec::Matchers.define :be_a_multiple_of do |expected|
@@ -96,7 +96,7 @@ Feature: define matcher
96
96
 
97
97
  Scenario: overriding the description
98
98
  Given a file named "matcher_overriding_description_spec.rb" with:
99
- """
99
+ """ruby
100
100
  require 'rspec/expectations'
101
101
 
102
102
  RSpec::Matchers.define :be_a_multiple_of do |expected|
@@ -124,7 +124,7 @@ Feature: define matcher
124
124
 
125
125
  Scenario: with no args
126
126
  Given a file named "matcher_with_no_args_spec.rb" with:
127
- """
127
+ """ruby
128
128
  require 'rspec/expectations'
129
129
 
130
130
  RSpec::Matchers.define :have_7_fingers do
@@ -148,7 +148,7 @@ Feature: define matcher
148
148
 
149
149
  Scenario: with multiple args
150
150
  Given a file named "matcher_with_multiple_args_spec.rb" with:
151
- """
151
+ """ruby
152
152
  require 'rspec/expectations'
153
153
 
154
154
  RSpec::Matchers.define :be_the_sum_of do |a,b,c,d|
@@ -168,7 +168,7 @@ Feature: define matcher
168
168
 
169
169
  Scenario: with helper methods
170
170
  Given a file named "matcher_with_internal_helper_spec.rb" with:
171
- """
171
+ """ruby
172
172
  require 'rspec/expectations'
173
173
 
174
174
  RSpec::Matchers.define :have_same_elements_as do |sample|
@@ -193,7 +193,7 @@ Feature: define matcher
193
193
 
194
194
  Scenario: scoped in a module
195
195
  Given a file named "scoped_matcher_spec.rb" with:
196
- """
196
+ """ruby
197
197
  require 'rspec/expectations'
198
198
 
199
199
  module MyHelpers
@@ -225,7 +225,7 @@ Feature: define matcher
225
225
 
226
226
  Scenario: scoped in an example group
227
227
  Given a file named "scoped_matcher_spec.rb" with:
228
- """
228
+ """ruby
229
229
  require 'rspec/expectations'
230
230
 
231
231
  describe "group with matcher" do
@@ -259,7 +259,7 @@ Feature: define matcher
259
259
 
260
260
  Scenario: matcher with separate logic for should and should_not
261
261
  Given a file named "matcher_with_separate_should_not_logic_spec.rb" with:
262
- """
262
+ """ruby
263
263
  RSpec::Matchers.define :contain do |*expected|
264
264
  match_for_should do |actual|
265
265
  expected.all? { |e| actual.include?(e) }
@@ -287,7 +287,7 @@ Feature: define matcher
287
287
 
288
288
  Scenario: use define_method to create a helper method with access to matcher params
289
289
  Given a file named "define_method_spec.rb" with:
290
- """
290
+ """ruby
291
291
  RSpec::Matchers.define :be_a_multiple_of do |expected|
292
292
  define_method :is_multiple? do |actual|
293
293
  actual % expected == 0
@@ -312,7 +312,7 @@ Feature: define matcher
312
312
 
313
313
  Scenario: include a module with helper methods in the matcher
314
314
  Given a file named "include_module_spec.rb" with:
315
- """
315
+ """ruby
316
316
  module MatcherHelpers
317
317
  def is_multiple?(actual, expected)
318
318
  actual % expected == 0
@@ -6,7 +6,7 @@ Feature: define matcher outside rspec
6
6
 
7
7
  Scenario: define a matcher with default messages
8
8
  Given a file named "test_multiples.rb" with:
9
- """
9
+ """ruby
10
10
  require "rspec/expectations"
11
11
  require "test/unit"
12
12
 
@@ -4,7 +4,7 @@ Feature: define matcher with fluent interface
4
4
 
5
5
  Scenario: chained method with argumetn
6
6
  Given a file named "between_spec.rb" with:
7
- """
7
+ """ruby
8
8
  RSpec::Matchers.define :be_bigger_than do |first|
9
9
  match do |actual|
10
10
  (actual > first) && (actual < @second)
@@ -6,7 +6,7 @@ Feature: customized message
6
6
 
7
7
  Scenario: customize failure message
8
8
  Given a file named "example_spec.rb" with:
9
- """
9
+ """ruby
10
10
  describe Array do
11
11
  context "when created with `new`" do
12
12
  it "is empty" do
@@ -4,7 +4,7 @@ Feature: diffing
4
4
 
5
5
  Scenario: diff for a multiline string
6
6
  Given a file named "example_spec.rb" with:
7
- """
7
+ """ruby
8
8
  describe "a multiline string" do
9
9
  it "is like another string" do
10
10
  expected = <<-EXPECTED
@@ -34,7 +34,7 @@ Feature: diffing
34
34
 
35
35
  Scenario: diff for a multiline string and a regexp
36
36
  Given a file named "example_spec.rb" with:
37
- """
37
+ """ruby
38
38
  describe "a multiline string" do
39
39
  it "is like another string" do
40
40
  expected = /expected/m
@@ -60,7 +60,7 @@ Feature: diffing
60
60
 
61
61
  Scenario: no diff for a single line strings
62
62
  Given a file named "example_spec.rb" with:
63
- """
63
+ """ruby
64
64
  describe "a single line string" do
65
65
  it "is like another string" do
66
66
  expected = "this string"
@@ -74,7 +74,7 @@ Feature: diffing
74
74
 
75
75
  Scenario: no diff for numbers
76
76
  Given a file named "example_spec.rb" with:
77
- """
77
+ """ruby
78
78
  describe "a number" do
79
79
  it "is like another number" do
80
80
  1.should eq(2)
@@ -6,7 +6,7 @@ Feature: implicit docstrings
6
6
 
7
7
  Scenario: run passing examples
8
8
  Given a file named "implicit_docstrings_spec.rb" with:
9
- """
9
+ """ruby
10
10
  describe "Examples with no docstrings generate their own:" do
11
11
 
12
12
  specify { 3.should be < 5 }
@@ -26,7 +26,7 @@ Feature: implicit docstrings
26
26
 
27
27
  Scenario: run failing examples
28
28
  Given a file named "failing_implicit_docstrings_spec.rb" with:
29
- """
29
+ """ruby
30
30
  describe "Failing examples with no descriptions" do
31
31
 
32
32
  # description is auto-generated as "should equal(5)" based on the last #should
@@ -7,7 +7,7 @@ Feature: Test::Unit integration
7
7
 
8
8
  Scenario: Basic Test::Unit usage
9
9
  Given a file named "rspec_expectations_test.rb" with:
10
- """
10
+ """ruby
11
11
  require 'test/unit'
12
12
  require 'rspec/expectations'
13
13
 
@@ -36,6 +36,7 @@ module RSpec
36
36
  end
37
37
  #Handle the last remaining hunk
38
38
  output << oldhunk.diff(format) << "\n"
39
+ color_diff output
39
40
  end
40
41
 
41
42
  def diff_as_object(actual, expected)
@@ -48,7 +49,7 @@ module RSpec
48
49
  "between #{actual} and #{expected} is empty. Check the " \
49
50
  "implementation of #{actual}.==."
50
51
  else
51
- diff
52
+ color_diff diff
52
53
  end
53
54
  end
54
55
 
@@ -62,6 +63,39 @@ module RSpec
62
63
  3
63
64
  end
64
65
 
66
+ def color(text, color_code)
67
+ "\e[#{color_code}m#{text}\e[0m"
68
+ end
69
+
70
+ def red(text)
71
+ color(text, 31)
72
+ end
73
+
74
+ def green(text)
75
+ color(text, 32)
76
+ end
77
+
78
+ def blue(text)
79
+ color(text, 34)
80
+ end
81
+
82
+ def color_diff(diff)
83
+ return diff unless RSpec::Matchers.configuration.color?
84
+
85
+ diff.lines.map { |line|
86
+ case line[0].chr
87
+ when "+"
88
+ green line
89
+ when "-"
90
+ red line
91
+ when "@"
92
+ line[1].chr == "@" ? blue(line) : line
93
+ else
94
+ line
95
+ end
96
+ }.join
97
+ end
98
+
65
99
  def object_to_string(object)
66
100
  case object
67
101
  when Hash
@@ -1,18 +1,32 @@
1
1
  module RSpec
2
2
  module Expectations
3
- class PositiveExpectationHandler
3
+
4
+ class ExpectationHandler
5
+ def self.message_must_be_string(msg)
6
+ "WARNING: ignoring the provided expectation message argument " +
7
+ "(#{msg.inspect}) since it is not a string."
8
+ end
9
+
10
+ def self.check_message(msg)
11
+ ::Kernel.warn message_must_be_string(msg) unless msg.nil? || msg.is_a?(String)
12
+ end
13
+ end
14
+
15
+ class PositiveExpectationHandler < ExpectationHandler
16
+
4
17
  def self.handle_matcher(actual, matcher, message=nil, &block)
18
+ check_message(message)
5
19
  ::RSpec::Matchers.last_should = :should
6
20
  ::RSpec::Matchers.last_matcher = matcher
7
21
  return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) if matcher.nil?
8
22
 
9
23
  match = matcher.matches?(actual, &block)
10
24
  return match if match
11
-
25
+
12
26
  message ||= matcher.respond_to?(:failure_message_for_should) ?
13
27
  matcher.failure_message_for_should :
14
28
  matcher.failure_message
15
-
29
+
16
30
  if matcher.respond_to?(:diffable?) && matcher.diffable?
17
31
  ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
18
32
  else
@@ -21,17 +35,18 @@ module RSpec
21
35
  end
22
36
  end
23
37
 
24
- class NegativeExpectationHandler
38
+ class NegativeExpectationHandler < ExpectationHandler
25
39
  def self.handle_matcher(actual, matcher, message=nil, &block)
40
+ check_message(message)
26
41
  ::RSpec::Matchers.last_should = :should_not
27
42
  ::RSpec::Matchers.last_matcher = matcher
28
43
  return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) if matcher.nil?
29
-
44
+
30
45
  match = matcher.respond_to?(:does_not_match?) ?
31
46
  !matcher.does_not_match?(actual, &block) :
32
47
  matcher.matches?(actual, &block)
33
48
  return match unless match
34
-
49
+
35
50
  message ||= matcher.respond_to?(:failure_message_for_should_not) ?
36
51
  matcher.failure_message_for_should_not :
37
52
  matcher.negative_failure_message
@@ -2,7 +2,7 @@ module RSpec
2
2
  module Expectations
3
3
  # @private
4
4
  module Version
5
- STRING = '2.11.3'
5
+ STRING = '2.12.0'
6
6
  end
7
7
  end
8
8
  end
@@ -5,6 +5,10 @@ module RSpec
5
5
  def match(expected, actual)
6
6
  actual.instance_of? expected
7
7
  end
8
+
9
+ def description
10
+ "be an instance of #{expected}"
11
+ end
8
12
  end
9
13
  end
10
14
  end