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.



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

def initialize(group_class, output, config = nil)
  @group_class,@output = group_class,output
  @config = 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 = nil
  @max_failures = nil
  @has_brink = false
end

Instance Method Details

#assertion_passedObject



156
157
158
159
# File 'lib/dohtest/group_runner.rb', line 156

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

#caught_error(error, test_name = nil) ⇒ Object



161
162
163
164
# File 'lib/dohtest/group_runner.rb', line 161

def caught_error(error, test_name = nil)
  @error_count += 1
  @output.test_error(@group_name, test_name || @test_name, error)
end

#create_groupObject



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

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

#determine_test_methodsObject



147
148
149
150
151
152
153
154
# File 'lib/dohtest/group_runner.rb', line 147

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



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

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



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

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

#past_brink?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/dohtest/group_runner.rb', line 124

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

#runObject



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

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



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

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

#run_after_eachObject



99
100
101
102
103
# File 'lib/dohtest/group_runner.rb', line 99

def run_after_each
  @group.send(@after_each_method)
rescue => error
  caught_error(error)
end

#run_before_allObject



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

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

#run_before_eachObject



92
93
94
95
96
97
# File 'lib/dohtest/group_runner.rb', line 92

def run_before_each
  @group.send(@before_each_method)
rescue => error
  @before_each_failed = true
  caught_error(error)
end

#run_test_methodObject



105
106
107
108
109
110
111
112
# File 'lib/dohtest/group_runner.rb', line 105

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

#run_testsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dohtest/group_runner.rb', line 128

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?
    @test_name = method_name
    @before_each_failed = false
    @output.test_begin(@group_name, @test_name)
    run_before_each if @before_each_method
    run_test_method unless @before_each_failed
    run_after_each if @after_each_method
    @tests_ran += 1
    @output.test_end(@group_name, @test_name)
  end
end

#setup_brinkObject



114
115
116
117
118
119
120
121
122
# File 'lib/dohtest/group_runner.rb', line 114

def setup_brink
  if @config.key?(:max_errors) then
    @max_errors = @config[:max_errors].to_i
  end
  if @config.key?(:max_failures) then
    @max_failures = @config[:max_failures].to_i
  end
  @has_brink = @max_errors || @max_failures
end

#total_problemsObject



166
167
168
# File 'lib/dohtest/group_runner.rb', line 166

def total_problems
  @error_count + @assertions_failed
end