PageRenderTime 36ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/minitest/spec.rb

https://gitlab.com/klauer/ruby
Ruby | 328 lines | 122 code | 67 blank | 139 comment | 10 complexity | 6a1ad350bddd8f8856699a8f3b2f76a7 MD5 | raw file
  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
  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, &block
  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. # Create your own expectations from MiniTest::Assertions using a
  23. # flexible set of rules. If you don't like must/wont, then this
  24. # method is your friend. For an example of its usage see the bottom
  25. # of minitest/spec.rb.
  26. def infect_with_assertions(pos_prefix, neg_prefix,
  27. skip_re,
  28. dont_flip_re = /\c0/,
  29. map = {})
  30. MiniTest::Assertions.public_instance_methods(false).sort.each do |meth|
  31. meth = meth.to_s
  32. new_name = case meth
  33. when /^assert/ then
  34. meth.sub(/^assert/, pos_prefix.to_s)
  35. when /^refute/ then
  36. meth.sub(/^refute/, neg_prefix.to_s)
  37. end
  38. next unless new_name
  39. next if new_name =~ skip_re
  40. regexp, replacement = map.find { |re, _| new_name =~ re }
  41. new_name.sub! regexp, replacement if replacement
  42. puts "\n##\n# :method: #{new_name}\n# See MiniTest::Assertions##{meth}" if
  43. $0 == __FILE__
  44. infect_an_assertion meth, new_name, new_name =~ dont_flip_re
  45. end
  46. end
  47. end
  48. module Kernel
  49. ##
  50. # Describe a series of expectations for a given target +desc+.
  51. #
  52. # TODO: find good tutorial url.
  53. #
  54. # Defines a test class subclassing from either
  55. # MiniTest::Unit::TestCase or from the surrounding describe's class.
  56. def describe desc, &block
  57. stack = MiniTest::Spec.describe_stack
  58. name = [stack.last, desc].compact.join("::")
  59. cls = Class.new(stack.last || MiniTest::Spec)
  60. # :stopdoc:
  61. # omg this sucks
  62. (class << cls; self; end).send(:define_method, :to_s) { name }
  63. # :startdoc:
  64. cls.nuke_test_methods!
  65. stack.push cls
  66. cls.class_eval(&block)
  67. stack.pop
  68. cls
  69. end
  70. private :describe
  71. end
  72. class Module
  73. def classes type = Object # :nodoc:
  74. constants.map { |n| const_get n }.find_all { |c|
  75. c.class == Class and type > c
  76. } - [self]
  77. end
  78. end
  79. ##
  80. # MiniTest::Spec -- The faster, better, less-magical spec framework!
  81. #
  82. # For a list of expectations, see Object.
  83. class MiniTest::Spec < MiniTest::Unit::TestCase
  84. @@describe_stack = []
  85. def self.describe_stack # :nodoc:
  86. @@describe_stack
  87. end
  88. def self.current # :nodoc:
  89. @@current_spec
  90. end
  91. def initialize name # :nodoc:
  92. super
  93. @@current_spec = self
  94. end
  95. def self.nuke_test_methods! # :nodoc:
  96. self.public_instance_methods.grep(/^test_/).each do |name|
  97. self.send :undef_method, name
  98. end
  99. end
  100. def self.define_inheritable_method name, &block # :nodoc:
  101. super_method = self.superclass.instance_method name
  102. define_method name do
  103. super_method.bind(self).call if super_method # regular super() warns
  104. instance_eval(&block)
  105. end
  106. end
  107. ##
  108. # Define a 'before' action. Inherits the way normal methods should.
  109. #
  110. # NOTE: +type+ is ignored and is only there to make porting easier.
  111. #
  112. # Equivalent to MiniTest::Unit::TestCase#setup.
  113. def self.before type = :each, &block
  114. raise "unsupported before type: #{type}" unless type == :each
  115. define_inheritable_method :setup, &block
  116. end
  117. ##
  118. # Define an 'after' action. Inherits the way normal methods should.
  119. #
  120. # NOTE: +type+ is ignored and is only there to make porting easier.
  121. #
  122. # Equivalent to MiniTest::Unit::TestCase#teardown.
  123. def self.after type = :each, &block
  124. raise "unsupported after type: #{type}" unless type == :each
  125. define_inheritable_method :teardown, &block
  126. end
  127. ##
  128. # Define an expectation with name +desc+. Name gets morphed to a
  129. # proper test method name. For some freakish reason, people who
  130. # write specs don't like class inheritence, so this goes way out of
  131. # its way to make sure that expectations aren't inherited.
  132. #
  133. # Hint: If you _do_ want inheritence, use minitest/unit. You can mix
  134. # and match between assertions and expectations as much as you want.
  135. def self.it desc, &block
  136. block ||= proc { skip "(no tests defined)" }
  137. @specs ||= 0
  138. @specs += 1
  139. name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ]
  140. define_method name, &block
  141. classes(MiniTest::Spec).each do |mod|
  142. mod.send :undef_method, name if mod.respond_to? name
  143. end
  144. end
  145. end
  146. Object.infect_with_assertions(:must, :wont,
  147. /^(must|wont)$|wont_(throw)|
  148. must_(block|not?_|nothing|raise$)/x,
  149. /(must|wont)_(include|respond_to)/,
  150. /(must_throw)s/ => '\1',
  151. /(?!not)_same/ => '_be_same_as',
  152. /_in_/ => '_be_within_',
  153. /_operator/ => '_be',
  154. /_includes/ => '_include',
  155. /(must|wont)_(.*_of|nil|silent|empty)/ => '\1_be_\2',
  156. /must_raises/ => 'must_raise')
  157. class Object
  158. alias :must_be_close_to :must_be_within_delta
  159. alias :wont_be_close_to :wont_be_within_delta
  160. if $0 == __FILE__ then
  161. { "must" => "assert", "wont" => "refute" }.each do |a, b|
  162. puts "\n"
  163. puts "##"
  164. puts "# :method: #{a}_be_close_to"
  165. puts "# See MiniTest::Assertions##{b}_in_delta"
  166. end
  167. end
  168. ##
  169. # :method: must_be
  170. # See MiniTest::Assertions#assert_operator
  171. ##
  172. # :method: must_be_close_to
  173. # See MiniTest::Assertions#assert_in_delta
  174. ##
  175. # :method: must_be_empty
  176. # See MiniTest::Assertions#assert_empty
  177. ##
  178. # :method: must_be_instance_of
  179. # See MiniTest::Assertions#assert_instance_of
  180. ##
  181. # :method: must_be_kind_of
  182. # See MiniTest::Assertions#assert_kind_of
  183. ##
  184. # :method: must_be_nil
  185. # See MiniTest::Assertions#assert_nil
  186. ##
  187. # :method: must_be_same_as
  188. # See MiniTest::Assertions#assert_same
  189. ##
  190. # :method: must_be_silent
  191. # See MiniTest::Assertions#assert_silent
  192. ##
  193. # :method: must_be_within_delta
  194. # See MiniTest::Assertions#assert_in_delta
  195. ##
  196. # :method: must_be_within_epsilon
  197. # See MiniTest::Assertions#assert_in_epsilon
  198. ##
  199. # :method: must_equal
  200. # See MiniTest::Assertions#assert_equal
  201. ##
  202. # :method: must_include
  203. # See MiniTest::Assertions#assert_includes
  204. ##
  205. # :method: must_match
  206. # See MiniTest::Assertions#assert_match
  207. ##
  208. # :method: must_output
  209. # See MiniTest::Assertions#assert_output
  210. ##
  211. # :method: must_raise
  212. # See MiniTest::Assertions#assert_raises
  213. ##
  214. # :method: must_respond_to
  215. # See MiniTest::Assertions#assert_respond_to
  216. ##
  217. # :method: must_send
  218. # See MiniTest::Assertions#assert_send
  219. ##
  220. # :method: must_throw
  221. # See MiniTest::Assertions#assert_throws
  222. ##
  223. # :method: wont_be
  224. # See MiniTest::Assertions#refute_operator
  225. ##
  226. # :method: wont_be_close_to
  227. # See MiniTest::Assertions#refute_in_delta
  228. ##
  229. # :method: wont_be_empty
  230. # See MiniTest::Assertions#refute_empty
  231. ##
  232. # :method: wont_be_instance_of
  233. # See MiniTest::Assertions#refute_instance_of
  234. ##
  235. # :method: wont_be_kind_of
  236. # See MiniTest::Assertions#refute_kind_of
  237. ##
  238. # :method: wont_be_nil
  239. # See MiniTest::Assertions#refute_nil
  240. ##
  241. # :method: wont_be_same_as
  242. # See MiniTest::Assertions#refute_same
  243. ##
  244. # :method: wont_be_within_delta
  245. # See MiniTest::Assertions#refute_in_delta
  246. ##
  247. # :method: wont_be_within_epsilon
  248. # See MiniTest::Assertions#refute_in_epsilon
  249. ##
  250. # :method: wont_equal
  251. # See MiniTest::Assertions#refute_equal
  252. ##
  253. # :method: wont_include
  254. # See MiniTest::Assertions#refute_includes
  255. ##
  256. # :method: wont_match
  257. # See MiniTest::Assertions#refute_match
  258. ##
  259. # :method: wont_respond_to
  260. # See MiniTest::Assertions#refute_respond_to
  261. end