-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cjs
122 lines (99 loc) · 3.41 KB
/
test.cjs
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
// noinspection JSUnresolvedFunction
const { typeAssert, enableChainAPI, preventErrTrace } = require('./typeAssert.cjs')
preventErrTrace(true)
enableChainAPI()
typeAssert(1, 'number')
typeAssert('2', 'string')
const expectFailure = testedCode => {
try {
testedCode()
console.error('expected failure not caught')
process.exit(-1)
} catch (e) {
console.info('expected failure caught:', e)
}
}
expectFailure(() => typeAssert(2, 'string'))
typeAssert({ x: 1, y: '3' }, 'object')
typeAssert({ x: 1, y: '3' }, {})
typeAssert({ x: 1, y: '3' }, { x: 'number', y: 'string' })
// By this time, arrays are also considered a kind of 'object'.
// this may change in further editions
typeAssert([], 'object')
typeAssert([], {})
expectFailure(() => typeAssert([], { x: 'number' }))
expectFailure(() => typeAssert({ x: 1, y: '2' }, { x: 'number', y: 'number' }))
typeAssert([1, 2, 3], 'Array')
typeAssert([1, 2, 3], [])
typeAssert([1, 2, 3], ['number'])
typeAssert(['1', '2', '3'], ['string'])
expectFailure(() => typeAssert({}, []))
expectFailure(() => typeAssert({}, 'Array'))
expectFailure(() => typeAssert([1, 2, 3], ['string']))
const sumAssertion = 'string'.sumWith('number')
typeAssert('abc', sumAssertion)
typeAssert(123, sumAssertion)
expectFailure(() => typeAssert(() => 114514, sumAssertion))
const nullableAssertion = { x: 'number', y: 'function' }.orNull()
typeAssert({ x: 114, y: () => 514 }, nullableAssertion)
typeAssert(null, nullableAssertion)
// cannot nest nullable modification
expectFailure(() => 'number?'.orNull())
expectFailure(() => { return { x: 'string' }.orNull().orNull()} )
const compoundAssertion = {
a: 'number',
b: 'string',
c: [
{
x: 'function',
y: 'function'
}.sumWith({
x: [],
y: {}
}).orNull()
]
}
typeAssert({
a: 114,
b: '514',
c: [
null,
{
x: [1, 2, 3],
y: {
z: '4'
}
},
{
x: console.log,
y: Array.prototype.push
}
]
}, compoundAssertion)
const nonNegativeAssertion = 'number'
.chainWith(x => x > 0 || 'no negative numbers')
.chainWith(x => x <= 100 || 'no greater than 100')
typeAssert(5, nonNegativeAssertion)
expectFailure(() => typeAssert(-1, nonNegativeAssertion))
expectFailure(() => typeAssert(101, nonNegativeAssertion))
const nonNegativeAssertion2 = 'number?'
.chainWith(x => x > 0 || 'no negative numbers')
.chainWith(x => x <= 100 || 'no greater than 100')
typeAssert(5, nonNegativeAssertion2)
typeAssert(null, nonNegativeAssertion2)
typeAssert(undefined, nonNegativeAssertion2)
expectFailure(() => typeAssert('114514', nonNegativeAssertion2))
expectFailure(() => typeAssert(-1, nonNegativeAssertion2))
expectFailure(() => typeAssert(101, nonNegativeAssertion2))
typeAssert(5, 'number'.assertValue(5))
expectFailure(() => typeAssert(5, 'number'.assertValue(114514)))
const objectValueAssertion = {}.assertObjectValue('number')
typeAssert({ a: 114, b: 514 }, objectValueAssertion)
expectFailure(() => typeAssert({ a: 114, b: '514' }, objectValueAssertion))
const objectValueAssertion2 = 'object?'.assertObjectValue('number'.chainWith(x => x >= 100 || 'no less than 100'))
typeAssert({ a: 114, b: 514 }, objectValueAssertion2)
typeAssert(null, objectValueAssertion2)
typeAssert(undefined, objectValueAssertion2)
expectFailure(() => typeAssert({ a: 114, b: '514' }, objectValueAssertion2))
expectFailure(() => typeAssert({ a: 114, b: 90 }, objectValueAssertion2))
console.info('mission accomplished')