Class: Spec::Runner::Formatter::BaseTextFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/spec/runner/formatter/base_text_formatter.rb

Overview

Baseclass for text-based formatters. Can in fact be used for non-text based ones too - just ignore the output constructor argument.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseFormatter

#add_behaviour, #example_failed, #example_passed, #example_started, #start, #start_dump

Constructor Details

#initialize(where) ⇒ BaseTextFormatter

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 13

def initialize(where)
  super(where)
  if where.is_a?(String)
    @output = File.open(where, 'w')
  elsif where == STDOUT
    @output = Kernel
    def @output.flush
      STDOUT.flush
    end
  else
    @output = where
  end
  @colour = false
  @dry_run = false
  @snippet_extractor = SnippetExtractor.new
  @pending_examples = []
end

Instance Attribute Details

#dry_run=(value) ⇒ Object (writeonly)

Sets the attribute dry_run

Parameters:

  • value

    the value to set the attribute dry_run to.



8
9
10
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 8

def dry_run=(value)
  @dry_run = value
end

Instance Method Details

#closeObject



91
92
93
94
95
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 91

def close
  if IO === @output
    @output.close 
  end
end

#colour=(colour) ⇒ Object



35
36
37
38
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 35

def colour=(colour)
  @colour = colour
  begin ; require 'Win32/Console/ANSI' if @colour && PLATFORM =~ /win32/ ; rescue LoadError ; raise "You must gem install win32console to use colour on Windows" ; end
end

#colourise(s, failure) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 48

def colourise(s, failure)
  if(failure.expectation_not_met?)
    red(s)
  elsif(failure.pending_fixed?)
    blue(s)
  else
    magenta(s)
  end
end

#dump_failure(counter, failure) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 40

def dump_failure(counter, failure)
  @output.puts
  @output.puts "#{counter.to_s})"
  @output.puts colourise("#{failure.header}\n#{failure.exception.message}", failure)
  @output.puts format_backtrace(failure.exception.backtrace)
  @output.flush
end

#dump_pendingObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 80

def dump_pending
  unless @pending_examples.empty?
    @output.puts
    @output.puts "Pending:"
    @pending_examples.each do |pending_example|
      @output.puts "#{pending_example[0]} (#{pending_example[1]})" 
    end
  end
  @output.flush
end

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 58

def dump_summary(duration, example_count, failure_count, pending_count)
  return if @dry_run
  @output.puts
  @output.puts "Finished in #{duration} seconds"
  @output.puts

  summary = "#{example_count} example#{'s' unless example_count == 1}, #{failure_count} failure#{'s' unless failure_count == 1}"
  summary << ", #{pending_count} pending" if pending_count > 0  

  if failure_count == 0
    if pending_count > 0
      @output.puts yellow(summary)
    else
      @output.puts green(summary)
    end
  else
    @output.puts red(summary)
  end
  @output.flush
  dump_pending
end

#example_pending(behaviour_name, example_name, message) ⇒ Object



31
32
33
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 31

def example_pending(behaviour_name, example_name, message)
  @pending_examples << ["#{behaviour_name} #{example_name}", message]
end

#format_backtrace(backtrace) ⇒ Object



97
98
99
100
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 97

def format_backtrace(backtrace)
  return "" if backtrace.nil?
  backtrace.map { |line| backtrace_line(line) }.join("\n")
end