rspec-expectations 2.14.0 → 3.13.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.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +1 -1
  5. data/Changelog.md +976 -25
  6. data/{License.txt → LICENSE.md} +5 -3
  7. data/README.md +162 -26
  8. data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
  9. data/lib/rspec/expectations/configuration.rb +230 -0
  10. data/lib/rspec/expectations/expectation_target.rb +127 -51
  11. data/lib/rspec/expectations/fail_with.rb +17 -57
  12. data/lib/rspec/expectations/failure_aggregator.rb +229 -0
  13. data/lib/rspec/expectations/handler.rb +146 -32
  14. data/lib/rspec/expectations/minitest_integration.rb +58 -0
  15. data/lib/rspec/expectations/syntax.rb +68 -100
  16. data/lib/rspec/expectations/version.rb +1 -1
  17. data/lib/rspec/expectations.rb +58 -23
  18. data/lib/rspec/matchers/aliased_matcher.rb +116 -0
  19. data/lib/rspec/matchers/built_in/all.rb +86 -0
  20. data/lib/rspec/matchers/built_in/base_matcher.rb +191 -20
  21. data/lib/rspec/matchers/built_in/be.rb +114 -114
  22. data/lib/rspec/matchers/built_in/be_between.rb +77 -0
  23. data/lib/rspec/matchers/built_in/be_instance_of.rb +15 -4
  24. data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
  25. data/lib/rspec/matchers/built_in/be_within.rb +35 -18
  26. data/lib/rspec/matchers/built_in/change.rb +389 -80
  27. data/lib/rspec/matchers/built_in/compound.rb +290 -0
  28. data/lib/rspec/matchers/built_in/contain_exactly.rb +310 -0
  29. data/lib/rspec/matchers/built_in/count_expectation.rb +169 -0
  30. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  31. data/lib/rspec/matchers/built_in/eq.rb +30 -8
  32. data/lib/rspec/matchers/built_in/eql.rb +23 -8
  33. data/lib/rspec/matchers/built_in/equal.rb +55 -22
  34. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  35. data/lib/rspec/matchers/built_in/has.rb +141 -22
  36. data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
  37. data/lib/rspec/matchers/built_in/include.rb +184 -32
  38. data/lib/rspec/matchers/built_in/match.rb +95 -1
  39. data/lib/rspec/matchers/built_in/operators.rb +128 -0
  40. data/lib/rspec/matchers/built_in/output.rb +207 -0
  41. data/lib/rspec/matchers/built_in/raise_error.rb +192 -44
  42. data/lib/rspec/matchers/built_in/respond_to.rb +154 -28
  43. data/lib/rspec/matchers/built_in/satisfy.rb +39 -9
  44. data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
  45. data/lib/rspec/matchers/built_in/throw_symbol.rb +58 -14
  46. data/lib/rspec/matchers/built_in/yield.rb +240 -161
  47. data/lib/rspec/matchers/built_in.rb +47 -33
  48. data/lib/rspec/matchers/composable.rb +171 -0
  49. data/lib/rspec/matchers/dsl.rb +531 -10
  50. data/lib/rspec/matchers/english_phrasing.rb +58 -0
  51. data/lib/rspec/matchers/fail_matchers.rb +42 -0
  52. data/lib/rspec/matchers/generated_descriptions.rb +14 -8
  53. data/lib/rspec/matchers/matcher_delegator.rb +61 -0
  54. data/lib/rspec/matchers/matcher_protocol.rb +105 -0
  55. data/lib/rspec/matchers/multi_matcher_diff.rb +82 -0
  56. data/lib/rspec/matchers.rb +520 -173
  57. data.tar.gz.sig +0 -0
  58. metadata +141 -242
  59. metadata.gz.sig +2 -0
  60. data/features/README.md +0 -48
  61. data/features/Upgrade.md +0 -53
  62. data/features/built_in_matchers/README.md +0 -90
  63. data/features/built_in_matchers/be.feature +0 -175
  64. data/features/built_in_matchers/be_within.feature +0 -48
  65. data/features/built_in_matchers/cover.feature +0 -47
  66. data/features/built_in_matchers/end_with.feature +0 -48
  67. data/features/built_in_matchers/equality.feature +0 -139
  68. data/features/built_in_matchers/exist.feature +0 -45
  69. data/features/built_in_matchers/expect_change.feature +0 -59
  70. data/features/built_in_matchers/expect_error.feature +0 -144
  71. data/features/built_in_matchers/have.feature +0 -109
  72. data/features/built_in_matchers/include.feature +0 -174
  73. data/features/built_in_matchers/match.feature +0 -52
  74. data/features/built_in_matchers/operators.feature +0 -227
  75. data/features/built_in_matchers/predicates.feature +0 -137
  76. data/features/built_in_matchers/respond_to.feature +0 -84
  77. data/features/built_in_matchers/satisfy.feature +0 -33
  78. data/features/built_in_matchers/start_with.feature +0 -48
  79. data/features/built_in_matchers/throw_symbol.feature +0 -91
  80. data/features/built_in_matchers/types.feature +0 -116
  81. data/features/built_in_matchers/yield.feature +0 -161
  82. data/features/custom_matchers/access_running_example.feature +0 -53
  83. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  84. data/features/custom_matchers/define_matcher.feature +0 -368
  85. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
  86. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  87. data/features/customized_message.feature +0 -22
  88. data/features/diffing.feature +0 -85
  89. data/features/implicit_docstrings.feature +0 -52
  90. data/features/step_definitions/additional_cli_steps.rb +0 -22
  91. data/features/support/env.rb +0 -14
  92. data/features/syntax_configuration.feature +0 -71
  93. data/features/test_frameworks/test_unit.feature +0 -44
  94. data/lib/rspec/expectations/deprecation.rb +0 -17
  95. data/lib/rspec/expectations/differ.rb +0 -133
  96. data/lib/rspec/expectations/errors.rb +0 -9
  97. data/lib/rspec/expectations/extensions/array.rb +0 -9
  98. data/lib/rspec/expectations/extensions/object.rb +0 -29
  99. data/lib/rspec/expectations/extensions.rb +0 -2
  100. data/lib/rspec/matchers/be_close.rb +0 -9
  101. data/lib/rspec/matchers/built_in/have.rb +0 -124
  102. data/lib/rspec/matchers/built_in/match_array.rb +0 -51
  103. data/lib/rspec/matchers/built_in/start_and_end_with.rb +0 -48
  104. data/lib/rspec/matchers/compatibility.rb +0 -14
  105. data/lib/rspec/matchers/configuration.rb +0 -108
  106. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  107. data/lib/rspec/matchers/matcher.rb +0 -300
  108. data/lib/rspec/matchers/method_missing.rb +0 -12
  109. data/lib/rspec/matchers/operator_matcher.rb +0 -109
  110. data/lib/rspec/matchers/pretty.rb +0 -70
  111. data/lib/rspec/matchers/test_unit_integration.rb +0 -11
  112. data/lib/rspec-expectations.rb +0 -1
  113. data/spec/rspec/expectations/differ_spec.rb +0 -192
  114. data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
  115. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  116. data/spec/rspec/expectations/fail_with_spec.rb +0 -114
  117. data/spec/rspec/expectations/handler_spec.rb +0 -227
  118. data/spec/rspec/expectations/syntax_spec.rb +0 -139
  119. data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
  120. data/spec/rspec/matchers/be_close_spec.rb +0 -22
  121. data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
  122. data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
  123. data/spec/rspec/matchers/be_spec.rb +0 -516
  124. data/spec/rspec/matchers/be_within_spec.rb +0 -137
  125. data/spec/rspec/matchers/change_spec.rb +0 -553
  126. data/spec/rspec/matchers/configuration_spec.rb +0 -206
  127. data/spec/rspec/matchers/cover_spec.rb +0 -69
  128. data/spec/rspec/matchers/description_generation_spec.rb +0 -190
  129. data/spec/rspec/matchers/dsl_spec.rb +0 -57
  130. data/spec/rspec/matchers/eq_spec.rb +0 -60
  131. data/spec/rspec/matchers/eql_spec.rb +0 -41
  132. data/spec/rspec/matchers/equal_spec.rb +0 -78
  133. data/spec/rspec/matchers/exist_spec.rb +0 -124
  134. data/spec/rspec/matchers/has_spec.rb +0 -122
  135. data/spec/rspec/matchers/have_spec.rb +0 -455
  136. data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
  137. data/spec/rspec/matchers/include_spec.rb +0 -531
  138. data/spec/rspec/matchers/match_array_spec.rb +0 -194
  139. data/spec/rspec/matchers/match_spec.rb +0 -61
  140. data/spec/rspec/matchers/matcher_spec.rb +0 -471
  141. data/spec/rspec/matchers/matchers_spec.rb +0 -37
  142. data/spec/rspec/matchers/method_missing_spec.rb +0 -28
  143. data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
  144. data/spec/rspec/matchers/raise_error_spec.rb +0 -485
  145. data/spec/rspec/matchers/respond_to_spec.rb +0 -292
  146. data/spec/rspec/matchers/satisfy_spec.rb +0 -44
  147. data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
  148. data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
  149. data/spec/rspec/matchers/yield_spec.rb +0 -514
  150. data/spec/spec_helper.rb +0 -54
  151. data/spec/support/classes.rb +0 -56
  152. data/spec/support/in_sub_process.rb +0 -38
  153. data/spec/support/matchers.rb +0 -22
  154. data/spec/support/ruby_version.rb +0 -10
  155. data/spec/support/shared_examples.rb +0 -13
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Expectations
5
- # so our examples below can set expectations about the target
6
- ExpectationTarget.send(:attr_reader, :target)
7
-
8
- describe ExpectationTarget do
9
- context 'when constructed via #expect' do
10
- it 'constructs a new instance targetting the given argument' do
11
- expect(expect(7).target).to eq(7)
12
- end
13
-
14
- it 'constructs a new instance targetting the given block' do
15
- block = lambda {}
16
- expect(expect(&block).target).to be(block)
17
- end
18
-
19
- it 'raises an ArgumentError when given an argument and a block' do
20
- expect {
21
- expect(7) { }
22
- }.to raise_error(ArgumentError)
23
- end
24
-
25
- it 'raises an ArgumentError when given neither an argument nor a block' do
26
- expect {
27
- expect
28
- }.to raise_error(ArgumentError)
29
- end
30
-
31
- it 'can be passed nil' do
32
- expect(expect(nil).target).to be_nil
33
- end
34
-
35
- it 'passes a valid positive expectation' do
36
- expect(5).to eq(5)
37
- end
38
-
39
- it 'passes a valid negative expectation' do
40
- expect(5).not_to eq(4)
41
- end
42
-
43
- it 'passes a valid negative expectation with a split infinitive' do
44
- expect(5).to_not eq(4)
45
- end
46
-
47
- it 'fails an invalid positive expectation' do
48
- expect {
49
- expect(5).to eq(4)
50
- }.to fail_with(/expected: 4.*got: 5/m)
51
- end
52
-
53
- it 'fails an invalid negative expectation' do
54
- message = /expected 5 not to be a kind of Fixnum/
55
- expect {
56
- expect(5).not_to be_a(Fixnum)
57
- }.to fail_with(message)
58
- end
59
-
60
- it 'fails an invalid negative expectation with a split infinitive' do
61
- message = /expected 5 not to be a kind of Fixnum/
62
- expect {
63
- expect(5).to_not be_a(Fixnum)
64
- }.to fail_with(message)
65
- end
66
-
67
- it 'does not support operator matchers from #to' do
68
- expect {
69
- expect(3).to == 3
70
- }.to raise_error(ArgumentError)
71
- end
72
-
73
- it 'does not support operator matchers from #not_to' do
74
- expect {
75
- expect(3).not_to == 4
76
- }.to raise_error(ArgumentError)
77
- end
78
- end
79
- end
80
- end
81
- end
82
-
@@ -1,67 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Object, "#should" do
4
- before(:each) do
5
- @target = "target"
6
- @matcher = double("matcher")
7
- @matcher.stub(:matches?).and_return(true)
8
- @matcher.stub(:failure_message_for_should)
9
- end
10
-
11
- it "accepts and interacts with a matcher" do
12
- @matcher.should_receive(:matches?).with(@target).and_return(true)
13
- expect(@target).to @matcher
14
- end
15
-
16
- it "asks for a failure_message_for_should when matches? returns false" do
17
- @matcher.should_receive(:matches?).with(@target).and_return(false)
18
- @matcher.should_receive(:failure_message_for_should).and_return("the failure message")
19
- expect {
20
- expect(@target).to @matcher
21
- }.to fail_with("the failure message")
22
- end
23
-
24
- context "on interpretters that have BasicObject", :if => defined?(BasicObject) do
25
- let(:proxy_class) do
26
- Class.new(BasicObject) do
27
- def initialize(target)
28
- @target = target
29
- end
30
-
31
- def proxied?
32
- true
33
- end
34
-
35
- def method_missing(name, *args)
36
- @target.send(name, *args)
37
- end
38
- end
39
- end
40
-
41
- it 'works properly on BasicObject-subclassed proxy objects' do
42
- expect(proxy_class.new(Object.new)).to be_proxied
43
- end
44
- end
45
- end
46
-
47
- describe Object, "#should_not" do
48
- before(:each) do
49
- @target = "target"
50
- @matcher = double("matcher")
51
- end
52
-
53
- it "accepts and interacts with a matcher" do
54
- @matcher.should_receive(:matches?).with(@target).and_return(false)
55
- @matcher.stub(:failure_message_for_should_not)
56
-
57
- expect(@target).not_to @matcher
58
- end
59
-
60
- it "asks for a failure_message_for_should_not when matches? returns true" do
61
- @matcher.should_receive(:matches?).with(@target).and_return(true)
62
- @matcher.should_receive(:failure_message_for_should_not).and_return("the failure message for should not")
63
- expect {
64
- expect(@target).not_to @matcher
65
- }.to fail_with("the failure message for should not")
66
- end
67
- end
@@ -1,114 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
-
5
- describe RSpec::Expectations, "#fail_with with diff of arrays" do
6
- before { RSpec::Matchers.configuration.stub(:color? => false) }
7
-
8
- it "splits items with newlines" do
9
- expected_diff = "\nDiff:\n@@ -1 +1,3 @@\n+a\\nb\n+c\\nd\n"
10
- expect {
11
- RSpec::Expectations.fail_with("", [], ["a\nb", "c\nd"])
12
- }.to fail_with(expected_diff)
13
- end
14
-
15
- it "shows inner arrays on a single line" do
16
- expected_diff = "\nDiff:\n@@ -1 +1,3 @@\n+a\\nb\n+[\"c\\nd\"]\n"
17
- expect {
18
- RSpec::Expectations.fail_with("", [], ["a\nb", ["c\nd"]])
19
- }.to fail_with(expected_diff)
20
- end
21
- end
22
-
23
- describe RSpec::Expectations, "#fail_with with diff" do
24
- let(:differ) { double("differ") }
25
-
26
- before(:each) do
27
- RSpec::Expectations.stub(:differ) { differ }
28
- end
29
-
30
- it "calls differ if expected/actual are not strings (or numbers or procs)" do
31
- differ.should_receive(:diff_as_object).and_return("diff")
32
- expect {
33
- RSpec::Expectations.fail_with "the message", Object.new, Object.new
34
- }.to fail_with("the message\nDiff:diff")
35
- end
36
-
37
- context "with two strings" do
38
- context "and actual is multiline" do
39
- it "calls differ" do
40
- differ.should_receive(:diff_as_string).and_return("diff")
41
- expect {
42
- RSpec::Expectations.fail_with "the message", "expected\nthis", "actual"
43
- }.to fail_with("the message\nDiff:diff")
44
- end
45
- end
46
-
47
- context "and expected is multiline" do
48
- it "calls differ" do
49
- differ.should_receive(:diff_as_string).and_return("diff")
50
- expect {
51
- RSpec::Expectations.fail_with "the message", "expected", "actual\nthat"
52
- }.to fail_with("the message\nDiff:diff")
53
- end
54
- end
55
-
56
- context "and both are single line strings" do
57
- it "does not call differ" do
58
- differ.should_not_receive(:diff_as_string)
59
- expect {
60
- RSpec::Expectations.fail_with("the message", "expected", "actual")
61
- }.to fail_with("the message")
62
- end
63
- end
64
-
65
- context "and they are UTF-16LE encoded", :if => String.method_defined?(:encode) do
66
- it 'does not diff when they are not multiline' do
67
- differ.should_not_receive(:diff_as_string)
68
-
69
- str_1 = "This is a pile of poo: 💩".encode("UTF-16LE")
70
- str_2 = "This is a pile of poo: 💩".encode("UTF-16LE")
71
-
72
- expect {
73
- RSpec::Expectations.fail_with("the message", str_1, str_2)
74
- }.to fail_with("the message")
75
- end
76
-
77
- it 'diffs when they are multiline' do
78
- differ.should_receive(:diff_as_string).and_return("diff")
79
-
80
- str_1 = "This is a pile of poo:\n💩".encode("UTF-16LE")
81
- str_2 = "This is a pile of poo:\n💩".encode("UTF-16LE")
82
-
83
- expect {
84
- RSpec::Expectations.fail_with("the message", str_1, str_2)
85
- }.to fail_with("the message\nDiff:diff")
86
- end
87
- end
88
- end
89
-
90
- it "does not call differ if no expected/actual" do
91
- expect {
92
- RSpec::Expectations.fail_with "the message"
93
- }.to fail_with("the message")
94
- end
95
-
96
- it "does not call differ expected is Numeric" do
97
- expect {
98
- RSpec::Expectations.fail_with "the message", 1, "1"
99
- }.to fail_with("the message")
100
- end
101
-
102
- it "does not call differ when actual is Numeric" do
103
- expect {
104
- RSpec::Expectations.fail_with "the message", "1", 1
105
- }.to fail_with("the message")
106
- end
107
-
108
- it "does not call differ if expected or actual are procs" do
109
- expect {
110
- RSpec::Expectations.fail_with "the message", lambda {}, lambda {}
111
- }.to fail_with("the message")
112
- end
113
- end
114
-
@@ -1,227 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ExampleExpectations
4
-
5
- class ArbitraryMatcher
6
- def initialize(*args, &block)
7
- if args.last.is_a? Hash
8
- @expected = args.last[:expected]
9
- end
10
- @expected = block.call if block
11
- @block = block
12
- end
13
-
14
- def matches?(target)
15
- @target = target
16
- return @expected == target
17
- end
18
-
19
- def with(new_value)
20
- @expected = new_value
21
- self
22
- end
23
-
24
- def failure_message
25
- "expected #{@expected}, got #{@target}"
26
- end
27
-
28
- def negative_failure_message
29
- "expected not #{@expected}, got #{@target}"
30
- end
31
- end
32
-
33
- class PositiveOnlyMatcher < ArbitraryMatcher
34
- undef negative_failure_message rescue nil
35
- end
36
-
37
- def arbitrary_matcher(*args, &block)
38
- ArbitraryMatcher.new(*args, &block)
39
- end
40
-
41
- def positive_only_matcher(*args, &block)
42
- PositiveOnlyMatcher.new(*args, &block)
43
- end
44
-
45
- end
46
-
47
- module RSpec
48
- module Expectations
49
- describe PositiveExpectationHandler do
50
- describe "#handle_matcher" do
51
- it "asks the matcher if it matches" do
52
- matcher = double("matcher")
53
- actual = Object.new
54
- matcher.should_receive(:matches?).with(actual).and_return(true)
55
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)
56
- end
57
-
58
- it "returns the match value" do
59
- matcher = double("matcher")
60
- actual = Object.new
61
- matcher.should_receive(:matches?).with(actual).and_return(:this_value)
62
- expect(RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)).to eq :this_value
63
- end
64
-
65
- it "calls failure_message_for_should if the matcher implements it" do
66
- matcher = double("matcher", :failure_message_for_should => "message", :matches? => false)
67
- actual = Object.new
68
-
69
- ::RSpec::Expectations.should_receive(:fail_with).with("message")
70
-
71
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)
72
- end
73
-
74
- it "calls fail if matcher.diffable?" do
75
- matcher = double("matcher",
76
- :diffable? => true,
77
- :failure_message_for_should => "message",
78
- :matches? => false,
79
- :expected => 1,
80
- :actual => 2
81
- )
82
- actual = Object.new
83
-
84
- ::RSpec::Expectations.should_receive(:fail_with).with("message", 1, 2)
85
-
86
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)
87
- end
88
-
89
- it "calls failure_message if the matcher does not implement failure_message_for_should" do
90
- matcher = double("matcher", :failure_message => "message", :matches? => false)
91
- actual = Object.new
92
-
93
- ::RSpec::Expectations.should_receive(:fail_with).with("message")
94
-
95
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)
96
-
97
- end
98
-
99
- it "uses the custom failure message when one is provided" do
100
- matcher = double("matcher", :failure_message_for_should => "message", :matches? => false)
101
- actual = Object.new
102
-
103
- ::RSpec::Expectations.should_receive(:fail_with).with("custom")
104
-
105
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher, "custom")
106
- end
107
-
108
- it "uses the custom failure message when one is provided as a callable object" do
109
- matcher = double("matcher", :failure_message_for_should => "message", :matches? => false)
110
- actual = Object.new
111
-
112
- failure_message = double("failure message", :call => "custom")
113
-
114
- ::RSpec::Expectations.should_receive(:fail_with).with("custom")
115
-
116
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher, failure_message)
117
- end
118
- end
119
- end
120
-
121
- describe NegativeExpectationHandler do
122
- describe "#handle_matcher" do
123
- it "asks the matcher if it doesn't match when the matcher responds to #does_not_match?" do
124
- matcher = double("matcher", :does_not_match? => true, :negative_failure_message => nil)
125
- actual = Object.new
126
- matcher.should_receive(:does_not_match?).with(actual).and_return(true)
127
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
128
- end
129
-
130
- it "asks the matcher if it matches when the matcher doesn't respond to #does_not_match?" do
131
- matcher = double("matcher")
132
- actual = Object.new
133
- matcher.stub(:negative_failure_message)
134
- matcher.should_receive(:matches?).with(actual).and_return(false)
135
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
136
- end
137
-
138
- it "returns the match value" do
139
- matcher = double("matcher")
140
- actual = Object.new
141
- matcher.should_receive(:matches?).with(actual).and_return(false)
142
- matcher.stub(:negative_failure_message).and_return("ignore")
143
- expect(RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)).to be_false
144
- end
145
-
146
-
147
- it "calls failure_message_for_should_not if the matcher implements it" do
148
- matcher = double("matcher", :failure_message_for_should_not => "message", :matches? => true)
149
- actual = Object.new
150
-
151
- ::RSpec::Expectations.should_receive(:fail_with).with("message")
152
-
153
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
154
-
155
- end
156
-
157
- it "calls negative_failure_message if the matcher does not implement failure_message_for_should_not" do
158
- matcher = double("matcher", :negative_failure_message => "message", :matches? => true)
159
- actual = Object.new
160
-
161
- ::RSpec::Expectations.should_receive(:fail_with).with("message")
162
-
163
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
164
-
165
- end
166
-
167
-
168
- it "calls fail if matcher.diffable?" do
169
- matcher = double("matcher",
170
- :diffable? => true,
171
- :failure_message_for_should_not => "message",
172
- :matches? => true,
173
- :expected => 1,
174
- :actual => 2
175
- )
176
- actual = Object.new
177
-
178
- ::RSpec::Expectations.should_receive(:fail_with).with("message", 1, 2)
179
-
180
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
181
- end
182
-
183
- it "uses the custom failure message when one is provided" do
184
- matcher = double("matcher", :failure_message_for_should_not => "message", :matches? => true)
185
- actual = Object.new
186
-
187
- ::RSpec::Expectations.should_receive(:fail_with).with("custom")
188
-
189
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher, "custom")
190
- end
191
-
192
- it "uses the custom failure message when one is provided as a callable object" do
193
- matcher = double("matcher", :failure_message_for_should_not => "message", :matches? => true)
194
- actual = Object.new
195
-
196
- failure_message = double("failure message", :call => "custom")
197
-
198
- ::RSpec::Expectations.should_receive(:fail_with).with("custom")
199
-
200
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher, failure_message)
201
- end
202
- end
203
- end
204
-
205
- describe PositiveExpectationHandler do
206
- include ExampleExpectations
207
-
208
- it "handles submitted args" do
209
- expect(5).to arbitrary_matcher(:expected => 5)
210
- expect(5).to arbitrary_matcher(:expected => "wrong").with(5)
211
- expect { expect(5).to arbitrary_matcher(:expected => 4) }.to fail_with("expected 4, got 5")
212
- expect { expect(5).to arbitrary_matcher(:expected => 5).with(4) }.to fail_with("expected 4, got 5")
213
- expect(5).not_to arbitrary_matcher(:expected => 4)
214
- expect(5).not_to arbitrary_matcher(:expected => 5).with(4)
215
- expect { expect(5).not_to arbitrary_matcher(:expected => 5) }.to fail_with("expected not 5, got 5")
216
- expect { expect(5).not_to arbitrary_matcher(:expected => 4).with(5) }.to fail_with("expected not 5, got 5")
217
- end
218
-
219
- it "handles the submitted block" do
220
- expect(5).to arbitrary_matcher { 5 }
221
- expect(5).to arbitrary_matcher(:expected => 4) { 5 }
222
- expect(5).to arbitrary_matcher(:expected => 4).with(5) { 3 }
223
- end
224
-
225
- end
226
- end
227
- end
@@ -1,139 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Expectations
5
- describe Syntax do
6
- context "when passing a message to an expectation" do
7
- let(:warner) { ::Kernel }
8
-
9
- let(:string_like_object) do
10
- Struct.new(:to_str, :to_s).new(*(["Ceci n'est pas une Chaine."]*2))
11
- end
12
-
13
- let(:insufficiently_string_like_object) do
14
- Struct.new(:to_s).new("Ceci n'est pas une Chaine.")
15
- end
16
-
17
- let(:callable_object) do
18
- Struct.new(:call).new("Ceci n'est pas une Chaine.")
19
- end
20
-
21
- describe "expect(...).to" do
22
- it "prints a warning when the message object isn't a String" do
23
- warner.should_receive(:warn).with(/ignoring.*message/)
24
- expect(3).to eq(3), :not_a_string
25
- end
26
-
27
- it "doesn't print a warning when message is a String" do
28
- warner.should_not_receive(:warn)
29
- expect(3).to eq(3), "a string"
30
- end
31
-
32
- it "doesn't print a warning when message responds to to_str" do
33
- warner.should_not_receive(:warn)
34
- expect(3).to eq(3), string_like_object
35
- end
36
-
37
- it "prints a warning when the message object handles to_s but not to_str" do
38
- warner.should_receive(:warn).with(/ignoring.*message/)
39
- expect(3).to eq(3), insufficiently_string_like_object
40
- end
41
-
42
- it "doesn't print a warning when message responds to call" do
43
- warner.should_not_receive(:warn)
44
- expect(3).to eq(3), callable_object
45
- end
46
- end
47
-
48
- describe "expect(...).not_to" do
49
- it "prints a warning when the message object isn't a String" do
50
- warner.should_receive(:warn).with(/ignoring.*message/)
51
- expect(3).not_to eq(4), :not_a_string
52
- end
53
-
54
- it "doesn't print a warning when message is a String" do
55
- warner.should_not_receive(:warn)
56
- expect(3).not_to eq(4), "a string"
57
- end
58
-
59
- it "doesn't print a warning when message responds to to_str" do
60
- warner.should_not_receive(:warn)
61
- expect(3).not_to eq(4), string_like_object
62
- end
63
-
64
- it "prints a warning when the message object handles to_s but not to_str" do
65
- warner.should_receive(:warn).with(/ignoring.*message/)
66
- expect(3).not_to eq(4), insufficiently_string_like_object
67
- end
68
-
69
- it "doesn't print a warning when message responds to call" do
70
- warner.should_not_receive(:warn)
71
- expect(3).not_to eq(4), callable_object
72
- end
73
- end
74
- end
75
-
76
- describe "expression generation" do
77
- let(:target) { "foo" }
78
- let(:expectation) { "eq('bar')" }
79
- let(:positive_expect_example) { "expect(foo).to eq('bar')" }
80
- let(:positive_should_example) { "foo.should eq('bar')" }
81
- let(:negative_expect_example) { "expect(foo).not_to eq('bar')" }
82
- let(:negative_should_example) { "foo.should_not eq('bar')" }
83
-
84
- def positive_expression
85
- Syntax.positive_expression(target, expectation)
86
- end
87
-
88
- def negative_expression
89
- Syntax.negative_expression(target, expectation)
90
- end
91
-
92
- context "when only :expect is enabled" do
93
- before do
94
- expect(Syntax.should_enabled?).to be_false
95
- expect(Syntax.expect_enabled?).to be_true
96
- end
97
-
98
- it 'generates a positive expression using the expect syntax' do
99
- expect(positive_expression).to eq(positive_expect_example)
100
- end
101
-
102
- it 'generates a negative expression using the expect syntax' do
103
- expect(negative_expression).to eq(negative_expect_example)
104
- end
105
- end
106
-
107
- context "when both :should and :expect are enabled", :uses_should do
108
- before do
109
- expect(Syntax.should_enabled?).to be_true
110
- expect(Syntax.expect_enabled?).to be_true
111
- end
112
-
113
- it 'generates a positive expression using the expect syntax' do
114
- expect(positive_expression).to eq(positive_expect_example)
115
- end
116
-
117
- it 'generates a negative expression using the expect syntax' do
118
- expect(negative_expression).to eq(negative_expect_example)
119
- end
120
- end
121
-
122
- context "when only :should is enabled", :uses_only_should do
123
- before do
124
- Syntax.should_enabled?.should be_true
125
- Syntax.expect_enabled?.should be_false
126
- end
127
-
128
- it 'generates a positive expression using the expect syntax' do
129
- positive_expression.should eq(positive_should_example)
130
- end
131
-
132
- it 'generates a negative expression using the expect syntax' do
133
- negative_expression.should eq(negative_should_example)
134
- end
135
- end
136
- end
137
- end
138
- end
139
- end
@@ -1,62 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec::Matchers::BuiltIn
4
- describe BaseMatcher do
5
- describe "#match_unless_raises" do
6
- let(:matcher) do
7
- Class.new(BaseMatcher).new
8
- end
9
-
10
- it "returns true if there are no errors" do
11
- expect(matcher.match_unless_raises {}).to be_true
12
- end
13
-
14
- it "returns false if there is an error" do
15
- expect(matcher.match_unless_raises { raise }).to be_false
16
- end
17
-
18
- it "returns false if the only submitted error is raised" do
19
- expect(matcher.match_unless_raises(RuntimeError){ raise "foo" }).to be_false
20
- end
21
-
22
- it "returns false if any of several errors submitted is raised" do
23
- expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise "foo" }).to be_false
24
- expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise ArgumentError.new('') }).to be_false
25
- expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise NameError.new('') }).to be_false
26
- end
27
-
28
- it "re-raises any error other than one of those specified" do
29
- expect do
30
- matcher.match_unless_raises(ArgumentError){ raise "foo" }
31
- end.to raise_error
32
- end
33
-
34
- it "stores the rescued exception for use in messages" do
35
- matcher.match_unless_raises(RuntimeError){ raise "foo" }
36
- expect(matcher.rescued_exception).to be_a(RuntimeError)
37
- expect(matcher.rescued_exception.message).to eq("foo")
38
- end
39
-
40
- end
41
-
42
- describe "#==" do
43
- it "responds the same way as matches?" do
44
- matcher = Class.new(BaseMatcher) do
45
- def initialize(expected)
46
- @expected = expected
47
- end
48
-
49
- def matches?(actual)
50
- (@actual = actual) == @expected
51
- end
52
- end
53
-
54
- expect(matcher.new(3).matches?(3)).to be_true
55
- expect(matcher.new(3)).to eq(3)
56
-
57
- expect(matcher.new(3).matches?(4)).to be_false
58
- expect(matcher.new(3)).not_to eq(4)
59
- end
60
- end
61
- end
62
- end