Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 65 lines (53 sloc) 1.584 kb
ff0d068 Simon Rozet Use contest instead of test/spec/mini
sr authored
1 require "test/unit"
2
3 # Test::Unit loads a default test if the suite is empty, and the only
4 # purpose of that test is to fail. As having empty contexts is a common
5 # practice, we decided to overwrite TestSuite#empty? in order to
6 # allow them. Having a failure when no tests have been defined seems
7 # counter-intuitive.
8 class Test::Unit::TestSuite
da37a88 Tim Carey-Smith Less warnings to sate zenspider's needs
halorgium authored
9 unless method_defined?(:empty?)
10 def empty?
11 false
12 end
ff0d068 Simon Rozet Use contest instead of test/spec/mini
sr authored
13 end
14 end
15
16 # We added setup, test and context as class methods, and the instance
17 # method setup now iterates on the setup blocks. Note that all setup
18 # blocks must be defined with the block syntax. Adding a setup instance
19 # method defeats the purpose of this library.
20 class Test::Unit::TestCase
21 def self.setup(&block)
22 setup_blocks << block
23 end
24
25 def setup
26 self.class.setup_blocks.each do |block|
27 instance_eval(&block)
28 end
29 end
30
31 def self.context(name, &block)
32 subclass = Class.new(self.superclass)
33 subclass.setup_blocks.unshift(*setup_blocks)
34 subclass.class_eval(&block)
35 const_set(context_name(name), subclass)
36 end
37
38 def self.test(name, &block)
39 define_method(test_name(name), &block)
40 end
41
42 class << self
43 alias_method :should, :test
44 alias_method :describe, :context
45 end
46
47 private
48
49 def self.setup_blocks
50 @setup_blocks ||= []
51 end
52
53 def self.context_name(name)
54 "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
55 end
56
57 def self.test_name(name)
58 "test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
59 end
60
61 def self.sanitize_name(name)
62 name.gsub(/\W+/, ' ').strip
63 end
64 end
Something went wrong with that request. Please try again.