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