]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/spec/spec/expectations/differs/default_spec.rb
ea720846b85d543d571102056e86c45ecd0ef394
[rails.git] / vendor / gems / rspec-1.1.2 / spec / spec / expectations / differs / default_spec.rb
1 require File.dirname(__FILE__) + '/../../../spec_helper.rb'
2
3 module Spec
4   module Fixtures
5     class Animal
6       def initialize(name,species)
7         @name,@species = name,species
8       end
9
10       def inspect
11         <<-EOA
12 <Animal
13   name=#{@name},
14   species=#{@species}
15 >
16         EOA
17       end
18     end
19   end
20 end
21
22 describe "Diff" do
23   before(:each) do
24     @options = ::Spec::Runner::Options.new(StringIO.new, StringIO.new)
25     @differ = Spec::Expectations::Differs::Default.new(@options)
26   end
27
28   it "should output unified diff of two strings" do
29     expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
30     actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
31     expected_diff="\n\n@@ -1,6 +1,6 @@\n foo\n-bar\n zap\n+bar\n this\n is\n soo\n@@ -9,5 +9,6 @@\n equal\n insert\n a\n+another\n line\n"
32     diff = @differ.diff_as_string(expected, actual)
33     diff.should eql(expected_diff)
34   end
35
36   it "should output unified diff message of two arrays" do
37     expected = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'charlie', :width, 'quite wide' ]
38     actual   = [ :foo, 'bar', :baz, 'quux', :metasyntactic, 'variable', :delta, 'tango'  , :width, 'very wide'  ]
39
40     expected_diff = <<'EOD'
41
42
43 @@ -5,7 +5,7 @@
44   :metasyntactic,
45   "variable",
46   :delta,
47 - "charlie",
48 + "tango",
49   :width,
50 - "quite wide"]
51 + "very wide"]
52 EOD
53
54
55     diff = @differ.diff_as_object(expected,actual)
56     diff.should == expected_diff
57   end
58
59   it "should output unified diff message of two objects" do
60     expected = Spec::Fixtures::Animal.new "bob", "giraffe"
61     actual   = Spec::Fixtures::Animal.new "bob", "tortoise"
62
63     expected_diff = <<'EOD'
64
65 @@ -1,5 +1,5 @@
66  <Animal
67    name=bob,
68 -  species=giraffe
69 +  species=tortoise
70  >
71 EOD
72
73     diff = @differ.diff_as_object(expected,actual)
74     diff.should == expected_diff
75   end
76
77 end
78
79
80 describe "Diff in context format" do
81   before(:each) do
82     @options = Spec::Runner::Options.new(StringIO.new, StringIO.new)
83     @options.diff_format = :context
84     @differ = Spec::Expectations::Differs::Default.new(@options)
85   end
86
87   it "should output unified diff message of two objects" do
88     expected = Spec::Fixtures::Animal.new "bob", "giraffe"
89     actual   = Spec::Fixtures::Animal.new "bob", "tortoise"
90
91     expected_diff = <<'EOD'
92
93 ***************
94 *** 1,5 ****
95   <Animal
96     name=bob,
97 !   species=giraffe
98   >
99 --- 1,5 ----
100   <Animal
101     name=bob,
102 !   species=tortoise
103   >
104 EOD
105
106     diff = @differ.diff_as_object(expected,actual)
107     diff.should == expected_diff
108   end
109 end