-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig-source.test.ts
212 lines (169 loc) · 5.55 KB
/
config-source.test.ts
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
202
203
204
205
206
207
208
209
210
211
212
import {
ConfigSource,
LiteralSource,
CombinedSource,
FallbackSource,
FileType,
stringify,
} from './config-source';
import { ParsingExtension } from './parsed-value';
import { NotFoundError } from './errors';
class FailingSource extends ConfigSource {
readContents(): Promise<[string, FileType]> {
throw new NotFoundError();
}
}
const flattenExtension: ParsingExtension = (value, [_, key]) => {
if (key === '$flatten') {
return (parse) => parse(value, { shouldFlatten: true });
}
return false;
};
const uppercaseExtension: ParsingExtension = (value) => {
if (typeof value === 'string') {
return (parse) => parse(value.toUpperCase());
}
return false;
};
describe('Parsing', () => {
it('interprets an empty object', async () => {
const source = new LiteralSource({});
const parsed = await source.read();
expect(parsed.sources[0]).toBe(source);
expect(parsed.raw).toEqual({});
expect(parsed.toJSON()).toEqual({});
expect(parsed.toString()).toEqual('{}');
});
it('uses parsing extensions', async () => {
const parsed = await new LiteralSource({
$flatten: 'bar',
}).read([flattenExtension]);
expect(parsed.toJSON()).toEqual('bar');
});
it('uses parsing extensions in nested objects', async () => {
const parsed = await new LiteralSource({
foo: {
$flatten: 'bar',
},
}).read([flattenExtension]);
expect(parsed.toJSON()).toEqual({ foo: 'bar' });
});
it('uses value transform extension', async () => {
const parsed = await new LiteralSource({
foo: 'bar',
}).read([uppercaseExtension]);
expect(parsed.toJSON()).toEqual({ foo: 'BAR' });
});
it('uses multiple extensions', async () => {
const parsed = await new LiteralSource({
foo: {
$flatten: 'bar',
},
}).read([flattenExtension, uppercaseExtension]);
expect(parsed.toJSON()).toEqual({ foo: 'BAR' });
});
it('uses readToJSON shorthand', async () => {
const parsed = await new LiteralSource({
foo: {
$flatten: 'bar',
},
}).readToJSON([flattenExtension]);
expect(parsed).toEqual({ foo: 'bar' });
});
it('loads using readContents correctly', async () => {
const [text, fileType] = await new LiteralSource({ foo: 'bar' }).readContents();
expect([text, fileType]).toMatchSnapshot();
});
});
describe('CombinedSource', () => {
it('fails when no sources are provided', () => {
expect(() => new CombinedSource([])).toThrow();
});
it('combines a few sources', async () => {
const source = new CombinedSource([
new LiteralSource({ foo: 1 }),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]);
const parsed = await source.read();
expect(parsed.sources[0]).toBe(source);
expect(parsed.toJSON()).toEqual({ foo: 1, bar: 2, baz: 3 });
});
it('loads using readContents correctly', async () => {
const [text, fileType] = await new CombinedSource([
new LiteralSource({ foo: 1 }),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]).readContents();
expect([text, fileType]).toMatchSnapshot();
});
it('loads using readValue correctly', async () => {
const value = await new CombinedSource([
new LiteralSource({ foo: 1 }),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]).readValue();
expect(value).toEqual({ foo: 1, bar: 2, baz: 3 });
});
});
describe('FallbackSource', () => {
it('fails when no sources are provided', () => {
expect(() => new FallbackSource([])).toThrow();
});
it('selects the first source', async () => {
const source = new FallbackSource([
new LiteralSource({ foo: 1 }),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]);
const parsed = await source.read();
expect(parsed.sources[0]).toBe(source.sources[0]);
expect(parsed.toJSON()).toEqual({ foo: 1 });
});
it('selects source when first one fails', async () => {
const source = new FallbackSource([
new FailingSource(),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]);
const parsed = await source.read();
expect(parsed.sources[0]).toBe(source.sources[1]);
expect(parsed.toJSON()).toEqual({ bar: 2 });
});
it('loads using readContents correctly', async () => {
const [text, fileType] = await new FallbackSource([
new LiteralSource({ foo: 1 }),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]).readContents();
expect([text, fileType]).toMatchSnapshot();
});
it('loads using readValue correctly', async () => {
const value = await new FallbackSource([
new LiteralSource({ foo: 1 }),
new LiteralSource({ bar: 2 }),
new LiteralSource({ baz: 3 }),
]).readValue();
expect(value).toEqual({ foo: 1 });
});
});
describe('stringify', () => {
it('stringifies JSON', () => {
expect(stringify({ foo: 'bar' }, FileType.JSON)).toMatchSnapshot();
});
it('stringifies JSON5', () => {
expect(stringify({ foo: 'bar' }, FileType.JSON5)).toMatchSnapshot();
});
it('stringifies YAML', () => {
expect(stringify({ foo: 'bar' }, FileType.YAML)).toMatchSnapshot();
});
it('stringifies TOML', () => {
expect(stringify({ foo: 'bar' }, FileType.TOML)).toMatchSnapshot();
});
it('stringifies RAW', () => {
// RAW only stringifies primitive values
expect(stringify(11, FileType.RAW)).toMatchSnapshot();
expect(stringify('foobar', FileType.RAW)).toMatchSnapshot();
expect(stringify(true, FileType.RAW)).toMatchSnapshot();
});
});