class TestGarden

Not directly instantiated by the user. See README and examples.

Constants

VERSION

Attributes

pattern[R]

Array of regexes that restrict which topics are traversed.

stack[R]

Array of nested topics in descending order from the main topic to the topic of the current test.

status[R]

Hash of counters for pass, fail, skip, error cases.

teardowns[R]

Stack of arrays of procs that will be called to tear down the current setup.

Public Class Methods

new() click to toggle source
   # File lib/test-garden.rb
47 def initialize
48   @pos = []
49   @next = nil
50   @did_one_test = false
51   @stack = []
52   @status = Hash.new(0)
53   @enabled = false
54   @pattern = params[:pattern]
55   @teardowns = []
56   @finishing = false
57 end
params(argv=ARGV) click to toggle source

Reads params from command line, or from given array of strings. If passing an array, you should call this method before all tests.

   # File lib/test-garden.rb
34 def self.params argv=ARGV
35   @params ||= {
36     :verbose => argv.delete("-v") || argv.delete("--verbose"),
37     :pattern => argv.map {|arg| /#{arg}/i}
38   }
39 end

Public Instance Methods

do_teardowns() click to toggle source
    # File lib/test-garden.rb
123 def do_teardowns
124   teardowns.pop.reverse_each {|block| block.call}
125 end
enabled?() click to toggle source
   # File lib/test-garden.rb
59 def enabled?
60   @enabled
61 end
handle_test_exceptions() { || ... } click to toggle source
    # File lib/test-garden.rb
148 def handle_test_exceptions
149   yield
150 
151 rescue Wrong::Assert::AssertionFailedError => ex
152   status[:fail] += 1
153   line = nil
154   ex.backtrace.reverse_each {|l| break if /wrong\/assert.rb/ =~ l; line = l}
155   msg = "F: #{stack.join(": ")}: failed assertion, at #{line}"
156   puts msg.color(:yellow), ex.message
157   throw :break_test
158 
159 rescue IncompleteTest => ex
160   status[:incomplete] += 1
161   if verbose?
162     msg = "I: #{stack.join(": ")}"
163     msg = msg.color(:white)
164     puts msg
165   end
166   throw :break_test
167 
168 rescue => ex
169   status[:err]  += 1
170   bt = []
171   ex.backtrace.each {|l| break if /wrong\/assert.rb/ =~ l; bt << l}
172   bts = bt.join("\n  from ")
173   msg = "E: #{stack.join(": ")}: #{ex} (#{ex.class}), at #{bts}"
174   puts msg.color(:red)
175   throw :break_test
176 
177 else
178   if enabled?
179     if @finishing
180       status[:pass] += 1
181       puts "P: #{@finishing.join(": ")}" if verbose?
182       @finishing = false
183     end
184   else
185     raise
186   end
187 end
main(topic) { || ... } click to toggle source
    # File lib/test-garden.rb
189 def main topic
190   begin
191     nest topic do
192       handle_test_exceptions do
193         yield
194         do_teardowns
195       end
196     end
197     @did_one_test = false
198   end while @next
199 ensure
200   print_report
201 end
nest(topic) { || ... } click to toggle source
    # File lib/test-garden.rb
 67 def nest topic
 68   topic = topic.to_s
 69   @main_topic ||= topic
 70 
 71   if @did_one_test
 72     if not @next
 73       @next = @pos.dup
 74     end
 75     @pos[-1] += 1 if @pos.length > 0
 76     return
 77   end
 78   
 79   if @next
 80     len = [@pos.length, @next.length].min
 81     if @next[0...len] != @pos[0...len]
 82       @pos[-1] += 1 if @pos.length > 0
 83       return
 84     end
 85     
 86     if @next == @pos
 87       @next = nil
 88     end
 89   end
 90       
 91   begin
 92     stack.push topic
 93     @pos << 0
 94     teardowns << []
 95     old_enabled = @enabled
 96     @enabled = pattern.zip(stack).all? {|pat,subj| !subj or pat === subj}
 97     if enabled?
 98       puts "T: #{stack.join(": ")}" if verbose?
 99       @finishing = false
100       catch :break_test do
101         yield
102         @finishing = stack.dup
103       end
104     else
105       puts "S: #{stack.join(": ")}" if verbose?
106       status[:skip] += 1
107     end
108   
109   ensure
110     if not @did_one_test
111       @did_one_test = true
112     else
113       @finishing = false
114     end
115 
116     @enabled = old_enabled
117     @pos.pop
118     stack.pop
119     @pos[-1] += 1 if @pos.length > 0
120   end
121 end
params() click to toggle source

By default, share params for all TestGardens, but allow per-instance variation by modifying the params hash.

   # File lib/test-garden.rb
43 def params
44   @params ||= self.class.params.dup
45 end
print_report() click to toggle source
verbose?() click to toggle source
   # File lib/test-garden.rb
63 def verbose?
64   params[:verbose]
65 end