-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertion.ts
More file actions
141 lines (126 loc) · 3.56 KB
/
assertion.ts
File metadata and controls
141 lines (126 loc) · 3.56 KB
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
import { AssertionError } from 'node:assert/strict'
import { DEFAULT_TOLERANCE, isClose, isGeq, isLeq } from '../common/support/floating.ts'
import { repr } from '../common/support/strings.ts'
import { type float64 } from '../common/types/numbers.ts'
export function assertTrue(value: boolean) {
if (value !== true) {
throw new AssertionError({
message: 'Expected value to be true',
actual: value,
expected: true,
operator: '===',
})
}
}
export function assertFalse(value: boolean) {
if (value !== false) {
throw new AssertionError({
message: 'Expected value to be false',
actual: value,
expected: false,
operator: '===',
})
}
}
export function assertNull(value: unknown) {
if (value !== null) {
throw new AssertionError({
message: 'Expected value to be null',
actual: value,
operator: '===',
})
}
}
export function assertUndefined(value: unknown) {
if (value !== undefined) {
throw new AssertionError({
message: 'Expected value to be undefined',
actual: value,
operator: '===',
})
}
}
export function assertEmpty(object: object) {
if (Object.keys(object).length > 0) {
throw new AssertionError({
message: 'Expected object to be empty',
actual: object,
})
}
}
export function assertNonEmpty(object: object) {
if (Object.keys(object).length === 0) {
throw new AssertionError({
message: 'Expected object to be nonempty',
actual: object,
})
}
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function assertInstanceOf(value: unknown, cls: Function) {
if (!(value instanceof cls)) {
throw new AssertionError({
message: 'Expected value to be an instance',
actual: value,
expected: cls,
operator: 'instanceof',
})
}
}
export function assertEqualRepr<T>(a: T, b: T) {
const aRepr = repr(a)
const bRepr = repr(b)
if (aRepr !== bRepr) {
throw new AssertionError({
message: 'Expected string representations to match',
actual: aRepr,
expected: bRepr,
operator: 'repr',
})
}
}
interface SupportCustomEquality {
equals(other: unknown): boolean
}
export function assertCustomEqual(a: SupportCustomEquality, b: SupportCustomEquality) {
if (!a.equals(b)) {
throw new AssertionError({
message: "Expected values to match with respect to their 'equals' method.",
actual: a,
expected: b,
operator: '.equals',
})
}
}
export function assertSameNumber(a: float64, b: float64, tolerance: float64 = DEFAULT_TOLERANCE) {
if (!isClose(a, b, tolerance)) {
throw new AssertionError({
message: `Expected values to be equal up to ${repr(tolerance)}`,
actual: a,
expected: b,
operator: 'isClose',
})
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertGreaterThan(a: number, b: number, tolerance: float64 = DEFAULT_TOLERANCE) {
if (!isGeq(a, b, tolerance)) {
throw new AssertionError({
message: `Expected value ${repr(a)} to be greater than ${repr(b)} up to ${repr(tolerance)}`,
actual: a,
expected: b,
operator: 'isGeq',
})
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertLessThan(a: number, b: number, tolerance: float64 = DEFAULT_TOLERANCE) {
if (!isLeq(a, b, tolerance)) {
throw new AssertionError({
message: `Expected value ${repr(a)} to be less than ${repr(b)} up to ${repr(tolerance)}`,
actual: a,
expected: b,
operator: 'isLeq',
})
}
}