-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.test.ts
267 lines (215 loc) · 5.18 KB
/
util.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { assertEquals, assertThrows } from '@std/assert';
import { array } from './arr.ts';
import { uint8, Uint8Ptr } from './int/8.ts';
import { pointer, Ptr } from './ptr.ts';
import { Struct } from './struct.ts';
import { Union } from './union.ts';
import { assignType, assignView, constant, getMembers } from './util.ts';
import { member } from './member.ts';
Deno.test('constant', () => {
class A {
public static readonly foo: string = 'bar';
static {
constant(this, 'foo');
}
}
assertEquals(Object.getOwnPropertyDescriptor(A, 'foo'), {
value: 'bar',
writable: false,
enumerable: false,
configurable: false,
});
class B {
public static readonly foo: string;
static {
constant(this, 'foo', 'bar');
}
}
assertEquals(Object.getOwnPropertyDescriptor(B, 'foo'), {
value: 'bar',
writable: false,
enumerable: false,
configurable: false,
});
class C {
declare public static readonly foo: string;
static {
constant(this, 'foo', 'bar');
}
}
assertEquals(Object.getOwnPropertyDescriptor(C, 'foo'), {
value: 'bar',
writable: false,
enumerable: false,
configurable: false,
});
class D {
public static foo: string = 'bar';
static {
// @ts-expect-error: Not readonly.
constant(this, 'foo');
}
}
assertEquals(Object.getOwnPropertyDescriptor(D, 'foo'), {
value: 'bar',
writable: false,
enumerable: false,
configurable: false,
});
});
Deno.test('assignView', () => {
const src = new Uint8Array([0xff, 0xfe, 0xfd, 0xfc]);
const dst = new Int8Array(src.length);
assignView(dst.subarray(1, 3), src.subarray(2, 4));
assertEquals(dst, new Int8Array([0, -3, -4, 0]));
assertThrows(() => assignView(dst, src.slice(1)), RangeError);
});
Deno.test('assignStruct', () => {
class Test extends Struct {
declare public alpha: number;
declare public beta: number;
static {
uint8(this, 'alpha');
uint8(this, 'beta');
}
}
class TestExt extends Test {
declare public gamma: number;
static {
uint8(this, 'gamma');
}
}
{
const src = new Test(new ArrayBuffer(Test.BYTE_LENGTH + 1), 1);
src.alpha = 65;
src.beta = 66;
const dst = new Test(new ArrayBuffer(Test.BYTE_LENGTH + 2), 2);
assignType(dst, src);
assertEquals(dst.alpha, 65);
assertEquals(dst.beta, 66);
}
{
const src = new TestExt(new ArrayBuffer(TestExt.BYTE_LENGTH + 1), 1);
src.alpha = 65;
src.beta = 66;
src.gamma = 71;
const dst = new Test(new ArrayBuffer(Test.BYTE_LENGTH + 2), 2);
assignType(dst, src);
assertEquals(dst.alpha, 65);
assertEquals(dst.beta, 66);
}
});
Deno.test('getMembers: struct', () => {
const symA = Symbol('a');
const symB = Symbol('b');
class Base extends Struct {
declare public alpha: number;
declare public [symA]: number;
declare public beta: number;
static {
uint8(this, 'alpha');
uint8(this, symA);
uint8(this, 'beta');
}
}
class Extends extends Base {
declare public gamma: number;
declare protected [symB]: number;
declare private delta: number;
static {
uint8(this, 'gamma');
uint8(this, symB as never);
uint8(this, 'delta' as never);
}
}
assertEquals(getMembers(Base), ['alpha', 'beta', symA]);
assertEquals(getMembers(Extends), [
'alpha',
'beta',
symA,
'gamma',
'delta',
symB,
]);
});
Deno.test('getMembers: union', () => {
const symA = Symbol('a');
const symB = Symbol('b');
class Base extends Union {
declare public alpha: number;
declare public [symA]: number;
declare public beta: number;
static {
uint8(this, 'alpha');
uint8(this, symA);
uint8(this, 'beta');
}
}
class Extends extends Base {
declare public gamma: number;
declare protected [symB]: number;
declare private delta: number;
static {
uint8(this, 'gamma');
uint8(this, symB as never);
uint8(this, 'delta' as never);
}
}
assertEquals(getMembers(Base), ['alpha', 'beta', symA]);
assertEquals(getMembers(Extends), [
'alpha',
'beta',
symA,
'gamma',
'delta',
symB,
]);
});
Deno.test('getMembers: pointer', () => {
assertEquals(getMembers(Ptr), []);
assertEquals(getMembers(pointer(Struct)), []);
});
Deno.test('getMembers: array', () => {
assertEquals(getMembers(array(Uint8Ptr, 8)), [0, 1, 2, 3, 4, 5, 6, 7]);
const sym = Symbol('a');
class Weird extends array(Struct, 3) {
declare public alpha: number;
declare public [sym]: number;
declare public beta: number;
static {
uint8(this, 'alpha');
uint8(this, sym);
uint8(this, 'beta');
// Extremely weird, not recommended, but not technically invalid.
// Will not added to the list.
member(Struct, this, 1);
// Will add to the list as a string, as it is not within length.
member(Struct, this, 3);
member(Struct, this, -1);
}
}
assertEquals(getMembers(Weird), [0, 1, 2, '3', 'alpha', 'beta', '-1', sym]);
});
Deno.test('getMembers: override', () => {
class Base extends Struct {
declare public alpha: number;
declare public beta: number;
static {
uint8(this, 'alpha');
uint8(this, 'beta');
}
}
class Extends extends Base {
declare public gamma: number;
static {
uint8(this, 'gamma');
uint8(this, 'beta');
}
}
assertEquals(getMembers(Base), ['alpha', 'beta']);
assertEquals(getMembers(Extends), [
'alpha',
'beta',
'gamma',
]);
});