rspec-expectations 2.11.3 → 3.11.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 (152) 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 +1026 -21
  6. data/{License.txt → LICENSE.md} +5 -3
  7. data/README.md +174 -78
  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 +130 -55
  11. data/lib/rspec/expectations/fail_with.rb +17 -33
  12. data/lib/rspec/expectations/failure_aggregator.rb +212 -0
  13. data/lib/rspec/expectations/handler.rb +163 -29
  14. data/lib/rspec/expectations/minitest_integration.rb +58 -0
  15. data/lib/rspec/expectations/syntax.rb +68 -54
  16. data/lib/rspec/expectations/version.rb +1 -1
  17. data/lib/rspec/expectations.rb +59 -24
  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 +150 -20
  21. data/lib/rspec/matchers/built_in/be.rb +115 -109
  22. data/lib/rspec/matchers/built_in/be_between.rb +77 -0
  23. data/lib/rspec/matchers/built_in/be_instance_of.rb +16 -1
  24. data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
  25. data/lib/rspec/matchers/built_in/be_within.rb +43 -17
  26. data/lib/rspec/matchers/built_in/change.rb +392 -75
  27. data/lib/rspec/matchers/built_in/compound.rb +290 -0
  28. data/lib/rspec/matchers/built_in/contain_exactly.rb +302 -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 +26 -8
  32. data/lib/rspec/matchers/built_in/eql.rb +19 -8
  33. data/lib/rspec/matchers/built_in/equal.rb +56 -19
  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 +175 -20
  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 +212 -38
  42. data/lib/rspec/matchers/built_in/respond_to.rb +155 -29
  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 +252 -98
  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 +530 -10
  50. data/lib/rspec/matchers/english_phrasing.rb +58 -0
  51. data/lib/rspec/matchers/expecteds_for_multiple_diffs.rb +82 -0
  52. data/lib/rspec/matchers/fail_matchers.rb +42 -0
  53. data/lib/rspec/matchers/generated_descriptions.rb +15 -10
  54. data/lib/rspec/matchers/matcher_delegator.rb +35 -0
  55. data/lib/rspec/matchers/matcher_protocol.rb +105 -0
  56. data/lib/rspec/matchers.rb +604 -252
  57. data.tar.gz.sig +0 -0
  58. metadata +178 -278
  59. metadata.gz.sig +0 -0
  60. data/features/README.md +0 -49
  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 -173
  64. data/features/built_in_matchers/be_within.feature +0 -46
  65. data/features/built_in_matchers/cover.feature +0 -45
  66. data/features/built_in_matchers/end_with.feature +0 -46
  67. data/features/built_in_matchers/equality.feature +0 -145
  68. data/features/built_in_matchers/exist.feature +0 -43
  69. data/features/built_in_matchers/expect_change.feature +0 -59
  70. data/features/built_in_matchers/expect_error.feature +0 -138
  71. data/features/built_in_matchers/have.feature +0 -103
  72. data/features/built_in_matchers/include.feature +0 -121
  73. data/features/built_in_matchers/match.feature +0 -50
  74. data/features/built_in_matchers/operators.feature +0 -221
  75. data/features/built_in_matchers/predicates.feature +0 -128
  76. data/features/built_in_matchers/respond_to.feature +0 -78
  77. data/features/built_in_matchers/satisfy.feature +0 -31
  78. data/features/built_in_matchers/start_with.feature +0 -46
  79. data/features/built_in_matchers/throw_symbol.feature +0 -85
  80. data/features/built_in_matchers/types.feature +0 -114
  81. data/features/built_in_matchers/yield.feature +0 -146
  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 -340
  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 -5
  92. data/features/syntax_configuration.feature +0 -68
  93. data/features/test_frameworks/test_unit.feature +0 -46
  94. data/lib/rspec/expectations/deprecation.rb +0 -38
  95. data/lib/rspec/expectations/differ.rb +0 -81
  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 -39
  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 -108
  102. data/lib/rspec/matchers/built_in/match_array.rb +0 -45
  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 -66
  106. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  107. data/lib/rspec/matchers/matcher.rb +0 -299
  108. data/lib/rspec/matchers/method_missing.rb +0 -12
  109. data/lib/rspec/matchers/operator_matcher.rb +0 -84
  110. data/lib/rspec/matchers/pretty.rb +0 -60
  111. data/lib/rspec-expectations.rb +0 -1
  112. data/spec/rspec/expectations/differ_spec.rb +0 -153
  113. data/spec/rspec/expectations/expectation_target_spec.rb +0 -65
  114. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  115. data/spec/rspec/expectations/fail_with_spec.rb +0 -70
  116. data/spec/rspec/expectations/handler_spec.rb +0 -206
  117. data/spec/rspec/matchers/base_matcher_spec.rb +0 -60
  118. data/spec/rspec/matchers/be_close_spec.rb +0 -22
  119. data/spec/rspec/matchers/be_instance_of_spec.rb +0 -40
  120. data/spec/rspec/matchers/be_kind_of_spec.rb +0 -37
  121. data/spec/rspec/matchers/be_spec.rb +0 -452
  122. data/spec/rspec/matchers/be_within_spec.rb +0 -80
  123. data/spec/rspec/matchers/change_spec.rb +0 -528
  124. data/spec/rspec/matchers/configuration_spec.rb +0 -202
  125. data/spec/rspec/matchers/cover_spec.rb +0 -69
  126. data/spec/rspec/matchers/description_generation_spec.rb +0 -176
  127. data/spec/rspec/matchers/dsl_spec.rb +0 -57
  128. data/spec/rspec/matchers/eq_spec.rb +0 -54
  129. data/spec/rspec/matchers/eql_spec.rb +0 -41
  130. data/spec/rspec/matchers/equal_spec.rb +0 -60
  131. data/spec/rspec/matchers/exist_spec.rb +0 -110
  132. data/spec/rspec/matchers/has_spec.rb +0 -118
  133. data/spec/rspec/matchers/have_spec.rb +0 -461
  134. data/spec/rspec/matchers/include_spec.rb +0 -367
  135. data/spec/rspec/matchers/match_array_spec.rb +0 -124
  136. data/spec/rspec/matchers/match_spec.rb +0 -61
  137. data/spec/rspec/matchers/matcher_spec.rb +0 -434
  138. data/spec/rspec/matchers/matchers_spec.rb +0 -31
  139. data/spec/rspec/matchers/method_missing_spec.rb +0 -24
  140. data/spec/rspec/matchers/operator_matcher_spec.rb +0 -221
  141. data/spec/rspec/matchers/raise_error_spec.rb +0 -344
  142. data/spec/rspec/matchers/respond_to_spec.rb +0 -295
  143. data/spec/rspec/matchers/satisfy_spec.rb +0 -44
  144. data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -182
  145. data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
  146. data/spec/rspec/matchers/yield_spec.rb +0 -402
  147. data/spec/spec_helper.rb +0 -27
  148. data/spec/support/classes.rb +0 -56
  149. data/spec/support/in_sub_process.rb +0 -31
  150. data/spec/support/matchers.rb +0 -22
  151. data/spec/support/ruby_version.rb +0 -10
  152. data/spec/support/shared_examples.rb +0 -13
@@ -1,110 +0,0 @@
1
- require 'spec_helper'
2
- require 'ostruct'
3
-
4
- describe "exist matcher" do
5
- it_behaves_like "an RSpec matcher", :valid_value => OpenStruct.new(:exist? => true),
6
- :invalid_value => OpenStruct.new(:exist? => false) do
7
- let(:matcher) { exist }
8
- end
9
-
10
- context "when the object does not respond to #exist? or #exists?" do
11
- subject { mock }
12
-
13
- [:should, :should_not].each do |should_method|
14
- describe "#{should_method} exist" do
15
- it "raises an error" do
16
- expect {
17
- subject.send(should_method, exist)
18
- }.to raise_error(NoMethodError)
19
- end
20
- end
21
- end
22
- end
23
-
24
- [:exist?, :exists?].each do |predicate|
25
- context "when the object responds to ##{predicate}" do
26
- describe "should exist" do
27
- it "passes if #{predicate}" do
28
- mock(predicate => true).should exist
29
- end
30
-
31
- it "fails if not #{predicate}" do
32
- expect {
33
- mock(predicate => false).should exist
34
- }.to fail_with(/expected .* to exist/)
35
- end
36
- end
37
-
38
- describe "should not exist" do
39
- it "passes if not #{predicate}" do
40
- mock(predicate => false).should_not exist
41
- end
42
-
43
- it "fails if #{predicate}" do
44
- expect {
45
- mock(predicate => true).should_not exist
46
- }.to fail_with(/expected .* not to exist/)
47
- end
48
- end
49
- end
50
- end
51
-
52
- context "when the object responds to #exist? and #exists?" do
53
- context "when they both return falsey values" do
54
- subject { mock(:exist? => false, :exists? => nil) }
55
-
56
- describe "should_not exist" do
57
- it "passes" do
58
- subject.should_not exist
59
- end
60
- end
61
-
62
- describe "should exist" do
63
- it "fails" do
64
- expect {
65
- subject.should exist
66
- }.to fail_with(/expected .* to exist/)
67
- end
68
- end
69
- end
70
-
71
- context "when they both return truthy values" do
72
- subject { mock(:exist? => true, :exists? => "something true") }
73
-
74
- describe "should_not exist" do
75
- it "fails" do
76
- expect {
77
- subject.should_not exist
78
- }.to fail_with(/expected .* not to exist/)
79
- end
80
- end
81
-
82
- describe "should exist" do
83
- it "passes" do
84
- subject.should exist
85
- end
86
- end
87
- end
88
-
89
- context "when they return values with different truthiness" do
90
- subject { mock(:exist? => true, :exists? => false) }
91
-
92
- [:should, :should_not].each do |should_method|
93
- describe "#{should_method} exist" do
94
- it "raises an error" do
95
- expect {
96
- subject.send(should_method, exist)
97
- }.to raise_error(/#exist\? and #exists\? returned different values/)
98
- end
99
- end
100
- end
101
- end
102
- end
103
-
104
- it 'passes any provided arguments to the call to #exist?' do
105
- object = mock
106
- object.should_receive(:exist?).with(:foo, :bar) { true }
107
-
108
- object.should exist(:foo, :bar)
109
- end
110
- end
@@ -1,118 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "should 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
- {:a => "A"}.should have_key(:a)
11
- end
12
-
13
- it "fails if #has_sym?(*args) returns false" do
14
- lambda {
15
- {:b => "B"}.should have_key(:a)
16
- }.should fail_with("expected #has_key?(:a) to return true, got false")
17
- end
18
-
19
- it 'does not include any args in the failure message if no args were given to the matcher' do
20
- o = Object.new
21
- def o.has_some_stuff?; false; end
22
- expect {
23
- o.should have_some_stuff
24
- }.to fail_with("expected #has_some_stuff? to return true, got false")
25
- end
26
-
27
- it 'includes multiple args in the failure message if multiple args were given to the matcher' do
28
- o = Object.new
29
- def o.has_some_stuff?(*_); false; end
30
- expect {
31
- o.should have_some_stuff(:a, 7, "foo")
32
- }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return true, got false')
33
- end
34
-
35
- it "fails if #has_sym?(*args) returns nil" do
36
- klass = Class.new do
37
- def has_foo?
38
- end
39
- end
40
- lambda {
41
- klass.new.should have_foo
42
- }.should fail_with(/expected #has_foo.* to return true, got false/)
43
- end
44
-
45
- it "fails if target does not respond to #has_sym?" do
46
- lambda {
47
- Object.new.should have_key(:a)
48
- }.should raise_error(NoMethodError)
49
- end
50
-
51
- it "reraises an exception thrown in #has_sym?(*args)" do
52
- o = Object.new
53
- def o.has_sym?(*args)
54
- raise "Funky exception"
55
- end
56
- lambda { o.should have_sym(:foo) }.should raise_error("Funky exception")
57
- end
58
- end
59
-
60
- describe "should_not have_sym(*args)" do
61
- it "passes if #has_sym?(*args) returns false" do
62
- {:a => "A"}.should_not have_key(:b)
63
- end
64
-
65
- it "passes if #has_sym?(*args) returns nil" do
66
- klass = Class.new do
67
- def has_foo?
68
- end
69
- end
70
- klass.new.should_not have_foo
71
- end
72
-
73
- it "fails if #has_sym?(*args) returns true" do
74
- lambda {
75
- {:a => "A"}.should_not have_key(:a)
76
- }.should fail_with("expected #has_key?(:a) to return false, got true")
77
- end
78
-
79
- it "fails if target does not respond to #has_sym?" do
80
- lambda {
81
- Object.new.should have_key(:a)
82
- }.should raise_error(NoMethodError)
83
- end
84
-
85
- it "reraises an exception thrown in #has_sym?(*args)" do
86
- o = Object.new
87
- def o.has_sym?(*args)
88
- raise "Funky exception"
89
- end
90
- lambda { o.should_not have_sym(:foo) }.should raise_error("Funky exception")
91
- end
92
-
93
- it 'does not include any args in the failure message if no args were given to the matcher' do
94
- o = Object.new
95
- def o.has_some_stuff?; true; end
96
- expect {
97
- o.should_not have_some_stuff
98
- }.to fail_with("expected #has_some_stuff? to return false, got true")
99
- end
100
-
101
- it 'includes multiple args in the failure message if multiple args were given to the matcher' do
102
- o = Object.new
103
- def o.has_some_stuff?(*_); true; end
104
- expect {
105
- o.should_not have_some_stuff(:a, 7, "foo")
106
- }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return false, got true')
107
- end
108
- end
109
-
110
- describe "has" do
111
- it "works when the target implements #send" do
112
- o = {:a => "A"}
113
- def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
114
- lambda {
115
- o.should have_key(:a)
116
- }.should_not raise_error
117
- end
118
- end
@@ -1,461 +0,0 @@
1
- require 'spec_helper'
2
- require 'stringio'
3
-
4
- describe "have matcher" do
5
-
6
- before(:each) do
7
- if defined?(::ActiveSupport::Inflector) && ::ActiveSupport::Inflector.respond_to?(:pluralize)
8
- @active_support_was_defined = true
9
- else
10
- @active_support_was_defined = false
11
- module ::ActiveSupport
12
- class Inflector
13
- def self.pluralize(string)
14
- string.to_s + 's'
15
- end
16
- end
17
- end
18
- end
19
- end
20
-
21
- def create_collection_owner_with(n)
22
- owner = RSpec::Expectations::Helper::CollectionOwner.new
23
- (1..n).each do |number|
24
- owner.add_to_collection_with_length_method(number)
25
- owner.add_to_collection_with_size_method(number)
26
- owner.add_to_collection_with_count_method(number)
27
- end
28
- owner
29
- end
30
-
31
- describe "should have(n).items" do
32
- it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
33
- let(:matcher) { have(2).items }
34
- end
35
-
36
- it "passes if target has a collection of items with n members" do
37
- owner = create_collection_owner_with(3)
38
- owner.should have(3).items_in_collection_with_length_method
39
- owner.should have(3).items_in_collection_with_size_method
40
- owner.should have(3).items_in_collection_with_count_method
41
- end
42
-
43
- it "converts :no to 0" do
44
- owner = create_collection_owner_with(0)
45
- owner.should have(:no).items_in_collection_with_length_method
46
- owner.should have(:no).items_in_collection_with_size_method
47
- owner.should have(:no).items_in_collection_with_count_method
48
- end
49
-
50
- it "converts a String argument to Integer" do
51
- owner = create_collection_owner_with(3)
52
- owner.should have('3').items_in_collection_with_length_method
53
- owner.should have('3').items_in_collection_with_size_method
54
- owner.should have('3').items_in_collection_with_count_method
55
- end
56
-
57
- it "fails if target has a collection of items with < n members" do
58
- owner = create_collection_owner_with(3)
59
- lambda {
60
- owner.should have(4).items_in_collection_with_length_method
61
- }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
62
- lambda {
63
- owner.should have(4).items_in_collection_with_size_method
64
- }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
65
- lambda {
66
- owner.should have(4).items_in_collection_with_count_method
67
- }.should fail_with("expected 4 items_in_collection_with_count_method, got 3")
68
- end
69
-
70
- it "fails if target has a collection of items with > n members" do
71
- owner = create_collection_owner_with(3)
72
- lambda {
73
- owner.should have(2).items_in_collection_with_length_method
74
- }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
75
- lambda {
76
- owner.should have(2).items_in_collection_with_size_method
77
- }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
78
- lambda {
79
- owner.should have(2).items_in_collection_with_count_method
80
- }.should fail_with("expected 2 items_in_collection_with_count_method, got 3")
81
- end
82
- end
83
-
84
- describe 'should have(1).item when ActiveSupport::Inflector is defined' do
85
-
86
- it 'pluralizes the collection name' do
87
- owner = create_collection_owner_with(1)
88
- owner.should have(1).item
89
- end
90
-
91
- context "when ActiveSupport::Inflector is partially loaded without its inflectors" do
92
-
93
- it "does not pluralize the collection name" do
94
- (class << ::ActiveSupport::Inflector; self; end).send :undef_method, :pluralize
95
- owner = create_collection_owner_with(1)
96
- expect { owner.should have(1).item }.to raise_error(NoMethodError)
97
- end
98
-
99
- end
100
-
101
- after(:each) do
102
- unless @active_support_was_defined
103
- Object.__send__ :remove_const, :ActiveSupport
104
- end
105
- end
106
- end
107
-
108
- describe 'should have(1).item when Inflector is defined' do
109
-
110
- before(:each) do
111
- if defined?(Inflector)
112
- @inflector_was_defined = true
113
- else
114
- @inflector_was_defined = false
115
- class ::Inflector
116
- def self.pluralize(string)
117
- string.to_s + 's'
118
- end
119
- end
120
- end
121
- end
122
-
123
- it 'pluralizes the collection name' do
124
- owner = create_collection_owner_with(1)
125
- owner.should have(1).item
126
- end
127
-
128
- after(:each) do
129
- unless @inflector_was_defined
130
- Object.__send__ :remove_const, :Inflector
131
- end
132
- end
133
- end
134
-
135
- describe "should have(n).items where result responds to items but returns something other than a collection" do
136
- it "provides a meaningful error" do
137
- owner = Class.new do
138
- def items
139
- Object.new
140
- end
141
- end.new
142
- lambda do
143
- owner.should have(3).items
144
- end.should raise_error("expected items to be a collection but it does not respond to #length, #size or #count")
145
- end
146
- end
147
-
148
- describe "should_not have(n).items" do
149
-
150
- it "passes if target has a collection of items with < n members" do
151
- owner = create_collection_owner_with(3)
152
- owner.should_not have(4).items_in_collection_with_length_method
153
- owner.should_not have(4).items_in_collection_with_size_method
154
- owner.should_not have(4).items_in_collection_with_count_method
155
- end
156
-
157
- it "passes if target has a collection of items with > n members" do
158
- owner = create_collection_owner_with(3)
159
- owner.should_not have(2).items_in_collection_with_length_method
160
- owner.should_not have(2).items_in_collection_with_size_method
161
- owner.should_not have(2).items_in_collection_with_count_method
162
- end
163
-
164
- it "fails if target has a collection of items with n members" do
165
- owner = create_collection_owner_with(3)
166
- lambda {
167
- owner.should_not have(3).items_in_collection_with_length_method
168
- }.should fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
169
- lambda {
170
- owner.should_not have(3).items_in_collection_with_size_method
171
- }.should fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
172
- lambda {
173
- owner.should_not have(3).items_in_collection_with_count_method
174
- }.should fail_with("expected target not to have 3 items_in_collection_with_count_method, got 3")
175
- end
176
- end
177
-
178
- describe "should have_exactly(n).items" do
179
-
180
- it "passes if target has a collection of items with n members" do
181
- owner = create_collection_owner_with(3)
182
- owner.should have_exactly(3).items_in_collection_with_length_method
183
- owner.should have_exactly(3).items_in_collection_with_size_method
184
- owner.should have_exactly(3).items_in_collection_with_count_method
185
- end
186
-
187
- it "converts :no to 0" do
188
- owner = create_collection_owner_with(0)
189
- owner.should have_exactly(:no).items_in_collection_with_length_method
190
- owner.should have_exactly(:no).items_in_collection_with_size_method
191
- owner.should have_exactly(:no).items_in_collection_with_count_method
192
- end
193
-
194
- it "fails if target has a collection of items with < n members" do
195
- owner = create_collection_owner_with(3)
196
- lambda {
197
- owner.should have_exactly(4).items_in_collection_with_length_method
198
- }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
199
- lambda {
200
- owner.should have_exactly(4).items_in_collection_with_size_method
201
- }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
202
- lambda {
203
- owner.should have_exactly(4).items_in_collection_with_count_method
204
- }.should fail_with("expected 4 items_in_collection_with_count_method, got 3")
205
- end
206
-
207
- it "fails if target has a collection of items with > n members" do
208
- owner = create_collection_owner_with(3)
209
- lambda {
210
- owner.should have_exactly(2).items_in_collection_with_length_method
211
- }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
212
- lambda {
213
- owner.should have_exactly(2).items_in_collection_with_size_method
214
- }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
215
- lambda {
216
- owner.should have_exactly(2).items_in_collection_with_count_method
217
- }.should fail_with("expected 2 items_in_collection_with_count_method, got 3")
218
- end
219
- end
220
-
221
- describe "should have_at_least(n).items" do
222
-
223
- it "passes if target has a collection of items with n members" do
224
- owner = create_collection_owner_with(3)
225
- owner.should have_at_least(3).items_in_collection_with_length_method
226
- owner.should have_at_least(3).items_in_collection_with_size_method
227
- owner.should have_at_least(3).items_in_collection_with_count_method
228
- end
229
-
230
- it "passes if target has a collection of items with > n members" do
231
- owner = create_collection_owner_with(3)
232
- owner.should have_at_least(2).items_in_collection_with_length_method
233
- owner.should have_at_least(2).items_in_collection_with_size_method
234
- owner.should have_at_least(2).items_in_collection_with_count_method
235
- end
236
-
237
- it "fails if target has a collection of items with < n members" do
238
- owner = create_collection_owner_with(3)
239
- lambda {
240
- owner.should have_at_least(4).items_in_collection_with_length_method
241
- }.should fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
242
- lambda {
243
- owner.should have_at_least(4).items_in_collection_with_size_method
244
- }.should fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
245
- lambda {
246
- owner.should have_at_least(4).items_in_collection_with_count_method
247
- }.should fail_with("expected at least 4 items_in_collection_with_count_method, got 3")
248
- end
249
-
250
- it "provides educational negative failure messages" do
251
- #given
252
- owner = create_collection_owner_with(3)
253
- length_matcher = have_at_least(3).items_in_collection_with_length_method
254
- size_matcher = have_at_least(3).items_in_collection_with_size_method
255
- count_matcher = have_at_least(3).items_in_collection_with_count_method
256
-
257
- #when
258
- length_matcher.matches?(owner)
259
- size_matcher.matches?(owner)
260
- count_matcher.matches?(owner)
261
-
262
- #then
263
- length_matcher.failure_message_for_should_not.should eq <<-EOF
264
- Isn't life confusing enough?
265
- Instead of having to figure out the meaning of this:
266
- should_not have_at_least(3).items_in_collection_with_length_method
267
- We recommend that you use this instead:
268
- should have_at_most(2).items_in_collection_with_length_method
269
- EOF
270
-
271
- size_matcher.failure_message_for_should_not.should eq <<-EOF
272
- Isn't life confusing enough?
273
- Instead of having to figure out the meaning of this:
274
- should_not have_at_least(3).items_in_collection_with_size_method
275
- We recommend that you use this instead:
276
- should have_at_most(2).items_in_collection_with_size_method
277
- EOF
278
- count_matcher.failure_message_for_should_not.should eq <<-EOF
279
- Isn't life confusing enough?
280
- Instead of having to figure out the meaning of this:
281
- should_not have_at_least(3).items_in_collection_with_count_method
282
- We recommend that you use this instead:
283
- should have_at_most(2).items_in_collection_with_count_method
284
- EOF
285
- end
286
- end
287
-
288
- describe "should have_at_most(n).items" do
289
- it "passes if target has a collection of items with n members" do
290
- owner = create_collection_owner_with(3)
291
- owner.should have_at_most(3).items_in_collection_with_length_method
292
- owner.should have_at_most(3).items_in_collection_with_size_method
293
- owner.should have_at_most(3).items_in_collection_with_count_method
294
- end
295
-
296
- it "fails if target has a collection of items with > n members" do
297
- owner = create_collection_owner_with(3)
298
- lambda {
299
- owner.should have_at_most(2).items_in_collection_with_length_method
300
- }.should fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
301
- lambda {
302
- owner.should have_at_most(2).items_in_collection_with_size_method
303
- }.should fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
304
- lambda {
305
- owner.should have_at_most(2).items_in_collection_with_count_method
306
- }.should fail_with("expected at most 2 items_in_collection_with_count_method, got 3")
307
- end
308
-
309
- it "passes if target has a collection of items with < n members" do
310
- owner = create_collection_owner_with(3)
311
- owner.should have_at_most(4).items_in_collection_with_length_method
312
- owner.should have_at_most(4).items_in_collection_with_size_method
313
- owner.should have_at_most(4).items_in_collection_with_count_method
314
- end
315
-
316
- it "provides educational negative failure messages" do
317
- #given
318
- owner = create_collection_owner_with(3)
319
- length_matcher = have_at_most(3).items_in_collection_with_length_method
320
- size_matcher = have_at_most(3).items_in_collection_with_size_method
321
- count_matcher = have_at_most(3).items_in_collection_with_count_method
322
-
323
- #when
324
- length_matcher.matches?(owner)
325
- size_matcher.matches?(owner)
326
- count_matcher.matches?(owner)
327
-
328
- #then
329
- length_matcher.failure_message_for_should_not.should eq <<-EOF
330
- Isn't life confusing enough?
331
- Instead of having to figure out the meaning of this:
332
- should_not have_at_most(3).items_in_collection_with_length_method
333
- We recommend that you use this instead:
334
- should have_at_least(4).items_in_collection_with_length_method
335
- EOF
336
-
337
- size_matcher.failure_message_for_should_not.should eq <<-EOF
338
- Isn't life confusing enough?
339
- Instead of having to figure out the meaning of this:
340
- should_not have_at_most(3).items_in_collection_with_size_method
341
- We recommend that you use this instead:
342
- should have_at_least(4).items_in_collection_with_size_method
343
- EOF
344
-
345
- count_matcher.failure_message_for_should_not.should eq <<-EOF
346
- Isn't life confusing enough?
347
- Instead of having to figure out the meaning of this:
348
- should_not have_at_most(3).items_in_collection_with_count_method
349
- We recommend that you use this instead:
350
- should have_at_least(4).items_in_collection_with_count_method
351
- EOF
352
- end
353
- end
354
-
355
- describe "have(n).items(args, block)" do
356
- it "passes args to target" do
357
- target = mock("target")
358
- target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
359
- target.should have(3).items("arg1","arg2")
360
- end
361
-
362
- it "passes block to target" do
363
- target = mock("target")
364
- block = lambda { 5 }
365
- target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
366
- target.should have(3).items("arg1","arg2", block)
367
- end
368
- end
369
-
370
- describe "have(n).items where target IS a collection" do
371
- it "references the number of items IN the collection" do
372
- [1,2,3].should have(3).items
373
- end
374
-
375
- it "fails when the number of items IN the collection is not as expected" do
376
- lambda { [1,2,3].should have(7).items }.should fail_with("expected 7 items, got 3")
377
- end
378
- end
379
-
380
- describe "have(n).characters where target IS a String" do
381
- it "passes if the length is correct" do
382
- "this string".should have(11).characters
383
- end
384
-
385
- it "fails if the length is incorrect" do
386
- lambda { "this string".should have(12).characters }.should fail_with("expected 12 characters, got 11")
387
- end
388
- end
389
-
390
- describe "have(n).things on an object which is not a collection nor contains one" do
391
- it "fails" do
392
- lambda { Object.new.should have(2).things }.should raise_error(NoMethodError) {|e| e.name.should eq :things }
393
- end
394
- end
395
-
396
- describe RSpec::Matchers::BuiltIn::Have, "for a collection owner that implements #send" do
397
- before(:each) do
398
- @collection = Object.new
399
- def @collection.floozles; [1,2] end
400
- end
401
-
402
- it "works in the straightforward case" do
403
- lambda {
404
- @collection.should have(2).floozles
405
- }.should_not raise_error
406
- end
407
-
408
- it "works when doing automatic pluralization" do
409
- lambda {
410
- @collection.should have_at_least(1).floozle
411
- }.should_not raise_error
412
- end
413
-
414
- it "blows up when the owner doesn't respond to that method" do
415
- lambda {
416
- @collection.should have(99).problems
417
- }.should raise_error(NoMethodError, /problems/)
418
- end
419
- end
420
-
421
- describe RSpec::Matchers::BuiltIn::Have do
422
- it "has method_missing as private" do
423
- described_class.private_instance_methods.should include_method(:method_missing)
424
- end
425
-
426
- it "does not respond_to? method_missing (because it's private)" do
427
- formatter = described_class.new(0, StringIO.new)
428
- formatter.should_not respond_to(:method_missing)
429
- end
430
-
431
- describe "respond_to?" do
432
- before :each do
433
- @have = described_class.new(:foo)
434
- @a_method_which_have_defines = described_class.instance_methods.first
435
- @a_method_which_object_defines = Object.instance_methods.first
436
- end
437
-
438
- it "is true for a method which Have defines" do
439
- @have.should respond_to(@a_method_which_have_defines)
440
- end
441
-
442
- it "is true for a method that it's superclass (Object) defines" do
443
- @have.should respond_to(@a_method_which_object_defines)
444
- end
445
-
446
- it "is false for a method which neither Object nor nor Have defines" do
447
- @have.should_not respond_to(:foo_bar_baz)
448
- end
449
-
450
- it "is false if the owner doesn't respond to the method" do
451
- have = described_class.new(99)
452
- have.should_not respond_to(:problems)
453
- end
454
-
455
- it "is true if the owner responds to the method" do
456
- have = described_class.new(:a_symbol)
457
- have.should respond_to(:to_sym)
458
- end
459
- end
460
- end
461
- end