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

Attributes inherited from BaseFormatter

#example_group, #options, #where

Instance Method Summary collapse

Methods inherited from BaseFormatter

#add_example_group, #example_failed, #example_passed, #example_started, #start, #start_dump

Constructor Details

#initialize(options, 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).



15
16
17
18
19
20
21
22
23
24
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 15

def initialize(options, where)
  super
  if where.is_a?(String)
    FileUtils.mkdir_p(File.dirname(where))
    @output = File.open(where, 'w')
  else
    @output = where
  end
  @pending_examples = []
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



11
12
13
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 11

def output
  @output
end

#pending_examplesObject (readonly)

Returns the value of attribute pending_examples.



11
12
13
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 11

def pending_examples
  @pending_examples
end

Instance Method Details

#closeObject



85
86
87
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 85

def close
  @output.close  if (IO === @output) & (@output != $stdout)
end

#colorize_failure(message, failure) ⇒ Object



38
39
40
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 38

def colorize_failure(message, failure)
  failure.pending_fixed? ? blue(message) : red(message)
end

#colourise(message, failure) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 42

def colourise(message, failure)
  Kernel.warn <<-NOTICE
DEPRECATED: BaseTextFormatter#colourise is deprecated and will be
removed from a future version of RSpec.

Please use colorize_failure instead.
NOTICE
  colorize_failure(message, failure)
end

#dump_failure(counter, failure) ⇒ Object



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

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

#dump_pendingObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 73

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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 52

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
end

#example_pending(example, message, pending_caller) ⇒ Object



26
27
28
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 26

def example_pending(example, message, pending_caller)
  @pending_examples << ["#{@example_group.description} #{example.description}", message, pending_caller]
end

#format_backtrace(backtrace) ⇒ Object



89
90
91
92
# File 'lib/spec/runner/formatter/base_text_formatter.rb', line 89

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