spec 5.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +34 -0
  3. data/.gitignore +3 -0
  4. data/History.txt +911 -0
  5. data/Manifest.txt +26 -0
  6. data/README.txt +497 -0
  7. data/Rakefile +214 -0
  8. data/design_rationale.rb +52 -0
  9. data/lib/hoe/minitest.rb +26 -0
  10. data/lib/minitest/assertions.rb +649 -0
  11. data/lib/minitest/autorun.rb +12 -0
  12. data/lib/minitest/benchmark.rb +423 -0
  13. data/lib/minitest/expectations.rb +268 -0
  14. data/lib/minitest/hell.rb +11 -0
  15. data/lib/minitest/mock.rb +220 -0
  16. data/lib/minitest/parallel_each.rb +120 -0
  17. data/lib/minitest/pride.rb +4 -0
  18. data/lib/minitest/pride_plugin.rb +143 -0
  19. data/lib/minitest/spec.rb +292 -0
  20. data/lib/minitest/test.rb +272 -0
  21. data/lib/minitest/unit.rb +45 -0
  22. data/lib/minitest.rb +839 -0
  23. data/lib/spec.rb +3 -0
  24. data/readme.md +7 -0
  25. data/release_notes.md +49 -0
  26. data/spec.gemspec +36 -0
  27. data/test/manual/appium.rb +14 -0
  28. data/test/manual/appium_after_last.rb +24 -0
  29. data/test/manual/appium_before_first.rb +23 -0
  30. data/test/manual/assert.rb +61 -0
  31. data/test/manual/before_first_0.rb +27 -0
  32. data/test/manual/before_first_1.rb +29 -0
  33. data/test/manual/debug.rb +37 -0
  34. data/test/manual/do_end.rb +31 -0
  35. data/test/manual/raise.rb +61 -0
  36. data/test/manual/run2.rb +74 -0
  37. data/test/manual/run3.rb +91 -0
  38. data/test/manual/setup.rb +13 -0
  39. data/test/manual/simple.rb +19 -0
  40. data/test/manual/simple2.rb +20 -0
  41. data/test/manual/t.rb +11 -0
  42. data/test/manual/trace.rb +19 -0
  43. data/test/manual/trace2.rb +15 -0
  44. data/test/minitest/metametameta.rb +78 -0
  45. data/test/minitest/test_helper.rb +20 -0
  46. data/test/minitest/test_minitest_benchmark.rb +131 -0
  47. data/test/minitest/test_minitest_mock.rb +490 -0
  48. data/test/minitest/test_minitest_reporter.rb +270 -0
  49. data/test/minitest/test_minitest_spec.rb +794 -0
  50. data/test/minitest/test_minitest_unit.rb +1846 -0
  51. metadata +147 -0
@@ -0,0 +1,292 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ require 'minitest/unit'
4
+
5
+ class Module # :nodoc:
6
+ def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
7
+ # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
8
+ self.class_eval <<-EOM
9
+ def #{new_name} *args
10
+ case
11
+ when Proc === self then
12
+ Minitest::Spec.current.#{meth}(*args, &self)
13
+ when #{!!dont_flip} then
14
+ Minitest::Spec.current.#{meth}(self, *args)
15
+ else
16
+ Minitest::Spec.current.#{meth}(args.first, self, *args[1..-1])
17
+ end
18
+ end
19
+ EOM
20
+ end
21
+ end
22
+
23
+ module Kernel # :nodoc:
24
+ ##
25
+ # Describe a series of expectations for a given target +desc+.
26
+ #
27
+ # Defines a test class subclassing from either Minitest::Spec or
28
+ # from the surrounding describe's class. The surrounding class may
29
+ # subclass Minitest::Spec manually in order to easily share code:
30
+ #
31
+ # class MySpec < Minitest::Spec
32
+ # # ... shared code ...
33
+ # end
34
+ #
35
+ # class TestStuff < MySpec
36
+ # it "does stuff" do
37
+ # # shared code available here
38
+ # end
39
+ # describe "inner stuff" do
40
+ # it "still does stuff" do
41
+ # # ...and here
42
+ # end
43
+ # end
44
+ # end
45
+ #
46
+ # For more information on getting started with writing specs, see:
47
+ #
48
+ # http://www.rubyinside.com/a-minitestspec-tutorial-elegant-spec-style-testing-that-comes-with-ruby-5354.html
49
+ #
50
+ # For some suggestions on how to improve your specs, try:
51
+ #
52
+ # http://betterspecs.org
53
+ #
54
+ # but do note that several items there are debatable or specific to
55
+ # rspec.
56
+
57
+
58
+ def describe desc, additional_desc = nil, &block # :doc:
59
+ stack = Minitest::Spec.describe_stack
60
+ name = [stack.last, desc, additional_desc].compact.join("::")
61
+ sclas = stack.last || if Class === self && is_a?(Minitest::Spec::DSL) then
62
+ self
63
+ else
64
+ Minitest::Spec.spec_type desc
65
+ end
66
+
67
+ cls = sclas.create name, desc
68
+
69
+ stack.push cls
70
+ cls.class_eval(&block)
71
+ stack.pop
72
+ cls
73
+ end
74
+ private :describe
75
+ end
76
+
77
+ ##
78
+ # Minitest::Spec -- The faster, better, less-magical spec framework!
79
+ #
80
+ # For a list of expectations, see Minitest::Expectations.
81
+
82
+ class Minitest::Spec < Minitest::Test
83
+
84
+ def self.current # :nodoc:
85
+ Thread.current[:current_spec]
86
+ end
87
+
88
+ def initialize name # :nodoc:
89
+ super
90
+ Thread.current[:current_spec] = self
91
+ end
92
+
93
+ ##
94
+ # Oh look! A Minitest::Spec::DSL module! Eat your heart out DHH.
95
+
96
+ module DSL
97
+ ##
98
+ # Contains pairs of matchers and Spec classes to be used to
99
+ # calculate the superclass of a top-level describe. This allows for
100
+ # automatically customizable spec types.
101
+ #
102
+ # See: register_spec_type and spec_type
103
+
104
+ TYPES = [[//, Minitest::Spec]]
105
+
106
+ ##
107
+ # Register a new type of spec that matches the spec's description.
108
+ # This method can take either a Regexp and a spec class or a spec
109
+ # class and a block that takes the description and returns true if
110
+ # it matches.
111
+ #
112
+ # Eg:
113
+ #
114
+ # register_spec_type(/Controller$/, Minitest::Spec::Rails)
115
+ #
116
+ # or:
117
+ #
118
+ # register_spec_type(Minitest::Spec::RailsModel) do |desc|
119
+ # desc.superclass == ActiveRecord::Base
120
+ # end
121
+
122
+ def register_spec_type(*args, &block)
123
+ if block then
124
+ matcher, klass = block, args.first
125
+ else
126
+ matcher, klass = *args
127
+ end
128
+ TYPES.unshift [matcher, klass]
129
+ end
130
+
131
+ ##
132
+ # Figure out the spec class to use based on a spec's description. Eg:
133
+ #
134
+ # spec_type("BlahController") # => Minitest::Spec::Rails
135
+
136
+ def spec_type desc
137
+ TYPES.find { |matcher, klass|
138
+ if matcher.respond_to? :call then
139
+ matcher.call desc
140
+ else
141
+ matcher === desc.to_s
142
+ end
143
+ }.last
144
+ end
145
+
146
+ def describe_stack # :nodoc:
147
+ Thread.current[:describe_stack] ||= []
148
+ end
149
+
150
+ ##
151
+ # Returns the children of this spec.
152
+
153
+ def children
154
+ @children ||= []
155
+ end
156
+
157
+ def nuke_test_methods! # :nodoc:
158
+ self.public_instance_methods.grep(/^test_/).each do |name|
159
+ self.send :undef_method, name
160
+ end
161
+ end
162
+
163
+ def before_first type = nil, &block
164
+ define_method :before_first_method do
165
+ super()
166
+ self.instance_eval(&block)
167
+ end
168
+ end
169
+
170
+ ##
171
+ # Define a 'before' action. Inherits the way normal methods should.
172
+ #
173
+ # NOTE: +type+ is ignored and is only there to make porting easier.
174
+ #
175
+ # Equivalent to Minitest::Test#setup.
176
+
177
+ def before type = nil, &block
178
+ define_method :setup do
179
+ super()
180
+ self.instance_eval(&block)
181
+ end
182
+ end
183
+
184
+ ##
185
+ # Define an 'after' action. Inherits the way normal methods should.
186
+ #
187
+ # NOTE: +type+ is ignored and is only there to make porting easier.
188
+ #
189
+ # Equivalent to Minitest::Test#teardown.
190
+
191
+ def after type = nil, &block
192
+ define_method :teardown do
193
+ self.instance_eval(&block)
194
+ super()
195
+ end
196
+ end
197
+
198
+ def after_last type = nil, &block
199
+ define_method :after_last_method do
200
+ self.instance_eval(&block)
201
+ super()
202
+ end
203
+ end
204
+
205
+ ##
206
+ # Define an expectation with name +desc+. Name gets morphed to a
207
+ # proper test method name. For some freakish reason, people who
208
+ # write specs don't like class inheritance, so this goes way out of
209
+ # its way to make sure that expectations aren't inherited.
210
+ #
211
+ # This is also aliased to #specify and doesn't require a +desc+ arg.
212
+ #
213
+ # Hint: If you _do_ want inheritence, use minitest/test. You can mix
214
+ # and match between assertions and expectations as much as you want.
215
+
216
+ def it desc = "anonymous", &block
217
+ block ||= proc { skip "(no tests defined)" }
218
+
219
+ @specs ||= 0
220
+ @specs += 1
221
+
222
+ name = "test_%04d_%s" % [ @specs, desc ]
223
+
224
+ define_method name, &block
225
+
226
+ self.children.each do |mod|
227
+ mod.send :undef_method, name if mod.public_method_defined? name
228
+ end
229
+
230
+ name
231
+ end
232
+
233
+ alias_method :t, :it
234
+
235
+ ##
236
+ # Essentially, define an accessor for +name+ with +block+.
237
+ #
238
+ # Why use let instead of def? I honestly don't know.
239
+
240
+ def let name, &block
241
+ raise ArgumentError, 'name cannot begin with "test"' if name.to_s =~ /\Atest/
242
+ define_method name do
243
+ @_memoized ||= {}
244
+ @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
245
+ end
246
+ end
247
+
248
+ ##
249
+ # Another lazy man's accessor generator. Made even more lazy by
250
+ # setting the name for you to +subject+.
251
+
252
+ def subject &block
253
+ let :subject, &block
254
+ end
255
+
256
+ def create name, desc # :nodoc:
257
+ cls = Class.new(self) do
258
+ @name = name
259
+ @desc = desc
260
+
261
+ nuke_test_methods!
262
+ end
263
+
264
+ children << cls
265
+
266
+ cls
267
+ end
268
+
269
+ def name # :nodoc:
270
+ defined?(@name) ? @name : super
271
+ end
272
+
273
+ def to_s # :nodoc:
274
+ name # Can't alias due to 1.8.7, not sure why
275
+ end
276
+
277
+ # :stopdoc:
278
+ attr_reader :desc
279
+ alias :specify :it
280
+ # :startdoc:
281
+ end
282
+
283
+ extend DSL
284
+
285
+ TYPES = DSL::TYPES # :nodoc:
286
+ end
287
+
288
+ require "minitest/expectations"
289
+
290
+ class Object # :nodoc:
291
+ include Minitest::Expectations unless ENV["MT_NO_EXPECTATIONS"]
292
+ end
@@ -0,0 +1,272 @@
1
+ require "minitest" unless defined? Minitest::Runnable
2
+
3
+ module Minitest
4
+ ##
5
+ # Subclass Test to create your own tests. Typically you'll want a
6
+ # Test subclass per implementation class.
7
+ #
8
+ # See Minitest::Assertions
9
+
10
+ class Test < Runnable
11
+ require "minitest/assertions"
12
+ include Minitest::Assertions
13
+
14
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, # :nodoc:
15
+ Interrupt, SystemExit]
16
+
17
+ ##
18
+ # Call this at the top of your tests when you absolutely
19
+ # positively need to have ordered tests. In doing so, you're
20
+ # admitting that you suck and your tests are weak.
21
+
22
+ def self.i_suck_and_my_tests_are_order_dependent!
23
+ class << self
24
+ undef_method :test_order if method_defined? :test_order
25
+ define_method :test_order do :alpha end
26
+ end
27
+ end
28
+
29
+ ##
30
+ # Make diffs for this Test use #pretty_inspect so that diff
31
+ # in assert_equal can have more details. NOTE: this is much slower
32
+ # than the regular inspect but much more usable for complex
33
+ # objects.
34
+
35
+ def self.make_my_diffs_pretty!
36
+ require "pp"
37
+
38
+ define_method :mu_pp do |o|
39
+ o.pretty_inspect
40
+ end
41
+ end
42
+
43
+ ##
44
+ # Call this at the top of your tests when you want to run your
45
+ # tests in parallel. In doing so, you're admitting that you rule
46
+ # and your tests are awesome.
47
+
48
+ def self.parallelize_me!
49
+ require "minitest/parallel_each"
50
+
51
+ class << self
52
+ undef_method :test_order if method_defined? :test_order
53
+ define_method :test_order do :parallel end
54
+ end
55
+ end
56
+
57
+ ##
58
+ # Returns all instance methods starting with "test_".
59
+
60
+ def self.runnable_methods
61
+ methods_matching(/^test_/)
62
+ end
63
+
64
+ ##
65
+ # Defines the order to run tests (:random by default). Override
66
+ # this or use a convenience method to change it for your tests.
67
+
68
+ def self.test_order
69
+ :random
70
+ end
71
+
72
+ ##
73
+ # The time it took to run this test.
74
+
75
+ attr_accessor :time
76
+
77
+ def marshal_dump # :nodoc:
78
+ super << self.time
79
+ end
80
+
81
+ def marshal_load ary # :nodoc:
82
+ self.time = ary.pop
83
+ super
84
+ end
85
+
86
+ ##
87
+ # Runs a single test with setup/teardown hooks.
88
+
89
+ def run
90
+ with_info_handler do
91
+ time_it do
92
+ capture_exceptions do
93
+ before_setup; setup; after_setup
94
+
95
+ self.send self.name
96
+ end
97
+
98
+ %w{ before_teardown teardown after_teardown }.each do |hook|
99
+ capture_exceptions do
100
+ self.send hook
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ self # per contract
107
+ end
108
+
109
+ ##
110
+ # Provides before/after hooks for setup and teardown. These are
111
+ # meant for library writers, NOT for regular test authors. See
112
+ # #before_setup for an example.
113
+
114
+ module LifecycleHooks
115
+
116
+ ##
117
+ # Runs before every test, before setup. This hook is meant for
118
+ # libraries to extend minitest. It is not meant to be used by
119
+ # test developers.
120
+ #
121
+ # As a simplistic example:
122
+ #
123
+ # module MyMinitestPlugin
124
+ # def before_setup
125
+ # super
126
+ # # ... stuff to do before setup is run
127
+ # end
128
+ #
129
+ # def after_setup
130
+ # # ... stuff to do after setup is run
131
+ # super
132
+ # end
133
+ #
134
+ # def before_teardown
135
+ # super
136
+ # # ... stuff to do before teardown is run
137
+ # end
138
+ #
139
+ # def after_teardown
140
+ # # ... stuff to do after teardown is run
141
+ # super
142
+ # end
143
+ # end
144
+ #
145
+ # class MiniTest::Test
146
+ # include MyMinitestPlugin
147
+ # end
148
+
149
+ def before_setup; end
150
+
151
+ ##
152
+ # Runs before every test. Use this to set up before each test
153
+ # run.
154
+
155
+ def setup; end
156
+
157
+ ##
158
+ # Runs before every test, after setup. This hook is meant for
159
+ # libraries to extend minitest. It is not meant to be used by
160
+ # test developers.
161
+ #
162
+ # See #before_setup for an example.
163
+
164
+ def after_setup; end
165
+
166
+ ##
167
+ # Runs after every test, before teardown. This hook is meant for
168
+ # libraries to extend minitest. It is not meant to be used by
169
+ # test developers.
170
+ #
171
+ # See #before_setup for an example.
172
+
173
+ def before_teardown; end
174
+
175
+ ##
176
+ # Runs after every test. Use this to clean up after each test
177
+ # run.
178
+
179
+ def teardown; end
180
+
181
+ ##
182
+ # Runs after every test, after teardown. This hook is meant for
183
+ # libraries to extend minitest. It is not meant to be used by
184
+ # test developers.
185
+ #
186
+ # See #before_setup for an example.
187
+
188
+ def after_teardown; end
189
+ end # LifecycleHooks
190
+
191
+ def capture_exceptions # :nodoc:
192
+ begin
193
+ yield
194
+ rescue *PASSTHROUGH_EXCEPTIONS
195
+ raise
196
+ rescue Assertion => e
197
+ self.failures << e
198
+ rescue Exception => e
199
+ self.failures << UnexpectedError.new(e)
200
+ end
201
+ end
202
+
203
+ ##
204
+ # Did this run error?
205
+
206
+ def error?
207
+ self.failures.any? { |f| UnexpectedError === f }
208
+ end
209
+
210
+ ##
211
+ # The location identifier of this test.
212
+
213
+ def location
214
+ loc = " [#{self.failure.location}]" unless passed? or error?
215
+ "#{self.class}##{self.name}#{loc}"
216
+ end
217
+
218
+ ##
219
+ # Did this run pass?
220
+ #
221
+ # Note: skipped runs are not considered passing, but they don't
222
+ # cause the process to exit non-zero.
223
+
224
+ def passed?
225
+ not self.failure
226
+ end
227
+
228
+ ##
229
+ # Returns ".", "F", or "E" based on the result of the run.
230
+
231
+ def result_code
232
+ self.failure and self.failure.result_code or "."
233
+ end
234
+
235
+ ##
236
+ # Was this run skipped?
237
+
238
+ def skipped?
239
+ self.failure and Skip === self.failure
240
+ end
241
+
242
+ def time_it # :nodoc:
243
+ t0 = Time.now
244
+
245
+ yield
246
+ ensure
247
+ self.time = Time.now - t0
248
+ end
249
+
250
+ def to_s # :nodoc:
251
+ return location if passed? and not skipped?
252
+
253
+ failures.map { |failure|
254
+ "#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"
255
+ }.join "\n"
256
+ end
257
+
258
+ def with_info_handler &block # :nodoc:
259
+ t0 = Time.now
260
+
261
+ handler = lambda do
262
+ warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Time.now - t0]
263
+ end
264
+
265
+ self.class.on_signal "INFO", handler, &block
266
+ end
267
+
268
+ include LifecycleHooks
269
+ include Guard
270
+ extend Guard
271
+ end # Test
272
+ end
@@ -0,0 +1,45 @@
1
+ # :stopdoc:
2
+
3
+ unless defined?(Minitest) then
4
+ # all of this crap is just to avoid circular requires and is only
5
+ # needed if a user requires "minitest/unit" directly instead of
6
+ # "minitest/autorun", so we also warn
7
+
8
+ from = caller.reject { |s| s =~ /rubygems/ }.join("\n ")
9
+ warn "Warning: you should require 'minitest/autorun' instead."
10
+ warn %(Warning: or add 'gem "minitest"' before 'require "minitest/autorun"')
11
+ warn "From:\n #{from}"
12
+
13
+ module Minitest; end
14
+ MiniTest = Minitest # prevents minitest.rb from requiring back to us
15
+ require "minitest"
16
+ end
17
+
18
+ MiniTest = Minitest unless defined?(MiniTest)
19
+
20
+ module Minitest
21
+ class Unit
22
+ VERSION = Minitest::VERSION
23
+ class TestCase < Minitest::Test
24
+ def self.inherited klass # :nodoc:
25
+ from = caller.first
26
+ warn "MiniTest::Unit::TestCase is now Minitest::Test. From #{from}"
27
+ super
28
+ end
29
+ end
30
+
31
+ def self.autorun # :nodoc:
32
+ from = caller.first
33
+ warn "MiniTest::Unit.autorun is now Minitest.autorun. From #{from}"
34
+ Minitest.autorun
35
+ end
36
+
37
+ def self.after_tests(&b)
38
+ from = caller.first
39
+ warn "MiniTest::Unit.after_tests is now Minitest.after_run. From #{from}"
40
+ Minitest.after_run(&b)
41
+ end
42
+ end
43
+ end
44
+
45
+ # :startdoc: