Blob of rspec/spec/spec/runner/formatter/html_formatter_spec.rb (raw blob data)

1 require File.dirname(__FILE__) + '/../../../spec_helper'
2 require 'hpricot' # Needed to compare generated with wanted HTML
3 require 'spec/runner/formatter/html_formatter'
4
5 module Spec
6 module Runner
7 module Formatter
8 describe HtmlFormatter do
9 ['--diff', '--dry-run'].each do |opt|
10 def jruby?
11 PLATFORM == 'java'
12 end
13
14 it "should produce HTML identical to the one we designed manually with #{opt}" do
15 root = File.expand_path(File.dirname(__FILE__) + '/../../../..')
16 suffix = jruby? ? '-jruby' : ''
17 expected_file = File.dirname(__FILE__) + "/html_formatted-#{::VERSION}#{suffix}.html"
18 raise "There is no HTML file with expected content for this platform: #{expected_file}" unless File.file?(expected_file)
19 expected_html = File.read(expected_file)
20
21 Dir.chdir(root) do
22 args = ['failing_examples/mocking_example.rb', 'failing_examples/diffing_spec.rb', 'examples/pure/stubbing_example.rb', 'examples/pure/pending_example.rb', '--format', 'html', opt]
23 err = StringIO.new
24 out = StringIO.new
25 CommandLine.run(
26 OptionParser.parse(args, err, out)
27 )
28
29 seconds = /\d+\.\d+ seconds/
30 html = out.string.gsub seconds, 'x seconds'
31 expected_html.gsub! seconds, 'x seconds'
32
33 if opt == '--diff'
34 # Uncomment this line temporarily in order to overwrite the expected with actual.
35 # Use with care!!!
36 # File.open(expected_file, 'w') {|io| io.write(html)}
37
38 doc = Hpricot(html)
39 backtraces = doc.search("div.backtrace").collect {|e| e.at("/pre").inner_html}
40 doc.search("div.backtrace").remove
41
42 expected_doc = Hpricot(expected_html)
43 expected_backtraces = expected_doc.search("div.backtrace").collect {|e| e.at("/pre").inner_html}
44 expected_doc.search("div.backtrace").remove
45
46 doc.inner_html.should == expected_doc.inner_html
47
48 expected_backtraces.each_with_index do |expected_line, i|
49 expected_path, expected_line_number, expected_suffix = expected_line.split(':')
50 actual_path, actual_line_number, actual_suffix = backtraces[i].split(':')
51 File.expand_path(actual_path).should == File.expand_path(expected_path)
52 actual_line_number.should == expected_line_number
53 end
54 else
55 html.should =~ /This was a dry-run/m
56 end
57 end
58 end
59 end
60 end
61 end
62 end
63 end