Tag unstable CGI specs.
[rbx.git] / lib / mini / spec.rb
blob0609fbe8a613791c77ac2ec0adb3995d690314e4
1 #!/usr/bin/ruby -w
3 require 'mini/test'
5 class Module
6   def infect_with_assertions pos_prefix, neg_prefix, skip_re, map = {}
7     Mini::Assertions.public_instance_methods(false).each do |meth|
8       meth = meth.to_s
10       new_name = case meth
11                  when /^assert/ then
12                    meth.sub(/^assert/, pos_prefix.to_s)
13                  when /^refute/ then
14                    meth.sub(/^refute/, neg_prefix.to_s)
15                  end
16       next unless new_name
17       next if new_name =~ skip_re
19       regexp, replacement = map.find { |re, _| new_name =~ re }
20       new_name.sub! regexp, replacement if replacement
22       # warn "%-22p -> %p %p" % [meth, new_name, regexp]
23       self.class_eval <<-EOM
24         def #{new_name} *args, &block
25           return Mini::Spec.current.#{meth}(*args, &self)     if Proc === self
26           return Mini::Spec.current.#{meth}(args.first, self) if args.size == 1
27           return Mini::Spec.current.#{meth}(self, *args)
28         end
29       EOM
30     end
31   end
32 end
34 Object.infect_with_assertions(:must, :wont,
35                               /^(must|wont)$|wont_(throw)|
36                                  must_(block|not?_|nothing|raise$)/x,
37                               /(must_throw)s/                 => '\1',
38                               /(?!not)_same/                  => '_be_same_as',
39                               /_in_/                          => '_be_within_',
40                               /_operator/                     => '_be',
41                               /_includes/                     => '_include',
42                               /(must|wont)_(.*_of|nil|empty)/ => '\1_be_\2',
43                               /must_raises/                   => 'must_raise')
45 class Object
46   alias :must_be_close_to :must_be_within_delta
47   alias :wont_be_close_to :wont_be_within_delta
48 end
50 module Kernel
51   def describe desc, &block
52     cls = Class.new(Mini::Spec)
53     Object.const_set desc.to_s.split(/\W+/).map { |s| s.capitalize }.join, cls
55     cls.class_eval(&block)
56   end
57 end
59 class Mini::Spec < Mini::Test::TestCase
60   def self.current
61     @@current_spec
62   end
64   def initialize name
65     super
66     @@current_spec = self
67   end
69   def self.before(type = :each, &block)
70     raise "unsupported before type: #{type}" unless type == :each
71     define_method :setup, &block
72   end
74   def self.after(type = :each, &block)
75     raise "unsupported after type: #{type}" unless type == :each
76     define_method :teardown, &block
77   end
79   def self.it desc, &block
80     define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &block
81   end
82 end