-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_test.ts
121 lines (108 loc) · 3.11 KB
/
validate_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
import {
assertValidRangeResp,
isRangeResp,
isUnsatisfiedRange,
} from "./validate.ts";
import {
assert,
assertFalse,
assertIsError,
assertThrows,
describe,
it,
} from "./_dev_deps.ts";
import { RangeResp } from "./types.ts";
describe("assertValidRangeResp", () => {
it("should return void if the input is valid", () => {
const table: RangeResp[] = [
{ completeLength: undefined, firstPos: 0, lastPos: 1 },
{ completeLength: 100, firstPos: 0, lastPos: 1 },
{ completeLength: 100, firstPos: 0, lastPos: 0 },
{ completeLength: 1, firstPos: 0, lastPos: 0 },
// NaN is ok because each values check before
{ completeLength: NaN, firstPos: 0, lastPos: 0 },
{ completeLength: 1, firstPos: NaN, lastPos: 0 },
{ completeLength: 1, firstPos: 0, lastPos: NaN },
{ completeLength: NaN, firstPos: NaN, lastPos: NaN },
];
table.forEach((rangeResp) => {
assertFalse(assertValidRangeResp(rangeResp));
});
});
it("should throw error if the input is invalid", () => {
const table: RangeResp[] = [
{ completeLength: undefined, firstPos: 1, lastPos: 0 },
{ completeLength: 1, firstPos: 0, lastPos: 1 },
{ completeLength: 1, firstPos: 1, lastPos: 1 },
];
table.forEach((rangeResp) => {
assertThrows(() => assertValidRangeResp(rangeResp));
});
});
it("should be error message if the input is invalid", () => {
let err;
try {
assertValidRangeResp({
firstPos: 1,
lastPos: 0,
completeLength: 100,
});
} catch (e) {
err = e;
} finally {
assertIsError(
err,
Error,
`firsPos must be less than or equal to lastPos. firstPos: 1, lastPos: 0`,
);
}
});
it("should be error message if the input is invalid", () => {
let err;
try {
assertValidRangeResp({
firstPos: 1,
lastPos: 1,
completeLength: 1,
});
} catch (e) {
err = e;
} finally {
assertIsError(
err,
Error,
`lastPos must be less than completeLength. lastPos: 1, completeLength: 1`,
);
}
});
});
describe("isRangeResp", () => {
it("should return true", () => {
assert(isRangeResp({ completeLength: undefined, firstPos: 0, lastPos: 0 }));
assert(isRangeResp({ completeLength: 0, firstPos: 0, lastPos: 0 }));
});
it("should return false", () => {
assertFalse(isRangeResp({ completeLength: 0 }));
assertFalse(isRangeResp({ completeLength: 0, firstPos: 0 }));
assertFalse(isRangeResp({ completeLength: 0, lastPos: 0 }));
});
});
describe("isUnsatisfiedRange", () => {
it("should return true", () => {
assert(isUnsatisfiedRange({ completeLength: 0 }));
assert(isUnsatisfiedRange({ completeLength: 0, firstPos: 0 }));
assert(isUnsatisfiedRange({ completeLength: 0, lastPos: 0 }));
});
it("should return false", () => {
assertFalse(
isUnsatisfiedRange({
completeLength: undefined,
firstPos: 0,
lastPos: 0,
}),
);
assertFalse(
isUnsatisfiedRange({ completeLength: 0, firstPos: 0, lastPos: 0 }),
);
});
});