Class: DohTest::StreamOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/doh/test/stream_output.rb

Constant Summary collapse

DEFAULT_COLORS =
{:failure => :red, :error => :magenta, :info => :blue, :success => :green}.freeze

Instance Method Summary collapse

Constructor Details

#initializeStreamOutput

Returns a new instance of StreamOutput.



10
11
12
13
# File 'lib/doh/test/stream_output.rb', line 10

def initialize
  @error_count = @groups_ran = @groups_skipped = @tests_ran = @tests_skipped = @assertions_failed = @assertions_passed = 0
  @badness = Set.new
end

Instance Method Details

#assertion_failed(group_name, test_name, failure) ⇒ Object



103
104
105
106
107
# File 'lib/doh/test/stream_output.rb', line 103

def assertion_failed(group_name, test_name, failure)
  @badness.add(group_name)
  @assertions_failed += 1
  display_badness(group_name, test_name, failure)
end

#assertion_passed(group_name, test_name) ⇒ Object



109
110
111
# File 'lib/doh/test/stream_output.rb', line 109

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

#group_begin(group_name) ⇒ Object



70
71
# File 'lib/doh/test/stream_output.rb', line 70

def group_begin(group_name)
end

#group_end(group_name, tests_ran, tests_skipped, assertions_passed, assertions_failed) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/doh/test/stream_output.rb', line 73

def group_end(group_name, tests_ran, tests_skipped, assertions_passed, assertions_failed)
  @tests_skipped += tests_skipped
  if tests_ran == 0
    if tests_skipped > 0
      @groups_skipped += 1
    else
      puts colorize(:info, "no tests defined in #{group_name}")
    end
    return
  end
  @groups_ran += 1
  total_tests = tests_ran + tests_skipped
  total_assertions = assertions_passed + assertions_failed
  skipped_str = if tests_skipped > 0 then ": #{tests_ran} ran, #{tests_skipped} skipped" else '' end
  puts "success in #{group_name}: #{total_tests} tests#{skipped_str}; #{total_assertions} assertions" unless @badness.include?(group_name)
end

#run_begin(config) ⇒ Object



15
16
17
18
# File 'lib/doh/test/stream_output.rb', line 15

def run_begin(config)
  @config = config
  puts "running tests with config: #{config}"
end

#run_end(duration) ⇒ Object



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
67
68
# File 'lib/doh/test/stream_output.rb', line 20

def run_end(duration)
  total_assertions = @assertions_passed + @assertions_failed

  if duration >= 1
    tests_per_second = (@tests_ran / duration).round(2)
    assertions_per_second = (total_assertions / duration).round(2)
    puts "\n\ncompleted in #{duration.round(2)}s, #{tests_per_second} tests/s, #{assertions_per_second} assertions/s"
  else
    puts "\n\ncompleted in #{duration.round(2)}s"
  end

  if @error_count == 0
    error_str = "0 errors"
  else
    error_str = colorize(:error, "#@error_count errors")
  end

  if @groups_skipped == 0
    group_str = "#@groups_ran groups"
  else
    total_groups = @groups_ran + @groups_skipped
    group_str = "#{total_groups} groups: #@groups_ran ran, #@groups_skipped skipped"
  end

  if @tests_skipped == 0
    test_str = "#@tests_ran tests"
  else
    total_tests = @tests_ran + @tests_skipped
    test_str = "#{total_tests} tests: #@tests_ran ran, #@tests_skipped skipped"
  end

  if total_assertions == 0
    assertion_str = colorize(:info, "no assertions run")
  elsif @assertions_failed == 0
    assertion_str = "all #{total_assertions} assertions passed"
  else
    failed_str = colorize(:failure, "#@assertions_failed failed")
    assertion_str = "#{total_assertions} assertions: #@assertions_passed passed, #{failed_str}"
  end

  success = (total_assertions > 0) && (@error_count == 0) && (@assertions_failed == 0)

  msg = "#{error_str}; #{group_str}; #{test_str}; #{assertion_str}"
  msg = colorize(:success, msg) if success
  puts msg

  # this is to generate an exit code; true translates to 0, false to 1
  success
end

#test_begin(group_name, test_name) ⇒ Object



90
91
# File 'lib/doh/test/stream_output.rb', line 90

def test_begin(group_name, test_name)
end

#test_end(group_name, test_name) ⇒ Object



93
94
95
# File 'lib/doh/test/stream_output.rb', line 93

def test_end(group_name, test_name)
  @tests_ran += 1
end

#test_error(group_name, test_name, error) ⇒ Object



97
98
99
100
101
# File 'lib/doh/test/stream_output.rb', line 97

def test_error(group_name, test_name, error)
  @badness.add(group_name)
  @error_count += 1
  display_badness(group_name, test_name, error)
end