-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples.spec.ts
91 lines (84 loc) 路 2.14 KB
/
examples.spec.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
import { string, eq, oneOf, array, arrayOf, integer, be } from '../walli'
import { util } from '../walli'
import createFinalVerifiable from '../util/createFinalVerifiable'
import Verifiable from '../Verifiable'
import { funcify } from '../util'
const { createVerifiableClass } = util
describe('examples', function() {
it('should person', function() {
type Person = {
name: string
age: number
gender: 'F' | 'M'
father?: Person
mother?: Person
children?: Person[]
}
const person = createVerifiableClass({
getDisplayName() {
return 'person'
},
_check(req) {
return eq({
name: string,
age: integer,
gender: oneOf(['F', 'M']),
father: person().optional,
mother: person().optional,
children: arrayOf(person()).optional
}).check(req)
}
})
expect(
person().toUnlawfulString({
name: 'cy',
age: 19,
gender: 'F',
father: {
name: 'cyy',
age: 49,
gender: 'X'
}
})
).toBe("father['gender']: expected: oneOf(['F', 'M']), actual: 'X'.")
const fperson = createFinalVerifiable(person)
expect(
fperson.toUnlawfulString({
name: 'cy',
age: 19,
gender: 'F',
father: {
name: 'cyy',
age: 49,
gender: 'X'
}
})
).toBe("father['gender']: expected: oneOf(['F', 'M']), actual: 'X'.")
// Or using es6 syntax
class ES6Person extends Verifiable {
static displayName = 'person'
_check(req) {
return person().check(req)
}
}
expect(
createFinalVerifiable(ES6Person).toUnlawfulString({
name: 'cy',
age: 19,
gender: 'F',
father: {
name: 'cyy',
age: 49,
gender: 'X'
}
})
).toBe("father['gender']: expected: oneOf(['F', 'M']), actual: 'X'.")
})
it('should stringify', function() {
const a = { a: '2' }
a['circle'] = a
expect(be(a).toUnlawfulString('xxx')).toBe(
"expected: {a:'2', circle:[Circular]}, actual: 'xxx'."
)
})
})