-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathlayout_parser_spec.rb
201 lines (183 loc) · 5.74 KB
/
layout_parser_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require 'spec_helper'
describe Squib::LayoutParser do
it 'loads a normal layout with no extends' do
layout = Squib::LayoutParser.load_layout(layout_file('no-extends.yml'))
expect(layout).to eq({ 'frame' => {
'x' => 38,
'valign' => :middle,
'str' => 'blah',
'font' => 'Mr. Font',
}
}
)
end
it 'loads with a single extends' do
layout = Squib::LayoutParser.load_layout(layout_file('single-extends.yml'))
expect(layout).to eq({ 'frame' => {
'x' => 38,
'y' => 38,
},
'title' => {
'extends' => 'frame',
'x' => 38,
'y' => 50,
'width' => 100,
}
}
)
end
it 'applies the extends regardless of order' do
layout = Squib::LayoutParser.load_layout(layout_file('pre-extends.yml'))
expect(layout).to eq({ 'frame' => {
'x' => 38,
'y' => 38,
},
'title' => {
'extends' => 'frame',
'x' => 38,
'y' => 50,
'width' => 100,
}
}
)
end
it 'applies the single-level extends multiple times' do
layout = Squib::LayoutParser.load_layout(layout_file('single-level-multi-extends.yml'))
expect(layout).to eq({ 'frame' => {
'x' => 38,
'y' => 38,
},
'title' => {
'extends' => 'frame',
'x' => 38,
'y' => 50,
'width' => 100,
},
'title2' => {
'extends' => 'frame',
'x' => 75,
'y' => 150,
'width' => 150,
},
}
)
end
it 'applies multiple extends in a single rule' do
layout = Squib::LayoutParser.load_layout(layout_file('multi-extends-single-entry.yml'))
expect(layout).to eq({ 'aunt' => {
'a' => 101,
'b' => 102,
'c' => 103,
},
'uncle' => {
'x' => 104,
'y' => 105,
'b' => 106,
},
'child' => {
'extends' => ['uncle', 'aunt'],
'a' => 107, # my own
'b' => 102, # from the younger aunt
'c' => 103, # from aunt
'x' => 108, # my own
'y' => 105, # from uncle
},
}
)
end
it 'applies multi-level extends' do
layout = Squib::LayoutParser.load_layout(layout_file('multi-level-extends.yml'))
expect(layout).to eq({ 'frame' => {
'x' => 38,
'y' => 38,
},
'title' => {
'extends' => 'frame',
'x' => 38,
'y' => 50,
'width' => 100,
},
'subtitle' => {
'extends' => 'title',
'x' => 38,
'y' => 150,
'width' => 100,
},
}
)
end
it 'fails on a self-circular extends' do
file = layout_file('self-circular-extends.yml')
expect { Squib::LayoutParser.load_layout(file) }
.to raise_error(RuntimeError, 'Invalid layout: circular extends with \'a\'')
end
it 'fails on a easy-circular extends' do
file = layout_file('easy-circular-extends.yml')
expect { Squib::LayoutParser.load_layout(file) }
.to raise_error(RuntimeError, 'Invalid layout: circular extends with \'a\'')
end
it 'hard on a easy-circular extends' do
file = layout_file('hard-circular-extends.yml')
expect { Squib::LayoutParser.load_layout(file) }
.to raise_error(RuntimeError, 'Invalid layout: circular extends with \'a\'')
end
it 'redefines keys on multiple layouts' do
a = layout_file('multifile-a.yml')
b = layout_file('multifile-b.yml')
layout = Squib::LayoutParser.load_layout([a, b])
expect(layout).to eq({
'title' => { 'x' => 300 },
'subtitle' => { 'x' => 200 },
'desc' => { 'x' => 400 }
})
end
it 'evaluates extends on each file first' do
a = layout_file('multifile-extends-a.yml')
b = layout_file('multifile-extends-b.yml')
layout = Squib::LayoutParser.load_layout([a, b])
expect(layout).to eq({
'grandparent' => { 'x' => 100 },
'parent_a' => { 'x' => 110, 'extends' => 'grandparent' },
'parent_b' => { 'x' => 130, 'extends' => 'grandparent' },
'child_a' => { 'x' => 113, 'extends' => 'parent_a' },
'child_b' => { 'x' => 133, 'extends' => 'parent_b' }
})
end
it 'loads nothing on an empty layout file' do
layout = Squib::LayoutParser.load_layout(layout_file('empty.yml'))
expect(layout).to eq({})
end
it 'handles extends on a rule with no args' do
layout = Squib::LayoutParser.load_layout(layout_file('empty-rule.yml'))
expect(layout).to eq({
'empty' => nil
})
end
it 'logs an error when a file is not found' do
expect(Squib.logger).to receive(:error).once
Squib::LayoutParser.load_layout('yeti')
end
it 'freaks out if you extend something doesn\'t exist' do
expect(Squib.logger)
.to receive(:error)
.with("Processing layout: 'verbal' attempts to extend a missing 'kaisersoze'")
layout = Squib::LayoutParser.load_layout(layout_file('extends-nonexists.yml'))
expect(layout).to eq({
'verbal' => {
'font_size' => 25,
'extends' => 'kaisersoze'
}
})
end
it 'loads progressively on multiple calls' do
a = layout_file('multifile-a.yml')
b = layout_file('multifile-b.yml')
layout = Squib::LayoutParser.load_layout(a)
layout = Squib::LayoutParser.load_layout(b, layout)
expect(layout).to eq({
'title' => { 'x' => 300 },
'subtitle' => { 'x' => 200 },
'desc' => { 'x' => 400 }
})
end
end