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.

Methods

Attributes

dry_run  [W] 

Public Class methods

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).

[Source]

    # 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:         end

Public Instance methods

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 30
30:         def  colour=colour=(colour)
31:           @colour = colour
32:           begin ; require 'Win32/Console/ANSI' if @colour && PLATFORM =~ /win32/ ; rescue LoadError ; raise "You must gem install win32console to use colour on Windows" ; end
33:               end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 35
35:         def dump_failure(counter, failure)
36:           @output.puts
37:           @output.puts "#{counter.to_s})"
38:           if(failure.expectation_not_met?)
39:             @output.puts red(failure.header)
40:             @output.puts red(failure.exception.message)
41:           else
42:             @output.puts magenta(failure.header)
43:             @output.puts magenta(failure.exception.message)
44:           end
45:           @output.puts format_backtrace(failure.exception.backtrace)
46:           @output.flush
47:         end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 49
49:         def dump_summary(duration, example_count, failure_count, not_implemented_count)
50:           return if @dry_run
51:           @output.puts
52:           @output.puts "Finished in #{duration} seconds"
53:           @output.puts
54: 
55:           summary = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
56:           summary << ", #{not_implemented_count} not implemented" if not_implemented_count > 0  
57: 
58:           if failure_count == 0
59:             if not_implemented_count > 0
60:               @output.puts yellow(summary)
61:             else
62:               @output.puts green(summary)
63:             end
64:           else
65:             @output.puts red(summary)
66:           end
67:           @output.flush
68:         end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 70
70:         def format_backtrace(backtrace)
71:           return "" if backtrace.nil?
72:           backtrace.map { |line| backtrace_line(line) }.join("\n")
73:         end

Protected Instance methods

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 77
77:         def backtrace_line(line)
78:           line.sub(/\A([^:]+:\d+)$/, '\\1:')
79:         end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 81
81:         def colour(text, colour_code)
82:           return text unless @colour && output_to_tty?
83:           "#{colour_code}#{text}\e[0m"
84:         end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 95
95:         def green(text); colour(text, "\e[32m"); end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 96
96:         def magenta(text); colour(text, "\e[35m"); end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 86
86:         def output_to_tty?
87:           begin
88:             @output == Kernel || @output.tty?
89:           rescue NoMethodError
90:             false
91:           end
92:         end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 94
94:         def red(text); colour(text, "\e[31m"); end

[Source]

    # File lib/spec/runner/formatter/base_text_formatter.rb, line 97
97:         def yellow(text); colour(text, "\e[33m"); end

[Validate]