Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 92 lines (79 sloc) 3.237 kb
f38927e @rkh update contest
rkh authored
1 # Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte
498bad9 @etehtsea Remove trailing whitespaces
etehtsea authored
2 #
f38927e @rkh update contest
rkh authored
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
498bad9 @etehtsea Remove trailing whitespaces
etehtsea authored
9 #
f38927e @rkh update contest
rkh authored
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
498bad9 @etehtsea Remove trailing whitespaces
etehtsea authored
12 #
f38927e @rkh update contest
rkh authored
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
23c2e19 @vipulnsward - Removed test-unit dependency
vipulnsward authored
20 require "rubygems"
21 require "minitest/autorun"
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
22
f38927e @rkh update contest
rkh authored
23 # Contest adds +teardown+, +test+ and +context+ as class methods, and the
24 # instance methods +setup+ and +teardown+ now iterate on the corresponding
25 # blocks. Note that all setup and teardown blocks must be defined with the
26 # block syntax. Adding setup or teardown instance methods defeats the purpose
27 # of this library.
23c2e19 @vipulnsward - Removed test-unit dependency
vipulnsward authored
28 class Minitest::Test
60f7b47 @rkh fix contest on 1.8
rkh authored
29 def self.setup(&block) setup_blocks << block end
30 def self.teardown(&block) teardown_blocks << block end
31 def self.setup_blocks() @setup_blocks ||= [] end
32 def self.teardown_blocks() @teardown_blocks ||= [] end
33
34 def setup_blocks(base = self.class)
35 setup_blocks base.superclass if base.superclass.respond_to? :setup_blocks
36 base.setup_blocks.each do |block|
f38927e @rkh update contest
rkh authored
37 instance_eval(&block)
38 end
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
39 end
40
60f7b47 @rkh fix contest on 1.8
rkh authored
41 def teardown_blocks(base = self.class)
42 teardown_blocks base.superclass if base.superclass.respond_to? :teardown_blocks
43 base.teardown_blocks.each do |block|
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
44 instance_eval(&block)
45 end
46 end
47
60f7b47 @rkh fix contest on 1.8
rkh authored
48 alias setup setup_blocks
49 alias teardown teardown_blocks
50
f38927e @rkh update contest
rkh authored
51 def self.context(*name, &block)
52 subclass = Class.new(self)
53 remove_tests(subclass)
54 subclass.class_eval(&block) if block_given?
55 const_set(context_name(name.join(" ")), subclass)
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
56 end
57
58 def self.test(name, &block)
59 define_method(test_name(name), &block)
60 end
61
62 class << self
63 alias_method :should, :test
64 alias_method :describe, :context
65 end
66
67 private
68
69 def self.context_name(name)
f38927e @rkh update contest
rkh authored
70 # "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
71 name = "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}"
72 name.tr(" ", "_").to_sym
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
73 end
74
75 def self.test_name(name)
3223f19 @rkh avoid test name collision
rkh authored
76 name = "test_#{sanitize_name(name).gsub(/\s+/,'_')}_0"
77 name = name.succ while method_defined? name
78 name.to_sym
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
79 end
80
81 def self.sanitize_name(name)
f38927e @rkh update contest
rkh authored
82 # name.gsub(/\W+/, ' ').strip
83 name.gsub(/\W+/, ' ')
84 end
85
86 def self.remove_tests(subclass)
87 subclass.public_instance_methods.grep(/^test_/).each do |meth|
88 subclass.send(:undef_method, meth.to_sym)
89 end
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
90 end
91 end
Something went wrong with that request. Please try again.