require 'spec' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'diffy')) describe Diffy::Diff do describe "diffing two files" do def tempfile(string) t = Tempfile.new('diffy-spec') t.print(string) t.flush t.path end it "should accept file paths as arguments" do string1 = "foo\nbar\nbang\n" string2 = "foo\nbang\n" path1, path2 = tempfile(string1), tempfile(string2) Diffy::Diff.new(path1, path2, :source => 'files').to_s.should == <<-DIFF foo -bar bang DIFF end describe "with no line different" do before do string1 = "foo\nbar\nbang\n" string2 = "foo\nbar\nbang\n" @path1, @path2 = tempfile(string1), tempfile(string2) end it "should show everything" do Diffy::Diff.new(@path1, @path2, :source => 'files').to_s.should == <<-DIFF foo bar bang DIFF end end end describe "#to_s" do describe "with no line different" do before do @string1 = "foo\nbar\nbang\n" @string2 = "foo\nbar\nbang\n" end it "should show everything" do Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF foo bar bang DIFF end end describe "with one line different" do before do @string1 = "foo\nbar\nbang\n" @string2 = "foo\nbang\n" end it "should show one line removed" do Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF foo -bar bang DIFF end it "to_s should accept a format key" do Diffy::Diff.new(@string1, @string2).to_s(:color). should == " foo\n\e[31m-bar\e[0m\n bang\n" end it "should accept a default format option" do old_format = Diffy::Diff.default_format Diffy::Diff.default_format = :color Diffy::Diff.new(@string1, @string2).to_s. should == " foo\n\e[31m-bar\e[0m\n bang\n" Diffy::Diff.default_format = old_format end it "should show one line added" do Diffy::Diff.new(@string2, @string1).to_s.should == <<-DIFF foo +bar bang DIFF end end describe "with one line changed" do before do @string1 = "foo\nbar\nbang\n" @string2 = "foo\nbong\nbang\n" end it "should show one line added and one removed" do Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF foo -bar +bong bang DIFF end end describe "with totally different strings" do before do @string1 = "foo\nbar\nbang\n" @string2 = "one\ntwo\nthree\n" end it "should show one line added and one removed" do Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF -foo -bar -bang +one +two +three DIFF end end describe "with a somewhat complicated diff" do before do @string1 = "foo\nbar\nbang\nwoot\n" @string2 = "one\ntwo\nthree\nbar\nbang\nbaz\n" @diff = Diffy::Diff.new(@string1, @string2) end it "should show one line added and one removed" do @diff.to_s.should == <<-DIFF -foo +one +two +three bar bang -woot +baz DIFF end it "should make an awesome simple html diff" do @diff.to_s(:html_simple).should == <<-HTML
HTML end it "should accept overrides to diff's options" do @diff = Diffy::Diff.new(@string1, @string2, :diff => "--rcs") @diff.to_s.should == <<-DIFF d1 1 a1 3 one two three d4 1 a4 1 baz DIFF end end describe "html" do it "should highlight the changes within the line" do @string1 = "hahaha\ntime flies like an arrow\nfoo bar\nbang baz\n" @string2 = "hahaha\nfruit flies like a banana\nbang baz\n" @diff = Diffy::Diff.new(@string1, @string2) html = <<-HTML
HTML @diff.to_s(:html).should == html end it "should not duplicate some lines" do @string1 = "hahaha\ntime flies like an arrow\n" @string2 = "hahaha\nfruit flies like a banana\nbang baz" @diff = Diffy::Diff.new(@string1, @string2) html = <<-HTML
HTML @diff.to_s(:html).should == html end it "should escape html" do @string1 = "ha
haha\ntime flies like an arrow\n" @string2 = "ha
haha\nfruit flies like a banana\nbang baz" @diff = Diffy::Diff.new(@string1, @string2) html = <<-HTML
HTML @diff.to_s(:html).should == html end it "should highlight the changes within the line with windows style line breaks" do @string1 = "hahaha\r\ntime flies like an arrow\r\nfoo bar\r\nbang baz\n" @string2 = "hahaha\r\nfruit flies like a banana\r\nbang baz\n" @diff = Diffy::Diff.new(@string1, @string2) html = <<-HTML
HTML @diff.to_s(:html).should == html end end it "should escape diffed html in html output" do diff = Diffy::Diff.new("", "").to_s(:html) diff.should include('<script>') diff.should_not include('