11import * as vm from 'vm' ;
2+ import * as assert from 'uvu/assert' ;
3+ import * as uvu from 'uvu' ;
24import { devalue } from './devalue.js' ;
35
4- let passed = 0 ;
5- let failed = 0 ;
6-
76/**
87 * @typedef {(name: string, input: any, expected: string) => void } TestFunction
98 */
@@ -12,21 +11,15 @@ let failed = 0;
1211 * @param {string } name
1312 * @param {(test: TestFunction) => void } fn
1413 */
15- function describe ( name , fn ) {
16- console . group ( `\n ${ name } ` ) ;
14+ function compare ( name , fn ) {
15+ const test = uvu . suite ( name ) ;
1716 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+ } ) ;
2821 } ) ;
29- console . groupEnd ( ) ;
22+ test . run ( ) ;
3023}
3124
3225/**
@@ -61,7 +54,7 @@ function allows(name, fn) {
6154 }
6255}
6356
64- describe ( 'basics' , ( t ) => {
57+ compare ( 'basics' , ( t ) => {
6558 t ( 'number' , 42 , '42' ) ;
6659 t ( 'negative number' , - 42 , '-42' ) ;
6760 t ( 'negative zero' , - 0 , '-0' ) ;
@@ -87,7 +80,7 @@ describe('basics', (t) => {
8780 t ( 'BigInt' , BigInt ( '1' ) , '1n' ) ;
8881} ) ;
8982
90- describe ( 'strings' , ( t ) => {
83+ compare ( 'strings' , ( t ) => {
9184 t ( 'newline' , 'a\nb' , JSON . stringify ( 'a\nb' ) ) ;
9285 t ( 'double quotes' , '"yar"' , JSON . stringify ( '"yar"' ) ) ;
9386 t ( 'lone low surrogate' , 'a\uDC00b' , '"a\\uDC00b"' ) ;
@@ -100,7 +93,7 @@ describe('strings', (t) => {
10093 t ( 'backslash' , '\\' , JSON . stringify ( '\\' ) ) ;
10194} ) ;
10295
103- describe ( 'cycles' , ( t ) => {
96+ compare ( 'cycles' , ( t ) => {
10497 let map = new Map ( ) ;
10598 map . set ( 'self' , map ) ;
10699 t ( 'Map (cyclical)' , map , `(function(a){a.set("self", a);return a}(new Map))` ) ;
@@ -144,7 +137,7 @@ describe('cycles', (t) => {
144137 ) ;
145138} ) ;
146139
147- describe ( 'repetition' , ( t ) => {
140+ compare ( 'repetition' , ( t ) => {
148141 let str = 'a string' ;
149142 t (
150143 'String (repetition)' ,
@@ -153,7 +146,7 @@ describe('repetition', (t) => {
153146 ) ;
154147} ) ;
155148
156- describe ( 'XSS' , ( t ) => {
149+ compare ( 'XSS' , ( t ) => {
157150 t (
158151 'Dangerous string' ,
159152 `</script><script src='https://evil.com/script.js'>alert('pwned')</script><script>` ,
@@ -171,36 +164,27 @@ describe('XSS', (t) => {
171164 ) ;
172165} ) ;
173166
174- describe ( 'misc' , ( t ) => {
167+ compare ( 'misc' , ( t ) => {
175168 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-
181169 t ( 'cross-realm POJO' , vm . runInNewContext ( '({})' ) , '{}' ) ;
170+ } ) ;
182171
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+ } ) ;
188177
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+ } ) ;
192181
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 ] ) ;
197186
198- eval ( serialized ) ;
199- } ) ;
187+ eval ( serialized ) ;
200188} ) ;
201189
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