| Class | Spec::Runner::Formatter::BaseTextFormatter |
| In: |
lib/spec/runner/formatter/base_text_formatter.rb
|
| Parent: | BaseFormatter |
Baseclass for text-based formatters. Can in fact be used for non-text based ones too - just ignore the output constructor argument.
| output | [R] | |
| pending_examples | [R] |
Creates a new instance that will write to where. If where is a String, output will be written to the File with that name, otherwise where is exected to be an IO (or an object that responds to puts and write).
# File lib/spec/runner/formatter/base_text_formatter.rb, line 14
14: def initialize(options, where)
15: super
16: if where.is_a?(String)
17: @output = File.open(where, 'w')
18: elsif where == STDOUT
19: @output = Kernel
20: def @output.flush
21: STDOUT.flush
22: end
23: else
24: @output = where
25: end
26: @pending_examples = []
27: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 83
83: def close
84: if IO === @output
85: @output.close
86: end
87: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 41
41: def colourise(s, failure)
42: if(failure.expectation_not_met?)
43: red(s)
44: elsif(failure.pending_fixed?)
45: blue(s)
46: else
47: magenta(s)
48: end
49: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 33
33: def dump_failure(counter, failure)
34: @output.puts
35: @output.puts "#{counter.to_s})"
36: @output.puts colourise("#{failure.header}\n#{failure.exception.message}", failure)
37: @output.puts format_backtrace(failure.exception.backtrace)
38: @output.flush
39: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 72
72: def dump_pending
73: unless @pending_examples.empty?
74: @output.puts
75: @output.puts "Pending:"
76: @pending_examples.each do |pending_example|
77: @output.puts "#{pending_example[0]} (#{pending_example[1]})"
78: end
79: end
80: @output.flush
81: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 51
51: def dump_summary(duration, example_count, failure_count, pending_count)
52: return if dry_run?
53: @output.puts
54: @output.puts "Finished in #{duration} seconds"
55: @output.puts
56:
57: summary = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
58: summary << ", #{pending_count} pending" if pending_count > 0
59:
60: if failure_count == 0
61: if pending_count > 0
62: @output.puts yellow(summary)
63: else
64: @output.puts green(summary)
65: end
66: else
67: @output.puts red(summary)
68: end
69: @output.flush
70: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 29
29: def example_pendingexample_pending(example, message)
30: @pending_examples << [example.__full_description, message]
31: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 89
89: def format_backtrace(backtrace)
90: return "" if backtrace.nil?
91: backtrace.map { |line| backtrace_line(line) }.join("\n")
92: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 104
104: def backtrace_line(line)
105: line.sub(/\A([^:]+:\d+)$/, '\\1:')
106: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 125
125: def blue(text); colour(text, "\e[34m"); end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 108
108: def colour(text, colour_code)
109: return text unless colour? && output_to_tty?
110: "#{colour_code}#{text}\e[0m"
111: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 96
96: def colour?
97: @options.colour ? true : false
98: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 100
100: def dry_run?
101: @options.dry_run ? true : false
102: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 121
121: def green(text); colour(text, "\e[32m"); end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 123
123: def magenta(text); colour(text, "\e[35m"); end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 113
113: def output_to_tty?
114: begin
115: @output == Kernel || @output.tty?
116: rescue NoMethodError
117: false
118: end
119: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 122
122: def red(text); colour(text, "\e[31m"); end