summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/runner/formatters/html_spec.rb
blob: 3783ab6a89295c7578ade5957c42682970ae2b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require File.dirname(__FILE__) + '/../../spec_helper'
require 'mspec/guards/guard'
require 'mspec/runner/formatters/html'
require 'mspec/runner/mspec'
require 'mspec/runner/example'
require 'mspec/utils/script'

describe HtmlFormatter do
  before :each do
    @formatter = HtmlFormatter.new
  end

  it "responds to #register by registering itself with MSpec for appropriate actions" do
    MSpec.stub(:register)
    MSpec.should_receive(:register).with(:start, @formatter)
    MSpec.should_receive(:register).with(:enter, @formatter)
    MSpec.should_receive(:register).with(:leave, @formatter)
    @formatter.register
  end
end

describe HtmlFormatter, "#start" do
  before :each do
    $stdout = @out = IOStub.new
    @formatter = HtmlFormatter.new
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints the HTML head" do
    @formatter.start
    ruby_engine = RUBY_ENGINE
    ruby_engine.should =~ /^#{ruby_engine}/
    @out.should ==
%[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Spec Output For #{ruby_engine} (#{RUBY_VERSION})</title>
<style type="text/css">
ul {
  list-style: none;
}
.fail {
  color: red;
}
.pass {
  color: green;
}
#details :target {
  background-color: #ffffe0;
}
</style>
</head>
<body>
]
  end
end

describe HtmlFormatter, "#enter" do
  before :each do
    $stdout = @out = IOStub.new
    @formatter = HtmlFormatter.new
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints the #describe string" do
    @formatter.enter "describe"
    @out.should == "<div><p>describe</p>\n<ul>\n"
  end
end

describe HtmlFormatter, "#leave" do
  before :each do
    $stdout = @out = IOStub.new
    @formatter = HtmlFormatter.new
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints the closing tags for the #describe string" do
    @formatter.leave
    @out.should == "</ul>\n</div>\n"
  end
end

describe HtmlFormatter, "#exception" do
  before :each do
    $stdout = @out = IOStub.new
    @formatter = HtmlFormatter.new
    @formatter.register
    @state = ExampleState.new ContextState.new("describe"), "it"
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints the #it string once for each exception raised" do
    exc = ExceptionState.new @state, nil, SpecExpectationNotMetError.new("disappointing")
    @formatter.exception exc
    exc = ExceptionState.new @state, nil, MSpecExampleError.new("painful")
    @formatter.exception exc
    @out.should ==
%[<li class="fail">- it (<a href="#details-1">FAILED - 1</a>)</li>
<li class="fail">- it (<a href="#details-2">ERROR - 2</a>)</li>
]
  end
end

describe HtmlFormatter, "#after" do
  before :each do
    $stdout = @out = IOStub.new
    @formatter = HtmlFormatter.new
    @formatter.register
    @state = ExampleState.new ContextState.new("describe"), "it"
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints the #it once when there are no exceptions raised" do
    @formatter.after @state
    @out.should == %[<li class="pass">- it</li>\n]
  end

  it "does not print any output if an exception is raised" do
    exc = ExceptionState.new @state, nil, SpecExpectationNotMetError.new("disappointing")
    @formatter.exception exc
    out = @out.dup
    @formatter.after @state
    @out.should == out
  end
end

describe HtmlFormatter, "#finish" do
  before :each do
    @tally = double("tally").as_null_object
    TallyAction.stub(:new).and_return(@tally)
    @timer = double("timer").as_null_object
    TimerAction.stub(:new).and_return(@timer)

    $stdout = @out = IOStub.new
    context = ContextState.new "describe"
    @state = ExampleState.new(context, "it")
    MSpec.stub(:register)
    @formatter = HtmlFormatter.new
    @formatter.register
    @exception = MSpecExampleError.new("broken")
    @exception.stub(:backtrace).and_return(["file.rb:1", "file.rb:2"])
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints a failure message for an exception" do
    exc = ExceptionState.new @state, nil, @exception
    @formatter.exception exc
    @formatter.finish
    @out.should include "<p>describe it ERROR</p>"
  end

  it "prints a backtrace for an exception" do
    exc = ExceptionState.new @state, nil, @exception
    exc.stub(:backtrace).and_return("path/to/some/file.rb:35:in method")
    @formatter.exception exc
    @formatter.finish
    @out.should =~ %r[<pre>.*path/to/some/file.rb:35:in method.*</pre>]m
  end

  it "prints a summary of elapsed time" do
    @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
    @formatter.finish
    @out.should include "<p>Finished in 2.0 seconds</p>\n"
  end

  it "prints a tally of counts" do
    @tally.should_receive(:format).and_return("1 example, 0 failures")
    @formatter.finish
    @out.should include '<p class="pass">1 example, 0 failures</p>'
  end

  it "prints errors, backtraces, elapsed time, and tallies" do
    exc = ExceptionState.new @state, nil, @exception
    exc.stub(:backtrace).and_return("path/to/some/file.rb:35:in method")
    @formatter.exception exc

    @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
    @tally.should_receive(:format).and_return("1 example, 1 failures")
    @formatter.finish
    @out.should ==
%[<li class=\"fail\">- it (<a href=\"#details-1\">ERROR - 1</a>)</li>
<hr>
<ol id="details">
<li id="details-1"><p>describe it ERROR</p>
<p>MSpecExampleError: broken</p>
<pre>
path/to/some/file.rb:35:in method</pre>
</li>
</ol>
<p>Finished in 2.0 seconds</p>
<p class="fail">1 example, 1 failures</p>
</body>
</html>
]
  end
end