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
@@ -1,9 +1,8 @@
1
1
  module Spec
2
2
  module Mocks
3
- class MockHandler
3
+ class Proxy
4
4
  DEFAULT_OPTIONS = {
5
5
  :null_object => false,
6
- :auto_verify => true
7
6
  }
8
7
 
9
8
  def initialize(target, name, options={})
@@ -84,12 +83,10 @@ module Spec
84
83
  @error_generator.raise_unexpected_message_error sym, *args
85
84
  end
86
85
 
87
- private
86
+ private
88
87
 
89
88
  def __add(expected_from, sym, block)
90
- # TODO - this is the only reference in the 'spec/mocks' to the Runner
91
- current_spec = Runner::Specification.current
92
- current_spec.after_teardown {verify} if current_spec && @options[:auto_verify]
89
+ $rspec_mocks.add(@target)
93
90
  define_expected_method(sym)
94
91
  end
95
92
 
@@ -101,7 +98,7 @@ module Spec
101
98
 
102
99
  metaclass_eval(<<-EOF, __FILE__, __LINE__)
103
100
  def #{sym}(*args, &block)
104
- __mock_handler.message_received :#{sym}, *args, &block
101
+ __mock_proxy.message_received :#{sym}, *args, &block
105
102
  end
106
103
  EOF
107
104
  end
@@ -0,0 +1,28 @@
1
+ module Spec
2
+ module Mocks
3
+ class Space
4
+ def add(obj)
5
+ mocks << obj unless mocks.include?(obj)
6
+ end
7
+
8
+ def verify_all
9
+ mocks.each do |mock|
10
+ mock.rspec_verify
11
+ end
12
+ end
13
+
14
+ def reset_all
15
+ mocks.each do |mock|
16
+ mock.rspec_reset
17
+ end
18
+ mocks.clear
19
+ end
20
+
21
+ private
22
+
23
+ def mocks
24
+ @mocks ||= []
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ module Spec
2
+ module Mocks
3
+ module SpecMethods
4
+ include Spec::Mocks::ArgumentConstraintMatchers
5
+
6
+ # Shortcut for creating an instance of Spec::Mocks::Mock.
7
+ def mock(name, options={})
8
+ Spec::Mocks::Mock.new(name, options)
9
+ end
10
+
11
+ # Shortcut for creating an instance of Spec::Mocks::Mock with
12
+ # predefined method stubs.
13
+ #
14
+ # == Examples
15
+ #
16
+ # stub_thing = stub("thing", :a => "A")
17
+ # stub_thing.a == "A" => true
18
+ #
19
+ # stub_person = stub("thing", :name => "Joe", :email => "joe@domain.com")
20
+ # stub_person.name => "Joe"
21
+ # stub_person.email => "joe@domain.com"
22
+ def stub(name, stubs={})
23
+ object_stub = mock(name)
24
+ stubs.each { |key, value| object_stub.stub!(key).and_return(value) }
25
+ object_stub
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -41,6 +41,7 @@ module Spec
41
41
  attr_accessor :spec_opts
42
42
 
43
43
  # Where RSpec's output is written. Defaults to STDOUT.
44
+ # DEPRECATED. Use --format FORMAT:WHERE in spec_opts.
44
45
  attr_accessor :out
45
46
 
46
47
  # Whether or not to use RCov (default is false)
@@ -103,31 +104,32 @@ module Spec
103
104
  end
104
105
  task @name do
105
106
  RakeFileUtils.verbose(@verbose) do
106
- ruby_opts = @ruby_opts.clone
107
- ruby_opts.push( "-I\"#{lib_path}\"" )
108
- ruby_opts.push( "-S rcov" ) if @rcov
109
- ruby_opts.push( "-w" ) if @warning
110
-
111
- redirect = @out.nil? ? "" : " > \"#{@out}\""
112
-
113
107
  unless spec_file_list.empty?
114
108
  # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- [spec_opts] examples
115
109
  # or
116
110
  # ruby [ruby_opts] -Ilib bin/spec [spec_opts] examples
117
- begin
118
- ruby(
119
- ruby_opts.join(" ") + " " +
120
- rcov_option_list +
121
- (@rcov ? %[ -o "#{@rcov_dir}" ] : "") +
122
- '"' + spec_script + '"' + " " +
123
- (@rcov ? "-- " : "") +
124
- spec_file_list.collect { |fn| %["#{fn}"] }.join(' ') + " " +
125
- spec_option_list + " " +
126
- redirect
127
- )
128
- rescue => e
129
- puts @failure_message if @failure_message
130
- raise e if @fail_on_error
111
+ cmd = "ruby "
112
+
113
+ ruby_opts = @ruby_opts.clone
114
+ ruby_opts << "-I\"#{lib_path}\""
115
+ ruby_opts << "-S rcov" if @rcov
116
+ ruby_opts << "-w" if @warning
117
+ cmd << ruby_opts.join(" ")
118
+ cmd << " "
119
+ cmd << rcov_option_list
120
+ cmd << %[ -o "#{@rcov_dir}" ] if @rcov
121
+ cmd << %Q|"#{spec_script}"|
122
+ cmd << " "
123
+ cmd << "-- " if @rcov
124
+ cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ')
125
+ cmd << " "
126
+ cmd << spec_option_list
127
+ cmd << " "
128
+ cmd << %Q| > "#{@out}"| if @out
129
+
130
+ unless system(cmd)
131
+ puts @failure_message if @failure_message
132
+ raise("Command #{cmd} failed") if @fail_on_error
131
133
  end
132
134
  end
133
135
  end
@@ -32,6 +32,7 @@ module RCov
32
32
  desc "Verify that rcov coverage is at least #{threshold}%"
33
33
  task @name do
34
34
  total_coverage = nil
35
+
35
36
  File.open(index_html).each_line do |line|
36
37
  if line =~ /<tt.*>(\d+\.\d+)%<\/tt>&nbsp;<\/td>/
37
38
  total_coverage = eval($1)
@@ -1,42 +1,38 @@
1
1
  require 'spec/runner/formatter'
2
- require 'spec/runner/context'
3
- require 'spec/runner/context_eval'
4
- require 'spec/runner/specification'
5
- require 'spec/runner/execution_context'
6
- require 'spec/runner/context_runner'
2
+ require 'spec/runner/behaviour_runner'
3
+ require 'spec/runner/options'
7
4
  require 'spec/runner/option_parser'
8
5
  require 'spec/runner/command_line'
9
6
  require 'spec/runner/drb_command_line'
10
7
  require 'spec/runner/backtrace_tweaker'
11
8
  require 'spec/runner/reporter'
12
- require 'spec/runner/spec_matcher'
13
9
  require 'spec/runner/extensions/object'
14
10
  require 'spec/runner/extensions/kernel'
15
- require 'spec/runner/spec_should_raise_handler'
16
11
  require 'spec/runner/spec_parser'
17
12
 
18
13
  module Spec
19
- # == Contexts and Specifications
14
+ # == Behaviours and Examples
20
15
  #
21
- # Rather than expressing examples in classes, RSpec uses a custom domain specific language to express
22
- # examples using contexts and specifications.
16
+ # Rather than expressing examples in classes, RSpec uses a custom domain specific language to
17
+ # describe Behaviours and Examples of those behaviours.
23
18
  #
24
- # A context is the equivalent of a fixture in xUnit-speak. It is a metaphor for the context
19
+ # A Behaviour is the equivalent of a fixture in xUnit-speak. It is a metaphor for the context
25
20
  # in which you will run your executable example - a set of known objects in a known starting state.
21
+ # We begin be describing
26
22
  #
27
- # context "A new account" do
23
+ # describe Account do
28
24
  #
29
- # setup do
25
+ # before do
30
26
  # @account = Account.new
31
27
  # end
32
28
  #
33
- # specify "should have a balance of $0" do
34
- # @account.balance.should_eql Money.new(0, :dollars)
29
+ # it "should have a balance of $0" do
30
+ # @account.balance.should == Money.new(0, :dollars)
35
31
  # end
36
32
  #
37
33
  # end
38
34
  #
39
- # We use the setup block to set up the context (given), and then the specify method to
35
+ # We use the before block to set up the Behaviour (given), and then the #it method to
40
36
  # hold the example code that expresses the event (when) and the expected outcome (then).
41
37
  #
42
38
  # == Helper Methods
@@ -51,51 +47,52 @@ module Spec
51
47
  #
52
48
  # == Setup and Teardown
53
49
  #
54
- # You can use setup, teardown, context_setup and context_teardown within a context:
50
+ # You can use before and after within a Behaviour. Both methods take an optional
51
+ # scope argument so you can run the block before :each example or before :all examples
55
52
  #
56
- # context "..." do
57
- # context_setup do
53
+ # describe "..." do
54
+ # before :all do
58
55
  # ...
59
56
  # end
60
57
  #
61
- # setup do
58
+ # before :each do
62
59
  # ...
63
60
  # end
64
61
  #
65
- # specify "number one" do
62
+ # it "should do something" do
66
63
  # ...
67
64
  # end
68
65
  #
69
- # specify "number two" do
66
+ # it "should do something else" do
70
67
  # ...
71
68
  # end
72
69
  #
73
- # teardown do
70
+ # after :each do
74
71
  # ...
75
72
  # end
76
73
  #
77
- # context_teardown do
74
+ # after :all do
78
75
  # ...
79
76
  # end
80
77
  #
81
78
  # end
82
79
  #
83
- # The <tt>setup</tt> block will run before each of the specs, once for each spec. Likewise,
84
- # the <tt>teardown</tt> block will run after each of the specs.
80
+ # The <tt>before :each</tt> block will run before each of the examples, once for each example. Likewise,
81
+ # the <tt>after :each</tt> block will run after each of the examples.
85
82
  #
86
- # It is also possible to specify a <tt>context_setup</tt> and <tt>context_teardown</tt>
87
- # block that will run only once for each context, respectively before the first <code>setup</code>
88
- # and after the last <code>teardown</code>. The use of these is generally discouraged, because it
89
- # introduces dependencies between the specs. Still, it might prove useful for very expensive operations
83
+ # It is also possible to specify a <tt>before :all</tt> and <tt>after :all</tt>
84
+ # block that will run only once for each behaviour, respectively before the first <code>before :each</code>
85
+ # and after the last <code>after :each</code>. The use of these is generally discouraged, because it
86
+ # introduces dependencies between the examples. Still, it might prove useful for very expensive operations
90
87
  # if you know what you are doing.
91
88
  #
92
89
  # == Local helper methods
93
90
  #
94
91
  # You can include local helper methods by simply expressing them within a context:
95
92
  #
96
- # context "..." do
93
+ # describe "..." do
97
94
  #
98
- # specify "..." do
95
+ # it "..." do
99
96
  # helper_method
100
97
  # end
101
98
  #
@@ -116,17 +113,73 @@ module Spec
116
113
  # end
117
114
  # end
118
115
  #
119
- # context "A new account" do
116
+ # describe "A new account" do
120
117
  # include AccountExampleHelperMethods
121
- # setup do
118
+ # before do
122
119
  # @account = Account.new
123
120
  # end
124
121
  #
125
- # specify "should have a balance of $0" do
122
+ # it "should have a balance of $0" do
126
123
  # helper_method
127
124
  # @account.balance.should eql(Money.new(0, :dollars))
128
125
  # end
129
126
  # end
127
+ #
128
+ # == Shared behaviour
129
+ #
130
+ # You can define a shared behaviour, that may be used on other behaviours
131
+ #
132
+ # describe "All Editions", :shared => true do
133
+ # it "all editions behaviour" ...
134
+ # end
135
+ #
136
+ # describe SmallEdition do
137
+ # it_should_behave_like "All Editions"
138
+ #
139
+ # it "should do small edition stuff" do
140
+ # ...
141
+ # end
142
+ # end
130
143
  module Runner
144
+ class << self
145
+ def configuration # :nodoc:
146
+ @configuration ||= Spec::DSL::Configuration.new
147
+ end
148
+
149
+ # Use this to configure various configurable aspects of
150
+ # RSpec. For example, to choose a mock framework from
151
+ # RSpec, mocha and flexmock, you can do this:
152
+ #
153
+ # Spec::Runner.configure do |config|
154
+ # config.mock_with :rspec #or :mocha, or :flexmock
155
+ # end
156
+ #
157
+ # To use any other mock framework, you'll have to provide
158
+ # your own adapter. This is simply a module that responds to
159
+ # setup_mocks_for_rspec, verify_mocks_for_rspec and teardown_mocks_for_rspec.
160
+ # These are your hooks into the lifecycle of a given example. RSpec will
161
+ # call setup_mocks_for_rspec before running anything else in each Example.
162
+ # After executing the #after methods, RSpec will then call verify_mocks_for_rspec
163
+ # and teardown_mocks_for_rspec (this is guaranteed to run even if there are
164
+ # failures in verify_mocks_for_rspec).
165
+ #
166
+ # Once you've defined this module, you can pass that to mock_with:
167
+ #
168
+ # Spec::Runner.configure do |config|
169
+ # config.mock_with MyMockFrameworkAdapter
170
+ # end
171
+ #
172
+ # You can also configure the following items:
173
+ #
174
+ # # include SomeModule in every Behaviour
175
+ # config.include SomeModule
176
+ #
177
+ # # generate a do_something predicate_matcher for every Behaviour
178
+ # # - See Spec::DSL::Behaviour#predicate_matchers
179
+ # config.predicate_matchers[:does_something?] = :do_something
180
+ def configure
181
+ yield configuration
182
+ end
183
+ end
131
184
  end
132
185
  end
@@ -32,7 +32,8 @@ module Spec
32
32
  # TextMate's Ruby and RSpec plugins
33
33
  /Ruby\.tmbundle\/Support\/tmruby.rb:/,
34
34
  /RSpec\.tmbundle\/Support\/lib/,
35
- /temp_textmate\./
35
+ /temp_textmate\./,
36
+ /mock_frameworks\/rspec/
36
37
  ]
37
38
  end
38
39
 
@@ -0,0 +1,102 @@
1
+ module Spec
2
+ module Runner
3
+ class BehaviourRunner
4
+
5
+ def initialize(options)
6
+ @behaviours = []
7
+ @options = options
8
+ end
9
+
10
+ def add_behaviour(behaviour)
11
+ unless specified_examples.nil? || specified_examples.empty? #|| behaviour.matches?(specified_examples)
12
+ behaviour.retain_examples_matching!(specified_examples) #if behaviour.matches?(specified_examples)
13
+ end
14
+ @behaviours << behaviour unless behaviour.number_of_examples == 0
15
+ end
16
+
17
+ # Runs all contexts and returns the number of failures.
18
+ def run(paths, exit_when_done)
19
+ unless paths.nil? # It's nil when running single specs with ruby
20
+ paths = find_paths(paths)
21
+ sorted_paths = sort_paths(paths)
22
+ load_specs(sorted_paths)
23
+ end
24
+ @options.reporter.start(number_of_examples)
25
+ behaviours = @options.reverse ? @behaviours.reverse : @behaviours
26
+ begin
27
+ run_behaviours(behaviours)
28
+ rescue Interrupt
29
+ ensure
30
+ @options.reporter.end
31
+ end
32
+ failure_count = @options.reporter.dump
33
+
34
+ heckle if(failure_count == 0 && !@options.heckle_runner.nil?)
35
+
36
+ if(exit_when_done)
37
+ exit_code = (failure_count == 0) ? 0 : 1
38
+ exit(exit_code)
39
+ end
40
+ failure_count
41
+ end
42
+
43
+ def run_behaviours(behaviours)
44
+ behaviours.each do |behaviour|
45
+ behaviour.run(@options.reporter, @options.dry_run, @options.reverse)
46
+ end
47
+ end
48
+
49
+ def number_of_examples
50
+ @behaviours.inject(0) {|sum, behaviour| sum + behaviour.number_of_examples}
51
+ end
52
+
53
+ FILE_SORTERS = {
54
+ 'mtime' => lambda {|file_a, file_b| File.mtime(file_b) <=> File.mtime(file_a)}
55
+ }
56
+
57
+ def sorter(paths)
58
+ FILE_SORTERS[@options.loadby]
59
+ end
60
+
61
+ def sort_paths(paths)
62
+ sorter = sorter(paths)
63
+ paths = paths.sort(&sorter) unless sorter.nil?
64
+ paths
65
+ end
66
+
67
+ private
68
+
69
+ def find_paths(paths)
70
+ result = []
71
+ paths.each do |path|
72
+ if File.directory?(path)
73
+ result += Dir["#{path}/**/*.rb"]
74
+ elsif File.file?(path)
75
+ result << path
76
+ else
77
+ raise "File or directory not found: #{path}"
78
+ end
79
+ end
80
+ result
81
+ end
82
+
83
+ def load_specs(paths)
84
+ paths.each do |path|
85
+ load path
86
+ end
87
+ end
88
+
89
+ def specified_examples
90
+ @options.examples
91
+ end
92
+
93
+ def heckle
94
+ heckle_runner = @options.heckle_runner
95
+ @options.heckle_runner = nil
96
+ behaviour_runner = self.class.new(@options)
97
+ behaviour_runner.instance_variable_set(:@behaviours, @behaviours)
98
+ heckle_runner.heckle_with(behaviour_runner)
99
+ end
100
+ end
101
+ end
102
+ end