This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparsing.test.ts
228 lines (166 loc) · 6.16 KB
/
parsing.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import Ajv from 'ajv'
import { date, number } from 'purify-ts/Codec'
import { Right } from 'purify-ts/Either'
import {
ensureArray,
ensureBool,
ensureFloat,
ensureInt,
mwsBoolean,
mwsDate,
NextToken,
nextToken,
ServiceStatus,
serviceStatus,
} from '../../src/parsing'
const ajv = new Ajv()
describe('ensureArray', () => {
it('just extracts the elements if the value to be decoded is already an array', () => {
expect.assertions(1)
expect(ensureArray('A', number).decode({ A: [1] })).toStrictEqual(Right([1]))
})
it("extracts the elements and wraps the value to be decoded in an array if it's not already", () => {
expect.assertions(1)
expect(ensureArray('A', number).decode({ A: 1 })).toStrictEqual(Right([1]))
})
it('handles empty arrays which get deserialized as empty string', () => {
expect.assertions(1)
expect(ensureArray('A', number).decode('')).toStrictEqual(Right([]))
})
it('has an encode that does nothing', () => {
expect.assertions(1)
expect(ensureArray('A', number).encode([1])).toStrictEqual([1])
})
})
describe('ensureBool', () => {
it('decodes strings as booleans', () => {
expect.assertions(5)
expect(ensureBool.decode('true')).toStrictEqual(Right(true))
expect(ensureBool.decode('false')).toStrictEqual(Right(false))
expect(ensureBool.decode('')).not.toStrictEqual(Right(false))
expect(ensureBool.decode(1)).not.toStrictEqual(Right(true))
expect(ensureBool.decode(0)).not.toStrictEqual(Right(false))
})
it('generates a valid JSON schema for a string or boolean', () => {
expect.assertions(5)
const schema = ensureBool.schema()
expect(ajv.validate(schema, 'true')).toStrictEqual(true)
expect(ajv.validate(schema, 'false')).toStrictEqual(true)
expect(ajv.validate(schema, true)).toStrictEqual(true)
expect(ajv.validate(schema, false)).toStrictEqual(true)
expect(ajv.validate(schema, 5)).toStrictEqual(false)
})
})
describe('ensureFloat', () => {
it('decodes strings as floats', () => {
expect.assertions(1)
expect(ensureFloat.decode('5.1')).toStrictEqual(Right(5.1))
})
it('generates a valid JSON schema for a string or number', () => {
expect.assertions(3)
const schema = ensureFloat.schema()
expect(ajv.validate(schema, '5.1')).toStrictEqual(true)
expect(ajv.validate(schema, 5.1)).toStrictEqual(true)
expect(ajv.validate(schema, false)).toStrictEqual(false)
})
})
describe('ensureInt', () => {
it('decodes strings as floats', () => {
expect.assertions(1)
expect(ensureInt.decode('5')).toStrictEqual(Right(5))
})
it('generates a valid JSON schema for a string or number', () => {
expect.assertions(3)
const schema = ensureInt.schema()
expect(ajv.validate(schema, '5')).toStrictEqual(true)
expect(ajv.validate(schema, 5)).toStrictEqual(true)
expect(ajv.validate(schema, false)).toStrictEqual(false)
})
})
describe('mwsBoolean', () => {
it('decodes the string "Yes" as true', () => {
expect.assertions(1)
expect(mwsBoolean.decode('Yes')).toStrictEqual(Right(true))
})
it('decodes the string "No" as false', () => {
expect.assertions(1)
expect(mwsBoolean.decode('No')).toStrictEqual(Right(false))
})
it('decodes any other string as a failure', () => {
expect.assertions(1)
expect(mwsBoolean.decode('YES')).toMatchSnapshot()
})
it('generates a valid JSON schema for an enum', () => {
expect.assertions(3)
const schema = mwsBoolean.schema()
expect(ajv.validate(schema, 'Yes')).toStrictEqual(true)
expect(ajv.validate(schema, 'No')).toStrictEqual(true)
expect(ajv.validate(schema, 'YES')).toStrictEqual(false)
})
it('has an encode that does nothing', () => {
expect.assertions(1)
expect(mwsBoolean.encode(false)).toStrictEqual(false)
})
})
describe('mwsDate', () => {
it('is like the date decoder but it handled uri encoded strings', () => {
expect.assertions(1)
expect(mwsDate.decode('2017-02-25T18%3A10%3A21.687Z')).toStrictEqual(
date.decode('2017-02-25T18:10:21.687Z'),
)
})
it('has the same encode as the date codec', () => {
expect.assertions(1)
expect(mwsDate.encode).toStrictEqual(date.encode)
})
it('has the same schema as the date codec', () => {
expect.assertions(1)
expect(mwsDate.schema).toStrictEqual(date.schema)
})
})
describe('serviceStatus', () => {
it('decodes the string "GREEN"', () => {
expect.assertions(1)
expect(serviceStatus.decode('GREEN')).toStrictEqual(Right(ServiceStatus.Green))
})
it('decodes the string "YELLOW"', () => {
expect.assertions(1)
expect(serviceStatus.decode('YELLOW')).toStrictEqual(Right(ServiceStatus.Yellow))
})
it('decodes the string "RED"', () => {
expect.assertions(1)
expect(serviceStatus.decode('RED')).toStrictEqual(Right(ServiceStatus.Red))
})
it('decodes any other string as a failure', () => {
expect.assertions(1)
expect(serviceStatus.decode('Green')).toMatchSnapshot()
})
it('generates a valid JSON schema for an enum', () => {
expect.assertions(4)
const schema = serviceStatus.schema()
expect(ajv.validate(schema, 'YELLOW')).toStrictEqual(true)
expect(ajv.validate(schema, 'GREEN')).toStrictEqual(true)
expect(ajv.validate(schema, 'RED')).toStrictEqual(true)
expect(ajv.validate(schema, 'BLUE')).toStrictEqual(false)
})
it('has an encode that does nothing to its argument', () => {
expect.assertions(1)
expect(serviceStatus.encode(ServiceStatus.Green)).toStrictEqual(ServiceStatus.Green)
})
})
describe('nextToken', () => {
it('is a string parser that constructs a NextToken object', () => {
expect.assertions(1)
expect(nextToken('Action').decode('123')).toStrictEqual(Right(new NextToken('Action', '123')))
})
it('generates a valid JSON schema', () => {
expect.assertions(2)
const schema = nextToken('').schema()
expect(ajv.validate(schema, 'some string')).toStrictEqual(true)
expect(ajv.validate(schema, 42)).toStrictEqual(false)
})
it('has an encode that does nothing to its argument', () => {
expect.assertions(1)
expect(nextToken('').encode(new NextToken('', '123'))).toStrictEqual(new NextToken('', '123'))
})
})