/
common.spec.ts
160 lines (139 loc) · 3.85 KB
/
common.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
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
import type { lexer } from 'good-enough-parser';
import { partial } from '../../../../../test/util';
import type { Ctx } from '../types';
import {
cleanupTempVars,
coalesceVariable,
findVariable,
increaseNestingDepth,
interpolateString,
loadFromTokenMap,
prependNestingDepth,
reduceNestingDepth,
storeInTokenMap,
storeVarToken,
stripReservedPrefixFromKeyTokens,
} from './common';
describe('modules/manager/gradle/parser/common', () => {
let ctx: Ctx;
const token = partial<lexer.Token>({ value: 'test' });
beforeEach(() => {
ctx = {
packageFile: '',
fileContents: {},
recursionDepth: 0,
globalVars: {},
deps: [],
registryUrls: [],
varTokens: [],
tmpNestingDepth: [],
tmpTokenStore: {},
tokenMap: {},
};
});
it('storeVarToken', () => {
storeVarToken(ctx, token);
expect(ctx.varTokens).toStrictEqual([token]);
});
it('increaseNestingDepth', () => {
ctx.tmpNestingDepth = ctx.varTokens = [token];
increaseNestingDepth(ctx);
expect(ctx).toMatchObject({
tmpNestingDepth: [token, token],
varTokens: [],
});
});
it('reduceNestingDepth', () => {
ctx.tmpNestingDepth = [token, token];
reduceNestingDepth(ctx);
expect(ctx.tmpNestingDepth).toHaveLength(1);
});
it('prependNestingDepth', () => {
ctx.tmpNestingDepth = ctx.varTokens = [token];
prependNestingDepth(ctx);
expect(ctx.varTokens).toEqual([token, token]);
coalesceVariable(ctx);
expect(ctx).toMatchObject({
tmpNestingDepth: [{ value: 'test' }],
varTokens: [{ value: 'test.test' }],
});
});
it('storeInTokenMap', () => {
ctx.varTokens = [token];
storeInTokenMap(ctx, 'foo');
expect(ctx.tokenMap).toStrictEqual({ foo: [token] });
});
it('loadFromTokenMap', () => {
expect(() => loadFromTokenMap(ctx, 'foo')).toThrow(
'Expected token foo not found'
);
ctx.varTokens = [token];
storeInTokenMap(ctx, 'foo');
expect(loadFromTokenMap(ctx, 'foo')).toStrictEqual([token]);
});
it('cleanupTempVars', () => {
ctx.tokenMap['some'] = [token];
ctx.varTokens.push(token);
cleanupTempVars(ctx);
expect(ctx.tokenMap).toBeEmptyObject();
expect(ctx.varTokens).toBeEmptyArray();
});
it('stripReservedPrefixFromKeyTokens', () => {
const tokenValues = [
'rootProject',
'project',
'ext',
'extra',
'properties',
'foo',
];
ctx.varTokens.push(
...tokenValues.map((value) => partial<lexer.Token>({ value }))
);
stripReservedPrefixFromKeyTokens(ctx);
expect(ctx.varTokens).toStrictEqual([{ value: 'foo' }]);
});
it('coalesceVariable', () => {
const tokenValues = ['foo', 'bar', 'baz', 'qux'];
ctx.varTokens.push(
...tokenValues.map((value) => partial<lexer.Token>({ value }))
);
coalesceVariable(ctx);
expect(ctx.varTokens).toStrictEqual([{ value: 'foo.bar.baz.qux' }]);
});
it('findVariable', () => {
ctx.globalVars = {
foo: { key: 'foo', value: 'bar' },
};
expect(findVariable('unknown-global-var', ctx)).toBeUndefined();
expect(findVariable('foo', ctx)).toStrictEqual(ctx.globalVars['foo']);
});
it('interpolateString', () => {
expect(interpolateString([], ctx)).toBeEmptyString();
expect(
interpolateString(
partial<lexer.Token>([
{ type: 'string-value', value: 'foo' },
{ type: 'symbol', value: 'bar' },
{ type: 'string-value', value: 'baz' },
]),
ctx,
{
bar: { key: '', value: 'BAR' },
}
)
).toBe('fooBARbaz');
expect(
interpolateString(
partial<lexer.Token>([{ type: 'symbol', value: 'foo' }]),
ctx
)
).toBeNull();
expect(
interpolateString(
partial<lexer.Token>([{ type: '_', value: 'foo' }]),
ctx
)
).toBeNull();
});
});