PageRenderTime 60ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/minitest/spec.rb

http://github.com/ruby/ruby
Ruby | 522 lines | 149 code | 101 blank | 272 comment | 7 complexity | 5dfc4dbd1b5bed6fa596ed40420a7346 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. ######################################################################
  2. # This file is imported from the minitest project.
  3. # DO NOT make modifications in this repo. They _will_ be reverted!
  4. # File a patch instead and assign it to Ryan Davis.
  5. ######################################################################
  6. #!/usr/bin/ruby -w
  7. require 'minitest/unit'
  8. class Module # :nodoc:
  9. def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
  10. # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
  11. self.class_eval <<-EOM
  12. def #{new_name} *args
  13. return MiniTest::Spec.current.#{meth}(*args, &self) if
  14. Proc === self
  15. return MiniTest::Spec.current.#{meth}(args.first, self) if
  16. args.size == 1 unless #{!!dont_flip}
  17. return MiniTest::Spec.current.#{meth}(self, *args)
  18. end
  19. EOM
  20. end
  21. ##
  22. # infect_with_assertions has been removed due to excessive clever.
  23. # Use infect_an_assertion directly instead.
  24. def infect_with_assertions(pos_prefix, neg_prefix,
  25. skip_re,
  26. dont_flip_re = /\c0/,
  27. map = {})
  28. abort "infect_with_assertions is dead. Use infect_an_assertion directly"
  29. end
  30. end
  31. module Kernel # :nodoc:
  32. ##
  33. # Describe a series of expectations for a given target +desc+.
  34. #
  35. # TODO: find good tutorial url.
  36. #
  37. # Defines a test class subclassing from either MiniTest::Spec or
  38. # from the surrounding describe's class. The surrounding class may
  39. # subclass MiniTest::Spec manually in order to easily share code:
  40. #
  41. # class MySpec < MiniTest::Spec
  42. # # ... shared code ...
  43. # end
  44. #
  45. # class TestStuff < MySpec
  46. # it "does stuff" do
  47. # # shared code available here
  48. # end
  49. # describe "inner stuff" do
  50. # it "still does stuff" do
  51. # # ...and here
  52. # end
  53. # end
  54. # end
  55. def describe desc, additional_desc = nil, &block # :doc:
  56. stack = MiniTest::Spec.describe_stack
  57. name = [stack.last, desc, additional_desc].compact.join("::")
  58. sclas = stack.last || if Class === self && self < MiniTest::Spec then
  59. self
  60. else
  61. MiniTest::Spec.spec_type desc
  62. end
  63. cls = sclas.create name, desc
  64. stack.push cls
  65. cls.class_eval(&block)
  66. stack.pop
  67. cls
  68. end
  69. private :describe
  70. end
  71. ##
  72. # MiniTest::Spec -- The faster, better, less-magical spec framework!
  73. #
  74. # For a list of expectations, see MiniTest::Expectations.
  75. class MiniTest::Spec < MiniTest::Unit::TestCase
  76. ##
  77. # Contains pairs of matchers and Spec classes to be used to
  78. # calculate the superclass of a top-level describe. This allows for
  79. # automatically customizable spec types.
  80. #
  81. # See: register_spec_type and spec_type
  82. TYPES = [[//, MiniTest::Spec]]
  83. ##
  84. # Register a new type of spec that matches the spec's description.
  85. # This method can take either a Regexp and a spec class or a spec
  86. # class and a block that takes the description and returns true if
  87. # it matches.
  88. #
  89. # Eg:
  90. #
  91. # register_spec_type(/Controller$/, MiniTest::Spec::Rails)
  92. #
  93. # or:
  94. #
  95. # register_spec_type(MiniTest::Spec::RailsModel) do |desc|
  96. # desc.superclass == ActiveRecord::Base
  97. # end
  98. def self.register_spec_type(*args, &block)
  99. if block then
  100. matcher, klass = block, args.first
  101. else
  102. matcher, klass = *args
  103. end
  104. TYPES.unshift [matcher, klass]
  105. end
  106. ##
  107. # Figure out the spec class to use based on a spec's description. Eg:
  108. #
  109. # spec_type("BlahController") # => MiniTest::Spec::Rails
  110. def self.spec_type desc
  111. TYPES.find { |matcher, klass|
  112. if matcher.respond_to? :call then
  113. matcher.call desc
  114. else
  115. matcher === desc.to_s
  116. end
  117. }.last
  118. end
  119. @@describe_stack = []
  120. def self.describe_stack # :nodoc:
  121. @@describe_stack
  122. end
  123. ##
  124. # Returns the children of this spec.
  125. def self.children
  126. @children ||= []
  127. end
  128. def self.nuke_test_methods! # :nodoc:
  129. self.public_instance_methods.grep(/^test_/).each do |name|
  130. self.send :undef_method, name
  131. end
  132. end
  133. ##
  134. # Define a 'before' action. Inherits the way normal methods should.
  135. #
  136. # NOTE: +type+ is ignored and is only there to make porting easier.
  137. #
  138. # Equivalent to MiniTest::Unit::TestCase#setup.
  139. def self.before type = :each, &block
  140. raise "unsupported before type: #{type}" unless type == :each
  141. add_setup_hook {|tc| tc.instance_eval(&block) }
  142. end
  143. ##
  144. # Define an 'after' action. Inherits the way normal methods should.
  145. #
  146. # NOTE: +type+ is ignored and is only there to make porting easier.
  147. #
  148. # Equivalent to MiniTest::Unit::TestCase#teardown.
  149. def self.after type = :each, &block
  150. raise "unsupported after type: #{type}" unless type == :each
  151. add_teardown_hook {|tc| tc.instance_eval(&block) }
  152. end
  153. ##
  154. # Define an expectation with name +desc+. Name gets morphed to a
  155. # proper test method name. For some freakish reason, people who
  156. # write specs don't like class inheritence, so this goes way out of
  157. # its way to make sure that expectations aren't inherited.
  158. #
  159. # This is also aliased to #specify and doesn't require a +desc+ arg.
  160. #
  161. # Hint: If you _do_ want inheritence, use minitest/unit. You can mix
  162. # and match between assertions and expectations as much as you want.
  163. def self.it desc = "anonymous", &block
  164. block ||= proc { skip "(no tests defined)" }
  165. @specs ||= 0
  166. @specs += 1
  167. name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ]
  168. define_method name, &block
  169. self.children.each do |mod|
  170. mod.send :undef_method, name if mod.public_method_defined? name
  171. end
  172. end
  173. def self.let name, &block
  174. define_method name do
  175. @_memoized ||= {}
  176. @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
  177. end
  178. end
  179. def self.subject &block
  180. let :subject, &block
  181. end
  182. def self.create name, desc # :nodoc:
  183. cls = Class.new(self) do
  184. @name = name
  185. @desc = desc
  186. nuke_test_methods!
  187. end
  188. children << cls
  189. cls
  190. end
  191. def self.to_s # :nodoc:
  192. defined?(@name) ? @name : super
  193. end
  194. # :stopdoc:
  195. class << self
  196. attr_reader :desc
  197. alias :specify :it
  198. alias :name :to_s
  199. end
  200. # :startdoc:
  201. end
  202. module MiniTest::Expectations
  203. ##
  204. # See MiniTest::Assertions#assert_empty.
  205. #
  206. # collection.must_be_empty
  207. #
  208. # :method: must_be_empty
  209. infect_an_assertion :assert_empty, :must_be_empty
  210. ##
  211. # See MiniTest::Assertions#assert_equal
  212. #
  213. # a.must_equal b
  214. #
  215. # :method: must_equal
  216. infect_an_assertion :assert_equal, :must_equal
  217. ##
  218. # See MiniTest::Assertions#assert_in_delta
  219. #
  220. # n.must_be_close_to m [, delta]
  221. #
  222. # :method: must_be_within_delta
  223. infect_an_assertion :assert_in_delta, :must_be_close_to
  224. alias :must_be_within_delta :must_be_close_to
  225. ##
  226. # See MiniTest::Assertions#assert_in_epsilon
  227. #
  228. # n.must_be_within_epsilon m [, epsilon]
  229. #
  230. # :method: must_be_within_epsilon
  231. infect_an_assertion :assert_in_epsilon, :must_be_within_epsilon
  232. ##
  233. # See MiniTest::Assertions#assert_includes
  234. #
  235. # collection.must_include obj
  236. #
  237. # :method: must_include
  238. infect_an_assertion :assert_includes, :must_include, :reverse
  239. ##
  240. # See MiniTest::Assertions#assert_instance_of
  241. #
  242. # obj.must_be_instance_of klass
  243. #
  244. # :method: must_be_instance_of
  245. infect_an_assertion :assert_instance_of, :must_be_instance_of
  246. ##
  247. # See MiniTest::Assertions#assert_kind_of
  248. #
  249. # obj.must_be_kind_of mod
  250. #
  251. # :method: must_be_kind_of
  252. infect_an_assertion :assert_kind_of, :must_be_kind_of
  253. ##
  254. # See MiniTest::Assertions#assert_match
  255. #
  256. # a.must_match b
  257. #
  258. # :method: must_match
  259. infect_an_assertion :assert_match, :must_match
  260. ##
  261. # See MiniTest::Assertions#assert_nil
  262. #
  263. # obj.must_be_nil
  264. #
  265. # :method: must_be_nil
  266. infect_an_assertion :assert_nil, :must_be_nil
  267. ##
  268. # See MiniTest::Assertions#assert_operator
  269. #
  270. # n.must_be :<=, 42
  271. #
  272. # This can also do predicates:
  273. #
  274. # str.must_be :empty?
  275. #
  276. # :method: must_be
  277. infect_an_assertion :assert_operator, :must_be, :reverse
  278. ##
  279. # See MiniTest::Assertions#assert_output
  280. #
  281. # proc { ... }.must_output out_or_nil [, err]
  282. #
  283. # :method: must_output
  284. infect_an_assertion :assert_output, :must_output
  285. ##
  286. # See MiniTest::Assertions#assert_raises
  287. #
  288. # proc { ... }.must_raise exception
  289. #
  290. # :method: must_raise
  291. infect_an_assertion :assert_raises, :must_raise
  292. ##
  293. # See MiniTest::Assertions#assert_respond_to
  294. #
  295. # obj.must_respond_to msg
  296. #
  297. # :method: must_respond_to
  298. infect_an_assertion :assert_respond_to, :must_respond_to, :reverse
  299. ##
  300. # See MiniTest::Assertions#assert_same
  301. #
  302. # a.must_be_same_as b
  303. #
  304. # :method: must_be_same_as
  305. infect_an_assertion :assert_same, :must_be_same_as
  306. ##
  307. # See MiniTest::Assertions#assert_send
  308. # TODO: remove me
  309. #
  310. # a.must_send
  311. #
  312. # :method: must_send
  313. infect_an_assertion :assert_send, :must_send
  314. ##
  315. # See MiniTest::Assertions#assert_silent
  316. #
  317. # proc { ... }.must_be_silent
  318. #
  319. # :method: must_be_silent
  320. infect_an_assertion :assert_silent, :must_be_silent
  321. ##
  322. # See MiniTest::Assertions#assert_throws
  323. #
  324. # proc { ... }.must_throw sym
  325. #
  326. # :method: must_throw
  327. infect_an_assertion :assert_throws, :must_throw
  328. ##
  329. # See MiniTest::Assertions#refute_empty
  330. #
  331. # collection.wont_be_empty
  332. #
  333. # :method: wont_be_empty
  334. infect_an_assertion :refute_empty, :wont_be_empty
  335. ##
  336. # See MiniTest::Assertions#refute_equal
  337. #
  338. # a.wont_equal b
  339. #
  340. # :method: wont_equal
  341. infect_an_assertion :refute_equal, :wont_equal
  342. ##
  343. # See MiniTest::Assertions#refute_in_delta
  344. #
  345. # n.wont_be_close_to m [, delta]
  346. #
  347. # :method: wont_be_within_delta
  348. infect_an_assertion :refute_in_delta, :wont_be_within_delta
  349. alias :wont_be_close_to :wont_be_within_delta
  350. # FIX: reverse aliases
  351. ##
  352. # See MiniTest::Assertions#refute_in_epsilon
  353. #
  354. # n.wont_be_within_epsilon m [, epsilon]
  355. #
  356. # :method: wont_be_within_epsilon
  357. infect_an_assertion :refute_in_epsilon, :wont_be_within_epsilon
  358. ##
  359. # See MiniTest::Assertions#refute_includes
  360. #
  361. # collection.wont_include obj
  362. #
  363. # :method: wont_include
  364. infect_an_assertion :refute_includes, :wont_include, :reverse
  365. ##
  366. # See MiniTest::Assertions#refute_instance_of
  367. #
  368. # obj.wont_be_instance_of klass
  369. #
  370. # :method: wont_be_instance_of
  371. infect_an_assertion :refute_instance_of, :wont_be_instance_of
  372. ##
  373. # See MiniTest::Assertions#refute_kind_of
  374. #
  375. # obj.wont_be_kind_of mod
  376. #
  377. # :method: wont_be_kind_of
  378. infect_an_assertion :refute_kind_of, :wont_be_kind_of
  379. ##
  380. # See MiniTest::Assertions#refute_match
  381. #
  382. # a.wont_match b
  383. #
  384. # :method: wont_match
  385. infect_an_assertion :refute_match, :wont_match
  386. ##
  387. # See MiniTest::Assertions#refute_nil
  388. #
  389. # obj.wont_be_nil
  390. #
  391. # :method: wont_be_nil
  392. infect_an_assertion :refute_nil, :wont_be_nil
  393. ##
  394. # See MiniTest::Assertions#refute_operator
  395. #
  396. # n.wont_be :<=, 42
  397. #
  398. # This can also do predicates:
  399. #
  400. # str.wont_be :empty?
  401. #
  402. # :method: wont_be
  403. infect_an_assertion :refute_operator, :wont_be, :reverse
  404. ##
  405. # See MiniTest::Assertions#refute_respond_to
  406. #
  407. # obj.wont_respond_to msg
  408. #
  409. # :method: wont_respond_to
  410. infect_an_assertion :refute_respond_to, :wont_respond_to, :reverse
  411. ##
  412. # See MiniTest::Assertions#refute_same
  413. #
  414. # a.wont_be_same_as b
  415. #
  416. # :method: wont_be_same_as
  417. infect_an_assertion :refute_same, :wont_be_same_as
  418. end
  419. class Object
  420. include MiniTest::Expectations
  421. end