Class: DohTest::StreamOutput

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(ios = nil) ⇒ StreamOutput

Returns a new instance of StreamOutput.



10
11
12
13
14
15
# File 'lib/dohtest/stream_output.rb', line 10

def initialize(ios = nil)
  @error_count = @groups_ran = @groups_skipped = @tests_ran = @tests_skipped = @assertions_failed = @assertions_passed = 0
  @callback_succeeded = true
  @badness = Set.new
  @ios = ios || $stdout
end

Instance Method Details

#assertion_failed(group_name, test_name, failure) ⇒ Object



111
112
113
114
115
# File 'lib/dohtest/stream_output.rb', line 111

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



122
123
124
# File 'lib/dohtest/stream_output.rb', line 122

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

#callback_failed(test_name) ⇒ Object



117
118
119
120
# File 'lib/dohtest/stream_output.rb', line 117

def callback_failed(test_name)
  @callback_succeeded = false
  warn colorize(:error, "callback #{test_name} failed")
end

#group_begin(group_name) ⇒ Object



76
77
# File 'lib/dohtest/stream_output.rb', line 76

def group_begin(group_name)
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dohtest/stream_output.rb', line 79

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
      @ios.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
  if @verbose
    skipped_str = if tests_skipped > 0 then ": #{tests_ran} ran, #{tests_skipped} skipped" else '' end
    @ios.puts "success in #{group_name}: #{total_tests} tests#{skipped_str}; #{total_assertions} assertions" unless @badness.include?(group_name)
  end
end

#run_begin(config) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/dohtest/stream_output.rb', line 17

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

  has_terminal = @ios.tty?
  @no_color = !has_terminal || @config[:no_color]
  @verbose = (has_terminal && !@config[:quiet]) || @config[:verbose]
end

#run_end(duration) ⇒ Object



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
69
70
71
72
73
74
# File 'lib/dohtest/stream_output.rb', line 26

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)
    @ios.puts "\n\ncompleted in #{duration.round(2)}s, #{tests_per_second} tests/s, #{assertions_per_second} assertions/s"
  else
    @ios.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) && @callback_succeeded

  msg = "#{error_str}; #{group_str}; #{test_str}; #{assertion_str}"
  msg = colorize(:success, msg) if success
  @ios.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



98
99
# File 'lib/dohtest/stream_output.rb', line 98

def test_begin(group_name, test_name)
end

#test_end(group_name, test_name) ⇒ Object



101
102
103
# File 'lib/dohtest/stream_output.rb', line 101

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

#test_error(group_name, test_name, error) ⇒ Object



105
106
107
108
109
# File 'lib/dohtest/stream_output.rb', line 105

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