forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conform.test.ts
160 lines (118 loc) · 4.39 KB
/
conform.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
namespace $ {
$mol_test({
'return source when same object'() {
const target = {}
$mol_assert_equal( $mol_conform( target , target ) , target )
} ,
'return target when some is not object'() {
const obj = { a : 1 }
$mol_assert_equal( $mol_conform( true , obj ) , true )
$mol_assert_equal( $mol_conform( obj , true ) , obj )
} ,
'return target when some is null'() {
const obj = { a : 1 }
$mol_assert_equal( $mol_conform( null , obj ) , null )
$mol_assert_equal( $mol_conform( obj , null ) , obj )
} ,
'return target when some is undefined'() {
const obj = { a : 1 }
$mol_assert_equal( $mol_conform( undefined , obj ) , undefined )
$mol_assert_equal( $mol_conform( obj , undefined ) , obj )
} ,
'return target when different keys count'() {
const target = [ 1 , 2 , 3 ]
const source = [ 1 , 2 , 3 , undefined ]
const result = $mol_conform( target , source )
$mol_assert_equal( result , target )
$mol_assert_equal( result.join(',') , '1,2,3' )
} ,
'return source when array values are strong equal'() {
const source = [ 1 , 2 , 3 ]
$mol_assert_equal( $mol_conform( [ 1 , 2 , 3 ] , source ) , source )
} ,
'return source when object values are strong equal'() {
const source = { a : 1 , b : 2 }
$mol_assert_equal( $mol_conform( { a : 1 , b : 2 } , source ) , source )
} ,
'return target when some values are not equal'() {
const target = [ 1 , 2 , 3 ]
const source = [ 1 , 2 , 5 ]
const result = $mol_conform( target , source )
$mol_assert_equal( result , target )
$mol_assert_equal( result.join(',') , '1,2,3' )
} ,
'return source when values are deep equal'() {
const source = { foo : { bar : 1 } }
$mol_assert_equal( $mol_conform( { foo : { bar : 1 } } , source ) , source )
} ,
'return target with equal values from source and not equal from target'() {
const source = { foo : { xxx : 1 } , bar : { xxx : 2 } }
const target = { foo : { xxx : 1 } , bar : { xxx : 3 } }
const result = $mol_conform( target , source )
$mol_assert_equal( result , target )
$mol_assert_equal( result.foo , source.foo )
$mol_assert_equal( result.bar , target.bar )
} ,
'return target when equal but with different class'() {
const target = { '0' : 1 }
$mol_assert_equal( $mol_conform( target , [ 1 ] ) , target )
} ,
'return target when conformer for class is not defined'() {
const Obj = class {}
const source = new Obj
const target = new Obj
const result = $mol_conform( target , source )
$mol_assert_equal( result , target )
} ,
'return target when has cyclic reference'() {
const source = { foo : {} }
source['self'] = source
const target = { foo : {} }
target['self'] = target
const result = $mol_conform( target , source )
$mol_assert_equal( result , target )
$mol_assert_equal( result['self'] , target )
$mol_assert_equal( result.foo , source.foo )
} ,
'return source when equal dates'() {
const source = new Date( 12345 )
const target = new Date( 12345 )
const result = $mol_conform( target , source )
$mol_assert_equal( result , source )
} ,
'return source when equal regular expressions'() {
const source = /\x22/mig
const target = /\x22/mig
const result = $mol_conform( target , source )
$mol_assert_equal( result , source )
} ,
'return cached value if already conformed'() {
const source = { foo : { xxx : 1 } , bar : { xxx : 3 } }
const target = { foo : { xxx : 2 } , bar : { xxx : 3 } }
const result = $mol_conform( target , source )
target.foo.xxx = 1
$mol_assert_equal( $mol_conform( target.foo , source.foo ) , target.foo )
} ,
'skip readlony fields'() {
const source = { foo : {} , bar : {} }
const target = { foo : {} , bar : {} }
Object.defineProperty( target , 'bar' , { value : {} , writable : false } )
const result = $mol_conform( target , source )
$mol_assert_equal( result , target )
$mol_assert_equal( result.foo , source.foo )
$mol_assert_equal( result.bar , target.bar )
} ,
'object with NaN'() {
const source = { foo : Number.NaN }
const target = { foo : Number.NaN }
const result = $mol_conform( target , source )
$mol_assert_equal( result , source )
} ,
'array with NaN'() {
const source = [ Number.NaN ]
const target = [ Number.NaN ]
const result = $mol_conform( target , source )
$mol_assert_equal( result , source )
} ,
})
}