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(std_ios = nil, err_ios = nil) ⇒ StreamOutput

Returns a new instance of StreamOutput.



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

def initialize(std_ios = nil, err_ios = nil)
  @error_count = @groups_ran = @groups_skipped = @tests_ran = @tests_skipped = @assertions_failed = @assertions_passed = 0
  @callbacks_succeeded = true
  @badness = Set.new
  @std_ios = std_ios || $stdout
  @err_ios = err_ios || $stderr
end

Instance Method Details

#assertion_failed(group_name, test_name, failure) ⇒ Object



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

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



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

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

#callback_failed(proc_name) ⇒ Object



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

def callback_failed(proc_name)
  @callbacks_succeeded = false
  @err_ios.puts colorize(:error, "callback #{proc_name} failed")
end

#group_begin(group_name) ⇒ Object



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

def group_begin(group_name)
end

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



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

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
      @std_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
    @std_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



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

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

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

#run_end(duration) ⇒ Object



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
75
# File 'lib/dohtest/stream_output.rb', line 27

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

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



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

def test_begin(group_name, test_name)
end

#test_end(group_name, test_name) ⇒ Object



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

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

#test_error(group_name, test_name, error) ⇒ Object



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

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