require File.expand_path(File.dirname(__FILE__) + '/spec_helper') U = DataCatalog::ImporterFramework::Utility describe "Utility" do describe "normalize_url" do it "add trailing slash if missing" do U.normalize_url("sunlightlabs.com").should == "http://sunlightlabs.com/" end it "lowercases" do U.normalize_url("http://SunlightLabs.com/").should == "http://sunlightlabs.com/" end it "adds http if missing" do U.normalize_url("sunlightlabs.com/").should == "http://sunlightlabs.com/" end end describe "absolute_url" do it "should work" do U.absolute_url("http://sunlightlabs.com", "/contact").should == "http://sunlightlabs.com/contact" end end describe "single_line_clean" do it "should clean up leading and trailing whitespace" do U.single_line_clean("\t \ttext\t\t ").should == "text" end it "should clean up leading and trailing newlines" do U.single_line_clean("\n\ntext\n\n").should == "text" end it "should clean up all newlines" do U.single_line_clean("sunlight\nlabs").should == "sunlight labs" end end describe "multi_line_clean" do it "should remove leading and trailing newlines" do input = "\nline 1\nline 2\nline 3\n" U.multi_line_clean(input).should == "line 1\nline 2\nline 3" end end describe "fetch" do before do @readable = Object.new @readable.stub(:read).and_return("result") @sleep_count = 0 U.stub(:sleep).and_return { @sleep_count += 1 } end it "should work" do U.stub(:open).and_return(@readable) U.fetch("fake", :quiet => true).should == "result" end it "bad fetches below retry limit are ok" do @count = 0 U.stub(:open).and_return { @count += 1 if @count <= 2 raise StandardError else @readable end } U.fetch("fake", :max_attempts => 3, :quiet => true).should == "result" @sleep_count.should == 2 end it "bad fetches above retry limit give nil" do @count = 0 U.stub(:open).and_return { @count += 1 if @count <= 2 raise StandardError else @readable end } U.fetch("fake", :max_attempts => 2, :quiet => true).should == nil @sleep_count.should == 1 end end end