1
1
import * as vm from 'vm' ;
2
+ import * as assert from 'uvu/assert' ;
3
+ import * as uvu from 'uvu' ;
2
4
import { devalue } from './devalue.js' ;
3
5
4
- let passed = 0 ;
5
- let failed = 0 ;
6
-
7
6
/**
8
7
* @typedef {(name: string, input: any, expected: string) => void } TestFunction
9
8
*/
@@ -12,21 +11,15 @@ let failed = 0;
12
11
* @param {string } name
13
12
* @param {(test: TestFunction) => void } fn
14
13
*/
15
- function describe ( name , fn ) {
16
- console . group ( `\n ${ name } ` ) ;
14
+ function compare ( name , fn ) {
15
+ const test = uvu . suite ( name ) ;
17
16
fn ( ( name , input , expected ) => {
18
- const actual = devalue ( input ) ;
19
- if ( actual === expected ) {
20
- console . log ( `✅ ${ name } ` ) ;
21
- passed += 1 ;
22
- } else {
23
- console . log ( `❌ ${ name } ` ) ;
24
- console . log ( ` actual: ${ actual } ` ) ;
25
- console . log ( ` expected: ${ expected } ` ) ;
26
- failed += 1 ;
27
- }
17
+ test ( name , ( ) => {
18
+ const actual = devalue ( input ) ;
19
+ assert . equal ( actual , expected ) ;
20
+ } ) ;
28
21
} ) ;
29
- console . groupEnd ( ) ;
22
+ test . run ( ) ;
30
23
}
31
24
32
25
/**
@@ -61,7 +54,7 @@ function allows(name, fn) {
61
54
}
62
55
}
63
56
64
- describe ( 'basics' , ( t ) => {
57
+ compare ( 'basics' , ( t ) => {
65
58
t ( 'number' , 42 , '42' ) ;
66
59
t ( 'negative number' , - 42 , '-42' ) ;
67
60
t ( 'negative zero' , - 0 , '-0' ) ;
@@ -87,7 +80,7 @@ describe('basics', (t) => {
87
80
t ( 'BigInt' , BigInt ( '1' ) , '1n' ) ;
88
81
} ) ;
89
82
90
- describe ( 'strings' , ( t ) => {
83
+ compare ( 'strings' , ( t ) => {
91
84
t ( 'newline' , 'a\nb' , JSON . stringify ( 'a\nb' ) ) ;
92
85
t ( 'double quotes' , '"yar"' , JSON . stringify ( '"yar"' ) ) ;
93
86
t ( 'lone low surrogate' , 'a\uDC00b' , '"a\\uDC00b"' ) ;
@@ -100,7 +93,7 @@ describe('strings', (t) => {
100
93
t ( 'backslash' , '\\' , JSON . stringify ( '\\' ) ) ;
101
94
} ) ;
102
95
103
- describe ( 'cycles' , ( t ) => {
96
+ compare ( 'cycles' , ( t ) => {
104
97
let map = new Map ( ) ;
105
98
map . set ( 'self' , map ) ;
106
99
t ( 'Map (cyclical)' , map , `(function(a){a.set("self", a);return a}(new Map))` ) ;
@@ -144,7 +137,7 @@ describe('cycles', (t) => {
144
137
) ;
145
138
} ) ;
146
139
147
- describe ( 'repetition' , ( t ) => {
140
+ compare ( 'repetition' , ( t ) => {
148
141
let str = 'a string' ;
149
142
t (
150
143
'String (repetition)' ,
@@ -153,7 +146,7 @@ describe('repetition', (t) => {
153
146
) ;
154
147
} ) ;
155
148
156
- describe ( 'XSS' , ( t ) => {
149
+ compare ( 'XSS' , ( t ) => {
157
150
t (
158
151
'Dangerous string' ,
159
152
`</script><script src='https://evil.com/script.js'>alert('pwned')</script><script>` ,
@@ -171,36 +164,27 @@ describe('XSS', (t) => {
171
164
) ;
172
165
} ) ;
173
166
174
- describe ( 'misc' , ( t ) => {
167
+ compare ( 'misc' , ( t ) => {
175
168
t ( 'Object without prototype' , Object . create ( null ) , 'Object.create(null)' ) ;
176
-
177
- // let arr = [];
178
- // arr.x = 42;
179
- // test('Array with named properties', arr, `TODO`);
180
-
181
169
t ( 'cross-realm POJO' , vm . runInNewContext ( '({})' ) , '{}' ) ;
170
+ } ) ;
182
171
183
- throws ( 'throws for non-POJOs' , ( ) => {
184
- class Foo { }
185
- const foo = new Foo ( ) ;
186
- devalue ( foo ) ;
187
- } ) ;
172
+ uvu . test ( 'throws for non-POJOs' , ( ) => {
173
+ class Foo { }
174
+ const foo = new Foo ( ) ;
175
+ assert . throws ( ( ) => devalue ( foo ) ) ;
176
+ } ) ;
188
177
189
- throws ( 'throws for symbolic keys' , ( ) => {
190
- devalue ( { [ Symbol ( ) ] : null } ) ;
191
- } ) ;
178
+ uvu . test ( 'throws for symbolic keys' , ( ) => {
179
+ assert . throws ( ( ) => devalue ( { [ Symbol ( ) ] : null } ) ) ;
180
+ } ) ;
192
181
193
- allows ( 'does not create duplicate parameter names' , ( ) => {
194
- const foo = new Array ( 20000 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
195
- const bar = foo . map ( ( _ , i ) => ( { [ i ] : foo [ i ] } ) ) ;
196
- const serialized = devalue ( [ foo , ...bar ] ) ;
182
+ uvu . test ( 'does not create duplicate parameter names' , ( ) => {
183
+ const foo = new Array ( 20000 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ;
184
+ const bar = foo . map ( ( _ , i ) => ( { [ i ] : foo [ i ] } ) ) ;
185
+ const serialized = devalue ( [ foo , ...bar ] ) ;
197
186
198
- eval ( serialized ) ;
199
- } ) ;
187
+ eval ( serialized ) ;
200
188
} ) ;
201
189
202
- console . log ( `\n---\n${ passed } passed, ${ failed } failed\n` ) ;
203
-
204
- if ( failed > 0 ) {
205
- process . exit ( 1 ) ;
206
- }
190
+ uvu . test . run ( ) ;
0 commit comments