rspec 0.7.5.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/CHANGES +60 -1
  2. data/EXAMPLES.rd +38 -19
  3. data/MIT-LICENSE +1 -1
  4. data/README +24 -17
  5. data/RELEASE-PLAN +117 -0
  6. data/Rakefile +24 -18
  7. data/TODO.0.8.0 +5 -0
  8. data/examples/auto_spec_name_generation_example.rb +18 -0
  9. data/examples/custom_expectation_matchers.rb +53 -0
  10. data/examples/dynamic_spec.rb +9 -0
  11. data/examples/io_processor_spec.rb +2 -2
  12. data/examples/mocking_example.rb +4 -4
  13. data/examples/partial_mock_example.rb +2 -2
  14. data/examples/predicate_example.rb +2 -2
  15. data/examples/stack_spec.rb +32 -36
  16. data/examples/stubbing_example.rb +19 -19
  17. data/examples/test_case_spec.rb +6 -6
  18. data/lib/spec.rb +3 -0
  19. data/lib/spec/callback.rb +8 -0
  20. data/lib/spec/callback/extensions/object.rb +4 -0
  21. data/lib/spec/deprecated.rb +3 -0
  22. data/lib/spec/expectations.rb +44 -17
  23. data/lib/spec/expectations/extensions.rb +1 -2
  24. data/lib/spec/expectations/extensions/object.rb +78 -130
  25. data/lib/spec/expectations/extensions/string_and_symbol.rb +17 -0
  26. data/lib/spec/expectations/handler.rb +47 -0
  27. data/lib/spec/expectations/should/base.rb +32 -29
  28. data/lib/spec/expectations/should/change.rb +1 -1
  29. data/lib/spec/expectations/should/have.rb +9 -17
  30. data/lib/spec/expectations/should/not.rb +54 -56
  31. data/lib/spec/expectations/should/should.rb +59 -65
  32. data/lib/spec/expectations/sugar.rb +27 -4
  33. data/lib/spec/matchers.rb +160 -0
  34. data/lib/spec/matchers/be.rb +161 -0
  35. data/lib/spec/matchers/be_close.rb +37 -0
  36. data/lib/spec/matchers/change.rb +120 -0
  37. data/lib/spec/matchers/eql.rb +43 -0
  38. data/lib/spec/matchers/equal.rb +43 -0
  39. data/lib/spec/matchers/has.rb +44 -0
  40. data/lib/spec/matchers/have.rb +140 -0
  41. data/lib/spec/matchers/include.rb +50 -0
  42. data/lib/spec/matchers/match.rb +41 -0
  43. data/lib/spec/matchers/raise_error.rb +100 -0
  44. data/lib/spec/matchers/respond_to.rb +35 -0
  45. data/lib/spec/matchers/satisfy.rb +47 -0
  46. data/lib/spec/matchers/throw_symbol.rb +75 -0
  47. data/lib/spec/mocks.rb +224 -1
  48. data/lib/spec/mocks/argument_expectation.rb +16 -2
  49. data/lib/spec/mocks/error_generator.rb +5 -3
  50. data/lib/spec/mocks/errors.rb +2 -2
  51. data/lib/spec/mocks/extensions/object.rb +1 -1
  52. data/lib/spec/mocks/message_expectation.rb +29 -19
  53. data/lib/spec/mocks/{mock_methods.rb → methods.rb} +5 -5
  54. data/lib/spec/mocks/mock.rb +2 -2
  55. data/lib/spec/mocks/mock_handler.rb +81 -68
  56. data/lib/spec/rake/spectask.rb +7 -12
  57. data/lib/spec/rake/verify_rcov.rb +1 -1
  58. data/lib/spec/runner.rb +117 -0
  59. data/lib/spec/runner/command_line.rb +8 -5
  60. data/lib/spec/runner/context.rb +13 -37
  61. data/lib/spec/runner/context_eval.rb +4 -3
  62. data/lib/spec/runner/context_runner.rb +7 -4
  63. data/lib/spec/runner/drb_command_line.rb +1 -1
  64. data/lib/spec/runner/execution_context.rb +3 -11
  65. data/lib/spec/runner/extensions/kernel.rb +7 -5
  66. data/lib/spec/runner/extensions/object.rb +4 -1
  67. data/lib/spec/runner/formatter/base_text_formatter.rb +11 -3
  68. data/lib/spec/runner/formatter/html_formatter.rb +21 -10
  69. data/lib/spec/runner/heckle_runner.rb +24 -8
  70. data/lib/spec/runner/heckle_runner_win.rb +10 -0
  71. data/lib/spec/runner/option_parser.rb +58 -13
  72. data/lib/spec/runner/spec_matcher.rb +22 -29
  73. data/lib/spec/runner/spec_parser.rb +1 -0
  74. data/lib/spec/runner/specification.rb +36 -22
  75. data/lib/spec/translator.rb +87 -0
  76. data/lib/spec/version.rb +16 -7
  77. data/spec/spec/callback/callback_container_spec.rb +27 -0
  78. data/spec/spec/callback/module_spec.rb +37 -0
  79. data/spec/spec/callback/object_spec.rb +90 -0
  80. data/spec/spec/callback/object_with_class_callback_spec.rb +19 -0
  81. data/spec/spec/expectations/differs/default_spec.rb +107 -0
  82. data/spec/spec/expectations/extensions/object_spec.rb +46 -0
  83. data/spec/spec/expectations/fail_with_spec.rb +71 -0
  84. data/spec/spec/expectations/should/should_==_spec.rb +19 -0
  85. data/spec/spec/expectations/should/should_=~_spec.rb +13 -0
  86. data/spec/spec/expectations/should/should_be_a_kind_of_spec.rb +21 -0
  87. data/spec/spec/expectations/should/should_be_an_instance_of_spec.rb +30 -0
  88. data/spec/spec/expectations/should/should_be_arbitrary_predicate_spec.rb +81 -0
  89. data/spec/spec/expectations/should/should_be_close_spec.rb +18 -0
  90. data/spec/spec/expectations/should/should_be_comparison_operator_spec.rb +44 -0
  91. data/spec/spec/expectations/should/should_be_false_spec.rb +39 -0
  92. data/spec/spec/expectations/should/should_be_spec.rb +11 -0
  93. data/spec/spec/expectations/should/should_be_true_spec.rb +27 -0
  94. data/spec/spec/expectations/should/should_change_spec.rb +184 -0
  95. data/spec/spec/expectations/should/should_eql_spec.rb +11 -0
  96. data/spec/spec/expectations/should/should_equal_spec.rb +11 -0
  97. data/spec/spec/expectations/should/should_have_at_least_spec.rb +53 -0
  98. data/spec/spec/expectations/should/should_have_at_most_spec.rb +45 -0
  99. data/spec/spec/expectations/should/should_have_key_spec.rb +21 -0
  100. data/spec/spec/expectations/should/should_have_spec.rb +64 -0
  101. data/spec/spec/expectations/should/should_include_spec.rb +59 -0
  102. data/spec/spec/expectations/should/should_match_spec.rb +25 -0
  103. data/spec/spec/expectations/should/should_not_==_spec.rb +15 -0
  104. data/spec/spec/expectations/should/should_not_be_a_kind_of_spec.rb +21 -0
  105. data/spec/spec/expectations/should/should_not_be_an_instance_of_spec.rb +11 -0
  106. data/spec/spec/expectations/should/should_not_be_arbitrary_predicate_spec.rb +68 -0
  107. data/spec/spec/expectations/should/should_not_be_spec.rb +11 -0
  108. data/spec/spec/expectations/should/should_not_change_spec.rb +24 -0
  109. data/spec/spec/expectations/should/should_not_eql_spec.rb +11 -0
  110. data/spec/spec/expectations/should/should_not_equal_spec.rb +11 -0
  111. data/spec/spec/expectations/should/should_not_have_key_spec.rb +15 -0
  112. data/spec/spec/expectations/should/should_not_include_spec.rb +58 -0
  113. data/spec/spec/expectations/should/should_not_match_spec.rb +11 -0
  114. data/spec/spec/expectations/should/should_not_raise_spec.rb +75 -0
  115. data/spec/spec/expectations/should/should_not_respond_to_spec.rb +15 -0
  116. data/spec/spec/expectations/should/should_not_throw_spec.rb +35 -0
  117. data/spec/spec/expectations/should/should_raise_spec.rb +66 -0
  118. data/spec/spec/expectations/should/should_respond_to_spec.rb +15 -0
  119. data/spec/spec/expectations/should/should_satisfy_spec.rb +35 -0
  120. data/spec/spec/expectations/should/should_throw_spec.rb +27 -0
  121. data/spec/spec/matchers/be_close_spec.rb +33 -0
  122. data/spec/spec/matchers/be_spec.rb +182 -0
  123. data/spec/spec/matchers/change_spec.rb +232 -0
  124. data/spec/spec/matchers/description_generation_spec.rb +147 -0
  125. data/spec/spec/matchers/eql_spec.rb +41 -0
  126. data/spec/spec/matchers/equal_spec.rb +41 -0
  127. data/spec/spec/matchers/handler_spec.rb +75 -0
  128. data/spec/spec/matchers/has_spec.rb +37 -0
  129. data/spec/spec/matchers/have_spec.rb +259 -0
  130. data/spec/spec/matchers/include_spec.rb +33 -0
  131. data/spec/spec/matchers/match_spec.rb +37 -0
  132. data/spec/spec/matchers/matcher_methods_spec.rb +85 -0
  133. data/spec/spec/matchers/raise_error_spec.rb +147 -0
  134. data/spec/spec/matchers/respond_to_spec.rb +30 -0
  135. data/spec/spec/matchers/satisfy_spec.rb +36 -0
  136. data/spec/spec/matchers/throw_symbol_spec.rb +59 -0
  137. data/spec/spec/mocks/any_number_of_times_spec.rb +34 -0
  138. data/spec/spec/mocks/at_least_spec.rb +97 -0
  139. data/spec/spec/mocks/at_most_spec.rb +97 -0
  140. data/spec/spec/mocks/bug_report_7611_spec.rb +19 -0
  141. data/spec/spec/mocks/bug_report_7805_spec.rb +22 -0
  142. data/spec/spec/mocks/bug_report_8165_spec.rb +31 -0
  143. data/spec/spec/mocks/bug_report_8302_spec.rb +26 -0
  144. data/spec/spec/mocks/failing_mock_argument_constraints_spec.rb +74 -0
  145. data/spec/spec/mocks/mock_ordering_spec.rb +80 -0
  146. data/spec/spec/mocks/mock_spec.rb +407 -0
  147. data/spec/spec/mocks/multiple_return_value_spec.rb +113 -0
  148. data/spec/spec/mocks/null_object_mock_spec.rb +40 -0
  149. data/spec/spec/mocks/once_counts_spec.rb +56 -0
  150. data/spec/spec/mocks/options_hash_spec.rb +31 -0
  151. data/spec/spec/mocks/partial_mock_spec.rb +52 -0
  152. data/spec/spec/mocks/partial_mock_using_mocks_directly_spec.rb +64 -0
  153. data/spec/spec/mocks/passing_mock_argument_constraints_spec.rb +92 -0
  154. data/spec/spec/mocks/precise_counts_spec.rb +56 -0
  155. data/spec/spec/mocks/record_messages_spec.rb +26 -0
  156. data/spec/spec/mocks/stub_spec.rb +159 -0
  157. data/spec/spec/mocks/twice_counts_spec.rb +67 -0
  158. data/spec/spec/runner/command_line_spec.rb +32 -0
  159. data/spec/spec/runner/context_matching_spec.rb +28 -0
  160. data/spec/spec/runner/context_runner_spec.rb +100 -0
  161. data/spec/spec/runner/context_spec.rb +405 -0
  162. data/spec/spec/runner/drb_command_line_spec.rb +74 -0
  163. data/spec/spec/runner/execution_context_spec.rb +52 -0
  164. data/spec/spec/runner/formatter/html_formatter_spec.rb +40 -0
  165. data/spec/spec/runner/formatter/progress_bar_formatter_dry_run_spec.rb +21 -0
  166. data/spec/spec/runner/formatter/progress_bar_formatter_failure_dump_spec.rb +36 -0
  167. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +78 -0
  168. data/spec/spec/runner/formatter/rdoc_formatter_dry_run_spec.rb +18 -0
  169. data/spec/spec/runner/formatter/rdoc_formatter_spec.rb +41 -0
  170. data/spec/spec/runner/formatter/specdoc_formatter_dry_run_spec.rb +21 -0
  171. data/spec/spec/runner/formatter/specdoc_formatter_spec.rb +46 -0
  172. data/spec/spec/runner/heckle_runner_spec.rb +63 -0
  173. data/spec/spec/runner/heckler_spec.rb +14 -0
  174. data/spec/spec/runner/kernel_ext_spec.rb +16 -0
  175. data/spec/spec/runner/noisy_backtrace_tweaker_spec.rb +45 -0
  176. data/spec/spec/runner/object_ext_spec.rb +11 -0
  177. data/spec/spec/runner/option_parser_spec.rb +269 -0
  178. data/spec/spec/runner/quiet_backtrace_tweaker_spec.rb +47 -0
  179. data/spec/spec/runner/reporter_spec.rb +126 -0
  180. data/spec/spec/runner/spec_matcher_spec.rb +107 -0
  181. data/spec/spec/runner/spec_name_generation_spec.rb +102 -0
  182. data/spec/spec/runner/spec_parser_spec.rb +37 -0
  183. data/spec/spec/runner/specification_class_spec.rb +72 -0
  184. data/spec/spec/runner/specification_instance_spec.rb +160 -0
  185. data/spec/spec/runner/specification_should_raise_spec.rb +136 -0
  186. data/spec/spec/spec_classes.rb +102 -0
  187. data/spec/spec/translator_spec.rb +79 -0
  188. data/spec/spec_helper.rb +35 -0
  189. metadata +141 -9
  190. data/bin/drbspec +0 -3
  191. data/lib/spec/expectations/diff.rb +0 -28
  192. data/lib/spec/expectations/extensions/numeric.rb +0 -19
  193. data/lib/spec/expectations/extensions/string.rb +0 -22
  194. data/lib/spec/expectations/message_builder.rb +0 -13
@@ -0,0 +1,147 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ context "Matchers should be able to generate a description" do
4
+ setup do
5
+ @desc = nil
6
+ @callback = lambda { |desc| @desc = desc }
7
+ Spec::Matchers.description_generated(&@callback)
8
+ end
9
+
10
+ specify "should == expected" do
11
+ "this".should == "this"
12
+ @desc.should == "should == \"this\""
13
+ end
14
+
15
+ specify "should_not == expected" do
16
+ "this".should_not == "that"
17
+ @desc.should == "should not == \"that\""
18
+ end
19
+
20
+ specify "should be empty (arbitrary predicate)" do
21
+ [].should be_empty
22
+ @desc.should == "should be empty"
23
+ end
24
+
25
+ specify "should not be empty (arbitrary predicate)" do
26
+ [1].should_not be_empty
27
+ @desc.should == "should not be empty"
28
+ end
29
+
30
+ specify "should be true" do
31
+ true.should be_true
32
+ @desc.should == "should be true"
33
+ end
34
+
35
+ specify "should be false" do
36
+ false.should be_false
37
+ @desc.should == "should be false"
38
+ end
39
+
40
+ specify "should be nil" do
41
+ nil.should be_nil
42
+ @desc.should == "should be nil"
43
+ end
44
+
45
+ specify "should be > n" do
46
+ 5.should be > 3
47
+ @desc.should == "should be > 3"
48
+ end
49
+
50
+ specify "should be close" do
51
+ 5.0.should be_close(5.0, 0.5)
52
+ @desc.should == "should be close to 5.0 (+- 0.5)"
53
+ end
54
+
55
+ specify "should equal" do
56
+ expected = "expected"
57
+ expected.should equal(expected)
58
+ @desc.should == "should equal \"expected\""
59
+ end
60
+
61
+ specify "should_not equal" do
62
+ 5.should_not equal(37)
63
+ @desc.should == "should not equal 37"
64
+ end
65
+
66
+ specify "should eql" do
67
+ "string".should eql("string")
68
+ @desc.should == "should eql \"string\""
69
+ end
70
+
71
+ specify "should not eql" do
72
+ "a".should_not eql(:a)
73
+ @desc.should == "should not eql :a"
74
+ end
75
+
76
+ specify "should have_key" do
77
+ {:a => "a"}.should have_key(:a)
78
+ @desc.should == "should have key :a"
79
+ end
80
+
81
+ specify "should have n items" do
82
+ team.should have(3).players
83
+ @desc.should == "should have 3 players"
84
+ end
85
+
86
+ specify "should have at least n items" do
87
+ team.should have_at_least(2).players
88
+ @desc.should == "should have at least 2 players"
89
+ end
90
+
91
+ specify "should have at most n items" do
92
+ team.should have_at_most(4).players
93
+ @desc.should == "should have at most 4 players"
94
+ end
95
+
96
+ specify "should include" do
97
+ [1,2,3].should include(3)
98
+ @desc.should == "should include 3"
99
+ end
100
+
101
+ specify "should match" do
102
+ "this string".should match(/this string/)
103
+ @desc.should == "should match /this string/"
104
+ end
105
+
106
+ specify "should raise_error" do
107
+ lambda { raise }.should raise_error
108
+ @desc.should == "should raise Exception"
109
+ end
110
+
111
+ specify "should raise_error with type" do
112
+ lambda { raise }.should raise_error(RuntimeError)
113
+ @desc.should == "should raise RuntimeError"
114
+ end
115
+
116
+ specify "should raise_error with type and message" do
117
+ lambda { raise "there was an error" }.should raise_error(RuntimeError, "there was an error")
118
+ @desc.should == "should raise RuntimeError with \"there was an error\""
119
+ end
120
+
121
+ specify "should respond_to" do
122
+ [].should respond_to(:insert)
123
+ @desc.should == "should respond to #insert"
124
+ end
125
+
126
+ specify "should throw symbol" do
127
+ lambda { throw :what_a_mess }.should throw_symbol
128
+ @desc.should == "should throw a Symbol"
129
+ end
130
+
131
+ specify "should throw symbol (with named symbol)" do
132
+ lambda { throw :what_a_mess }.should throw_symbol(:what_a_mess)
133
+ @desc.should == "should throw :what_a_mess"
134
+ end
135
+
136
+ def team
137
+ Class.new do
138
+ def players
139
+ [1,2,3]
140
+ end
141
+ end.new
142
+ end
143
+
144
+ teardown do
145
+ Spec::Matchers.unregister_callback(:description_generated, @callback)
146
+ end
147
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ context "should eql(expected)" do
4
+
5
+ specify "should pass if target.eql?(expected)" do
6
+ 1.should eql(1)
7
+ end
8
+
9
+ specify "should fail unless target.eql?(expected)" do
10
+ lambda {
11
+ 1.should eql("1")
12
+ }.should fail
13
+ end
14
+
15
+ specify "should provide message, expected and actual on failure" do
16
+ matcher = eql("1")
17
+ matcher.matches?(1)
18
+ matcher.failure_message.should == ["expected \"1\", got 1 (using .eql?)", "1", 1]
19
+ end
20
+
21
+ end
22
+
23
+ context "should_not eql(expected)" do
24
+
25
+ specify "should pass unless target.eql?(expected)" do
26
+ 1.should_not eql("1")
27
+ end
28
+
29
+ specify "should fail if target.eql?(expected)" do
30
+ lambda {
31
+ 1.should_not eql(1)
32
+ }.should fail
33
+ end
34
+
35
+ specify "should provide message, expected and actual on failure" do
36
+ matcher = eql(1)
37
+ matcher.matches?(1)
38
+ matcher.negative_failure_message.should == ["expected 1 not to equal 1 (using .eql?)", 1, 1]
39
+ end
40
+
41
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ context "should equal(expected)" do
4
+
5
+ specify "should pass if target.equal?(expected)" do
6
+ 1.should equal(1)
7
+ end
8
+
9
+ specify "should fail unless target.equal?(expected)" do
10
+ lambda {
11
+ "1".should equal("1")
12
+ }.should fail
13
+ end
14
+
15
+ specify "should provide message, expected and actual on failure" do
16
+ matcher = equal("1")
17
+ matcher.matches?("1")
18
+ matcher.failure_message.should == ["expected \"1\", got \"1\" (using .equal?)", "1", "1"]
19
+ end
20
+
21
+ end
22
+
23
+ context "should_not equal(expected)" do
24
+
25
+ specify "should pass unless target.equal?(expected)" do
26
+ "1".should_not equal("1")
27
+ end
28
+
29
+ specify "should fail if target.equal?(expected)" do
30
+ lambda {
31
+ 1.should_not equal(1)
32
+ }.should fail
33
+ end
34
+
35
+ specify "should provide message, expected and actual on failure" do
36
+ matcher = equal(1)
37
+ matcher.matches?(1)
38
+ matcher.negative_failure_message.should == ["expected 1 not to equal 1 (using .equal?)", 1, 1]
39
+ end
40
+
41
+ end
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
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
+ if block_given?
11
+ @expected = block.call
12
+ end
13
+ @block = block
14
+ end
15
+
16
+ def matches?(target)
17
+ @target = target
18
+ return @expected == target
19
+ end
20
+
21
+ def with(new_value)
22
+ @expected = new_value
23
+ self
24
+ end
25
+
26
+ def failure_message
27
+ "expected #{@expected}, got #{@target}"
28
+ end
29
+
30
+ def negative_failure_message
31
+ "expected not #{@expected}, got #{@target}"
32
+ end
33
+ end
34
+
35
+ class PositiveOnlyMatcher < ArbitraryMatcher
36
+ undef negative_failure_message
37
+ end
38
+
39
+ def arbitrary_matcher(*args, &block)
40
+ ArbitraryMatcher.new(*args, &block)
41
+ end
42
+
43
+ def positive_only_matcher(*args, &block)
44
+ PositiveOnlyMatcher.new(*args, &block)
45
+ end
46
+
47
+ end
48
+
49
+ context "ExpectationMatcherHandler" do
50
+ include ExampleExpectations
51
+
52
+ specify "should handle submitted args" do
53
+ 5.should arbitrary_matcher(:expected => 5)
54
+ 5.should arbitrary_matcher(:expected => "wrong").with(5)
55
+ lambda { 5.should arbitrary_matcher(:expected => 4) }.should fail_with("expected 4, got 5")
56
+ lambda { 5.should arbitrary_matcher(:expected => 5).with(4) }.should fail_with("expected 4, got 5")
57
+ 5.should_not arbitrary_matcher(:expected => 4)
58
+ 5.should_not arbitrary_matcher(:expected => 5).with(4)
59
+ lambda { 5.should_not arbitrary_matcher(:expected => 5) }.should fail_with("expected not 5, got 5")
60
+ lambda { 5.should_not arbitrary_matcher(:expected => 4).with(5) }.should fail_with("expected not 5, got 5")
61
+ end
62
+
63
+ specify "should handle the submitted block" do
64
+ 5.should arbitrary_matcher { 5 }
65
+ 5.should arbitrary_matcher(:expected => 4) { 5 }
66
+ 5.should arbitrary_matcher(:expected => 4).with(5) { 3 }
67
+ end
68
+
69
+ specify "should explain when matcher does not support should_not" do
70
+ lambda {
71
+ 5.should_not positive_only_matcher(:expected => 5)
72
+ }.should fail_with(/Matcher does not support should_not.\n/)
73
+ end
74
+
75
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ context "should have_sym(*args)" do
4
+ specify "should pass if #has_sym?(*args) returns true" do
5
+ {:a => "A"}.should have_key(:a)
6
+ end
7
+
8
+ specify "should fail if #has_sym?(*args) returns false" do
9
+ lambda {
10
+ {:b => "B"}.should have_key(:a)
11
+ }.should fail_with("expected #has_key?(:a) to return true, got false")
12
+ end
13
+
14
+ specify "should fail if target does not respond to #has_sym?" do
15
+ lambda {
16
+ Object.new.should have_key(:a)
17
+ }.should raise_error(NoMethodError)
18
+ end
19
+ end
20
+
21
+ context "should_not have_sym(*args)" do
22
+ specify "should pass if #has_sym?(*args) returns false" do
23
+ {:a => "A"}.should_not have_key(:b)
24
+ end
25
+
26
+ specify "should fail if #has_sym?(*args) returns true" do
27
+ lambda {
28
+ {:a => "A"}.should_not have_key(:a)
29
+ }.should fail_with("expected #has_key?(:a) to return false, got true")
30
+ end
31
+
32
+ specify "should fail if target does not respond to #has_sym?" do
33
+ lambda {
34
+ Object.new.should have_key(:a)
35
+ }.should raise_error(NoMethodError)
36
+ end
37
+ end
@@ -0,0 +1,259 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module HaveSpecHelper
4
+ def create_collection_owner_with(n)
5
+ owner = Spec::Expectations::Helper::CollectionOwner.new
6
+ (1..n).each do |n|
7
+ owner.add_to_collection_with_length_method(n)
8
+ owner.add_to_collection_with_size_method(n)
9
+ end
10
+ owner
11
+ end
12
+ end
13
+
14
+ context "should have(n).items" do
15
+ include HaveSpecHelper
16
+
17
+ specify "should pass if target has a collection of items with n members" do
18
+ owner = create_collection_owner_with(3)
19
+ owner.should have(3).items_in_collection_with_length_method
20
+ owner.should have(3).items_in_collection_with_size_method
21
+ end
22
+
23
+ specify "should convert :no to 0" do
24
+ owner = create_collection_owner_with(0)
25
+ owner.should have(:no).items_in_collection_with_length_method
26
+ owner.should have(:no).items_in_collection_with_size_method
27
+ end
28
+
29
+ specify "should fail if target has a collection of items with < n members" do
30
+ owner = create_collection_owner_with(3)
31
+ lambda {
32
+ owner.should have(4).items_in_collection_with_length_method
33
+ }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
34
+ lambda {
35
+ owner.should have(4).items_in_collection_with_size_method
36
+ }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
37
+ end
38
+
39
+ specify "should fail if target has a collection of items with > n members" do
40
+ owner = create_collection_owner_with(3)
41
+ lambda {
42
+ owner.should have(2).items_in_collection_with_length_method
43
+ }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
44
+ lambda {
45
+ owner.should have(2).items_in_collection_with_size_method
46
+ }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
47
+ end
48
+ end
49
+
50
+ context "should_not have(n).items" do
51
+ include HaveSpecHelper
52
+
53
+ specify "should pass if target has a collection of items with < n members" do
54
+ owner = create_collection_owner_with(3)
55
+ owner.should_not have(4).items_in_collection_with_length_method
56
+ owner.should_not have(4).items_in_collection_with_size_method
57
+ end
58
+
59
+ specify "should pass if target has a collection of items with > n members" do
60
+ owner = create_collection_owner_with(3)
61
+ owner.should_not have(2).items_in_collection_with_length_method
62
+ owner.should_not have(2).items_in_collection_with_size_method
63
+ end
64
+
65
+ specify "should fail if target has a collection of items with n members" do
66
+ owner = create_collection_owner_with(3)
67
+ lambda {
68
+ owner.should_not have(3).items_in_collection_with_length_method
69
+ }.should fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
70
+ lambda {
71
+ owner.should_not have(3).items_in_collection_with_size_method
72
+ }.should fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
73
+ end
74
+ end
75
+
76
+ context "should have_exactly(n).items" do
77
+ include HaveSpecHelper
78
+
79
+ specify "should pass if target has a collection of items with n members" do
80
+ owner = create_collection_owner_with(3)
81
+ owner.should have_exactly(3).items_in_collection_with_length_method
82
+ owner.should have_exactly(3).items_in_collection_with_size_method
83
+ end
84
+
85
+ specify "should convert :no to 0" do
86
+ owner = create_collection_owner_with(0)
87
+ owner.should have_exactly(:no).items_in_collection_with_length_method
88
+ owner.should have_exactly(:no).items_in_collection_with_size_method
89
+ end
90
+
91
+ specify "should fail if target has a collection of items with < n members" do
92
+ owner = create_collection_owner_with(3)
93
+ lambda {
94
+ owner.should have_exactly(4).items_in_collection_with_length_method
95
+ }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
96
+ lambda {
97
+ owner.should have_exactly(4).items_in_collection_with_size_method
98
+ }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
99
+ end
100
+
101
+ specify "should fail if target has a collection of items with > n members" do
102
+ owner = create_collection_owner_with(3)
103
+ lambda {
104
+ owner.should have_exactly(2).items_in_collection_with_length_method
105
+ }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
106
+ lambda {
107
+ owner.should have_exactly(2).items_in_collection_with_size_method
108
+ }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
109
+ end
110
+ end
111
+
112
+ context "should have_at_least(n).items" do
113
+ include HaveSpecHelper
114
+
115
+ specify "should pass if target has a collection of items with n members" do
116
+ owner = create_collection_owner_with(3)
117
+ owner.should have_at_least(3).items_in_collection_with_length_method
118
+ owner.should have_at_least(3).items_in_collection_with_size_method
119
+ end
120
+
121
+ specify "should pass if target has a collection of items with > n members" do
122
+ owner = create_collection_owner_with(3)
123
+ owner.should have_at_least(2).items_in_collection_with_length_method
124
+ owner.should have_at_least(2).items_in_collection_with_size_method
125
+ end
126
+
127
+ specify "should fail if target has a collection of items with < n members" do
128
+ owner = create_collection_owner_with(3)
129
+ lambda {
130
+ owner.should have_at_least(4).items_in_collection_with_length_method
131
+ }.should fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
132
+ lambda {
133
+ owner.should have_at_least(4).items_in_collection_with_size_method
134
+ }.should fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
135
+ end
136
+
137
+ specify "should provide educational negative failure messages" do
138
+ #given
139
+ owner = create_collection_owner_with(3)
140
+ length_matcher = have_at_least(3).items_in_collection_with_length_method
141
+ size_matcher = have_at_least(3).items_in_collection_with_size_method
142
+
143
+ #when
144
+ length_matcher.matches?(owner)
145
+ size_matcher.matches?(owner)
146
+
147
+ #then
148
+ length_matcher.negative_failure_message.should == <<-EOF
149
+ Isn't life confusing enough?
150
+ Instead of having to figure out the meaning of this:
151
+ should_not have_at_least(3).items_in_collection_with_length_method
152
+ We recommend that you use this instead:
153
+ should have_at_most(2).items_in_collection_with_length_method
154
+ EOF
155
+
156
+ size_matcher.negative_failure_message.should == <<-EOF
157
+ Isn't life confusing enough?
158
+ Instead of having to figure out the meaning of this:
159
+ should_not have_at_least(3).items_in_collection_with_size_method
160
+ We recommend that you use this instead:
161
+ should have_at_most(2).items_in_collection_with_size_method
162
+ EOF
163
+ end
164
+ end
165
+
166
+ context "should have_at_most(n).items" do
167
+ include HaveSpecHelper
168
+
169
+ specify "should pass if target has a collection of items with n members" do
170
+ owner = create_collection_owner_with(3)
171
+ owner.should have_at_most(3).items_in_collection_with_length_method
172
+ owner.should have_at_most(3).items_in_collection_with_size_method
173
+ end
174
+
175
+ specify "should fail if target has a collection of items with > n members" do
176
+ owner = create_collection_owner_with(3)
177
+ lambda {
178
+ owner.should have_at_most(2).items_in_collection_with_length_method
179
+ }.should fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
180
+ lambda {
181
+ owner.should have_at_most(2).items_in_collection_with_size_method
182
+ }.should fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
183
+ end
184
+
185
+ specify "should pass if target has a collection of items with < n members" do
186
+ owner = create_collection_owner_with(3)
187
+ owner.should have_at_most(4).items_in_collection_with_length_method
188
+ owner.should have_at_most(4).items_in_collection_with_size_method
189
+ end
190
+
191
+ specify "should provide educational negative failure messages" do
192
+ #given
193
+ owner = create_collection_owner_with(3)
194
+ length_matcher = have_at_most(3).items_in_collection_with_length_method
195
+ size_matcher = have_at_most(3).items_in_collection_with_size_method
196
+
197
+ #when
198
+ length_matcher.matches?(owner)
199
+ size_matcher.matches?(owner)
200
+
201
+ #then
202
+ length_matcher.negative_failure_message.should == <<-EOF
203
+ Isn't life confusing enough?
204
+ Instead of having to figure out the meaning of this:
205
+ should_not have_at_most(3).items_in_collection_with_length_method
206
+ We recommend that you use this instead:
207
+ should have_at_least(4).items_in_collection_with_length_method
208
+ EOF
209
+
210
+ size_matcher.negative_failure_message.should == <<-EOF
211
+ Isn't life confusing enough?
212
+ Instead of having to figure out the meaning of this:
213
+ should_not have_at_most(3).items_in_collection_with_size_method
214
+ We recommend that you use this instead:
215
+ should have_at_least(4).items_in_collection_with_size_method
216
+ EOF
217
+ end
218
+ end
219
+
220
+ context "have(n).items(args, block)" do
221
+ specify "should pass args to target" do
222
+ target = mock("target")
223
+ target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
224
+ target.should have(3).items("arg1","arg2")
225
+ end
226
+
227
+ specify "should pass block to target" do
228
+ target = mock("target")
229
+ block = lambda { 5 }
230
+ target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
231
+ target.should have(3).items("arg1","arg2", block)
232
+ end
233
+ end
234
+
235
+ context "have(n).items where target IS a collection" do
236
+ specify "should reference the number of items IN the collection" do
237
+ [1,2,3].should have(3).items
238
+ end
239
+
240
+ specify "should reference the number of items IN the collection" do
241
+ lambda { [1,2,3].should have(7).items }.should_fail_with("expected 7 items, got 3")
242
+ end
243
+ end
244
+
245
+ context "have(n).characters where target IS a String" do
246
+ specify "should pass if the length is correct" do
247
+ "this string".should have(11).characters
248
+ end
249
+
250
+ specify "should fail if the length is incorrect" do
251
+ lambda { "this string".should have(12).characters }.should_fail_with("expected 12 characters, got 11")
252
+ end
253
+ end
254
+
255
+ context "have(n).things on an object which is not a collection nor contains one" do
256
+ specify "should fail" do
257
+ lambda { Object.new.should have(2).things }.should_raise(NoMethodError, /undefined method `things' for #<Object:/)
258
+ end
259
+ end