/
context.spec.ts
98 lines (88 loc) · 2.81 KB
/
context.spec.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
import { Ctx, CtxProcessingError } from './context';
import * as fragments from './fragments';
describe('modules/manager/bazel-module/context', () => {
describe('Ctx', () => {
it('construct simple bazel_dep', () => {
const ctx = new Ctx()
.startRule('bazel_dep')
.startAttribute('name')
.addString('rules_foo')
.startAttribute('version')
.addString('1.2.3')
.endRule();
expect(ctx.results).toEqual([
fragments.record(
{
rule: fragments.string('bazel_dep'),
name: fragments.string('rules_foo'),
version: fragments.string('1.2.3'),
},
true
),
]);
});
it('construct a rule with array arg', () => {
const ctx = new Ctx()
.startRule('foo_library')
.startAttribute('name')
.addString('my_library')
.startAttribute('srcs')
.startArray()
.addString('first')
.addString('second')
.endArray()
.endRule();
expect(ctx.results).toEqual([
fragments.record(
{
rule: fragments.string('foo_library'),
name: fragments.string('my_library'),
srcs: fragments.array(
[fragments.string('first'), fragments.string('second')],
true
),
},
true
),
]);
});
describe('.currentRecord', () => {
it('returns the record fragment if it is current', () => {
const ctx = new Ctx().startRecord();
expect(ctx.currentRecord).toEqual(fragments.record());
});
it('throws if there is no current', () => {
const ctx = new Ctx();
expect(() => ctx.currentRecord).toThrow(
new Error('Requested current, but no value.')
);
});
it('throws if the current is not a record fragment', () => {
const ctx = new Ctx().startArray();
expect(() => ctx.currentRecord).toThrow(
new Error('Requested current record, but does not exist.')
);
});
});
describe('.currentArray', () => {
it('returns the array fragment if it is current', () => {
const ctx = new Ctx().startArray();
expect(ctx.currentArray).toEqual(fragments.array());
});
it('throws if the current is not a record fragment', () => {
const ctx = new Ctx().startRecord();
expect(() => ctx.currentArray).toThrow(
new Error('Requested current array, but does not exist.')
);
});
});
it('throws if add an attribute without a parent', () => {
const ctx = new Ctx().startAttribute('name');
expect(() => ctx.addString('chicken')).toThrow(
new CtxProcessingError(
fragments.attribute('name', fragments.string('chicken'))
)
);
});
});
});