1
1
import { describe , expect , test } from 'vitest'
2
2
import {
3
3
parseAsArrayOf ,
4
+ parseAsBoolean ,
4
5
parseAsFloat ,
5
6
parseAsHex ,
6
7
parseAsInteger ,
7
- parseAsIsoDateTime ,
8
8
parseAsIsoDate ,
9
+ parseAsIsoDateTime ,
10
+ parseAsNumberLiteral ,
9
11
parseAsString ,
12
+ parseAsStringEnum ,
13
+ parseAsStringLiteral ,
10
14
parseAsTimestamp
11
15
} from './parsers'
16
+ import { testParseThenSerialize , testSerializeThenParse } from './testing'
12
17
13
18
describe ( 'parsers' , ( ) => {
19
+ test ( 'parseAsString' , ( ) => {
20
+ expect ( parseAsString . parse ( '' ) ) . toBe ( '' )
21
+ expect ( parseAsString . parse ( 'foo' ) ) . toBe ( 'foo' )
22
+ testParseThenSerialize ( parseAsString , 'foo' )
23
+ testSerializeThenParse ( parseAsString , 'foo' )
24
+ } )
14
25
test ( 'parseAsInteger' , ( ) => {
15
26
expect ( parseAsInteger . parse ( '' ) ) . toBeNull ( )
16
27
expect ( parseAsInteger . parse ( '1' ) ) . toBe ( 1 )
17
28
expect ( parseAsInteger . parse ( '3.14' ) ) . toBe ( 3 )
18
29
expect ( parseAsInteger . parse ( '3,14' ) ) . toBe ( 3 )
19
30
expect ( parseAsInteger . serialize ( 3.14 ) ) . toBe ( '3' )
31
+ testParseThenSerialize ( parseAsInteger , '3' )
32
+ testSerializeThenParse ( parseAsInteger , 3 )
33
+ expect ( ( ) => testParseThenSerialize ( parseAsInteger , '3.14' ) ) . toThrow ( )
34
+ expect ( ( ) => testSerializeThenParse ( parseAsInteger , 3.14 ) ) . toThrow ( )
35
+ } )
36
+ test ( 'parseAsHex' , ( ) => {
37
+ expect ( parseAsHex . parse ( '' ) ) . toBeNull ( )
38
+ expect ( parseAsHex . parse ( '1' ) ) . toBe ( 1 )
39
+ expect ( parseAsHex . parse ( 'a' ) ) . toBe ( 0xa )
40
+ expect ( parseAsHex . parse ( 'g' ) ) . toBeNull ( )
41
+ expect ( parseAsHex . serialize ( 0xa ) ) . toBe ( '0a' )
42
+ for ( let i = 0 ; i < 256 ; i ++ ) {
43
+ const hex = i . toString ( 16 ) . padStart ( 2 , '0' )
44
+ testParseThenSerialize ( parseAsHex , hex )
45
+ testSerializeThenParse ( parseAsHex , i )
46
+ }
20
47
} )
21
48
test ( 'parseAsFloat' , ( ) => {
22
49
expect ( parseAsFloat . parse ( '' ) ) . toBeNull ( )
@@ -26,17 +53,31 @@ describe('parsers', () => {
26
53
expect ( parseAsFloat . serialize ( 3.14 ) ) . toBe ( '3.14' )
27
54
// https://0.30000000000000004.com/
28
55
expect ( parseAsFloat . serialize ( 0.1 + 0.2 ) ) . toBe ( '0.30000000000000004' )
56
+ testParseThenSerialize ( parseAsFloat , '3.14' )
57
+ testSerializeThenParse ( parseAsFloat , 3.14 )
29
58
} )
30
- test ( 'parseAsHex' , ( ) => {
31
- expect ( parseAsHex . parse ( '' ) ) . toBeNull ( )
32
- expect ( parseAsHex . parse ( '1' ) ) . toBe ( 1 )
33
- expect ( parseAsHex . parse ( 'a' ) ) . toBe ( 0xa )
34
- expect ( parseAsHex . parse ( 'g' ) ) . toBeNull ( )
35
- expect ( parseAsHex . serialize ( 0xa ) ) . toBe ( '0a' )
59
+ test ( 'parseAsBoolean' , ( ) => {
60
+ expect ( parseAsBoolean . parse ( '' ) ) . toBe ( false )
61
+ // In only triggers on 'true', everything else is false
62
+ expect ( parseAsBoolean . parse ( 'true' ) ) . toBe ( true )
63
+ expect ( parseAsBoolean . parse ( 'false' ) ) . toBe ( false )
64
+ expect ( parseAsBoolean . parse ( '0' ) ) . toBe ( false )
65
+ expect ( parseAsBoolean . parse ( '1' ) ) . toBe ( false )
66
+ expect ( parseAsBoolean . parse ( 'yes' ) ) . toBe ( false )
67
+ expect ( parseAsBoolean . parse ( 'no' ) ) . toBe ( false )
68
+ expect ( parseAsBoolean . serialize ( true ) ) . toBe ( 'true' )
69
+ expect ( parseAsBoolean . serialize ( false ) ) . toBe ( 'false' )
70
+ testParseThenSerialize ( parseAsBoolean , 'true' )
71
+ testSerializeThenParse ( parseAsBoolean , true )
72
+ testParseThenSerialize ( parseAsBoolean , 'false' )
73
+ testSerializeThenParse ( parseAsBoolean , false )
36
74
} )
75
+
37
76
test ( 'parseAsTimestamp' , ( ) => {
38
77
expect ( parseAsTimestamp . parse ( '' ) ) . toBeNull ( )
39
78
expect ( parseAsTimestamp . parse ( '0' ) ) . toStrictEqual ( new Date ( 0 ) )
79
+ testParseThenSerialize ( parseAsTimestamp , '0' )
80
+ testSerializeThenParse ( parseAsTimestamp , new Date ( 1234567890 ) )
40
81
} )
41
82
test ( 'parseAsIsoDateTime' , ( ) => {
42
83
expect ( parseAsIsoDateTime . parse ( '' ) ) . toBeNull ( )
@@ -48,6 +89,8 @@ describe('parsers', () => {
48
89
expect ( parseAsIsoDateTime . parse ( moment . slice ( 0 , 16 ) + 'Z' ) ) . toStrictEqual (
49
90
ref
50
91
)
92
+ testParseThenSerialize ( parseAsIsoDateTime , moment )
93
+ testSerializeThenParse ( parseAsIsoDateTime , ref )
51
94
} )
52
95
test ( 'parseAsIsoDate' , ( ) => {
53
96
expect ( parseAsIsoDate . parse ( '' ) ) . toBeNull ( )
@@ -56,12 +99,61 @@ describe('parsers', () => {
56
99
const ref = new Date ( moment )
57
100
expect ( parseAsIsoDate . parse ( moment ) ) . toStrictEqual ( ref )
58
101
expect ( parseAsIsoDate . serialize ( ref ) ) . toEqual ( moment )
102
+ testParseThenSerialize ( parseAsIsoDate , moment )
103
+ testSerializeThenParse ( parseAsIsoDate , ref )
59
104
} )
105
+ test ( 'parseAsStringEnum' , ( ) => {
106
+ enum Test {
107
+ A = 'a' ,
108
+ B = 'b' ,
109
+ C = 'c'
110
+ }
111
+ const parser = parseAsStringEnum < Test > ( Object . values ( Test ) )
112
+ expect ( parser . parse ( '' ) ) . toBeNull ( )
113
+ expect ( parser . parse ( 'a' ) ) . toBe ( 'a' )
114
+ expect ( parser . parse ( 'b' ) ) . toBe ( 'b' )
115
+ expect ( parser . parse ( 'c' ) ) . toBe ( 'c' )
116
+ expect ( parser . parse ( 'd' ) ) . toBeNull ( )
117
+ expect ( parser . serialize ( Test . A ) ) . toBe ( 'a' )
118
+ expect ( parser . serialize ( Test . B ) ) . toBe ( 'b' )
119
+ expect ( parser . serialize ( Test . C ) ) . toBe ( 'c' )
120
+ testParseThenSerialize ( parser , 'a' )
121
+ testSerializeThenParse ( parser , Test . A )
122
+ } )
123
+ test ( 'parseAsStringLiteral' , ( ) => {
124
+ const parser = parseAsStringLiteral ( [ 'a' , 'b' , 'c' ] as const )
125
+ expect ( parser . parse ( '' ) ) . toBeNull ( )
126
+ expect ( parser . parse ( 'a' ) ) . toBe ( 'a' )
127
+ expect ( parser . parse ( 'b' ) ) . toBe ( 'b' )
128
+ expect ( parser . parse ( 'c' ) ) . toBe ( 'c' )
129
+ expect ( parser . parse ( 'd' ) ) . toBeNull ( )
130
+ expect ( parser . serialize ( 'a' ) ) . toBe ( 'a' )
131
+ expect ( parser . serialize ( 'b' ) ) . toBe ( 'b' )
132
+ expect ( parser . serialize ( 'c' ) ) . toBe ( 'c' )
133
+ testParseThenSerialize ( parser , 'a' )
134
+ testSerializeThenParse ( parser , 'a' )
135
+ } )
136
+ test ( 'parseAsNumberLiteral' , ( ) => {
137
+ const parser = parseAsNumberLiteral ( [ 1 , 2 , 3 ] as const )
138
+ expect ( parser . parse ( '' ) ) . toBeNull ( )
139
+ expect ( parser . parse ( '1' ) ) . toBe ( 1 )
140
+ expect ( parser . parse ( '2' ) ) . toBe ( 2 )
141
+ expect ( parser . parse ( '3' ) ) . toBe ( 3 )
142
+ expect ( parser . parse ( '4' ) ) . toBeNull ( )
143
+ expect ( parser . serialize ( 1 ) ) . toBe ( '1' )
144
+ expect ( parser . serialize ( 2 ) ) . toBe ( '2' )
145
+ expect ( parser . serialize ( 3 ) ) . toBe ( '3' )
146
+ testParseThenSerialize ( parser , '1' )
147
+ testSerializeThenParse ( parser , 1 )
148
+ } )
149
+
60
150
test ( 'parseAsArrayOf' , ( ) => {
61
151
const parser = parseAsArrayOf ( parseAsString )
62
152
expect ( parser . serialize ( [ ] ) ) . toBe ( '' )
63
153
// It encodes its separator
64
154
expect ( parser . serialize ( [ 'a' , ',' , 'b' ] ) ) . toBe ( 'a,%2C,b' )
155
+ testParseThenSerialize ( parser , 'a,b' )
156
+ testSerializeThenParse ( parser , [ 'a' , 'b' ] )
65
157
} )
66
158
67
159
test ( 'parseServerSide with default (#384)' , ( ) => {
0 commit comments