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
# 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
end

Instance Method Details

#assertion_passedObject



148
149
150
151
# File 'lib/dohtest/group_runner.rb', line 148

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

#caught_error(error, test_name = nil) ⇒ Object



153
154
155
156
# File 'lib/dohtest/group_runner.rb', line 153

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

#create_groupObject



25
26
27
28
29
30
31
32
33
# File 'lib/dohtest/group_runner.rb', line 25

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

#determine_test_methodsObject



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

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



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dohtest/group_runner.rb', line 74

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



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dohtest/group_runner.rb', line 60

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)


116
117
118
# File 'lib/dohtest/group_runner.rb', line 116

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

#runObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/dohtest/group_runner.rb', line 14

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dohtest/group_runner.rb', line 48

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



95
96
97
98
99
# File 'lib/dohtest/group_runner.rb', line 95

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

#run_before_allObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dohtest/group_runner.rb', line 35

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



88
89
90
91
92
93
# File 'lib/dohtest/group_runner.rb', line 88

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

#run_test_methodObject



101
102
103
104
105
106
107
108
# File 'lib/dohtest/group_runner.rb', line 101

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dohtest/group_runner.rb', line 120

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



110
111
112
113
114
# File 'lib/dohtest/group_runner.rb', line 110

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

#total_problemsObject



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

def total_problems
  @error_count + @assertions_failed
end