require 'rspec' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'diffy')) describe Diffy::Diff do describe "diffing two files" do def tempfile(string, fn = 'diffy-spec') t = Tempfile.new(fn) # ensure tempfiles aren't unlinked when GC runs by maintaining a # reference to them. @tempfiles ||=[] @tempfiles.push(t) t.print(string) t.flush t.close 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 it "should accept file paths with spaces as arguments" do string1 = "foo\nbar\nbang\n" string2 = "foo\nbang\n" path1, path2 = tempfile(string1, 'path with spaces'), tempfile(string2, 'path with spaces') Diffy::Diff.new(path1, path2, :source => 'files').to_s.should == <<-DIFF foo -bar bang DIFF end it "should accept file paths with spaces as arguments on windows" do begin orig_verbose, $VERBOSE = $VERBOSE, nil #silence redefine constant warnings orig_windows, Diffy::WINDOWS = Diffy::WINDOWS, true string1 = "foo\nbar\nbang\n" string2 = "foo\nbang\n" path1, path2 = tempfile(string1, 'path with spaces'), tempfile(string2, 'path with spaces') Diffy::Diff.new(path1, path2, :source => 'files').to_s.should == <<-DIFF foo -bar bang DIFF ensure Diffy::WINDOWS, $VERBOSE = orig_windows, orig_verbose end 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', :allow_empty_diff => false). to_s.should == <<-DIFF foo bar bang DIFF end it "should not show everything if the :allow_empty_diff option is set" do Diffy::Diff.new(@path1, @path2, :source => 'files', :allow_empty_diff => true).to_s.should == '' end end describe "with lines that start with backslashes" do before do string1 = "foo\n\\\\bag\nbang\n" string2 = "foo\n\\\\bar\nbang\n" @path1, @path2 = tempfile(string1), tempfile(string2) end it "should not leave lines out" do Diffy::Diff.new(@path1, @path2, :source => 'files').to_s.should == <<-DIFF foo -\\\\bag +\\\\bar bang DIFF end end describe "with non valid UTF bytes" do before do string1 = "Foo ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000\n" string2 = "Bar ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000\n" @path1, @path2 = tempfile(string1), tempfile(string2) end it "should not raise invalid encoding issues" do desired = <<-DIFF -Foo ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000 +Bar ICS95095010000000000083320000BS01030000004100+\xFF00000000000000000 DIFF desired.force_encoding("ASCII-8BIT") if desired.respond_to?(:force_encoding) Diffy::Diff.new(@path1, @path2, :source => 'files').to_s.should == desired end end end describe "options[:context]" do it "should limit context lines to 1" do diff = Diffy::Diff.new("foo\nfoo\nBAR\nbang\nbaz", "foo\nfoo\nbar\nbang\nbaz", :context => 1) diff.to_s.should == <<-DIFF foo -BAR +bar bang DIFF end end describe "options[:include_plus_and_minus_in_html]" do it "defaults to false" do @diffy = Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n") @diffy.options[:include_plus_and_minus_in_html].should == false end it "can be set to true" do @diffy = Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :include_plus_and_minus_in_html=> true ) @diffy.options[:include_plus_and_minus_in_html].should == true end describe "formats" do it "includes symbols in html_simple" do output = Diffy::Diff.new("foo\nbar\nbang\n", "foo\nbang\n", :include_plus_and_minus_in_html => true ). to_s(:html_simple) output.should == <<-HTML