-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
455 lines (455 loc) · 13.3 KB
/
index.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import type { Equal, Expect } from 'ctx-core/test'
import { test } from 'uvu'
import { equal, is, throws } from 'uvu/assert'
import {
type Be,
be_,
be__has_,
be__val_,
be_map__find,
type BeMapO,
ctx__clear,
ctx__delete,
ctx__new,
ctx__set,
type ctx_s_T,
type ctx_T,
globalThis__be_,
id_be_,
ns_be_,
ns_ctx__new,
ns_id_be_,
ondelete_be_,
type wide_ctx_s_T,
type wide_ctx_T
} from './index.js'
test.after(()=>{
delete (globalThis as { root_be?:Be<unknown> }).root_be
})
test('globalThis__be_', ()=>{
const ctx = ctx__new()
const be = globalThis__be_(()=>1, { id: 'root_be' })
equal(be(ctx), 1)
const be2 = globalThis__be_(()=>1, { id: 'root_be' })
equal(be2(ctx), 1)
})
test('be_|s|BeMap', ()=>{
const ctx = ctx__new()
let incrementer_num = 0
const incrementer = ()=>++incrementer_num
const root_ = be_(()=>
incrementer(),
{ id: 'root_' })
const child_ = be_(ctx=>
root_(ctx) + incrementer(),
{ id: 'child_' })
const child1_ = be_(ctx=>
root_(ctx) + child_(ctx),
{ id: 'child1_' })
equal(root_(ctx), 1)
equal(ctx.s[''].get('root_')![0], 1)
equal(child_(ctx), 3)
equal(ctx.s[''].get('child_')![0], 3)
equal(child1_(ctx), 4)
equal(ctx.s[''].get('child1_')![0], 4)
})
test('be_|ns_ctx__new|-ns', ()=>{
const ctx0 = ctx__new()
const ctx1 = ctx__new()
const ctx = ns_ctx__new(ctx0, ctx1)
const root_ = be_(()=>
1,
{ id: 'root_' })
equal(root_(ctx1), 1)
equal(root_(ctx), 1)
equal(ctx0.s[''].has(root_.id), true)
equal(ctx1.s[''].has(root_.id), true)
const child_ = be_(ctx=>
root_(ctx) + 1,
{ id: 'child_' })
equal(child_(ctx), 2)
equal(ctx0.s[''].has(child_.id), true)
equal(ctx1.s[''].has(child_.id), false)
})
test('be_|ns_ctx__new', ()=>{
const ctx0 = ctx__new()
const ctx1 = ctx__new()
const ctx2 = ctx__new()
const ctx3 = ctx__new()
const ctx = ns_ctx__new(ctx0, ctx1, ctx2, ctx3)
const root_ = be_(()=>
1,
{ id: 'root_' })
equal(root_(ctx3), 1)
equal(root_(ctx), 1)
equal(ctx0.s[''].has(root_.id), true)
equal(ctx1.s[''].has(root_.id), false)
equal(ctx2.s[''].has(root_.id), false)
equal(ctx3.s[''].has(root_.id), true)
const child_ = be_(
ctx=>root_(ctx) + 1,
{ id: 'child_' })
equal(child_(ctx), 2)
equal(ctx0.s[''].has(child_.id), true)
equal(ctx1.s[''].has(child_.id), false)
equal(ctx2.s[''].has(child_.id), false)
equal(ctx3.s[''].has(child_.id), false)
})
test('be_|ns', ()=>{
/* eslint-disable @typescript-eslint/no-unused-vars */
const ctx0 = ctx__new()
const ctx1 = ns_ctx__new('ctx1')
const ctx = ns_ctx__new(ctx0, ctx1)
const be__ctx_a:wide_ctx_T<'ctx1'>[] = []
const root_ = be_<number, 'ctx1'>(
ctx=>{
be__ctx_a.push(ctx)
return 1
}, {
id: 'root_',
ns: 'ctx1',
})
equal(root_(ctx), 1)
equal(be__ctx_a, [ctx])
// @ts-expect-error TS2322
equal(ctx0.s[''].has(root_.id), false)
// @ts-expect-error TS2322
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(root_.id), true)
})
test('be_|ns|types', ()=>{
/* eslint-disable @typescript-eslint/no-unused-vars */
const ctx0 = ctx__new()
type test_ctx0_s = Expect<Equal<typeof ctx0.s, ctx_s_T<''>>>
// @ts-expect-error TS2345
type test_ctx0_s_fail = Expect<Equal<typeof ctx0.s, ctx_s_T<'fail'>>>
type test_ctx0 = Expect<Equal<typeof ctx0, ctx_T<''>>>
// @ts-expect-error TS2345
type test_ctx0_fail = Expect<Equal<typeof ctx0, ctx_T<'fail'>>>
const ctx1 = ns_ctx__new('ctx1')
type test_ctx1_s = Expect<Equal<typeof ctx1.s, ctx_s_T<'ctx1'>>>
// @ts-expect-error TS2345
type test_ctx1_fail = Expect<Equal<typeof ctx1, ctx_T<'fail'>>>
// @ts-expect-error TS2345
type test_ctx1_s_fail = Expect<Equal<typeof ctx1, ctx_s_T<''|'fail'>>>
type test_ctx1_w_empty_string = Expect<Equal<typeof ctx1, ctx_T<'ctx1'>>>
type test_ctx1 = Expect<Equal<typeof ctx1, ctx_T<'ctx1'>>>
const ctx = ns_ctx__new(ctx0, ctx1)
type test_ctx_s_wide_T = Expect<Equal<
typeof ctx.s, typeof ctx extends wide_ctx_s_T<''|'ctx1'>
? typeof ctx.s
: never>>
type test_ctx_s_BeMapO = Expect<Equal<
typeof ctx.s, typeof ctx.s extends BeMapO<''|'ctx1'>
? typeof ctx.s
: never>>
type test_ctx_s = Expect<Equal<typeof ctx.s, ctx_s_T<''|'ctx1'>>>
type test_ctx_extends = Expect<Equal<
typeof ctx, typeof ctx extends wide_ctx_T<''|'ctx1'>
? typeof ctx
: never>>
type test_ctx_Ctx = Expect<Equal<typeof ctx, ctx_T<''|'ctx1'>>>
const root_ = be_<number, 'ctx1'>(
ctx=>{
type test_be_ctx_argument = Expect<Equal<typeof ctx, wide_ctx_T<'ctx1'>>>
return 1
}, {
ns: 'ctx1',
id: 'root_'
})
/* eslint-enable @typescript-eslint/no-unused-vars */
})
test('be_|ctx_T generic type', ()=>{
const valid_ctx = ns_ctx__new('test_ns')
const val_ = be_<boolean, 'test_ns'>(()=>
true,
{ id: 'val_', ns: 'test_ns' })
val_(valid_ctx)
// @ts-expect-error TS2322
throws(()=>val_(ctx__new()))
})
test('be_|ctx_T|ns', ()=>{
const ctx0 = ctx__new()
const ctx1 = ctx__new()
const ctx = ns_ctx__new(ctx0, ctx1)
const nested__ctx_ = be_<[ctx_T]>(ctx=>
[ctx],
{ id: 'nested__ctx_' })
equal(nested__ctx_(ctx), [ctx])
})
test('ns_be_', ()=>{
const valid_ctx = ns_ctx__new('test_ns')
const val_ = ns_be_('test_ns', ()=>true)
equal(val_(valid_ctx), true)
// @ts-expect-error TS2322
throws(()=>val_(ctx__new()))
})
test('id_be_', ()=>{
const valid_ctx = ctx__new()
const val_ = id_be_('test_id', ()=>true)
equal(val_(valid_ctx), true)
equal(val_.id, 'test_id')
// @ts-expect-error TS2322
throws(()=>val_(ns_ctx__new('test_ns')))
})
test('ns_id_be_', ()=>{
const valid_ctx = ns_ctx__new('test_ns')
const val_ = ns_id_be_('test_ns', 'test_id', ()=>true)
equal(val_(valid_ctx), true)
equal(val_.id, 'test_id')
// @ts-expect-error TS2322
throws(()=>val_(ctx__new()))
})
test('ctx__set', ()=>{
const ctx0 = ns_ctx__new('ctx0')
const ctx1 = ctx__new()
const ns_ctx = ns_ctx__new(ctx1, ctx0)
ctx__set(ns_ctx, 'key', 2, 'ctx0')
// @ts-expect-error TS7053
equal(ctx0.s[''], undefined)
equal(ctx0.s['ctx0'].get('key')![0], 2)
equal(ctx1.s[''].has('key'), false)
ctx__set(ns_ctx, 'key', 1)
// @ts-expect-error TS7053
equal(ctx0.s[''], undefined)
equal(ctx0.s['ctx0'].get('key')![0], 2)
equal(ctx1.s[''].get('key')![0], 1)
})
test('ctx__delete|id', ()=>{
const ctx0 = ctx__new()
const key_ = be_(()=>
true,
{ id: 'key' })
ctx__delete(ctx0, key_)
equal(ctx0.s[''].has('key'), false)
key_(ctx0)
equal(ctx0.s[''].get('key')![0], true)
ctx__delete(ctx0, key_)
equal(ctx0.s[''].has('key'), false)
const ctx1 = ctx__new()
const ns_ctx = ns_ctx__new(ctx0, ctx1, 'test_ns')
key_(ctx0)
key_(ctx1)
equal(ctx0.s[''].get('key')![0], true)
equal(ctx1.s[''].get('key')![0], true)
ctx__delete(ns_ctx, key_)
equal(ctx0.s[''].has('key'), false)
equal(ctx1.s[''].has('key'), true)
const ns_key_ = be_(()=>
true,
{ id: 'key', ns: 'test_ns' })
ns_key_(ns_ctx)
equal(ns_ctx.s['test_ns'].get('key')![0], true)
ctx__delete(ns_ctx, 'key')
equal(ns_ctx.s['test_ns'].get('key')![0], true)
ctx__delete(ns_ctx, 'key', 'test_ns')
equal(ns_ctx.s['test_ns'].has('key'), false)
})
test('ctx__delete|be', ()=>{
const ctx0 = ctx__new()
const num_ = be_(()=>1)
ctx__delete(ctx0, num_)
equal(ctx0.s[''].has(num_.id), false)
num_(ctx0)
equal(ctx0.s[''].has(num_.id), true)
ctx__delete(ctx0, num_)
equal(ctx0.s[''].has(num_.id), false)
num_(ctx0)
equal(ctx0.s[''].has(num_.id), true)
ctx__delete(ctx0, num_)
equal(ctx0.s[''].has(num_.id), false)
const ctx1 = ns_ctx__new('ctx1')
const ns_ctx = ns_ctx__new(ctx0, ctx1)
num_(ctx0)
// @ts-expect-error TS7053
throws(()=>num_(ctx1), Error(`ctx_no_ns: 'ctx1'`))
equal(ctx0.s[''].has(num_.id), true)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
ctx__delete(ns_ctx, num_)
equal(ctx0.s[''].has(num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
num_(ctx0)
// @ts-expect-error TS7053
throws(()=>num_(ctx1), `ctx_no_ns: 'ctx1'`)
equal(ctx0.s[''].has(num_.id), true)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
ctx__delete(ns_ctx, num_)
equal(ctx0.s[''].has(num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
const ns__num_ =
ns_be_('ctx1', ()=>1)
ns__num_(ns_ctx)
// @ts-expect-error TS2345
equal(ctx0.s[''].has(ns__num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(ns__num_.id), true)
ctx__delete(ns_ctx, ns__num_)
// @ts-expect-error TS2345
equal(ctx0.s[''].has(ns__num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(ns__num_.id), false)
ns__num_(ns_ctx)
// @ts-expect-error TS2345
equal(ctx0.s[''].has(ns__num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(ns__num_.id), true)
ctx__delete(ns_ctx, ns__num_)
// @ts-expect-error TS2345
equal(ctx0.s[''].has(ns__num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(ns__num_.id), false)
ns__num_(ns_ctx)
// @ts-expect-error TS2345
equal(ctx0.s[''].has(ns__num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(ns__num_.id), true)
ctx__delete(ns_ctx, ns__num_, '')
// @ts-expect-error TS2345
equal(ctx0.s[''].has(ns__num_.id), false)
// @ts-expect-error TS7053
equal(ctx1.s[''], undefined)
equal(ctx1.s['ctx1'].has(ns__num_.id), true)
})
test('be__has_', ()=>{
const ctx0 = ctx__new()
const key_ = be_(()=>
true,
{ id: 'key' })
ctx__delete(ctx0, 'key')
equal(be__has_('key', ctx0), false)
key_(ctx0)
equal(be__has_('key', ctx0), true)
ctx__delete(ctx0, 'key')
equal(be__has_('key', ctx0), false)
const ctx1 = ctx__new()
const ns_ctx = ns_ctx__new(ctx0, ctx1, 'test_ns')
key_(ctx1)
equal(be__has_('key', ns_ctx), false)
equal(be__has_('key', ns_ctx, 'test_ns'), false)
key_(ctx0)
equal(be__has_('key', ns_ctx), true)
equal(be__has_('key', ns_ctx, 'test_ns'), false)
const ns_key_ = be_(()=>
true,
{ id: 'key', ns: 'test_ns' })
ns_key_(ns_ctx)
equal(be__has_('key', ns_ctx, 'test_ns'), true)
})
test('be_map__find', ()=>{
const ctx0 = ctx__new()
const be_map0 = ctx0.s['']
const key_ = be_(()=>
true,
{ id: 'key' })
ctx__delete(ctx0, 'key')
equal(be_map__find('key', ctx0), undefined)
key_(ctx0)
equal(be_map__find('key', ctx0), be_map0)
ctx__delete(ctx0, 'key')
equal(be_map__find('key', ctx0), undefined)
const ctx1 = ctx__new()
const be_map1 = ctx1.s['']
const ns_ctx = ns_ctx__new(ctx0, ctx1, 'test_ns')
is.not(ns_ctx.s.test_ns, be_map0)
is.not(ns_ctx.s.test_ns, be_map1)
key_(ctx1)
equal(be_map__find('key', ns_ctx), undefined)
equal(be_map__find('key', ns_ctx, 'test_ns'), undefined)
key_(ctx0)
equal(be_map__find('key', ns_ctx), be_map0)
equal(be_map__find('key', ns_ctx, 'test_ns'), undefined)
const ns_key_ = be_(()=>
true,
{ id: 'key', ns: 'test_ns' })
ns_key_(ns_ctx)
equal(be_map__find('key', ns_ctx, 'test_ns'), ns_ctx.s.test_ns)
})
test('be__val_', ()=>{
const ctx0 = ctx__new()
const ctx1 = ctx__new()
const ns_ctx = ns_ctx__new(ctx0, ctx1, 'test_ns')
const val_ = be_(()=>
true,
{ id: 'val_' })
equal(val_(ns_ctx), true)
equal(ns_ctx.s[''].get(val_.id)![0], true)
equal(be__val_(val_, ns_ctx), true)
equal(be__val_('val_', ns_ctx), true)
ctx__set(ns_ctx, val_, false)
equal(val_(ns_ctx), false)
equal(be__val_(val_, ns_ctx), false)
equal(be__val_('val_', ns_ctx), false)
equal(be__val_(val_, ns_ctx, 'test_ns'), undefined)
equal(be__val_('val_', ns_ctx, 'test_ns'), undefined)
const ns_val_ = be_(()=>
true,
{ id: 'val_', ns: 'test_ns' })
ns_val_(ns_ctx)
equal(be__val_(val_, ns_ctx, 'test_ns'), true)
equal(be__val_('val_', ns_ctx, 'test_ns'), true)
})
test('ondelete_be_', ()=>{
const ondelete0_arg_aa:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>][] = []
const ondelete1_arg_aa:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>][] = []
const _ondelete0 = (...arg_a:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>])=>
ondelete0_arg_aa.push(arg_a)
const _ondelete1 = (...arg_a:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>])=>
ondelete1_arg_aa.push(arg_a)
const be =
ondelete_be_<number>((ctx, { ondelete })=>{
ondelete(_ondelete0)
ondelete(_ondelete1)
return 1
})
const ctx = ctx__new()
equal(be(ctx), 1)
equal(ondelete0_arg_aa, [])
equal(ondelete1_arg_aa, [])
ctx__delete(ctx, be)
equal(ondelete0_arg_aa, [[1, ctx, be]])
equal(ondelete1_arg_aa, [[1, ctx, be]])
equal(be(ctx), 1)
equal(ondelete0_arg_aa, [[1, ctx, be]])
equal(ondelete1_arg_aa, [[1, ctx, be]])
ctx__delete(ctx, be)
equal(ondelete0_arg_aa, [[1, ctx, be], [1, ctx, be]])
equal(ondelete1_arg_aa, [[1, ctx, be], [1, ctx, be]])
})
test('ctx__clear', ()=>{
const ondelete0_arg_aa:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>][] = []
const ondelete1_arg_aa:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>][] = []
const _ondelete0 = (...arg_a:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>])=>
ondelete0_arg_aa.push(arg_a)
const _ondelete1 = (...arg_a:[val:number, ctx:ctx_T, be:Be<number, ''|'test_ns'>])=>
ondelete1_arg_aa.push(arg_a)
const be0 =
ondelete_be_<number>((ctx, { ondelete })=>{
ondelete(_ondelete0)
ondelete(_ondelete1)
return 1
})
const be1 =
ondelete_be_<number, 'test_ns'>((ctx, { ondelete })=>{
ondelete(_ondelete0)
ondelete(_ondelete1)
return 1
}, { ns: 'test_ns' })
const ctx = ns_ctx__new('', 'test_ns')
equal(be0(ctx), 1)
equal(be1(ctx), 1)
equal(ondelete0_arg_aa, [])
equal(ondelete1_arg_aa, [])
ctx__clear(ctx)
equal(ondelete0_arg_aa, [[1, ctx, be0], [1, ctx, be1]])
equal(ondelete1_arg_aa, [[1, ctx, be0], [1, ctx, be1]])
})
test.run()