-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_spec.cr
137 lines (113 loc) · 6.43 KB
/
value_spec.cr
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.cr"
describe Stacker::Value do
describe ".from_yaml" do
it "convert YAML to internal structure" do
inspect = <<-'TPL'
Stacker::Value(@container={"foo" => Stacker::Value(@container={"bar" => [1, "127.0.0.1", true], "is_true" => true, "is_false" => false, "is_array" => [], "is_hash" => Stacker::Value(@container={}), "nested" => Stacker::Value(@container={"foo" => Stacker::Value(@container={"bar" => [1, "127.0.0.1", false], "is_true" => true, "is_false" => false, "is_array" => [], "is_hash" => Stacker::Value(@container={})})})})})
TPL
file = "spec/fixtures/merge_strategies/input/test.yml"
yaml = File.read(file)
hash = Stacker::Value.from_yaml(yaml)
generated_yaml = File.read("spec/fixtures/merge_strategies/output/test.yml")
hash.inspect.should eq(inspect)
YAML.dump(hash).should eq(generated_yaml)
end
end
describe ".convert_hash" do
it "convert YAML to internal structure" do
inspect = <<-'TPL'
Stacker::Value(@container={"foo" => Stacker::Value(@container={"bar" => [1, "127.0.0.1", true], "is_true" => true, "is_false" => false, "is_array" => [], "is_hash" => Stacker::Value(@container={}), "nested" => Stacker::Value(@container={"foo" => Stacker::Value(@container={"bar" => [1, "127.0.0.1", false], "is_true" => true, "is_false" => false, "is_array" => [], "is_hash" => Stacker::Value(@container={})})})})})
TPL
yaml = YAML.parse(File.read("spec/fixtures/merge_strategies/input/test.yml"))
hash = Stacker::Value.convert_hash(yaml.as_h)
generated_yaml = File.read("spec/fixtures/merge_strategies/output/test.yml")
hash.inspect.should eq(inspect)
YAML.dump(hash).should eq(generated_yaml)
end
end
describe ".deep_merge" do
it "deep merges internal structure " do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/deep_merge_dict1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/deep_merge_dict2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/deep_merge.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
describe "hash merging strategies" do
context "when strategy is merge last" do
it "should merge last hash" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/merge_last_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/merge_last_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_hash/merge_last.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
context "when strategy is merge first" do
it "should merge first hash" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/merge_first_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/merge_first_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_hash/merge_first.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
context "when strategy is remove" do
it "should remove hash" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/remove_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/remove_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_hash/remove.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
context "when strategy is overwrite" do
it "should overwrite hash" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/overwrite_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_hash/overwrite_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_hash/overwrite.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
end
describe "array merging strategies" do
context "when strategy is merge last" do
it "should merge last array" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/merge_last_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/merge_last_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_array/merge_last.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
context "when strategy is merge first" do
it "should merge first array" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/merge_first_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/merge_first_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_array/merge_first.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
context "when strategy is remove" do
it "should remove array" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/remove_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/remove_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_array/remove.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
context "when strategy is overwrite" do
it "should overwrite array" do
hash1 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/overwrite_1.yml")
hash2 = load_yaml("spec/fixtures/merge_strategies/input/merge_strategy_array/overwrite_2.yml")
generated_yaml = File.read("spec/fixtures/merge_strategies/output/merge_strategy_array/overwrite.yml")
Stacker::Value.deep_merge!(hash1, hash2)
YAML.dump(hash1).should eq(generated_yaml)
end
end
end
end
end