Class: Easytest::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/easytest/runner.rb

Instance Method Summary collapse

Instance Method Details

#add_case(new_case) ⇒ Object



72
73
74
# File 'lib/easytest/runner.rb', line 72

def add_case(new_case)
  cases << new_case
end

#add_hook(hook) ⇒ Object



80
81
82
# File 'lib/easytest/runner.rb', line 80

def add_hook(hook)
  hooks << hook
end

#casesObject



68
69
70
# File 'lib/easytest/runner.rb', line 68

def cases
  @cases ||= []
end

#hooksObject



76
77
78
# File 'lib/easytest/runner.rb', line 76

def hooks
  @hooks ||= []
end

#runObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/easytest/runner.rb', line 3

def run
  include_only_case = cases.any?(&:only?)
  hooks_by_file = hooks.group_by(&:file)

  cases.group_by(&:file).each do |file, cases_per_file|
    self.file_count += 1

    hooks = hooks_by_file.fetch(file, [])
    before_hooks = hooks.filter(&:before?)
    after_hooks = hooks.filter(&:after?)

    # @type var reports: Array[[Symbol, String]]
    reports = []

    cases_per_file.each do |c|
      if include_only_case && !c.only?
        c.skip!
      end

      result, report =
        begin
          before_hooks.each { |hook| hook.run(c) }
          c.run
        ensure
          after_hooks.each { |hook| hook.run(c) }
        end

      case result
      when :passed
        self.passed_count += 1
      when :failed
        self.failed_count += 1
      when :skipped
        self.skipped_count += 1
      when :todo
        self.todo_count += 1
      else
        raise "Unknown result: #{result.inspect}"
      end

      reports << [result, report] if report
    end

    link = Utils.terminal_hyperlink(file)

    if reports.none? { |result, _| result == :failed }
      puts "#{Rainbow(" PASS ").bright.bg(:green)} #{link}"
    else
      puts "#{Rainbow(" FAIL ").bright.bg(:red)} #{link}"
    end

    print_reports(reports)
  end

  if no_tests?
    print_error_for_no_tests if no_tests_forbidden?
    false
  else
    puts ""
    puts "#{Rainbow('Tests:').bright}  #{summary}"
    puts "#{Rainbow('Time:').bright}   #{elapsed_time.round(5)} seconds"
    all_passed?
  end
end