Class: DohTest::GroupRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/dohtest/group_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(group_class, output, config = nil) ⇒ GroupRunner

Returns a new instance of GroupRunner.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dohtest/group_runner.rb', line 7

def initialize(group_class, output, config = nil)
  @group_class,@output = group_class,output
  @config = config || DohTest.config
  @group_name = @group_class.to_s
  @before_all_failed = false
  @error_count = @tests_ran = @tests_skipped = @assertions_failed = @assertions_passed = 0
  @test_name = 'no_test_name_set_yet'
  @max_errors = 0
  @max_failures = 0
  @has_brink = false
end

Instance Method Details

#assertion_passedObject



177
178
179
180
# File 'lib/dohtest/group_runner.rb', line 177

def assertion_passed
  @assertions_passed += 1
  @output.assertion_passed(@group_name, @test_name)
end

#caught_error(error, test_name = nil) ⇒ Object



182
183
184
185
# File 'lib/dohtest/group_runner.rb', line 182

def caught_error(error, test_name = nil)
  @error_count += 1
  @output.test_error(@group_name, test_name || @test_name, error, @config[:dynamic_seed])
end

#create_groupObject



30
31
32
33
34
35
36
37
38
# File 'lib/dohtest/group_runner.rb', line 30

def create_group
  @group = @group_class.new
rescue => error
  caught_error(error, 'initialize')
  false
else
  @group.runner = self
  true
end

#determine_test_methodsObject



168
169
170
171
172
173
174
175
# File 'lib/dohtest/group_runner.rb', line 168

def determine_test_methods
  @test_methods = @group_class.public_instance_methods.grep(/^test/)
  return unless @config.key?(:grep)
  original_test_count = @test_methods.size
  grep_filter = Regexp.new(@config[:grep])
  @test_methods.select! { |method| method.to_s =~ grep_filter }
  @tests_skipped = original_test_count - @test_methods.size
end

#find_after_each_methodObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dohtest/group_runner.rb', line 79

def find_after_each_method
  has_after_each = @group.respond_to?(:after_each)
  has_teardown = @group.respond_to?(:teardown)
  if has_after_each && has_teardown
    raise ":after_each and :teardown both defined; please pick one"
  elsif has_after_each
    @after_each_method = :after_each
  elsif has_teardown
    @after_each_method = :teardown
  else
    @after_each_method = nil
  end
end

#find_before_each_methodObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dohtest/group_runner.rb', line 65

def find_before_each_method
  has_before_each = @group.respond_to?(:before_each)
  has_setup = @group.respond_to?(:setup)
  if has_before_each && has_setup
    raise ":before_each and :setup both defined; please pick one"
  elsif has_before_each
    @before_each_method = :before_each
  elsif has_setup
    @before_each_method = :setup
  else
    @before_each_method = nil
  end
end

#generate_new_seedObject



139
140
141
142
143
144
145
146
# File 'lib/dohtest/group_runner.rb', line 139

def generate_new_seed
  if @config[:dynamic_seed]
    @config[:dynamic_seed] = rand(100000000000)
    srand(@config[:dynamic_seed])
  else
    @config[:dynamic_seed] = @config[:seed]
  end
end

#past_brink?Boolean

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/dohtest/group_runner.rb', line 134

def past_brink?
  ((@max_errors > 0) && (@error_count >= @max_errors)) ||
  ((@max_failures > 0) && (@assertions_failed >= @max_failures))
end

#runObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/dohtest/group_runner.rb', line 19

def run
  @output.group_begin(@group_name)
  if create_group
    run_before_all
    run_tests unless @before_all_failed
    run_after_all
  end
  @output.group_end(@group_name, @tests_ran, @tests_skipped, @assertions_passed, @assertions_failed)
  past_brink?
end

#run_after_allObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dohtest/group_runner.rb', line 53

def run_after_all
  @group.after_all if @group.respond_to?(:after_all)
  @config[:post_group_callback].each do |callback|
    if (!callback.call(total_problems))
      @error_count += 1
      @output.callback_failed(callback.inspect)
    end
  end
rescue => error
  caught_error(error, 'after_all')
end

#run_after_eachObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dohtest/group_runner.rb', line 106

def run_after_each
  @group.send(@after_each_method) if @after_each_method
  @config[:post_each_callback].each do |callback|
    if !callback.call
      @error_count += 1
      @output.callback_failed(callback.inspect)
    end
  end
rescue => error
  caught_error(error)
end

#run_before_allObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dohtest/group_runner.rb', line 40

def run_before_all
  @group.before_all if @group.respond_to?(:before_all)
  @config[:pre_group_callback].each do |callback|
    if (!callback.call(@group))
      @error_count += 1
      @output.callback_failed(callback.inspect)
    end
  end
rescue => error
  @before_all_failed = true
  caught_error(error, 'before_all')
end

#run_before_eachObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dohtest/group_runner.rb', line 93

def run_before_each
  @config[:pre_each_callback].each do |callback|
    if !callback.call
      @error_count += 1
      @output.callback_failed(callback.inspect)
    end
  end
  @group.send(@before_each_method) if @before_each_method
rescue => error
  @before_each_failed = true
  caught_error(error)
end

#run_test_methodObject



118
119
120
121
122
123
124
125
# File 'lib/dohtest/group_runner.rb', line 118

def run_test_method
  @group.send(@test_name)
rescue DohTest::Failure => failure
  @assertions_failed += 1
  @output.assertion_failed(@group_name, @test_name, failure, @config[:dynamic_seed])
rescue => error
  caught_error(error)
end

#run_testsObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dohtest/group_runner.rb', line 148

def run_tests
  determine_test_methods
  find_before_each_method
  find_after_each_method
  setup_brink

  @test_methods.each do |method_name|
    break if @has_brink && past_brink?
    generate_new_seed
    @test_name = method_name
    @before_each_failed = false
    @output.test_begin(@group_name, @test_name)
    run_before_each
    run_test_method unless @before_each_failed
    run_after_each
    @tests_ran += 1
    @output.test_end(@group_name, @test_name)
  end
end

#setup_brinkObject



127
128
129
130
131
132
# File 'lib/dohtest/group_runner.rb', line 127

def setup_brink
  @max_errors = @config[:max_errors].to_i
  @max_failures = @config[:max_failures].to_i

  @has_brink = (@max_errors > 0) || (@max_failures > 0)
end

#total_problemsObject



187
188
189
# File 'lib/dohtest/group_runner.rb', line 187

def total_problems
  @error_count + @assertions_failed
end