Class: TestGarden

Inherits:
Object
  • Object
show all
Defined in:
lib/test-garden.rb

Overview

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

Defined Under Namespace

Classes: IncompleteTest

Constant Summary collapse

VERSION =
'0.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTestGarden

Returns a new instance of TestGarden.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/test-garden.rb', line 47

def initialize
  @pos = []
  @next = nil
  @did_one_test = false
  @stack = []
  @status = Hash.new(0)
  @enabled = false
  @pattern = params[:pattern]
  @teardowns = []
  @finishing = false
end

Instance Attribute Details

#patternObject (readonly)

Array of regexes that restrict which topics are traversed.



23
24
25
# File 'lib/test-garden.rb', line 23

def pattern
  @pattern
end

#stackObject (readonly)

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



17
18
19
# File 'lib/test-garden.rb', line 17

def stack
  @stack
end

#statusObject (readonly)

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



20
21
22
# File 'lib/test-garden.rb', line 20

def status
  @status
end

#teardownsObject (readonly)

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



26
27
28
# File 'lib/test-garden.rb', line 26

def teardowns
  @teardowns
end

Class Method Details

.params(argv = ARGV) ⇒ Object

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



34
35
36
37
38
39
# File 'lib/test-garden.rb', line 34

def self.params argv=ARGV
  @params ||= {
    :verbose => argv.delete("-v") || argv.delete("--verbose"),
    :pattern => argv.map {|arg| /#{arg}/i}
  }
end

Instance Method Details

#do_teardownsObject



123
124
125
# File 'lib/test-garden.rb', line 123

def do_teardowns
  teardowns.pop.reverse_each {|block| block.call}
end

#enabled?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/test-garden.rb', line 59

def enabled?
  @enabled
end

#handle_test_exceptionsObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/test-garden.rb', line 148

def handle_test_exceptions
  yield

rescue Wrong::Assert::AssertionFailedError => ex
  status[:fail] += 1
  line = nil
  ex.backtrace.reverse_each {|l| break if /wrong\/assert.rb/ =~ l; line = l}
  msg = "F: #{stack.join(": ")}: failed assertion, at #{line}"
  puts msg.color(:yellow), ex.message
  throw :break_test

rescue IncompleteTest => ex
  status[:incomplete] += 1
  if verbose?
    msg = "I: #{stack.join(": ")}"
    msg = msg.color(:white)
    puts msg
  end
  throw :break_test

rescue => ex
  status[:err]  += 1
  bt = []
  ex.backtrace.each {|l| break if /wrong\/assert.rb/ =~ l; bt << l}
  bts = bt.join("\n  from ")
  msg = "E: #{stack.join(": ")}: #{ex} (#{ex.class}), at #{bts}"
  puts msg.color(:red)
  throw :break_test

else
  if enabled?
    if @finishing
      status[:pass] += 1
      puts "P: #{@finishing.join(": ")}" if verbose?
      @finishing = false
    end
  else
    raise
  end
end

#main(topic) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/test-garden.rb', line 189

def main topic
  begin
    nest topic do
      handle_test_exceptions do
        yield
        do_teardowns
      end
    end
    @did_one_test = false
  end while @next
ensure
  print_report
end

#nest(topic) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/test-garden.rb', line 67

def nest topic
  topic = topic.to_s
  @main_topic ||= topic

  if @did_one_test
    if not @next
      @next = @pos.dup
    end
    @pos[-1] += 1 if @pos.length > 0
    return
  end
  
  if @next
    len = [@pos.length, @next.length].min
    if @next[0...len] != @pos[0...len]
      @pos[-1] += 1 if @pos.length > 0
      return
    end
    
    if @next == @pos
      @next = nil
    end
  end
      
  begin
    stack.push topic
    @pos << 0
    teardowns << []
    old_enabled = @enabled
    @enabled = pattern.zip(stack).all? {|pat,subj| !subj or pat === subj}
    if enabled?
      puts "T: #{stack.join(": ")}" if verbose?
      @finishing = false
      catch :break_test do
        yield
        @finishing = stack.dup
      end
    else
      puts "S: #{stack.join(": ")}" if verbose?
      status[:skip] += 1
    end
  
  ensure
    if not @did_one_test
      @did_one_test = true
    else
      @finishing = false
    end

    @enabled = old_enabled
    @pos.pop
    stack.pop
    @pos[-1] += 1 if @pos.length > 0
  end
end

#paramsObject

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



43
44
45
# File 'lib/test-garden.rb', line 43

def params
  @params ||= self.class.params.dup
end


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/test-garden.rb', line 127

def print_report
  ps = "%3d passed" % status[:pass]
  fs = "%3d failed" % status[:fail]
  fs = fs.color(:yellow) if status[:fail] > 0
  ss = "%3d skipped" % status[:skip]
  es = "%3d errors" % status[:err]
  es = es.color(:red) if status[:err] > 0
  report = [ps,fs,ss,es].join(", ")
  
  inc = status[:incomplete]
  if inc > 0
    is = "%3d incomplete" % inc
    is = is.color(:white)
    report << ", #{is}"
  end
  
  line = "#{report} in #{@main_topic}"
  line = line.bright if verbose?
  puts line
end

#verbose?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/test-garden.rb', line 63

def verbose?
  params[:verbose]
end