rspec-expectations 3.0.0.beta2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -2
  4. data/.yardopts +0 -1
  5. data/Changelog.md +115 -35
  6. data/README.md +2 -2
  7. data/lib/rspec/expectations.rb +13 -8
  8. data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
  9. data/lib/rspec/expectations/expectation_target.rb +72 -8
  10. data/lib/rspec/expectations/fail_with.rb +10 -52
  11. data/lib/rspec/expectations/handler.rb +9 -11
  12. data/lib/rspec/expectations/syntax.rb +37 -35
  13. data/lib/rspec/expectations/version.rb +1 -1
  14. data/lib/rspec/matchers.rb +60 -9
  15. data/lib/rspec/matchers/aliased_matcher.rb +6 -0
  16. data/lib/rspec/matchers/built_in.rb +9 -1
  17. data/lib/rspec/matchers/built_in/all.rb +78 -0
  18. data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
  19. data/lib/rspec/matchers/built_in/be.rb +117 -42
  20. data/lib/rspec/matchers/built_in/be_between.rb +22 -0
  21. data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
  22. data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
  23. data/lib/rspec/matchers/built_in/be_within.rb +26 -6
  24. data/lib/rspec/matchers/built_in/change.rb +89 -13
  25. data/lib/rspec/matchers/built_in/compound.rb +19 -3
  26. data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
  27. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  28. data/lib/rspec/matchers/built_in/eq.rb +20 -5
  29. data/lib/rspec/matchers/built_in/eql.rb +15 -3
  30. data/lib/rspec/matchers/built_in/equal.rb +23 -6
  31. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  32. data/lib/rspec/matchers/built_in/has.rb +58 -3
  33. data/lib/rspec/matchers/built_in/include.rb +16 -1
  34. data/lib/rspec/matchers/built_in/match.rb +14 -4
  35. data/lib/rspec/matchers/built_in/operators.rb +16 -0
  36. data/lib/rspec/matchers/built_in/output.rb +47 -5
  37. data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
  38. data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
  39. data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
  40. data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
  41. data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
  42. data/lib/rspec/matchers/built_in/yield.rb +148 -44
  43. data/lib/rspec/matchers/composable.rb +48 -7
  44. data/lib/rspec/matchers/dsl.rb +45 -17
  45. data/lib/rspec/matchers/generated_descriptions.rb +7 -0
  46. data/lib/rspec/matchers/matcher_delegator.rb +6 -2
  47. data/lib/rspec/matchers/pretty.rb +15 -19
  48. metadata +33 -236
  49. metadata.gz.sig +0 -0
  50. data/features/README.md +0 -48
  51. data/features/Upgrade.md +0 -53
  52. data/features/built_in_matchers/README.md +0 -96
  53. data/features/built_in_matchers/be.feature +0 -175
  54. data/features/built_in_matchers/be_within.feature +0 -48
  55. data/features/built_in_matchers/comparisons.feature +0 -97
  56. data/features/built_in_matchers/contain_exactly.feature +0 -46
  57. data/features/built_in_matchers/cover.feature +0 -47
  58. data/features/built_in_matchers/end_with.feature +0 -48
  59. data/features/built_in_matchers/equality.feature +0 -136
  60. data/features/built_in_matchers/exist.feature +0 -45
  61. data/features/built_in_matchers/expect_change.feature +0 -59
  62. data/features/built_in_matchers/expect_error.feature +0 -144
  63. data/features/built_in_matchers/include.feature +0 -126
  64. data/features/built_in_matchers/match.feature +0 -51
  65. data/features/built_in_matchers/output.feature +0 -70
  66. data/features/built_in_matchers/predicates.feature +0 -161
  67. data/features/built_in_matchers/respond_to.feature +0 -84
  68. data/features/built_in_matchers/satisfy.feature +0 -33
  69. data/features/built_in_matchers/start_with.feature +0 -48
  70. data/features/built_in_matchers/throw_symbol.feature +0 -91
  71. data/features/built_in_matchers/types.feature +0 -116
  72. data/features/built_in_matchers/yield.feature +0 -161
  73. data/features/composing_matchers.feature +0 -250
  74. data/features/compound_expectations.feature +0 -45
  75. data/features/custom_matchers/access_running_example.feature +0 -53
  76. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  77. data/features/custom_matchers/define_matcher.feature +0 -340
  78. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
  79. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  80. data/features/customized_message.feature +0 -39
  81. data/features/diffing.feature +0 -85
  82. data/features/implicit_docstrings.feature +0 -52
  83. data/features/step_definitions/additional_cli_steps.rb +0 -22
  84. data/features/support/env.rb +0 -21
  85. data/features/support/rubinius.rb +0 -6
  86. data/features/syntax_configuration.feature +0 -71
  87. data/features/test_frameworks/minitest.feature +0 -44
  88. data/lib/rspec-expectations.rb +0 -1
  89. data/lib/rspec/expectations/diff_presenter.rb +0 -141
  90. data/lib/rspec/expectations/differ.rb +0 -44
  91. data/lib/rspec/expectations/encoded_string.rb +0 -56
  92. data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
  93. data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
  94. data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
  95. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  96. data/spec/rspec/expectations/fail_with_spec.rb +0 -114
  97. data/spec/rspec/expectations/handler_spec.rb +0 -205
  98. data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
  99. data/spec/rspec/expectations/syntax_spec.rb +0 -89
  100. data/spec/rspec/expectations_spec.rb +0 -12
  101. data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
  102. data/spec/rspec/matchers/aliases_spec.rb +0 -449
  103. data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
  104. data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
  105. data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
  106. data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
  107. data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
  108. data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
  109. data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
  110. data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
  111. data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
  112. data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
  113. data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
  114. data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
  115. data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
  116. data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
  117. data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
  118. data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
  119. data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
  120. data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
  121. data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
  122. data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
  123. data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
  124. data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
  125. data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
  126. data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
  127. data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
  128. data/spec/rspec/matchers/configuration_spec.rb +0 -213
  129. data/spec/rspec/matchers/description_generation_spec.rb +0 -191
  130. data/spec/rspec/matchers/dsl_spec.rb +0 -895
  131. data/spec/rspec/matchers/legacy_spec.rb +0 -101
  132. data/spec/rspec/matchers_spec.rb +0 -74
  133. data/spec/spec_helper.rb +0 -85
  134. data/spec/support/matchers.rb +0 -22
  135. data/spec/support/shared_examples.rb +0 -35
@@ -1,161 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "expect(...).to have_sym(*args)" do
4
- it_behaves_like "an RSpec matcher", :valid_value => { :a => 1 },
5
- :invalid_value => {} do
6
- let(:matcher) { have_key(:a) }
7
- end
8
-
9
- it "passes if #has_sym?(*args) returns true" do
10
- expect({:a => "A"}).to have_key(:a)
11
- end
12
-
13
- it "fails if #has_sym?(*args) returns false" do
14
- expect {
15
- expect({:b => "B"}).to have_key(:a)
16
- }.to fail_with("expected #has_key?(:a) to return true, got false")
17
- end
18
-
19
- obj_with_block_method = Object.new
20
- def obj_with_block_method.has_some_stuff?; yield; end
21
-
22
- it 'forwards the given `{ }` block on to the `has_xyz?` method' do
23
- expect(obj_with_block_method).to have_some_stuff { true }
24
- expect(obj_with_block_method).to_not have_some_stuff { false }
25
- end
26
-
27
- it 'forwards the given `do..end` block on to the `has_xyz?` method' do
28
- expect(obj_with_block_method).to have_some_stuff do
29
- true
30
- end
31
-
32
- expect(obj_with_block_method).to_not have_some_stuff do
33
- false
34
- end
35
- end
36
-
37
- it 'favors a curly brace block over a do...end one since it binds to the matcher method' do
38
- expect(obj_with_block_method).to have_some_stuff { true } do
39
- false
40
- end
41
-
42
- expect(obj_with_block_method).not_to have_some_stuff { false } do
43
- true
44
- end
45
- end
46
-
47
- it 'does not include any args in the failure message if no args were given to the matcher' do
48
- o = Object.new
49
- def o.has_some_stuff?; false; end
50
- expect {
51
- expect(o).to have_some_stuff
52
- }.to fail_with("expected #has_some_stuff? to return true, got false")
53
- end
54
-
55
- it 'includes multiple args in the failure message if multiple args were given to the matcher' do
56
- o = Object.new
57
- def o.has_some_stuff?(*_); false; end
58
- expect {
59
- expect(o).to have_some_stuff(:a, 7, "foo")
60
- }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return true, got false')
61
- end
62
-
63
- it "fails if #has_sym?(*args) returns nil" do
64
- klass = Class.new do
65
- def has_foo?
66
- end
67
- end
68
- expect {
69
- expect(klass.new).to have_foo
70
- }.to fail_with(/expected #has_foo.* to return true, got false/)
71
- end
72
-
73
- it "fails if target does not respond to #has_sym?" do
74
- expect {
75
- expect(Object.new).to have_key(:a)
76
- }.to raise_error(NoMethodError)
77
- end
78
-
79
- it "reraises an exception thrown in #has_sym?(*args)" do
80
- o = Object.new
81
- def o.has_sym?(*args)
82
- raise "Funky exception"
83
- end
84
- expect {
85
- expect(o).to have_sym(:foo)
86
- }.to raise_error("Funky exception")
87
- end
88
-
89
- it 'allows composable aliases to be defined' do
90
- RSpec::Matchers.alias_matcher :an_object_having_sym, :have_sym
91
- o = Object.new
92
- def o.has_sym?(sym); sym == :foo; end
93
-
94
- expect(o).to an_object_having_sym(:foo)
95
- expect(o).not_to an_object_having_sym(:bar)
96
-
97
- expect(an_object_having_sym(:foo).description).to eq("an object having sym :foo")
98
- end
99
- end
100
-
101
- describe "expect(...).not_to have_sym(*args)" do
102
- it "passes if #has_sym?(*args) returns false" do
103
- expect({:a => "A"}).not_to have_key(:b)
104
- end
105
-
106
- it "passes if #has_sym?(*args) returns nil" do
107
- klass = Class.new do
108
- def has_foo?
109
- end
110
- end
111
- expect(klass.new).not_to have_foo
112
- end
113
-
114
- it "fails if #has_sym?(*args) returns true" do
115
- expect {
116
- expect({:a => "A"}).not_to have_key(:a)
117
- }.to fail_with("expected #has_key?(:a) to return false, got true")
118
- end
119
-
120
- it "fails if target does not respond to #has_sym?" do
121
- expect {
122
- expect(Object.new).to have_key(:a)
123
- }.to raise_error(NoMethodError)
124
- end
125
-
126
- it "reraises an exception thrown in #has_sym?(*args)" do
127
- o = Object.new
128
- def o.has_sym?(*args)
129
- raise "Funky exception"
130
- end
131
- expect {
132
- expect(o).not_to have_sym(:foo)
133
- }.to raise_error("Funky exception")
134
- end
135
-
136
- it 'does not include any args in the failure message if no args were given to the matcher' do
137
- o = Object.new
138
- def o.has_some_stuff?; true; end
139
- expect {
140
- expect(o).not_to have_some_stuff
141
- }.to fail_with("expected #has_some_stuff? to return false, got true")
142
- end
143
-
144
- it 'includes multiple args in the failure message if multiple args were given to the matcher' do
145
- o = Object.new
146
- def o.has_some_stuff?(*_); true; end
147
- expect {
148
- expect(o).not_to have_some_stuff(:a, 7, "foo")
149
- }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return false, got true')
150
- end
151
- end
152
-
153
- describe "has" do
154
- it "works when the target implements #send" do
155
- o = {:a => "A"}
156
- def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
157
- expect {
158
- expect(o).to have_key(:a)
159
- }.not_to raise_error
160
- end
161
- end
@@ -1,540 +0,0 @@
1
- require 'spec_helper'
2
- require 'uri'
3
-
4
- describe "#include matcher" do
5
- it "is diffable" do
6
- expect(include("a")).to be_diffable
7
- end
8
-
9
- describe "expect(...).to include(with_one_arg)" do
10
- it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
11
- let(:matcher) { include(2) }
12
- end
13
-
14
- context "for a string target" do
15
- it "passes if target includes expected" do
16
- expect("abc").to include("a")
17
- end
18
-
19
- it "fails if target does not include expected" do
20
- expect {
21
- expect("abc").to include("d")
22
- }.to fail_matching("expected \"abc\" to include \"d\"")
23
- end
24
-
25
- it "includes a diff when actual is multiline" do
26
- expect {
27
- expect("abc\ndef").to include("g")
28
- }.to fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff")
29
- end
30
-
31
- it "includes a diff when actual is multiline and there are multiple expecteds" do
32
- expect {
33
- expect("abc\ndef").to include("g", "h")
34
- }.to fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff")
35
- end
36
- end
37
-
38
- context "for an array target" do
39
- it "passes if target includes expected" do
40
- expect([1,2,3]).to include(3)
41
- end
42
-
43
- it "fails if target does not include expected" do
44
- expect {
45
- expect([1,2,3]).to include(4)
46
- }.to fail_matching("expected [1, 2, 3] to include 4")
47
- end
48
-
49
- it 'fails when given differing null doubles' do
50
- dbl_1 = double.as_null_object
51
- dbl_2 = double.as_null_object
52
-
53
- expect {
54
- expect([dbl_1]).to include(dbl_2)
55
- }.to fail_matching("expected [#{dbl_1.inspect}] to include")
56
- end
57
-
58
- it 'passes when given the same null double' do
59
- dbl = double.as_null_object
60
- expect([dbl]).to include(dbl)
61
- end
62
- end
63
-
64
- context "for a hash target" do
65
- it 'passes if target has the expected as a key' do
66
- expect({:key => 'value'}).to include(:key)
67
- end
68
-
69
- it "fails if target does not include expected" do
70
- expect {
71
- expect({:key => 'value'}).to include(:other)
72
- }.to fail_matching(%Q|expected {:key => "value"} to include :other|)
73
- end
74
-
75
- it "fails if target doesn't have a key and we expect nil" do
76
- expect {
77
- expect({}).to include(:something => nil)
78
- }.to fail_matching(%Q|expected {} to include {:something => nil}|)
79
- end
80
-
81
- it 'works even when an entry in the hash overrides #send' do
82
- hash = { :key => 'value' }
83
- def hash.send; :sent; end
84
- expect(hash).to include(hash)
85
- end
86
-
87
- context 'that overrides #send' do
88
- it 'still works' do
89
- array = [1, 2]
90
- def array.send; :sent; end
91
-
92
- expect(array).to include(*array)
93
- end
94
- end
95
- end
96
- end
97
-
98
- describe "expect(...).to include(with, multiple, args)" do
99
- it "has a description" do
100
- matcher = include("a")
101
- expect(matcher.description).to eq("include \"a\"")
102
- end
103
- context "for a string target" do
104
- it "passes if target includes all items" do
105
- expect("a string").to include("str", "a")
106
- end
107
-
108
- it "fails if target does not include any one of the items" do
109
- expect {
110
- expect("a string").to include("str", "a", "foo")
111
- }.to fail_matching(%Q{expected "a string" to include "str", "a", and "foo"})
112
- end
113
- end
114
-
115
- context "for an array target" do
116
- it "passes if target includes all items" do
117
- expect([1,2,3]).to include(1,2,3)
118
- end
119
-
120
- it "fails if target does not include any one of the items" do
121
- expect {
122
- expect([1,2,3]).to include(1,2,4)
123
- }.to fail_matching("expected [1, 2, 3] to include 1, 2, and 4")
124
- end
125
- end
126
-
127
- context "for a hash target" do
128
- it 'passes if target includes all items as keys' do
129
- expect({:key => 'value', :other => 'value'}).to include(:key, :other)
130
- end
131
-
132
- it 'fails if target is missing any item as a key' do
133
- expect {
134
- expect({:key => 'value'}).to include(:key, :other)
135
- }.to fail_matching(%Q|expected {:key => "value"} to include :key and :other|)
136
- end
137
- end
138
- end
139
-
140
- describe "expect(...).not_to include(expected)" do
141
- context "for a string target" do
142
- it "passes if target does not include expected" do
143
- expect("abc").not_to include("d")
144
- end
145
-
146
- it "fails if target includes expected" do
147
- expect {
148
- expect("abc").not_to include("c")
149
- }.to fail_with("expected \"abc\" not to include \"c\"")
150
- end
151
- end
152
-
153
- context "for an array target" do
154
- it "passes if target does not include expected" do
155
- expect([1,2,3]).not_to include(4)
156
- end
157
-
158
- it "fails if target includes expected" do
159
- expect {
160
- expect([1,2,3]).not_to include(3)
161
- }.to fail_with("expected [1, 2, 3] not to include 3")
162
- end
163
-
164
- it 'passes when given differing null doubles' do
165
- expect([double.as_null_object]).not_to include(double.as_null_object)
166
- end
167
-
168
- it 'fails when given the same null double' do
169
- dbl = double.as_null_object
170
-
171
- expect {
172
- expect([dbl]).not_to include(dbl)
173
- }.to fail_matching("expected [#{dbl.inspect}] not to include")
174
- end
175
- end
176
-
177
- context "for a hash target" do
178
- it 'passes if target does not have the expected as a key' do
179
- expect({:other => 'value'}).not_to include(:key)
180
- end
181
-
182
- it "fails if target includes expected key" do
183
- expect {
184
- expect({:key => 'value'}).not_to include(:key)
185
- }.to fail_matching(%Q|expected {:key => "value"} not to include :key|)
186
- end
187
- end
188
-
189
- end
190
-
191
- describe "expect(...).not_to include(with, multiple, args)" do
192
- context "for a string target" do
193
- it "passes if the target does not include any of the expected" do
194
- expect("abc").not_to include("d", "e", "f")
195
- end
196
-
197
- it "fails if the target includes all of the expected" do
198
- expect {
199
- expect("abc").not_to include("c", "a")
200
- }.to fail_with('expected "abc" not to include "c" and "a"')
201
- end
202
-
203
- it "fails if the target includes some (but not all) of the expected" do
204
- expect {
205
- expect("abc").not_to include("d", "a")
206
- }.to fail_with(%q{expected "abc" not to include "d" and "a"})
207
- end
208
- end
209
-
210
- context "for a hash target" do
211
- it "passes if it does not include any of the expected keys" do
212
- expect({ :a => 1, :b => 2 }).not_to include(:c, :d)
213
- end
214
-
215
- it "fails if the target includes all of the expected keys" do
216
- expect {
217
- expect({ :a => 1, :b => 2 }).not_to include(:a, :b)
218
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 2} not to include :a and :b|)
219
- end
220
-
221
- it "fails if the target includes some (but not all) of the expected keys" do
222
- expect {
223
- expect({ :a => 1, :b => 2 }).not_to include(:d, :b)
224
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 2} not to include :d and :b|)
225
- end
226
- end
227
-
228
- context "for an array target" do
229
- it "passes if the target does not include any of the expected" do
230
- expect([1, 2, 3]).not_to include(4, 5, 6)
231
- end
232
-
233
- it "fails if the target includes all of the expected" do
234
- expect {
235
- expect([1, 2, 3]).not_to include(3, 1)
236
- }.to fail_with(%q{expected [1, 2, 3] not to include 3 and 1})
237
- end
238
-
239
- it "fails if the target includes some (but not all) of the expected" do
240
- expect {
241
- expect([1, 2, 3]).not_to include(4, 1)
242
- }.to fail_with(%q{expected [1, 2, 3] not to include 4 and 1})
243
- end
244
- end
245
- end
246
-
247
- describe "expect(...).to include(:key => value)" do
248
- context 'for a hash target' do
249
- it "passes if target includes the key/value pair" do
250
- expect({:key => 'value'}).to include(:key => 'value')
251
- end
252
-
253
- it "passes if target includes the key/value pair among others" do
254
- expect({:key => 'value', :other => 'different'}).to include(:key => 'value')
255
- end
256
-
257
- it "fails if target has a different value for key" do
258
- expect {
259
- expect({:key => 'different'}).to include(:key => 'value')
260
- }.to fail_matching(%Q|expected {:key => "different"} to include {:key => "value"}|)
261
- end
262
-
263
- it "fails if target has a different key" do
264
- expect {
265
- expect({:other => 'value'}).to include(:key => 'value')
266
- }.to fail_matching(%Q|expected {:other => "value"} to include {:key => "value"}|)
267
- end
268
- end
269
-
270
- context 'for a non-hash target' do
271
- it "fails if the target does not contain the given hash" do
272
- expect {
273
- expect(['a', 'b']).to include(:key => 'value')
274
- }.to fail_matching(%q|expected ["a", "b"] to include {:key => "value"}|)
275
- end
276
-
277
- it "passes if the target contains the given hash" do
278
- expect(['a', { :key => 'value' } ]).to include(:key => 'value')
279
- end
280
- end
281
- end
282
-
283
- describe "expect(...).not_to include(:key => value)" do
284
- context 'for a hash target' do
285
- it "fails if target includes the key/value pair" do
286
- expect {
287
- expect({:key => 'value'}).not_to include(:key => 'value')
288
- }.to fail_matching(%Q|expected {:key => "value"} not to include {:key => "value"}|)
289
- end
290
-
291
- it "fails if target includes the key/value pair among others" do
292
- expect {
293
- expect({:key => 'value', :other => 'different'}).not_to include(:key => 'value')
294
- }.to fail_matching(%Q|expected #{hash_inspect :key => "value", :other => "different"} not to include {:key => "value"}|)
295
- end
296
-
297
- it "passes if target has a different value for key" do
298
- expect({:key => 'different'}).not_to include(:key => 'value')
299
- end
300
-
301
- it "passes if target has a different key" do
302
- expect({:other => 'value'}).not_to include(:key => 'value')
303
- end
304
- end
305
-
306
- context "for a non-hash target" do
307
- it "passes if the target does not contain the given hash" do
308
- expect(['a', 'b']).not_to include(:key => 'value')
309
- end
310
-
311
- it "fails if the target contains the given hash" do
312
- expect {
313
- expect(['a', { :key => 'value' } ]).not_to include(:key => 'value')
314
- }.to fail_matching(%Q|expected ["a", {:key => "value"}] not to include {:key => "value"}|)
315
- end
316
- end
317
- end
318
-
319
- describe "expect(...).to include(:key1 => value1, :key2 => value2)" do
320
- context 'for a hash target' do
321
- it "passes if target includes the key/value pairs" do
322
- expect({:a => 1, :b => 2}).to include(:b => 2, :a => 1)
323
- end
324
-
325
- it "passes if target includes the key/value pairs among others" do
326
- expect({:a => 1, :c => 3, :b => 2}).to include(:b => 2, :a => 1)
327
- end
328
-
329
- it "fails if target has a different value for one of the keys" do
330
- expect {
331
- expect({:a => 1, :b => 2}).to include(:a => 2, :b => 2)
332
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 2} to include #{hash_inspect :a => 2, :b => 2}|)
333
- end
334
-
335
- it "fails if target has a different value for both of the keys" do
336
- expect {
337
- expect({:a => 1, :b => 1}).to include(:a => 2, :b => 2)
338
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 1} to include #{hash_inspect :a => 2, :b => 2}|)
339
- end
340
-
341
- it "fails if target lacks one of the keys" do
342
- expect {
343
- expect({:a => 1, :b => 1}).to include(:a => 1, :c => 1)
344
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 1} to include #{hash_inspect :a => 1, :c => 1}|)
345
- end
346
-
347
- it "fails if target lacks both of the keys" do
348
- expect {
349
- expect({:a => 1, :b => 1}).to include(:c => 1, :d => 1)
350
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 1} to include #{hash_inspect :c => 1, :d => 1}|)
351
- end
352
- end
353
-
354
- context 'for a non-hash target' do
355
- it "fails if the target does not contain the given hash" do
356
- expect {
357
- expect(['a', 'b']).to include(:a => 1, :b => 1)
358
- }.to fail_matching(%Q|expected ["a", "b"] to include #{hash_inspect :a => 1, :b => 1}|)
359
- end
360
-
361
- it "passes if the target contains the given hash" do
362
- expect(['a', { :a => 1, :b => 2 } ]).to include(:a => 1, :b => 2)
363
- end
364
- end
365
- end
366
-
367
- describe "expect(...).not_to include(:key1 => value1, :key2 => value2)" do
368
- context 'for a hash target' do
369
- it "fails if target includes the key/value pairs" do
370
- expect {
371
- expect({:a => 1, :b => 2}).not_to include(:a => 1, :b => 2)
372
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 2} not to include #{hash_inspect :a => 1, :b => 2}|)
373
- end
374
-
375
- it "fails if target includes the key/value pairs among others" do
376
- hash = {:a => 1, :b => 2, :c => 3}
377
- expect {
378
- expect(hash).not_to include(:a => 1, :b => 2)
379
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 2, :c => 3} not to include #{hash_inspect :a => 1, :b => 2}|)
380
- end
381
-
382
- it "fails if target has a different value for one of the keys" do
383
- expect {
384
- expect({:a => 1, :b => 2}).not_to include(:a => 2, :b => 2)
385
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 2} not to include #{hash_inspect :a => 2, :b => 2}|)
386
- end
387
-
388
- it "passes if target has a different value for both of the keys" do
389
- expect({:a => 1, :b => 1}).not_to include(:a => 2, :b => 2)
390
- end
391
-
392
- it "fails if target lacks one of the keys" do
393
- expect {
394
- expect({:a => 1, :b => 1}).not_to include(:a => 1, :c => 1)
395
- }.to fail_matching(%Q|expected #{hash_inspect :a => 1, :b => 1} not to include #{hash_inspect :a => 1, :c => 1}|)
396
- end
397
-
398
- it "passes if target lacks both of the keys" do
399
- expect({:a => 1, :b => 1}).not_to include(:c => 1, :d => 1)
400
- end
401
- end
402
-
403
- context 'for a non-hash target' do
404
- it "passes if the target does not contain the given hash" do
405
- expect(['a', 'b']).not_to include(:a => 1, :b => 1)
406
- end
407
-
408
- it "fails if the target contains the given hash" do
409
- expect {
410
- expect(['a', { :a => 1, :b => 2 } ]).not_to include(:a => 1, :b => 2)
411
- }.to fail_matching(%Q|expected ["a", #{hash_inspect :a => 1, :b => 2}] not to include #{hash_inspect :a => 1, :b => 2}|)
412
- end
413
- end
414
- end
415
-
416
- describe "Composing matchers with `include`" do
417
- RSpec::Matchers.define :a_string_containing do |expected|
418
- match do |actual|
419
- actual.include?(expected)
420
- end
421
-
422
- description do
423
- "a string containing '#{expected}'"
424
- end
425
- end
426
-
427
- describe "expect(array).to include(matcher)" do
428
- it "passes when the matcher matches one of the values" do
429
- expect([10, 20, 30]).to include( a_value_within(5).of(24) )
430
- end
431
-
432
- it 'provides a description' do
433
- description = include( a_value_within(5).of(24) ).description
434
- expect(description).to eq("include (a value within 5 of 24)")
435
- end
436
-
437
- it 'fails with a clear message when the matcher matches none of the values' do
438
- expect {
439
- expect([10, 30]).to include( a_value_within(5).of(24) )
440
- }.to fail_with("expected [10, 30] to include (a value within 5 of 24)")
441
- end
442
-
443
- it 'works with comparison matchers' do
444
- expect {
445
- expect([100, 200]).to include(a_value < 90)
446
- }.to fail_with("expected [100, 200] to include (a value < 90)")
447
-
448
- expect([100, 200]).to include(a_value > 150)
449
- end
450
-
451
- it 'does not treat an object that only implements #matches? as a matcher' do
452
- domain = Struct.new(:domain) do
453
- def matches?(url)
454
- URI(url).host == self.domain
455
- end
456
- end
457
-
458
- expect([domain.new("rspec.info")]).to include(domain.new("rspec.info"))
459
-
460
- expect {
461
- expect([domain.new("rspec.info")]).to include(domain.new("foo.com"))
462
- }.to fail_matching("expected [#{domain.new("rspec.info").inspect}] to include")
463
- end
464
- end
465
-
466
- describe "expect(array).to include(multiple, matcher, arguments)" do
467
- it "passes if target includes items satisfying all matchers" do
468
- expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing('oo'))
469
- end
470
-
471
- it "fails if target does not include an item satisfying any one of the items" do
472
- expect {
473
- expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc"))
474
- }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} to include (a string containing 'ar') and (a string containing 'abc')|)
475
- end
476
- end
477
-
478
- describe "expect(hash).to include(key => matcher)" do
479
- it "passes when the matcher matches" do
480
- expect(:a => 12).to include(:a => a_value_within(3).of(10))
481
- end
482
-
483
- it 'provides a description' do
484
- description = include(:a => a_value_within(3).of(10)).description
485
- expect(description).to eq("include {:a => (a value within 3 of 10)}")
486
- end
487
-
488
- it "fails with a clear message when the matcher does not match" do
489
- expect {
490
- expect(:a => 15).to include(:a => a_value_within(3).of(10))
491
- }.to fail_matching("expected {:a => 15} to include {:a => (a value within 3 of 10)}")
492
- end
493
- end
494
-
495
- describe "expect(hash).to include(key_matcher)" do
496
- it "passes when the matcher matches a key", :if => (RUBY_VERSION.to_f > 1.8) do
497
- expect(:drink => "water", :food => "bread").to include(a_string_matching(/foo/))
498
- end
499
-
500
- it 'provides a description' do
501
- description = include(a_string_matching(/foo/)).description
502
- expect(description).to eq("include (a string matching /foo/)")
503
- end
504
-
505
- it 'fails with a clear message when the matcher does not match', :if => (RUBY_VERSION.to_f > 1.8) do
506
- expect {
507
- expect(:drink => "water", :food => "bread").to include(a_string_matching(/bar/))
508
- }.to fail_matching('expected {:drink => "water", :food => "bread"} to include (a string matching /bar/)')
509
- end
510
- end
511
-
512
- describe "expect(array).not_to include(multiple, matcher, arguments)" do
513
- it "passes if none of the target values satisfies any of the matchers" do
514
- expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("gh"), a_string_containing('de'))
515
- end
516
-
517
- it 'fails if all of the matchers are satisfied by one of the target values' do
518
- expect {
519
- expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('az'))
520
- }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} not to include (a string containing 'ar') and (a string containing 'az')|)
521
- end
522
-
523
- it 'fails if the some (but not all) of the matchers are satisifed' do
524
- expect {
525
- expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('bz'))
526
- }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} not to include (a string containing 'ar') and (a string containing 'bz')|)
527
- end
528
- end
529
- end
530
-
531
- include RSpec::Matchers::Pretty
532
- # We have to use Hash#inspect in examples that have multi-entry
533
- # hashes because the #inspect output on 1.8.7 is non-deterministic
534
- # due to the fact that hashes are not ordered. So we can't simply
535
- # put a literal string for what we expect because it varies.
536
- def hash_inspect(hash)
537
- improve_hash_formatting(hash.inspect)
538
- end
539
- end
540
-