specky/ruby/specky_formatter.rb
branchvim-stuff
changeset 22 ed72213b1788
parent 21 cd1f3381c1ed
child 23 050dd6dcf346
equal deleted inserted replaced
21:cd1f3381c1ed 22:ed72213b1788
    74 	### Called after all examples are run.  Emit details for each failed example,
    74 	### Called after all examples are run.  Emit details for each failed example,
    75 	### for Vim to fold.
    75 	### for Vim to fold.
    76 	###
    76 	###
    77 	def dump_failures
    77 	def dump_failures
    78 		self.out "\n" unless @failures.empty?
    78 		self.out "\n" unless @failures.empty?
       
    79 		cwd = Regexp.new( Dir.pwd )
       
    80 		bt_regexp = /(.+?):(\d+)(|:\d+)/
    79 
    81 
    80 		@failures.each_with_index do |example, index|
    82 		@failures.each_with_index do |example, index|
    81 			desc      = example.metadata[ :full_description ]
    83 			desc      = example.metadata[ :full_description ]
    82 			exception = example.execution_result[ :exception ]
    84 			exception = example.execution_result[ :exception ]
    83 			file = line = nil
    85 			file = line = nil
    84 
    86 
    85 			if exception.backtrace.first =~ /(.*):(\d+)/
    87 			# remove files that are not in within the cwd.
    86 				file, line = $1, $2.to_i
    88 			#
    87 			end
    89 			# this isn't optimal, but it does stay within specky's notion of
       
    90 			# running it from within the project directory, and makes sure we don't
       
    91 			# inadvertently display code from rspec itself.
       
    92 			#
       
    93 			bt_file = exception.backtrace.find { |line| line =~ bt_regexp && line =~ cwd }
       
    94 			file, line = $1, $2.to_i if bt_file =~ bt_regexp
    88 			self.out "FAILURE - #%d)" % [ index + 1 ]
    95 			self.out "FAILURE - #%d)" % [ index + 1 ]
    89 			self.out "%s:%d" % [ file, line ]
    96 			self.out "%s:%d" % [ file, line ] if bt_file
    90 
    97 
    91 			if RSpec::Core::PendingExampleFixedError === exception
    98 			if exception.respond_to?( :pending_fixed? ) && exception.pending_fixed?
    92 				self.out "%s FIXED" % [ desc ]
    99 				self.out "%s FIXED" % [ desc ]
    93 				self.out "Expected pending '%s' to fail.  No error was raised." % [
   100 				self.out "Expected pending '%s' to fail.  No error was raised." % [
    94 					example.metadata[ :execution_result ][ :pending_message ]
   101 					example.metadata[ :execution_result ][ :pending_message ]
    95 				]
   102 				]
    96 			else
   103 			else
   106 						break
   113 						break
   107 					end
   114 					end
   108 				end
   115 				end
   109 			end
   116 			end
   110 
   117 
   111 			self.out exception_source( file, line ) if file && line
   118 			self.out exception_source( file, line-1 ) if file && line
   112 		end
   119 		end
   113 	end
   120 	end
   114 
   121 
   115 
   122 
   116 	### Emit the source of the exception, with context lines.
   123 	### Emit the source of the exception, with context lines.
   178 		description = metadata[ :description ]
   185 		description = metadata[ :description ]
   179 		return "| %s (%0.3fs" % [ description, duration ]
   186 		return "| %s (%0.3fs" % [ description, duration ]
   180 	end
   187 	end
   181 end # SpeckyFormatter
   188 end # SpeckyFormatter
   182 
   189 
       
   190 
       
   191 ### Identical to the regular SpeckyFormatter, but it puts summary
       
   192 ### information at the bottom of the screen instead of the top, and just
       
   193 ### spits out rudamentary failure info.
       
   194 ###
       
   195 class SpeckyFormatterConsole < SpeckyFormatter
       
   196 	def close
       
   197 		puts "Failures:" unless @failures.empty?
       
   198 		@failures.each do |test|
       
   199 			metadata = test.metadata
       
   200 			msg = "- %s\n  %s\n  %s:%d\n\n" % [
       
   201 				metadata[:full_description],
       
   202 				test.exception.message,
       
   203 				metadata[:file_path],
       
   204 				metadata[:line_number]
       
   205 			]
       
   206 			puts msg
       
   207 		end
       
   208 		output.puts @summary
       
   209 	end
       
   210 end
       
   211