-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdict.test.tsx
108 lines (80 loc) · 2.8 KB
/
dict.test.tsx
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
/** @jsx $mol_jsx */
namespace $ {
$mol_test({
'number'() {
const dict = new $mol_dict< number , number >()
$mol_assert_equal( dict.get( 123 ) , undefined )
$mol_assert_equal( dict.has( 123 ) , false )
dict.set( 123 , 321 )
$mol_assert_equal( dict.get( 123 ) , 321 )
$mol_assert_equal( dict.has( 123 ) , true )
dict.delete( 123 )
$mol_assert_equal( dict.get( 123 ) , undefined )
$mol_assert_equal( dict.has( 123 ) , false )
} ,
'pojo as key'() {
const dict = new $mol_dict< { foo : number } , number >()
$mol_assert_equal( dict.get({ foo : 123 }) , undefined )
$mol_assert_equal( dict.has({ foo : 123 }) , false )
dict.set( { foo : 123 } , 321 )
$mol_assert_equal( dict.get({ foo : 123 }) , 321 )
$mol_assert_equal( dict.has({ foo : 123 }) , true )
dict.delete({ foo : 123 })
$mol_assert_equal( dict.get({ foo : 123 }) , undefined )
$mol_assert_equal( dict.has({ foo : 123 }) , false )
} ,
'array as key'() {
const dict = new $mol_dict< [ number ] , number >()
$mol_assert_equal( dict.get([ 123 ]) , undefined )
$mol_assert_equal( dict.has([ 123 ]) , false )
dict.set( [ 123 ] , 321 )
$mol_assert_equal( dict.get([ 123 ]) , 321 )
$mol_assert_equal( dict.has([ 123 ]) , true )
dict.delete([ 123 ])
$mol_assert_equal( dict.get([ 123 ]) , undefined )
$mol_assert_equal( dict.has([ 123 ]) , false )
} ,
'html element as key'() {
const el = <div />
const dict = new $mol_dict< Element , number >()
$mol_assert_equal( dict.get( el ) , undefined )
$mol_assert_equal( dict.has( el ) , false )
dict.set( el , 321 )
$mol_assert_equal( dict.get( el ) , 321 )
$mol_assert_equal( dict.has( el ) , true )
$mol_assert_equal( dict.get( <div/> ) , undefined )
$mol_assert_equal( dict.has( <div/> ) , false )
dict.delete( el )
$mol_assert_equal( dict.get( el ) , undefined )
$mol_assert_equal( dict.has( el ) , false )
} ,
'for-of key restore'() {
const dict = new $mol_dict([[ [123] , 321 ]])
const keys = [] as number[][]
const vals = [] as number[]
for( const [ key , val ] of dict ) {
keys.push( key )
vals.push( val )
}
$mol_assert_like( keys, [ [123] ] )
$mol_assert_like( vals, [ 321 ] )
} ,
'method iterators key restore'() {
const dict = new $mol_dict([[ [123] , 321 ]])
$mol_assert_like( [ ... dict.keys() ], [ [123] ] )
$mol_assert_like( [ ... dict.values() ], [ 321 ] )
$mol_assert_like( [ ... dict.entries() ], [ [ [123], 321 ] ] )
} ,
'forEach key restore'() {
const dict = new $mol_dict([[ [123] , 321 ]])
const keys = [] as number[][]
const vals = [] as number[]
dict.forEach( ( val , key )=> {
keys.push( key )
vals.push( val )
} )
$mol_assert_like( keys, [ [123] ] )
$mol_assert_like( vals, [ 321 ] )
} ,
})
}