rspec 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (245) hide show
  1. data/CHANGES +125 -9
  2. data/EXAMPLES.rd +50 -27
  3. data/README +14 -1
  4. data/Rakefile +95 -42
  5. data/UPGRADE +31 -0
  6. data/bin/spec +0 -1
  7. data/bin/spec_translator +6 -0
  8. data/examples/auto_spec_description_example.rb +19 -0
  9. data/examples/{setup_teardown_example.rb → before_and_after_example.rb} +6 -6
  10. data/examples/behave_as_example.rb +45 -0
  11. data/examples/custom_expectation_matchers.rb +13 -12
  12. data/examples/dynamic_spec.rb +2 -2
  13. data/examples/file_accessor_spec.rb +2 -2
  14. data/examples/greeter_spec.rb +3 -3
  15. data/examples/helper_method_example.rb +2 -2
  16. data/examples/io_processor_spec.rb +4 -4
  17. data/examples/legacy_spec.rb +10 -0
  18. data/examples/mocking_example.rb +5 -5
  19. data/examples/multi_threaded_behaviour_runner.rb +25 -0
  20. data/examples/partial_mock_example.rb +4 -4
  21. data/examples/predicate_example.rb +4 -4
  22. data/examples/priority.txt +1 -0
  23. data/examples/shared_behaviours_example.rb +31 -0
  24. data/examples/stack_spec.rb +52 -69
  25. data/examples/stubbing_example.rb +10 -10
  26. data/examples/test_case_adapter_example.rb +26 -0
  27. data/examples/test_case_spec.rb +6 -6
  28. data/lib/spec.rb +9 -4
  29. data/lib/spec/dsl.rb +10 -0
  30. data/lib/spec/dsl/behaviour.rb +189 -0
  31. data/lib/spec/dsl/behaviour_callbacks.rb +43 -0
  32. data/lib/spec/dsl/behaviour_eval.rb +170 -0
  33. data/lib/spec/dsl/behaviour_factory.rb +32 -0
  34. data/lib/spec/dsl/composite_proc_builder.rb +28 -0
  35. data/lib/spec/dsl/configuration.rb +38 -0
  36. data/lib/spec/dsl/description.rb +34 -0
  37. data/lib/spec/dsl/example.rb +114 -0
  38. data/lib/spec/dsl/example_matcher.rb +28 -0
  39. data/lib/spec/{runner/spec_should_raise_handler.rb → dsl/example_should_raise_handler.rb} +4 -4
  40. data/lib/spec/expectations.rb +0 -3
  41. data/lib/spec/expectations/differs/default.rb +0 -1
  42. data/lib/spec/expectations/extensions.rb +0 -1
  43. data/lib/spec/expectations/extensions/object.rb +10 -53
  44. data/lib/spec/expectations/handler.rb +14 -18
  45. data/lib/spec/extensions.rb +1 -0
  46. data/lib/spec/extensions/object.rb +6 -0
  47. data/lib/spec/matchers.rb +19 -21
  48. data/lib/spec/matchers/be.rb +40 -11
  49. data/lib/spec/matchers/be_close.rb +2 -2
  50. data/lib/spec/matchers/operator_matcher.rb +52 -0
  51. data/lib/spec/matchers/respond_to.rb +21 -11
  52. data/lib/spec/mocks.rb +5 -28
  53. data/lib/spec/mocks/argument_constraint_matchers.rb +12 -0
  54. data/lib/spec/mocks/argument_expectation.rb +7 -4
  55. data/lib/spec/mocks/methods.rb +11 -16
  56. data/lib/spec/mocks/mock.rb +6 -3
  57. data/lib/spec/mocks/{mock_handler.rb → proxy.rb} +4 -7
  58. data/lib/spec/mocks/space.rb +28 -0
  59. data/lib/spec/mocks/spec_methods.rb +30 -0
  60. data/lib/spec/rake/spectask.rb +23 -21
  61. data/lib/spec/rake/verify_rcov.rb +1 -0
  62. data/lib/spec/runner.rb +88 -35
  63. data/lib/spec/runner/backtrace_tweaker.rb +2 -1
  64. data/lib/spec/runner/behaviour_runner.rb +102 -0
  65. data/lib/spec/runner/command_line.rb +5 -17
  66. data/lib/spec/runner/drb_command_line.rb +2 -2
  67. data/lib/spec/runner/extensions/kernel.rb +22 -9
  68. data/lib/spec/runner/formatter.rb +4 -0
  69. data/lib/spec/runner/formatter/base_formatter.rb +63 -0
  70. data/lib/spec/runner/formatter/base_text_formatter.rb +22 -52
  71. data/lib/spec/runner/formatter/failing_behaviours_formatter.rb +25 -0
  72. data/lib/spec/runner/formatter/failing_examples_formatter.rb +22 -0
  73. data/lib/spec/runner/formatter/html_formatter.rb +74 -29
  74. data/lib/spec/runner/formatter/progress_bar_formatter.rb +6 -8
  75. data/lib/spec/runner/formatter/rdoc_formatter.rb +6 -6
  76. data/lib/spec/runner/formatter/snippet_extractor.rb +52 -0
  77. data/lib/spec/runner/formatter/specdoc_formatter.rb +6 -6
  78. data/lib/spec/runner/heckle_runner.rb +8 -7
  79. data/lib/spec/runner/option_parser.rb +136 -55
  80. data/lib/spec/runner/options.rb +26 -0
  81. data/lib/spec/runner/reporter.rb +38 -31
  82. data/lib/spec/runner/spec_parser.rb +22 -13
  83. data/lib/spec/test_case_adapter.rb +10 -0
  84. data/lib/spec/translator.rb +103 -86
  85. data/lib/spec/version.rb +7 -15
  86. data/plugins/mock_frameworks/flexmock.rb +27 -0
  87. data/plugins/mock_frameworks/mocha.rb +21 -0
  88. data/plugins/mock_frameworks/rspec.rb +18 -0
  89. data/spec/spec/dsl/behaviour_eval_spec.rb +49 -0
  90. data/spec/spec/dsl/behaviour_factory_spec.rb +30 -0
  91. data/spec/spec/dsl/behaviour_spec.rb +508 -0
  92. data/spec/spec/dsl/composite_proc_builder_spec.rb +57 -0
  93. data/spec/spec/dsl/configuration_spec.rb +43 -0
  94. data/spec/spec/dsl/description_spec.rb +51 -0
  95. data/spec/spec/dsl/example_class_spec.rb +24 -0
  96. data/spec/spec/dsl/example_instance_spec.rb +140 -0
  97. data/spec/spec/dsl/example_should_raise_spec.rb +137 -0
  98. data/spec/spec/dsl/predicate_matcher_spec.rb +21 -0
  99. data/spec/spec/dsl/shared_behaviour_spec.rb +186 -0
  100. data/spec/spec/expectations/differs/default_spec.rb +12 -12
  101. data/spec/spec/expectations/extensions/object_spec.rb +10 -10
  102. data/spec/spec/expectations/fail_with_spec.rb +20 -20
  103. data/spec/spec/matchers/be_close_spec.rb +37 -31
  104. data/spec/spec/matchers/be_spec.rb +50 -41
  105. data/spec/spec/matchers/change_spec.rb +54 -54
  106. data/spec/spec/matchers/description_generation_spec.rb +43 -31
  107. data/spec/spec/matchers/eql_spec.rb +24 -37
  108. data/spec/spec/matchers/equal_spec.rb +24 -37
  109. data/spec/spec/matchers/exist_spec.rb +48 -0
  110. data/spec/spec/matchers/handler_spec.rb +36 -23
  111. data/spec/spec/matchers/has_spec.rb +8 -8
  112. data/spec/spec/matchers/have_spec.rb +38 -38
  113. data/spec/spec/matchers/include_spec.rb +6 -6
  114. data/spec/spec/matchers/match_spec.rb +8 -8
  115. data/spec/spec/matchers/matcher_methods_spec.rb +24 -31
  116. data/spec/spec/matchers/raise_error_spec.rb +34 -34
  117. data/spec/spec/matchers/respond_to_spec.rb +32 -8
  118. data/spec/spec/matchers/satisfy_spec.rb +6 -6
  119. data/spec/spec/matchers/should_===_spec.rb +38 -0
  120. data/spec/spec/matchers/should_==_spec.rb +37 -0
  121. data/spec/spec/matchers/should_=~_spec.rb +36 -0
  122. data/spec/spec/matchers/throw_symbol_spec.rb +47 -55
  123. data/spec/spec/mocks/any_number_of_times_spec.rb +16 -21
  124. data/spec/spec/mocks/argument_expectation_spec.rb +3 -3
  125. data/spec/spec/mocks/at_least_spec.rb +30 -30
  126. data/spec/spec/mocks/at_most_spec.rb +53 -57
  127. data/spec/spec/mocks/bug_report_10260_spec.rb +8 -0
  128. data/spec/spec/mocks/bug_report_7611_spec.rb +3 -3
  129. data/spec/spec/mocks/bug_report_7805_spec.rb +3 -3
  130. data/spec/spec/mocks/bug_report_8165_spec.rb +5 -5
  131. data/spec/spec/mocks/bug_report_8302_spec.rb +5 -5
  132. data/spec/spec/mocks/failing_mock_argument_constraints_spec.rb +26 -27
  133. data/spec/spec/mocks/mock_ordering_spec.rb +19 -15
  134. data/spec/spec/mocks/mock_space_spec.rb +54 -0
  135. data/spec/spec/mocks/mock_spec.rb +111 -141
  136. data/spec/spec/mocks/multiple_return_value_spec.rb +48 -48
  137. data/spec/spec/mocks/null_object_mock_spec.rb +10 -10
  138. data/spec/spec/mocks/once_counts_spec.rb +32 -35
  139. data/spec/spec/mocks/options_hash_spec.rb +12 -10
  140. data/spec/spec/mocks/partial_mock_spec.rb +15 -15
  141. data/spec/spec/mocks/partial_mock_using_mocks_directly_spec.rb +24 -22
  142. data/spec/spec/mocks/passing_mock_argument_constraints_spec.rb +19 -19
  143. data/spec/spec/mocks/precise_counts_spec.rb +28 -32
  144. data/spec/spec/mocks/record_messages_spec.rb +10 -10
  145. data/spec/spec/mocks/stub_spec.rb +45 -45
  146. data/spec/spec/mocks/twice_counts_spec.rb +21 -21
  147. data/spec/spec/package/bin_spec_spec.rb +12 -0
  148. data/spec/spec/runner/behaviour_runner_spec.rb +114 -0
  149. data/spec/spec/runner/command_line_spec.rb +8 -8
  150. data/spec/spec/runner/context_matching_spec.rb +14 -15
  151. data/spec/spec/runner/drb_command_line_spec.rb +12 -12
  152. data/spec/spec/runner/execution_context_spec.rb +8 -29
  153. data/spec/spec/runner/extensions/kernel_spec.rb +36 -0
  154. data/spec/spec/runner/formatter/failing_behaviours_formatter_spec.rb +27 -0
  155. data/spec/spec/runner/formatter/failing_examples_formatter_spec.rb +28 -0
  156. data/spec/spec/runner/formatter/html_formatter_spec.rb +9 -8
  157. data/spec/spec/runner/formatter/progress_bar_formatter_dry_run_spec.rb +6 -6
  158. data/spec/spec/runner/formatter/progress_bar_formatter_failure_dump_spec.rb +10 -10
  159. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +22 -27
  160. data/spec/spec/runner/formatter/rdoc_formatter_dry_run_spec.rb +6 -5
  161. data/spec/spec/runner/formatter/rdoc_formatter_spec.rb +17 -17
  162. data/spec/spec/runner/formatter/snippet_extractor_spec.rb +11 -0
  163. data/spec/spec/runner/formatter/specdoc_formatter_dry_run_spec.rb +6 -6
  164. data/spec/spec/runner/formatter/specdoc_formatter_spec.rb +17 -17
  165. data/spec/spec/runner/heckle_runner_spec.rb +21 -21
  166. data/spec/spec/runner/heckler_spec.rb +5 -5
  167. data/spec/spec/runner/noisy_backtrace_tweaker_spec.rb +12 -12
  168. data/spec/spec/runner/object_ext_spec.rb +3 -3
  169. data/spec/spec/runner/option_parser_spec.rb +171 -102
  170. data/spec/spec/runner/quiet_backtrace_tweaker_spec.rb +21 -12
  171. data/spec/spec/runner/reporter_spec.rb +106 -97
  172. data/spec/spec/runner/spec_matcher_spec.rb +46 -51
  173. data/spec/spec/runner/spec_parser_spec.rb +72 -16
  174. data/spec/spec/spec_classes.rb +12 -3
  175. data/spec/spec/translator_spec.rb +165 -36
  176. metadata +66 -76
  177. data/RELEASE-PLAN +0 -117
  178. data/examples/auto_spec_name_generation_example.rb +0 -18
  179. data/lib/spec/callback.rb +0 -11
  180. data/lib/spec/callback/callback_container.rb +0 -60
  181. data/lib/spec/callback/extensions/module.rb +0 -24
  182. data/lib/spec/callback/extensions/object.rb +0 -37
  183. data/lib/spec/deprecated.rb +0 -3
  184. data/lib/spec/expectations/extensions/proc.rb +0 -57
  185. data/lib/spec/expectations/should.rb +0 -5
  186. data/lib/spec/expectations/should/base.rb +0 -64
  187. data/lib/spec/expectations/should/change.rb +0 -69
  188. data/lib/spec/expectations/should/have.rb +0 -128
  189. data/lib/spec/expectations/should/not.rb +0 -74
  190. data/lib/spec/expectations/should/should.rb +0 -81
  191. data/lib/spec/expectations/sugar.rb +0 -47
  192. data/lib/spec/runner/context.rb +0 -154
  193. data/lib/spec/runner/context_eval.rb +0 -142
  194. data/lib/spec/runner/context_runner.rb +0 -55
  195. data/lib/spec/runner/execution_context.rb +0 -17
  196. data/lib/spec/runner/spec_matcher.rb +0 -25
  197. data/lib/spec/runner/specification.rb +0 -114
  198. data/spec/spec/callback/callback_container_spec.rb +0 -27
  199. data/spec/spec/callback/module_spec.rb +0 -37
  200. data/spec/spec/callback/object_spec.rb +0 -90
  201. data/spec/spec/callback/object_with_class_callback_spec.rb +0 -19
  202. data/spec/spec/expectations/should/should_==_spec.rb +0 -19
  203. data/spec/spec/expectations/should/should_=~_spec.rb +0 -13
  204. data/spec/spec/expectations/should/should_be_a_kind_of_spec.rb +0 -21
  205. data/spec/spec/expectations/should/should_be_an_instance_of_spec.rb +0 -30
  206. data/spec/spec/expectations/should/should_be_arbitrary_predicate_spec.rb +0 -81
  207. data/spec/spec/expectations/should/should_be_close_spec.rb +0 -18
  208. data/spec/spec/expectations/should/should_be_comparison_operator_spec.rb +0 -44
  209. data/spec/spec/expectations/should/should_be_false_spec.rb +0 -39
  210. data/spec/spec/expectations/should/should_be_spec.rb +0 -11
  211. data/spec/spec/expectations/should/should_be_true_spec.rb +0 -27
  212. data/spec/spec/expectations/should/should_change_spec.rb +0 -184
  213. data/spec/spec/expectations/should/should_eql_spec.rb +0 -11
  214. data/spec/spec/expectations/should/should_equal_spec.rb +0 -11
  215. data/spec/spec/expectations/should/should_have_at_least_spec.rb +0 -53
  216. data/spec/spec/expectations/should/should_have_at_most_spec.rb +0 -45
  217. data/spec/spec/expectations/should/should_have_key_spec.rb +0 -21
  218. data/spec/spec/expectations/should/should_have_spec.rb +0 -64
  219. data/spec/spec/expectations/should/should_include_spec.rb +0 -59
  220. data/spec/spec/expectations/should/should_match_spec.rb +0 -25
  221. data/spec/spec/expectations/should/should_not_==_spec.rb +0 -15
  222. data/spec/spec/expectations/should/should_not_be_a_kind_of_spec.rb +0 -21
  223. data/spec/spec/expectations/should/should_not_be_an_instance_of_spec.rb +0 -11
  224. data/spec/spec/expectations/should/should_not_be_arbitrary_predicate_spec.rb +0 -68
  225. data/spec/spec/expectations/should/should_not_be_spec.rb +0 -11
  226. data/spec/spec/expectations/should/should_not_change_spec.rb +0 -24
  227. data/spec/spec/expectations/should/should_not_eql_spec.rb +0 -11
  228. data/spec/spec/expectations/should/should_not_equal_spec.rb +0 -11
  229. data/spec/spec/expectations/should/should_not_have_key_spec.rb +0 -15
  230. data/spec/spec/expectations/should/should_not_include_spec.rb +0 -58
  231. data/spec/spec/expectations/should/should_not_match_spec.rb +0 -11
  232. data/spec/spec/expectations/should/should_not_raise_spec.rb +0 -75
  233. data/spec/spec/expectations/should/should_not_respond_to_spec.rb +0 -15
  234. data/spec/spec/expectations/should/should_not_throw_spec.rb +0 -35
  235. data/spec/spec/expectations/should/should_raise_spec.rb +0 -66
  236. data/spec/spec/expectations/should/should_respond_to_spec.rb +0 -15
  237. data/spec/spec/expectations/should/should_satisfy_spec.rb +0 -35
  238. data/spec/spec/expectations/should/should_throw_spec.rb +0 -27
  239. data/spec/spec/runner/context_runner_spec.rb +0 -100
  240. data/spec/spec/runner/context_spec.rb +0 -405
  241. data/spec/spec/runner/kernel_ext_spec.rb +0 -16
  242. data/spec/spec/runner/spec_name_generation_spec.rb +0 -102
  243. data/spec/spec/runner/specification_class_spec.rb +0 -72
  244. data/spec/spec/runner/specification_instance_spec.rb +0 -160
  245. data/spec/spec/runner/specification_should_raise_spec.rb +0 -136
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Jim Weirich on 2007-04-10.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ begin
7
+ require 'rubygems'
8
+ rescue LoadError => ex
9
+ end
10
+ require 'flexmock/rspec'
11
+
12
+ module Spec
13
+ module Plugins
14
+ module MockFramework
15
+ include FlexMock::MockContainer
16
+ def setup_mocks_for_rspec
17
+ # No setup required
18
+ end
19
+ def verify_mocks_for_rspec
20
+ flexmock_verify
21
+ end
22
+ def teardown_mocks_for_rspec
23
+ flexmock_close
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ gem 'mocha'
3
+ require 'mocha/standalone'
4
+ require 'mocha/object'
5
+
6
+ module Spec
7
+ module Plugins
8
+ module MockFramework
9
+ include Mocha::Standalone
10
+ def setup_mocks_for_rspec
11
+ mocha_setup
12
+ end
13
+ def verify_mocks_for_rspec
14
+ mocha_verify
15
+ end
16
+ def teardown_mocks_for_rspec
17
+ mocha_teardown
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "spec", "mocks"))
2
+
3
+ module Spec
4
+ module Plugins
5
+ module MockFramework
6
+ include Spec::Mocks::SpecMethods
7
+ def setup_mocks_for_rspec
8
+ $rspec_mocks ||= Spec::Mocks::Space.new
9
+ end
10
+ def verify_mocks_for_rspec
11
+ $rspec_mocks.verify_all
12
+ end
13
+ def teardown_mocks_for_rspec
14
+ $rspec_mocks.reset_all
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ module Spec
4
+ module DSL
5
+ describe BehaviourEval do
6
+ def count
7
+ @count ||= 0
8
+ @count = @count + 1
9
+ @count
10
+ end
11
+
12
+ before(:all) do
13
+ count.should == 1
14
+ end
15
+
16
+ before(:all) do
17
+ count.should == 2
18
+ end
19
+
20
+ before(:each) do
21
+ count.should == 3
22
+ end
23
+
24
+ before(:each) do
25
+ count.should == 4
26
+ end
27
+
28
+ it "should run before(:all), before(:each), example, after(:each), after(:all) in order" do
29
+ count.should == 5
30
+ end
31
+
32
+ after(:each) do
33
+ count.should == 7
34
+ end
35
+
36
+ after(:each) do
37
+ count.should == 6
38
+ end
39
+
40
+ after(:all) do
41
+ count.should == 9
42
+ end
43
+
44
+ after(:all) do
45
+ count.should == 8
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ module Spec
4
+ module DSL
5
+ describe BehaviourFactory do
6
+ it "should create a Spec::DSL::Behaviour by default" do
7
+ Spec::DSL::BehaviourFactory.create("behaviour") {
8
+ }.should be_an_instance_of(Spec::DSL::Behaviour)
9
+ end
10
+
11
+ it "should create a Spec::DSL::Behaviour when :behaviour_type => :default" do
12
+ Spec::DSL::BehaviourFactory.create("behaviour", :behaviour_type => :default) {
13
+ }.should be_an_instance_of(Spec::DSL::Behaviour)
14
+ end
15
+
16
+ it "should create specified type when :behaviour_type => :something_other_than_default" do
17
+ behaviour_class = Class.new do
18
+ def initialize(*args, &block); end
19
+ end
20
+ Spec::DSL::BehaviourFactory.add_behaviour_class(:something_other_than_default, behaviour_class)
21
+ Spec::DSL::BehaviourFactory.create("behaviour", :behaviour_type => :something_other_than_default) {
22
+ }.should be_an_instance_of(behaviour_class)
23
+ end
24
+
25
+ after(:each) do
26
+ Spec::DSL::BehaviourFactory.remove_behaviour_class(:something_other_than_default)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,508 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ module Spec
4
+ module DSL
5
+ class FakeReporter < Spec::Runner::Reporter
6
+ attr_reader :added_behaviour
7
+ def add_behaviour(description)
8
+ @added_behaviour = description
9
+ end
10
+ end
11
+
12
+ describe Behaviour, "class methods" do
13
+ before :each do
14
+ @original_before_all_parts = Behaviour.before_all_parts.dup
15
+ @original_after_all_parts = Behaviour.after_all_parts.dup
16
+ @original_before_each_parts = Behaviour.before_each_parts.dup
17
+ @original_after_each_parts = Behaviour.after_each_parts.dup
18
+
19
+ @reporter = FakeReporter.new(mock("formatter", :null_object => true), mock("backtrace_tweaker", :null_object => true))
20
+ @behaviour = Behaviour.new("example") {}
21
+ end
22
+
23
+ after :each do
24
+ Behaviour.instance_variable_set(:@before_all_parts, @original_before_all_parts)
25
+ Behaviour.instance_variable_set(:@after_all_parts, @original_after_all_parts)
26
+ Behaviour.instance_variable_set(:@before_each_parts, @original_before_each_parts)
27
+ Behaviour.instance_variable_set(:@after_each_parts, @original_after_each_parts)
28
+ end
29
+
30
+ it "should not run before(:all) or after(:all) on dry run" do
31
+ before_all_ran = false
32
+ after_all_ran = false
33
+ Behaviour.before(:all) { before_all_ran = true }
34
+ Behaviour.after(:all) { after_all_ran = true }
35
+ @behaviour.it("should") {}
36
+ @behaviour.run(@reporter, true)
37
+ before_all_ran.should be_false
38
+ after_all_ran.should be_false
39
+ end
40
+
41
+ it "should not run any example if before(:all) fails" do
42
+ spec_ran = false
43
+ Behaviour.before(:all) { raise "help" }
44
+ @behaviour.it("test") {spec_ran = true}
45
+ @behaviour.run(@reporter)
46
+ spec_ran.should be_false
47
+ end
48
+
49
+ it "should run after(:all) if before(:all) fails" do
50
+ after_all_ran = false
51
+ Behaviour.before(:all) { raise }
52
+ Behaviour.after(:all) { after_all_ran = true }
53
+ @behaviour.run(@reporter)
54
+ after_all_ran.should be_true
55
+ end
56
+
57
+ it "should run after(:all) if before(:each) fails" do
58
+ after_all_ran = false
59
+ Behaviour.before(:each) { raise }
60
+ Behaviour.after(:all) { after_all_ran = true }
61
+ @behaviour.run(@reporter)
62
+ after_all_ran.should be_true
63
+ end
64
+
65
+ it "should run after(:all) if any example fails" do
66
+ after_all_ran = false
67
+ @behaviour.it("should") { raise "before all error" }
68
+ Behaviour.after(:all) { after_all_ran = true }
69
+ @behaviour.run(@reporter)
70
+ after_all_ran.should be_true
71
+ end
72
+
73
+ it "should supply before(:all) as description if failure in before(:all)" do
74
+ @reporter.should_receive(:example_finished) do |name, error, location|
75
+ name.should eql("before(:all)")
76
+ error.message.should eql("in before(:all)")
77
+ location.should eql("before(:all)")
78
+ end
79
+
80
+ Behaviour.before(:all) { raise "in before(:all)" }
81
+ @behaviour.it("test") {true}
82
+ @behaviour.run(@reporter)
83
+ end
84
+
85
+ it "should provide after(:all) as description if failure in after(:all)" do
86
+ @reporter.should_receive(:example_finished) do |name, error, location|
87
+ name.should eql("after(:all)")
88
+ error.message.should eql("in after(:all)")
89
+ location.should eql("after(:all)")
90
+ end
91
+
92
+ Behaviour.after(:all) { raise "in after(:all)" }
93
+ @behaviour.run(@reporter)
94
+ end
95
+ end
96
+
97
+ describe Behaviour do
98
+ before :each do
99
+ @original_before_all_parts = Behaviour.before_all_parts.dup
100
+ @original_after_all_parts = Behaviour.after_all_parts.dup
101
+ @original_before_each_parts = Behaviour.before_each_parts.dup
102
+ @original_after_each_parts = Behaviour.after_each_parts.dup
103
+
104
+ @reporter = FakeReporter.new(mock("formatter", :null_object => true), mock("backtrace_tweaker", :null_object => true))
105
+ @behaviour = Behaviour.new("example") {}
106
+ end
107
+
108
+ after :each do
109
+ Behaviour.instance_variable_set(:@before_all_parts, @original_before_all_parts)
110
+ Behaviour.instance_variable_set(:@after_all_parts, @original_after_all_parts)
111
+ Behaviour.instance_variable_set(:@before_each_parts, @original_before_each_parts)
112
+ Behaviour.instance_variable_set(:@after_each_parts, @original_after_each_parts)
113
+ end
114
+
115
+ it "should send reporter add_behaviour" do
116
+ @behaviour.run(@reporter)
117
+ @reporter.added_behaviour.should == "example"
118
+ end
119
+
120
+ it "should run example on run" do
121
+ example_ran = false
122
+ @behaviour.it("should") {example_ran = true}
123
+ @behaviour.run(@reporter)
124
+ example_ran.should be_true
125
+ end
126
+
127
+ it "should not run example on dry run" do
128
+ example_ran = false
129
+ @behaviour.it("should") {example_ran = true}
130
+ @behaviour.run(@reporter, true)
131
+ example_ran.should be_false
132
+ end
133
+
134
+ it "should not run before(:all) or after(:all) on dry run" do
135
+ before_all_ran = false
136
+ after_all_ran = false
137
+ @behaviour.before(:all) { before_all_ran = true }
138
+ @behaviour.after(:all) { after_all_ran = true }
139
+ @behaviour.it("should") {}
140
+ @behaviour.run(@reporter, true)
141
+ before_all_ran.should be_false
142
+ after_all_ran.should be_false
143
+ end
144
+
145
+ it "should not run any example if before(:all) fails" do
146
+ spec_ran = false
147
+ @behaviour.before(:all) { raise "help" }
148
+ @behaviour.it("test") {spec_ran = true}
149
+ @behaviour.run(@reporter)
150
+ spec_ran.should be_false
151
+ end
152
+
153
+ it "should run after(:all) if before(:all) fails" do
154
+ after_all_ran = false
155
+ @behaviour.before(:all) { raise }
156
+ @behaviour.after(:all) { after_all_ran = true }
157
+ @behaviour.run(@reporter)
158
+ after_all_ran.should be_true
159
+ end
160
+
161
+ it "should run after(:all) if before(:each) fails" do
162
+ after_all_ran = false
163
+ @behaviour.before(:each) { raise }
164
+ @behaviour.after(:all) { after_all_ran = true }
165
+ @behaviour.run(@reporter)
166
+ after_all_ran.should be_true
167
+ end
168
+
169
+ it "should run after(:all) if any example fails" do
170
+ after_all_ran = false
171
+ @behaviour.it("should") { raise "before all error" }
172
+ @behaviour.after(:all) { after_all_ran = true }
173
+ @behaviour.run(@reporter)
174
+ after_all_ran.should be_true
175
+ end
176
+
177
+ it "should supply before(:all) as description if failure in before(:all)" do
178
+ @reporter.should_receive(:example_finished) do |name, error, location|
179
+ name.should eql("before(:all)")
180
+ error.message.should eql("in before(:all)")
181
+ location.should eql("before(:all)")
182
+ end
183
+
184
+ @behaviour.before(:all) { raise "in before(:all)" }
185
+ @behaviour.it("test") {true}
186
+ @behaviour.run(@reporter)
187
+ end
188
+
189
+ it "should provide after(:all) as description if failure in after(:all)" do
190
+ @reporter.should_receive(:example_finished) do |name, error, location|
191
+ name.should eql("after(:all)")
192
+ error.message.should eql("in after(:all)")
193
+ location.should eql("after(:all)")
194
+ end
195
+
196
+ @behaviour.after(:all) { raise "in after(:all)" }
197
+ @behaviour.run(@reporter)
198
+ end
199
+
200
+ it "should run before(:all) block only once" do
201
+ before_all_run_count_run_count = 0
202
+ @behaviour.before(:all) {before_all_run_count_run_count += 1}
203
+ @behaviour.it("test") {true}
204
+ @behaviour.it("test2") {true}
205
+ @behaviour.run(@reporter)
206
+ before_all_run_count_run_count.should == 1
207
+ end
208
+
209
+ it "should run superclass setup method and before block" do
210
+ super_class_before_ran = false
211
+ super_class = Class.new do
212
+ define_method :setup do
213
+ super_class_before_ran = true
214
+ end
215
+ end
216
+ @behaviour.inherit super_class
217
+
218
+ before_ran = false
219
+ @behaviour.before {before_ran = true}
220
+ @behaviour.it("test") {true}
221
+ @behaviour.run(@reporter)
222
+ super_class_before_ran.should be_true
223
+ before_ran.should be_true
224
+ end
225
+
226
+ it "should run after(:all) block only once" do
227
+ after_all_run_count = 0
228
+ @behaviour.after(:all) {after_all_run_count += 1}
229
+ @behaviour.it("test") {true}
230
+ @behaviour.it("test2") {true}
231
+ @behaviour.run(@reporter)
232
+ after_all_run_count.should == 1
233
+ @reporter.rspec_verify
234
+ end
235
+
236
+ it "after(:all) should have access to all instance variables defined in before(:all)" do
237
+ context_instance_value_in = "Hello there"
238
+ context_instance_value_out = ""
239
+ @behaviour.before(:all) { @instance_var = context_instance_value_in }
240
+ @behaviour.after(:all) { context_instance_value_out = @instance_var }
241
+ @behaviour.it("test") {true}
242
+ @behaviour.run(@reporter)
243
+ context_instance_value_in.should == context_instance_value_out
244
+ end
245
+
246
+ it "should copy instance variables from before(:all)'s execution context into spec's execution context" do
247
+ context_instance_value_in = "Hello there"
248
+ context_instance_value_out = ""
249
+ @behaviour.before(:all) { @instance_var = context_instance_value_in }
250
+ @behaviour.it("test") {context_instance_value_out = @instance_var}
251
+ @behaviour.run(@reporter)
252
+ context_instance_value_in.should == context_instance_value_out
253
+ end
254
+
255
+ it "before callbacks are ordered from global to local" do
256
+ fiddle = []
257
+ super_class = Class.new do
258
+ define_method :setup do
259
+ fiddle << "superclass setup"
260
+ end
261
+ end
262
+ @behaviour.inherit super_class
263
+
264
+ Behaviour.before(:all) { fiddle << "Behaviour.before(:all)" }
265
+ @behaviour.before(:all) { fiddle << "before(:all)" }
266
+ @behaviour.before(:each) { fiddle << "before(:each)" }
267
+ @behaviour.it("test") {true}
268
+ @behaviour.run(@reporter)
269
+ fiddle.should == ['Behaviour.before(:all)', 'before(:all)', 'superclass setup', 'before(:each)']
270
+ end
271
+
272
+ it "after callbacks are ordered from local to global" do
273
+ @reporter.should_receive(:add_behaviour).with :any_args
274
+ @reporter.should_receive(:example_finished).with :any_args
275
+
276
+ fiddle = []
277
+ super_class = Class.new do
278
+ define_method :teardown do
279
+ fiddle << "superclass teardown"
280
+ end
281
+ end
282
+ @behaviour.inherit super_class
283
+
284
+ @behaviour.after(:all) { fiddle << "after(:all)" }
285
+ Behaviour.after(:all) { fiddle << "Behaviour.after(:all)" }
286
+ @behaviour.after(:each) { fiddle << "after(:each)" }
287
+ @behaviour.it("test") {true}
288
+ @behaviour.run(@reporter)
289
+ fiddle.should == ['after(:each)', 'superclass teardown', 'after(:all)', 'Behaviour.after(:all)']
290
+ end
291
+
292
+ it "should run superclass teardown method and after block" do
293
+ super_class_teardown_ran = false
294
+ super_class = Class.new do
295
+ define_method :teardown do
296
+ super_class_teardown_ran = true
297
+ end
298
+ end
299
+ @behaviour.inherit super_class
300
+
301
+ teardown_ran = false
302
+ @behaviour.after {teardown_ran = true}
303
+ @behaviour.it("test") {true}
304
+ @behaviour.run(@reporter)
305
+ super_class_teardown_ran.should be_true
306
+ teardown_ran.should be_true
307
+ @reporter.rspec_verify
308
+ end
309
+
310
+ it "should have accessible methods from inherited superclass" do
311
+ helper_method_ran = false
312
+ super_class = Class.new do
313
+ define_method :helper_method do
314
+ helper_method_ran = true
315
+ end
316
+ end
317
+ @behaviour.inherit super_class
318
+
319
+ @behaviour.it("test") {helper_method}
320
+ @behaviour.run(@reporter)
321
+ helper_method_ran.should be_true
322
+ end
323
+
324
+ it "should have accessible class methods from inherited superclass" do
325
+ class_method_ran = false
326
+ super_class = Class.new
327
+ (class << super_class; self; end).class_eval do
328
+ define_method :class_method do
329
+ class_method_ran = true
330
+ end
331
+ end
332
+ @behaviour.inherit super_class
333
+ @behaviour.class_method
334
+ class_method_ran.should be_true
335
+
336
+ lambda {@behaviour.foobar}.should raise_error(NoMethodError)
337
+ end
338
+
339
+ it "should include inherited class methods" do
340
+ class_method_ran = false
341
+ super_class = Class.new
342
+ class << super_class
343
+ def super_class_class_method; end
344
+ end
345
+ @behaviour.inherit super_class
346
+
347
+ @behaviour.methods.should include("super_class_class_method")
348
+ end
349
+
350
+ it "should have accessible instance methods from included module" do
351
+ @reporter.should_receive(:add_behaviour).with :any_args
352
+ @reporter.should_receive(:example_finished).with :any_args
353
+
354
+ mod1_method_called = false
355
+ mod1 = Module.new do
356
+ define_method :mod1_method do
357
+ mod1_method_called = true
358
+ end
359
+ end
360
+
361
+ mod2_method_called = false
362
+ mod2 = Module.new do
363
+ define_method :mod2_method do
364
+ mod2_method_called = true
365
+ end
366
+ end
367
+
368
+ @behaviour.include mod1
369
+ @behaviour.include mod2
370
+
371
+ @behaviour.it("test") do
372
+ mod1_method
373
+ mod2_method
374
+ end
375
+ @behaviour.run(@reporter)
376
+ mod1_method_called.should be_true
377
+ mod2_method_called.should be_true
378
+ end
379
+
380
+ it "should have accessible class methods from included module" do
381
+ mod1_method_called = false
382
+ mod1 = Module.new do
383
+ class_methods = Module.new do
384
+ define_method :mod1_method do
385
+ mod1_method_called = true
386
+ end
387
+ end
388
+
389
+ metaclass.class_eval do
390
+ define_method(:included) do |receiver|
391
+ receiver.extend class_methods
392
+ end
393
+ end
394
+ end
395
+
396
+ mod2_method_called = false
397
+ mod2 = Module.new do
398
+ class_methods = Module.new do
399
+ define_method :mod2_method do
400
+ mod2_method_called = true
401
+ end
402
+ end
403
+
404
+ metaclass.class_eval do
405
+ define_method(:included) do |receiver|
406
+ receiver.extend class_methods
407
+ end
408
+ end
409
+ end
410
+
411
+ @behaviour.include mod1
412
+ @behaviour.include mod2
413
+
414
+ @behaviour.mod1_method
415
+ @behaviour.mod2_method
416
+ mod1_method_called.should be_true
417
+ mod2_method_called.should be_true
418
+ end
419
+
420
+ it "should count number of specs" do
421
+ @behaviour.it("one") {}
422
+ @behaviour.it("two") {}
423
+ @behaviour.it("three") {}
424
+ @behaviour.it("four") {}
425
+ @behaviour.number_of_examples.should == 4
426
+ end
427
+
428
+ it "should not match anything when there are no examples" do
429
+ @behaviour.should_not be_matches(['context'])
430
+ end
431
+
432
+ it "should match when one of the examples match" do
433
+ example = mock('my example')
434
+ example.should_receive(:matches?).and_return(true)
435
+ @behaviour.stub!(:examples).and_return([example])
436
+ @behaviour.should be_matches(['jalla'])
437
+ end
438
+
439
+ it "should include any modules included using configuration" do
440
+ mod = Module.new do
441
+ class << self
442
+ def included(mod)
443
+ $included_module = mod
444
+ end
445
+ end
446
+ end
447
+
448
+ begin
449
+ $included_module = nil
450
+ Spec::Runner.configuration.include(mod)
451
+
452
+ behaviour = Behaviour.new('example') do
453
+ end.run(@reporter)
454
+
455
+ $included_module.should_not be_nil
456
+ ensure
457
+ Spec::Runner.configuration.included_modules.delete(mod)
458
+ end
459
+ end
460
+
461
+ it "should include any predicate_matchers included using configuration" do
462
+ $included_predicate_matcher_found = false
463
+ Spec::Runner.configuration.predicate_matchers[:does_something?] = :do_something
464
+ Behaviour.new('example') do
465
+ it "should respond to do_something" do
466
+ $included_predicate_matcher_found = respond_to?(:do_something)
467
+ end
468
+ end.run(@reporter)
469
+ $included_predicate_matcher_found.should be(true)
470
+ end
471
+
472
+ it "should use a mock framework set up in config" do
473
+ mod = Module.new do
474
+ class << self
475
+ def included(mod)
476
+ $included_module = mod
477
+ end
478
+ end
479
+ end
480
+
481
+ begin
482
+ $included_module = nil
483
+ Spec::Runner.configuration.mock_with mod
484
+
485
+ behaviour = Behaviour.new('example') do
486
+ end.run(@reporter)
487
+
488
+ $included_module.should_not be_nil
489
+ ensure
490
+ Spec::Runner.configuration.mock_with :rspec
491
+ end
492
+ end
493
+
494
+ end
495
+
496
+ class BehaviourSubclass < Behaviour
497
+ public :described_type
498
+ end
499
+
500
+ describe Behaviour, " subclass" do
501
+ it "should have access to the described_type" do
502
+ BehaviourSubclass.new(Example){}.described_type.should == Example
503
+ end
504
+
505
+ # TODO - add an example about shared behaviours
506
+ end
507
+ end
508
+ end