]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/runner/formatter/story/plain_text_formatter.rb
424e27cc67e403229a5e940a2e790acc26927066
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / runner / formatter / story / plain_text_formatter.rb
1 require 'spec/runner/formatter/base_text_formatter'
2
3 module Spec
4   module Runner
5     module Formatter
6       module Story
7         class PlainTextFormatter < BaseTextFormatter
8           def initialize(options, where)
9             super
10             @successful_scenario_count = 0
11             @pending_scenario_count = 0
12             @failed_scenarios = []
13             @pending_steps = []
14             @previous_type = nil
15           end
16         
17           def run_started(count)
18             @count = count
19             @output.puts "Running #@count scenarios\n\n"
20           end
21
22           def story_started(title, narrative)
23             @current_story_title = title
24             @output.puts "Story: #{title}\n\n"
25             narrative.each_line do |line|
26               @output.print "  "
27               @output.print line
28             end
29           end
30         
31           def story_ended(title, narrative)
32             @output.puts
33             @output.puts
34           end
35
36           def scenario_started(story_title, scenario_name)
37             @current_scenario_name = scenario_name
38             @scenario_already_failed = false
39             @output.print "\n\n  Scenario: #{scenario_name}"
40             @scenario_ok = true
41           end
42         
43           def scenario_succeeded(story_title, scenario_name)
44             @successful_scenario_count += 1
45           end
46         
47           def scenario_failed(story_title, scenario_name, err)
48             @options.backtrace_tweaker.tweak_backtrace(err)
49             @failed_scenarios << [story_title, scenario_name, err] unless @scenario_already_failed
50             @scenario_already_failed = true
51           end
52         
53           def scenario_pending(story_title, scenario_name, msg)
54             @pending_scenario_count += 1 unless @scenario_already_failed
55             @scenario_already_failed = true
56           end
57         
58           def run_ended
59             @output.puts "#@count scenarios: #@successful_scenario_count succeeded, #{@failed_scenarios.size} failed, #@pending_scenario_count pending"
60             unless @pending_steps.empty?
61               @output.puts "\nPending Steps:"
62               @pending_steps.each_with_index do |pending, i|
63                 story_name, scenario_name, msg = pending
64                 @output.puts "#{i+1}) #{story_name} (#{scenario_name}): #{msg}"
65               end
66             end
67             unless @failed_scenarios.empty?
68               @output.print "\nFAILURES:"
69               @failed_scenarios.each_with_index do |failure, i|
70                 title, scenario_name, err = failure
71                 @output.print %[
72     #{i+1}) #{title} (#{scenario_name}) FAILED
73     #{err.class}: #{err.message}
74     #{err.backtrace.join("\n")}
75 ]
76               end
77             end            
78           end
79         
80           def step_succeeded(type, description, *args)
81             found_step(type, description, false, *args)
82           end
83         
84           def step_pending(type, description, *args)
85             found_step(type, description, false, *args)
86             @pending_steps << [@current_story_title, @current_scenario_name, description]
87             @output.print " (PENDING)"
88             @scenario_ok = false
89           end
90         
91           def step_failed(type, description, *args)
92             found_step(type, description, true, *args)
93             @output.print red(@scenario_ok ? " (FAILED)" : " (SKIPPED)")
94             @scenario_ok = false
95           end
96           
97           def collected_steps(steps)
98           end
99           
100           def method_missing(sym, *args, &block) #:nodoc:
101             # noop - ignore unknown messages
102           end
103
104         private
105
106           def found_step(type, description, failed, *args)
107             desc_string = description.step_name
108             arg_regexp = description.arg_regexp
109             text = if(type == @previous_type)
110               "\n    And "
111             else
112               "\n\n    #{type.to_s.capitalize} "
113             end
114             i = -1
115             text << desc_string.gsub(arg_regexp) { |param| args[i+=1] }
116             @output.print(failed ? red(text) : green(text))
117
118             if type == :'given scenario'
119               @previous_type = :given
120             else
121               @previous_type = type
122             end
123           end
124         end
125       end
126     end
127   end
128 end