1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require 'mspec/guards/guard'
3 require 'mspec/runner/formatters/html'
4 require 'mspec/runner/mspec'
5 require 'mspec/runner/example'
7 describe HtmlFormatter do
9 @formatter = HtmlFormatter.new
12 it "responds to #register by registering itself with MSpec for appropriate actions" do
13 MSpec.stub!(:register)
14 MSpec.should_receive(:register).with(:start, @formatter)
15 MSpec.should_receive(:register).with(:enter, @formatter)
16 MSpec.should_receive(:register).with(:leave, @formatter)
21 describe HtmlFormatter, "#start" do
23 $stdout = @out = IOStub.new
24 @formatter = HtmlFormatter.new
31 it "prints the HTML head" do
34 ruby_name.should =~ /^ruby/
36 %[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
37 "http://www.w3.org/TR/html4/strict.dtd">
40 <title>Spec Output For #{ruby_name} (1.8.6)</title>
41 <style type="text/css">
52 background-color: #ffffe0;
61 describe HtmlFormatter, "#enter" do
63 $stdout = @out = IOStub.new
64 @formatter = HtmlFormatter.new
71 it "prints the #describe string" do
72 @formatter.enter "describe"
73 @out.should == "<div><p>describe</p>\n<ul>\n"
77 describe HtmlFormatter, "#leave" do
79 $stdout = @out = IOStub.new
80 @formatter = HtmlFormatter.new
87 it "prints the closing tags for the #describe string" do
89 @out.should == "</ul>\n</div>\n"
93 describe HtmlFormatter, "#exception" do
95 $stdout = @out = IOStub.new
96 @formatter = HtmlFormatter.new
98 @state = ExampleState.new ContextState.new("describe"), "it"
105 it "prints the #it string once for each exception raised" do
106 exc = ExceptionState.new @state, nil, ExpectationNotMetError.new("disappointing")
107 @formatter.exception exc
108 exc = ExceptionState.new @state, nil, MSpecExampleError.new("painful")
109 @formatter.exception exc
111 %[<li class="fail">- it (<a href="#details-1">FAILED - 1</a>)</li>
112 <li class="fail">- it (<a href="#details-2">ERROR - 2</a>)</li>
117 describe HtmlFormatter, "#after" do
119 $stdout = @out = IOStub.new
120 @formatter = HtmlFormatter.new
122 @state = ExampleState.new ContextState.new("describe"), "it"
129 it "prints the #it once when there are no exceptions raised" do
130 @formatter.after @state
131 @out.should == %[<li class="pass">- it</li>\n]
134 it "does not print any output if an exception is raised" do
135 exc = ExceptionState.new @state, nil, ExpectationNotMetError.new("disappointing")
136 @formatter.exception exc
138 @formatter.after @state
143 describe HtmlFormatter, "#finish" do
145 @tally = mock("tally", :null_object => true)
146 TallyAction.stub!(:new).and_return(@tally)
147 @timer = mock("timer", :null_object => true)
148 TimerAction.stub!(:new).and_return(@timer)
150 $stdout = @out = IOStub.new
151 context = ContextState.new "describe"
152 @state = ExampleState.new(context, "it")
153 MSpec.stub!(:register)
154 @formatter = HtmlFormatter.new
156 @exception = MSpecExampleError.new("broken")
157 @exception.stub!(:backtrace).and_return(["file.rb:1", "file.rb:2"])
164 it "prints a failure message for an exception" do
165 exc = ExceptionState.new @state, nil, @exception
166 @formatter.exception exc
168 @out.should =~ %r[<p>describe it ERROR</p>]
171 it "prints a backtrace for an exception" do
172 exc = ExceptionState.new @state, nil, @exception
173 exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
174 @formatter.exception exc
176 @out.should =~ %r[<pre>.*path/to/some/file.rb:35:in method.*</pre>]m
179 it "prints a summary of elapsed time" do
180 @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
182 @out.should =~ %r[<p>Finished in 2.0 seconds</p>\n]
185 it "prints a tally of counts" do
186 @tally.should_receive(:format).and_return("1 example, 0 failures")
188 @out.should =~ %r[<p class="pass">1 example, 0 failures</p>]
191 it "prints errors, backtraces, elapsed time, and tallies" do
192 exc = ExceptionState.new @state, nil, @exception
193 exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
194 @formatter.exception exc
196 @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
197 @tally.should_receive(:format).and_return("1 example, 1 failures")
200 %[<li class=\"fail\">- it (<a href=\"#details-1\">ERROR - 1</a>)</li>
203 <li id="details-1"><p>describe it ERROR</p>
204 <p>MSpecExampleError: broken</p>
206 path/to/some/file.rb:35:in method</pre>
209 <p>Finished in 2.0 seconds</p>
210 <p class="fail">1 example, 1 failures</p>