|
| 1 | +############################################################ |
| 2 | +# This file is imported from a different project. |
| 3 | +# DO NOT make modifications in this repo. |
| 4 | +# File a patch instead and assign it to Ryan Davis |
| 5 | +############################################################ |
| 6 | + |
| 7 | +#!/usr/bin/ruby -w |
| 8 | + |
| 9 | +require 'mini/test' |
| 10 | + |
| 11 | +class Module |
| 12 | + def infect_with_assertions pos_prefix, neg_prefix, skip_re, map = {} |
| 13 | + Mini::Assertions.public_instance_methods(false).each do |meth| |
| 14 | + meth = meth.to_s |
| 15 | + |
| 16 | + new_name = case meth |
| 17 | + when /^assert/ then |
| 18 | + meth.sub(/^assert/, pos_prefix.to_s) |
| 19 | + when /^refute/ then |
| 20 | + meth.sub(/^refute/, neg_prefix.to_s) |
| 21 | + end |
| 22 | + next unless new_name |
| 23 | + next if new_name =~ skip_re |
| 24 | + |
| 25 | + regexp, replacement = map.find { |re, _| new_name =~ re } |
| 26 | + new_name.sub! regexp, replacement if replacement |
| 27 | + |
| 28 | + # warn "%-22p -> %p %p" % [meth, new_name, regexp] |
| 29 | + self.class_eval <<-EOM |
| 30 | + def #{new_name} *args, &block |
| 31 | + return Mini::Spec.current.#{meth}(*args, &self) if Proc === self |
| 32 | + return Mini::Spec.current.#{meth}(args.first, self) if args.size == 1 |
| 33 | + return Mini::Spec.current.#{meth}(self, *args) |
| 34 | + end |
| 35 | + EOM |
| 36 | + end |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +Object.infect_with_assertions(:must, :wont, |
| 41 | + /^(must|wont)$|wont_(throw)| |
| 42 | + must_(block|not?_|nothing|raise$)/x, |
| 43 | + /(must_throw)s/ => '\1', |
| 44 | + /(?!not)_same/ => '_be_same_as', |
| 45 | + /_in_/ => '_be_within_', |
| 46 | + /_operator/ => '_be', |
| 47 | + /_includes/ => '_include', |
| 48 | + /(must|wont)_(.*_of|nil|empty)/ => '\1_be_\2', |
| 49 | + /must_raises/ => 'must_raise') |
| 50 | + |
| 51 | +class Object |
| 52 | + alias :must_be_close_to :must_be_within_delta |
| 53 | + alias :wont_be_close_to :wont_be_within_delta |
| 54 | +end |
| 55 | + |
| 56 | +module Kernel |
| 57 | + def describe desc, &block |
| 58 | + cls = Class.new(Mini::Spec) |
| 59 | + Object.const_set desc.to_s.split(/\W+/).map { |s| s.capitalize }.join, cls |
| 60 | + |
| 61 | + cls.class_eval(&block) |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +class Mini::Spec < Mini::Test::TestCase |
| 66 | + def self.current |
| 67 | + @@current_spec |
| 68 | + end |
| 69 | + |
| 70 | + def initialize name |
| 71 | + super |
| 72 | + @@current_spec = self |
| 73 | + end |
| 74 | + |
| 75 | + def self.before(type = :each, &block) |
| 76 | + raise "unsupported before type: #{type}" unless type == :each |
| 77 | + define_method :setup, &block |
| 78 | + end |
| 79 | + |
| 80 | + def self.after(type = :each, &block) |
| 81 | + raise "unsupported after type: #{type}" unless type == :each |
| 82 | + define_method :teardown, &block |
| 83 | + end |
| 84 | + |
| 85 | + def self.it desc, &block |
| 86 | + define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &block |
| 87 | + end |
| 88 | +end |
0 commit comments