Class: MicroTest::Formatter

Inherits:
BaseFormatter show all
Defined in:
lib/micro_test/formatters/mt.rb,
lib/micro_test/formatters/doc.rb,
lib/micro_test/formatters/min.rb,
lib/micro_test/formatters/dots.rb,
lib/micro_test/formatters/mt_async.rb

Instance Attribute Summary

Attributes inherited from BaseFormatter

#duration, #failed, #passed

Instance Method Summary collapse

Methods inherited from BaseFormatter

#after_class, #after_results, #before_suite, #before_test

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



6
7
8
# File 'lib/micro_test/formatters/doc.rb', line 6

def initialize
  @failures = []
end

Instance Method Details

#after_suite(test_classes) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/micro_test/formatters/mt.rb', line 50

def after_suite(test_classes)
  puts
  puts "".ljust(80, "-")
  print " #{passed + failed} Tests finished in #{yellow duration} seconds. "
  totals = []
  totals << green("#{passed} Passed") if passed > 0
  totals << red("#{failed} Failed") if failed > 0
  print "(#{totals.join(", ")})"
  puts
  puts
end

#after_test(test) ⇒ Object



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
# File 'lib/micro_test/formatters/mt.rb', line 10

def after_test(test)
  duration = (test.duration * 10**4).round.to_f / 10**4
  if duration < 0.01
    print yellow("  #{duration.to_s.ljust(6, "0")}")
  else
    print red("  #{duration.to_s.ljust(6, "0")}")
  end

  if test.passed?
    print green(" #{test.desc}")
  else
    puts red(" #{test.desc}")
    test.failed_asserts.each do |assert|
      puts
      print "".ljust(9)
      puts "#{assert[:file_path]}:#{red(assert[:line_num])}"
      puts "".ljust(9) + "".rjust(71, "-")
      index = assert[:line_num] - 1
      start = index - 2
      start = 0 if start <= 0
      finish = index + 2
      finish = assert[:lines].length - 1 if finish >= assert[:lines].length
      (start..finish).each do |i|
        print "".ljust(9)
        if i == index
          print red((i + 1).to_s.rjust(3, "0"))
          print red("|")
          print red(assert[:lines][i])
        else
          print (i + 1).to_s.rjust(3, "0")
          print "|"
          print assert[:lines][i]
        end
      end
    end
  end

  puts
end

#before_class(test_class) ⇒ Object



5
6
7
8
# File 'lib/micro_test/formatters/mt.rb', line 5

def before_class(test_class)
  puts
  puts test_class.name.ljust(80, "-")
end


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/micro_test/formatters/doc.rb', line 27

def print_failures(failures)
  puts
  puts "Failures:"
  puts
  failures.each_with_index do |failure, idx|
    test = failure[:test]
    assert = failure[:assert]
    puts
    puts "  #{idx + 1}) #{test.test_class.name} #{test.desc}"
    puts red("     Failure/Error: #{assert[:line].strip}")
    puts cyan("     #{assert[:file_path]}:#{assert[:line_num]}")
  end
end