-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAstCoreFactory.test.ts
More file actions
129 lines (110 loc) Β· 5.83 KB
/
AstCoreFactory.test.ts
File metadata and controls
129 lines (110 loc) Β· 5.83 KB
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
import { describe, it } from 'vitest';
import { AstCoreFactory } from '../lib/AstCoreFactory.js';
describe('astCoreFactory', () => {
describe('source location management', () => {
it('gen() creates autoGenerate location', ({ expect }) => {
const factory = new AstCoreFactory();
const loc = factory.gen();
expect(loc.sourceLocationType).toBe('autoGenerate');
});
it('sourceLocation creates source location when tracking enabled', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const loc = factory.sourceLocationSource(10, 20);
expect(factory.isSourceLocationSource(loc)).toBe(true);
expect(loc).toMatchObject({ start: 10, end: 20 });
});
it('sourceLocationStringReplace creates stringReplace location', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const loc = factory.sourceLocationStringReplace('new content', 5, 15);
expect(factory.isSourceLocationStringReplace(loc)).toBe(true);
if (factory.isSourceLocationStringReplace(loc)) {
expect(loc.newSource).toBe('new content');
expect(loc.start).toBe(5);
expect(loc.end).toBe(15);
}
});
it('sourceLocationStringReplace returns gen() when tracking disabled', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: false });
const loc = factory.sourceLocationStringReplace('new content', 5, 15);
expect(factory.isSourceLocationNodeAutoGenerate(loc)).toBe(true);
});
it('sourceLocationNodeReplace with number arguments', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const loc = factory.sourceLocationNodeReplace(0, 10);
expect(factory.isSourceLocationNodeReplace(loc)).toBe(true);
expect(loc.start).toBe(0);
expect(loc.end).toBe(10);
});
it('sourceLocationNodeReplace with SourceLocationSource argument', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const sourceLoc = factory.sourceLocationSource(5, 25);
if (factory.isSourceLocationSource(sourceLoc)) {
const loc = factory.sourceLocationNodeReplace(sourceLoc);
expect(factory.isSourceLocationNodeReplace(loc)).toBe(true);
}
});
it('sourceLocationNodeReplaceUnsafe handles SourceLocationSource', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const sourceLoc = factory.sourceLocationSource(0, 10);
if (factory.isSourceLocationSource(sourceLoc)) {
const loc = factory.sourceLocationNodeReplaceUnsafe(sourceLoc);
expect(factory.isSourceLocationNodeReplace(loc)).toBe(true);
}
});
it('sourceLocationNodeReplaceUnsafe throws for unsupported types', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const genLoc = factory.gen();
expect(() => factory.sourceLocationNodeReplaceUnsafe(genLoc))
.toThrowError('Cannot convert SourceLocation');
});
it('sourceLocationInlinedSource creates inlined source location', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const innerLoc = factory.sourceLocationSource(0, 10);
// SourceLocationInlinedSource(newSource, subLoc, start, end, startOnNew?, endOnNew?)
const loc = factory.sourceLocationInlinedSource('inline source', innerLoc, 5, 15);
expect(factory.isSourceLocationInlinedSource(loc)).toBe(true);
});
it('sourceLocationNoMaterialize creates noMaterialize location', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const loc = factory.sourceLocationNoMaterialize();
expect(factory.isSourceLocationNoMaterialize(loc)).toBe(true);
});
});
describe('type checking', () => {
it('isLocalized checks for loc property', ({ expect }) => {
const factory = new AstCoreFactory();
expect(factory.isLocalized({ loc: factory.gen() })).toBe(true);
expect(factory.isLocalized({})).toBe(false);
expect(factory.isLocalized({ loc: 'not a location' })).toBe(false);
});
it('isOfType checks type property', ({ expect }) => {
const factory = new AstCoreFactory();
expect(factory.isOfType({ type: 'test' }, 'test')).toBe(true);
expect(factory.isOfType({ type: 'other' }, 'test')).toBe(false);
expect(factory.isOfType({}, 'test')).toBe(false);
});
it('isOfSubType checks type and subType properties', ({ expect }) => {
const factory = new AstCoreFactory();
expect(factory.isOfSubType({ type: 'a', subType: 'b' }, 'a', 'b')).toBe(true);
expect(factory.isOfSubType({ type: 'a', subType: 'c' }, 'a', 'b')).toBe(false);
expect(factory.isOfSubType({ type: 'x', subType: 'b' }, 'a', 'b')).toBe(false);
});
});
describe('location types', () => {
it('identifies all location types correctly', ({ expect }) => {
const factory = new AstCoreFactory({ tracksSourceLocation: true });
const genLoc = factory.gen();
expect(factory.isSourceLocationNodeAutoGenerate(genLoc)).toBe(true);
expect(factory.isSourceLocationSource(genLoc)).toBe(false);
const sourceLoc = factory.sourceLocationSource(0, 10);
expect(factory.isSourceLocationSource(sourceLoc)).toBe(true);
expect(factory.isSourceLocationNodeAutoGenerate(sourceLoc)).toBe(false);
const noMatLoc = factory.sourceLocationNoMaterialize();
expect(factory.isSourceLocationNoMaterialize(noMatLoc)).toBe(true);
const strReplaceLoc = factory.sourceLocationStringReplace('x', 0, 1);
expect(factory.isSourceLocationStringReplace(strReplaceLoc)).toBe(true);
const nodeReplaceLoc = factory.sourceLocationNodeReplace(0, 5);
expect(factory.isSourceLocationNodeReplace(nodeReplaceLoc)).toBe(true);
});
});
});