-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathnonymous.test.ts
More file actions
146 lines (128 loc) · 4.18 KB
/
Copy pathnonymous.test.ts
File metadata and controls
146 lines (128 loc) · 4.18 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import {
parseNonymousName,
serializeNonymousName,
} from '../../profile-logic/nonymous';
import type { NonymousName } from '../../profile-logic/nonymous';
describe('parseNonymousName', () => {
it('parses a simple identifier', () => {
expect(parseNonymousName('foo')).toEqual([
{ kind: 'named', name: 'foo', contributesTo: false },
]);
});
it('parses a contributes-to name', () => {
expect(parseNonymousName('foo<')).toEqual([
{ kind: 'named', name: 'foo', contributesTo: true },
]);
});
it('parses a bare anonymous segment', () => {
expect(parseNonymousName('<')).toEqual([{ kind: 'anonymous' }]);
});
it('parses a scoped name', () => {
expect(parseNonymousName('outer/inner')).toEqual([
{ kind: 'named', name: 'outer', contributesTo: false },
{ kind: 'named', name: 'inner', contributesTo: false },
]);
});
it('parses a three-level scope chain', () => {
expect(parseNonymousName('foz/baz/bay')).toEqual([
{ kind: 'named', name: 'foz', contributesTo: false },
{ kind: 'named', name: 'baz', contributesTo: false },
{ kind: 'named', name: 'bay', contributesTo: false },
]);
});
it('parses a scoped contributes-to name', () => {
expect(parseNonymousName('main/foo<')).toEqual([
{ kind: 'named', name: 'main', contributesTo: false },
{ kind: 'named', name: 'foo', contributesTo: true },
]);
});
it('parses the canonical i2</fr/< example', () => {
expect(parseNonymousName('i2</fr/<')).toEqual([
{ kind: 'named', name: 'i2', contributesTo: true },
{ kind: 'named', name: 'fr', contributesTo: false },
{ kind: 'anonymous' },
]);
});
it('parses outer/< (anonymous inside named scope)', () => {
expect(parseNonymousName('outer/<')).toEqual([
{ kind: 'named', name: 'outer', contributesTo: false },
{ kind: 'anonymous' },
]);
});
it('parses a property chain name', () => {
expect(parseNonymousName('this.eventPool_.createObject')).toEqual([
{
kind: 'named',
name: 'this.eventPool_.createObject',
contributesTo: false,
},
]);
});
it('parses a numeric element access name', () => {
expect(parseNonymousName('arr[0]')).toEqual([
{ kind: 'named', name: 'arr[0]', contributesTo: false },
]);
});
it('returns anonymous for an empty string', () => {
expect(parseNonymousName('')).toEqual([{ kind: 'anonymous' }]);
});
});
describe('serializeNonymousName', () => {
it('serializes a simple named local', () => {
const name: NonymousName = [
{ kind: 'named', name: 'foo', contributesTo: false },
];
expect(serializeNonymousName(name)).toBe('foo');
});
it('serializes a contributes-to local', () => {
const name: NonymousName = [
{ kind: 'named', name: 'foo', contributesTo: true },
];
expect(serializeNonymousName(name)).toBe('foo<');
});
it('serializes an anonymous local', () => {
const name: NonymousName = [{ kind: 'anonymous' }];
expect(serializeNonymousName(name)).toBe('<');
});
it('serializes a scoped name', () => {
const name: NonymousName = [
{ kind: 'named', name: 'outer', contributesTo: false },
{ kind: 'named', name: 'inner', contributesTo: false },
];
expect(serializeNonymousName(name)).toBe('outer/inner');
});
it('round-trips all parse test cases', () => {
const cases = [
'foo',
'foo<',
'<',
'outer/inner',
'foz/baz/bay',
'main/foo<',
'i2</fr/<',
'outer/<',
'this.eventPool_.createObject',
'arr[0]',
];
for (const s of cases) {
expect(serializeNonymousName(parseNonymousName(s))).toBe(s);
}
});
it('round-trips every example from the module docstring', () => {
const cases = [
'foo',
'foo<',
'obj.method',
'outer/inner',
'outer/inner<',
'outer/<',
'i2</fr/<',
];
for (const s of cases) {
expect(serializeNonymousName(parseNonymousName(s))).toBe(s);
}
});
});