forked from wallace/yamldiff
/
yamldiff_spec.rb
137 lines (118 loc) · 3.99 KB
/
yamldiff_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require "spec_helper"
describe Yamldiff do
subject { Yamldiff }
describe ".compare_hashes" do
it "should be empty for equal hashes" do
subject.compare_hashes({ a: 1 }, { a: 1 }).should be_empty
end
it "should be empty for nested hashes" do
first = { a: { b: 2 } }
second = { a: { b: 2 } }
subject.compare_hashes(first, second).should be_empty
end
it "should recognize when a key is missing" do
first = { a: 1, b: 2 }
second = { a: 1 }
result = subject.compare_hashes(first, second).first
result.should be_an_instance_of(YamldiffKeyError)
result.key.should eql :b
result.context.should be_empty
end
it "should recognize when two keys' values are of a different type" do
first = { a: 1 }
second = { a: "1" }
result = subject.compare_hashes(first, second).first
result.should be_an_instance_of(YamldiffKeyValueTypeError)
result.key.should eql :a
result.context.should be_empty
end
it "should recognize when two keys' values are not equal" do
first = { a: { b: 2 } }
second = { a: { b: 1 } }
result = subject.compare_hashes(first, second).first
result.should be_an_instance_of(YamldiffKeyValueError)
result.key.should eql :b
result.context.should == [:a]
end
end
describe ".diff_yaml" do
it "returns no errors when the files have the same YAML structure", fakefs: true do
File.open("./en.yml", "w") do |f|
f.puts("en: ")
f.puts(" app_name: 'Verbosefish'")
f.puts("blah: 'Hello'")
end
File.open("./es.yml", "w") do |f|
f.puts("blah: 'Hello'")
f.puts("en: ")
f.puts(" app_name: 'Verbosefish'")
end
subject.diff_yaml("./en.yml", "./es.yml").values.all? {|e| e.should eql([])}
end
it "returns one top level error", fakefs: true do
first = "./en.yml"
second = "./es.yml"
File.open(first, "w") do |f|
f.puts("en: ")
f.puts(" app_name: 'Verbosefish'")
end
File.open(second, "w") do |f|
f.puts("es: ")
f.puts(" app_name: 'Verboszefish'")
end
result = subject.diff_yaml(first, second)
result[second].first.key.should eql("en")
end
it "returns second level error", fakefs: true do
first = "./en.yml"
second = "./es.yml"
File.open(first, "w") do |f|
f.puts("en: ")
f.puts(" app_name: 'Verbosefish'")
end
File.open(second, "w") do |f|
f.puts("en: ")
f.puts(" app_name: 'Verboszefish'")
end
result = subject.diff_yaml(first, second)
result[second].first.key.should eql("app_name")
result[second].first.context.should eql(["en"])
end
it "returns third level error", fakefs: true do
first = "./en.yml"
second = "./es.yml"
File.open(first, "w") do |f|
f.puts("en:")
f.puts(" app_name:")
f.puts(" foo: 'Verbosefish'")
f.puts(" baz:")
f.puts(" bar: 'Verbosefish'")
end
File.open(second, "w") do |f|
f.puts("en:")
f.puts(" app_name:")
f.puts(" foo: 'Verbosefish'")
f.puts(" baz:")
f.puts(" bar: 'Something completely different'")
end
result = subject.diff_yaml(first, second)
result[second].first.key.should eql("bar")
result[second].first.context.should eql(["en", "baz"])
end
it "diffs the output when the values are different and passes diff to error" do
first = "./en.yml"
second = "./es.yml"
File.open(first, "w") do |f|
f.puts("en: ")
f.puts(" app_name: 'Verbosefish'")
end
File.open(second, "w") do |f|
f.puts("en: ")
f.puts(" app_name: 'Verboszefish'")
end
Diffy::Diff.expects(:new).with("Verbosefish\n", "Verboszefish\n").returns("DIFF")
YamldiffKeyValueError.expects(:new).with('app_name', ['en'], 'DIFF')
result = subject.diff_yaml(first, second)
end
end
end