specky/ruby/specky_formatter.rb
author Mahlon E. Smith <mahlon@martini.nu>
Fri, 10 Feb 2012 16:08:43 -0800
branchvim-stuff
changeset 22 ed72213b1788
parent 21 cd1f3381c1ed
child 23 050dd6dcf346
permissions -rw-r--r--
Updates for rspec 2.8.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
require 'rspec/core/formatters/base_text_formatter'
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
### SpeckyFormatter: A basic RSpec 2.x text formatter, to be used
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
### with the 'Specky' vim plugin (or from the command line, if you
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
### dig it over the default 'documentation' format!)
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
### rspec -r /path/to/this/specky_formatter.rb -f SpeckyFormatter specs
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
class SpeckyFormatter < RSpec::Core::Formatters::BaseTextFormatter
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
	def initialize( *args )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
		super
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
		@indent_level  = 0
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
		@failure_index = 0
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
		@failures      = []
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
    17
		@txt           = ''
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
    18
		@summary       = ''
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
    21
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
	########################################################################
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
	### R S P E C  H O O K S
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
	########################################################################
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
	### Example group hook -- increase indentation, emit description
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
	def example_group_started( example_group )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
		self.out '+', '-' * (example_group.description.length + 2), '+'
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
		self.out '| ', example_group.description, ' |'
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
		self.out '+', '-' * (example_group.description.length + 2), '+'
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
		@indent_level += 1
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
	### Example group hook -- decrease indentation
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
	def example_group_finished( example_group )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
		@indent_level -= 1
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
	### Called on example success
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
	def example_passed( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
		msg = self.format_example( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
		msg << ')'
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
		self.out msg
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
	### Called on a pending example
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
	def example_pending( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
		msg = self.format_example( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
		pending_msg = example.metadata[ :execution_result ][ :pending_message ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
		msg << ", PENDING%s)" % [ ": #{pending_msg}" || '' ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
		self.out msg
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
	### Called on example failure
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
	def example_failed( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
		@failure_index += 1
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
		msg = self.format_example( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
		msg << ", FAILED - #%d)" % [ @failure_index ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
		self.out msg
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
		@failures << example
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
	### Called after all examples are run.  Emit details for each failed example,
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
	### for Vim to fold.
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
	def dump_failures
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
    78
		self.out "\n" unless @failures.empty?
22
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    79
		cwd = Regexp.new( Dir.pwd )
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    80
		bt_regexp = /(.+?):(\d+)(|:\d+)/
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
		@failures.each_with_index do |example, index|
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
			desc      = example.metadata[ :full_description ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
			exception = example.execution_result[ :exception ]
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
    85
			file = line = nil
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
22
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    87
			# remove files that are not in within the cwd.
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    88
			#
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    89
			# this isn't optimal, but it does stay within specky's notion of
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    90
			# running it from within the project directory, and makes sure we don't
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    91
			# inadvertently display code from rspec itself.
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    92
			#
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    93
			bt_file = exception.backtrace.find { |line| line =~ bt_regexp && line =~ cwd }
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    94
			file, line = $1, $2.to_i if bt_file =~ bt_regexp
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
			self.out "FAILURE - #%d)" % [ index + 1 ]
22
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    96
			self.out "%s:%d" % [ file, line ] if bt_file
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
22
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
    98
			if exception.respond_to?( :pending_fixed? ) && exception.pending_fixed?
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
				self.out "%s FIXED" % [ desc ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
				self.out "Expected pending '%s' to fail.  No error was raised." % [
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
					example.metadata[ :execution_result ][ :pending_message ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
				]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
			else
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
				self.out desc
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
				self.out "Failure/Error: %s" %  [ read_failed_line( exception, example).strip ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
				exception.message.split("\n").each {|l| self.out l}
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
				# logic taken from the base class
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
				example.example_group.ancestors.push(example.example_group).each do |group|
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
					if group.metadata[:shared_group_name]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
						self.out "Shared Example Group: \"#{group.metadata[:shared_group_name]}\" called from " +
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
							"#{backtrace_line(group.metadata[:example_group][:location])}"
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
						break
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
					end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
				end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
			end
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   117
22
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   118
			self.out exception_source( file, line-1 ) if file && line
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
		end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   123
	### Emit the source of the exception, with context lines.
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   124
	###
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   125
	def exception_source( file, line )
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   126
		context = ''
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   127
		low, high = line - 3, line + 3
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   128
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   129
		File.open( file ).each_with_index do |cline, i|
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   130
			cline.chomp!.rstrip!
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   131
			next unless i >= low && i <= high
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   132
			context << "  %s%4d: %s\n" % [ ( i == line ? '>>' : ' |' ), i, cline ]
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   133
		end
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   134
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   135
		return context
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   136
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   137
	rescue
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   138
		'Unable to parse exception context lines.'
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   139
	end
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   140
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   141
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
	### Emit summary data for all examples.
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
	def dump_summary( duration, example_count, failure_count, pending_count )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
		succeeded = example_count - failure_count - pending_count
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   146
		@summary << "+%s+\n" % [ '-' * 49 ]
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   147
		@summary << "|%s-- Summary --%s|\n" % [ ' ' * 18, ' ' * 18 ]
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   148
		@summary << "+----------+-----------+--------+---------+-------+\n"
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   149
		@summary << "| Duration | Succeeded | Failed | Pending | Total |\n"
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   150
		@summary << "+----------+-----------+--------+---------+-------+\n"
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   152
		@summary << "| %7ss | %9s | %6s | %7s | %5s |\n" % [
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
			"%0.3f" % duration, succeeded, failure_count,
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
			pending_count, example_count
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
		]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   157
		@summary << "+----------+-----------+--------+---------+-------+\n\n"
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   158
	end
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   159
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   160
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   161
	### End of run.  Dump it all out!
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   162
	###
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   163
	def close
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   164
		output.puts @summary
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   165
		output.puts @txt
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
	#########
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
	protected
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
	#########
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
	### Send a string to the output IO object, after indentation.
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
	def out( *msg )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
		msg = msg.join
21
cd1f3381c1ed Emit file and line for failure source (use gF to jump straight to it!).
Mahlon E. Smith <mahlon@martini.nu>
parents: 19
diff changeset
   177
		@txt << "%s%s\n" % [ '  ' * @indent_level, msg ]
19
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
	### Format the basic example information, along with the run duration.
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
	###
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
	def format_example( example )
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
		metadata    = example.metadata
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
		duration    = metadata[ :execution_result ][ :run_time ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
		description = metadata[ :description ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
		return "| %s (%0.3fs" % [ description, duration ]
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
	end
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
end # SpeckyFormatter
763cef799c74 Specky: support rspec 2.x by default.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
22
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   190
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   191
### Identical to the regular SpeckyFormatter, but it puts summary
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   192
### information at the bottom of the screen instead of the top, and just
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   193
### spits out rudamentary failure info.
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   194
###
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   195
class SpeckyFormatterConsole < SpeckyFormatter
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   196
	def close
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   197
		puts "Failures:" unless @failures.empty?
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   198
		@failures.each do |test|
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   199
			metadata = test.metadata
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   200
			msg = "- %s\n  %s\n  %s:%d\n\n" % [
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   201
				metadata[:full_description],
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   202
				test.exception.message,
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   203
				metadata[:file_path],
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   204
				metadata[:line_number]
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   205
			]
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   206
			puts msg
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   207
		end
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   208
		output.puts @summary
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   209
	end
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   210
end
ed72213b1788 Updates for rspec 2.8.
Mahlon E. Smith <mahlon@martini.nu>
parents: 21
diff changeset
   211