spec/butternut/formatter_spec.rb in butternut-0.0.1 vs spec/butternut/formatter_spec.rb in butternut-0.1.0

- old
+ new

@@ -10,10 +10,16 @@ nodes = doc.css(css) nodes.detect{ |node| node.text =~ regexp } end end + Spec::Matchers.define :be_an_existing_file do + match do |filename| + File.exist?(filename) + end + end + def setup_formatter(options = {}) @out = StringIO.new @formatter = Butternut::Formatter.new(step_mother, @out, options) end @@ -230,19 +236,19 @@ end end describe "displaying page source to file" do before(:each) do - @tmpdir = File.join(File.dirname(__FILE__), "..", "..", "tmp") + dir = File.join(File.dirname(__FILE__), "..", "..", "tmp") setup_formatter({:formats => [ - ['Butternut::Formatter', File.join(@tmpdir, "main", "huge.html")] + ['Butternut::Formatter', File.join(dir, "main", "huge.html")] ]}) run_defined_feature @doc = Nokogiri.HTML(@out.string) - dir = File.join(@tmpdir, "features", Date.today.to_s) - file = most_recent_html_file(dir) + @tmp_dir = File.join(dir, "features", Date.today.to_s) + file = most_recent_html_file(@tmp_dir) @page_doc = Nokogiri.HTML(open(file).read) end define_steps do Given(/foo/) do @@ -262,15 +268,26 @@ file = link['href'] file.should match(%r{^/features/#{Date.today.to_s}/butternut.+\.html}) end it "saves images and stylesheets and rewrites urls in page source" do - @page_doc.at('img')['src'].should == "picard.jpg" - @page_doc.at('link:first[rel="stylesheet"]')['href'].should == "foo.css" - @page_doc.at('link:last[rel="stylesheet"]')['href'].should == "bar.css" + @page_doc.at('img:nth(1)')['src'].should == "picard.jpg" + File.join(@tmp_dir, "picard.jpg").should be_an_existing_file + + @page_doc.at('link:nth(1)[rel="stylesheet"]')['href'].should == "foo.css" + File.join(@tmp_dir, "foo.css").should be_an_existing_file + + @page_doc.at('link:nth(2)[rel="stylesheet"]')['href'].should == "bar.css" + File.join(@tmp_dir, "bar.css").should be_an_existing_file end + it "saves assets and rewrites urls referred to by stylesheets" do + foo = open(File.join(@tmp_dir, "foo.css")).read + foo.should include("url(facepalm.jpg)") + File.join(@tmp_dir, "facepalm.jpg").should be_an_existing_file + end + it "turns off links" do @page_doc.css('a').each do |link| link['href'].should == "#" end end @@ -281,9 +298,21 @@ it "disables form elements" do @page_doc.css('input, select, textarea').each do |elt| elt['disabled'].should == "disabled" end + end + + it "handles Errno::ENOENT" do + @page_doc.at('img:nth(2)')['src'].should == "/roflpwnage/missing_file_omg.gif" + end + + it "handles OpenURI::HTTPError" do + @page_doc.at('img:nth(3)')['src'].should == "http://google.com/missing_file_omg.gif" + end + + it "handles Net::FTPPermError" do + @page_doc.at('img:nth(4)')['src'].should == "ftp://mirror.anl.gov/missing_file_omg.gif" end end end end