| 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.
| dry_run | [W] |
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 13
13: def initialize(where)
14: super(where)
15: if where.is_a?(String)
16: @output = File.open(where, 'w')
17: elsif where == STDOUT
18: @output = Kernel
19: def @output.flush
20: STDOUT.flush
21: end
22: else
23: @output = where
24: end
25: @colour = false
26: @dry_run = false
27: @snippet_extractor = SnippetExtractor.new
28: @pending_examples = []
29: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 91
91: def close
92: if IO === @output
93: @output.close
94: end
95: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 35
35: def colour=(colour)
36: @colour = colour
37: begin ; require 'Win32/Console/ANSI' if @colour && PLATFORM =~ /win32/ ; rescue LoadError ; raise "You must gem install win32console to use colour on Windows" ; end
38: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 48
48: def colourise(s, failure)
49: if(failure.expectation_not_met?)
50: red(s)
51: elsif(failure.pending_fixed?)
52: blue(s)
53: else
54: magenta(s)
55: end
56: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 40
40: def dump_failure(counter, failure)
41: @output.puts
42: @output.puts "#{counter.to_s})"
43: @output.puts colourise("#{failure.header}\n#{failure.exception.message}", failure)
44: @output.puts format_backtrace(failure.exception.backtrace)
45: @output.flush
46: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 80
80: def dump_pending
81: unless @pending_examples.empty?
82: @output.puts
83: @output.puts "Pending:"
84: @pending_examples.each do |pending_example|
85: @output.puts "#{pending_example[0]} (#{pending_example[1]})"
86: end
87: end
88: @output.flush
89: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 58
58: def dump_summary(duration, example_count, failure_count, pending_count)
59: return if @dry_run
60: @output.puts
61: @output.puts "Finished in #{duration} seconds"
62: @output.puts
63:
64: summary = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
65: summary << ", #{pending_count} pending" if pending_count > 0
66:
67: if failure_count == 0
68: if pending_count > 0
69: @output.puts yellow(summary)
70: else
71: @output.puts green(summary)
72: end
73: else
74: @output.puts red(summary)
75: end
76: @output.flush
77: dump_pending
78: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 31
31: def example_pendingexample_pending(behaviour_name, example_name, message)
32: @pending_examples << ["#{behaviour_name} #{example_name}", message]
33: end
# File lib/spec/runner/formatter/base_text_formatter.rb, line 97
97: def format_backtrace(backtrace)
98: return "" if backtrace.nil?
99: backtrace.map { |line| backtrace_line(line) }.join("\n")
100: 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 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