-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.ts
119 lines (107 loc) · 2.57 KB
/
utils_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
import {
assertToken,
assertToken68,
divideWhile,
isToken68,
trimStartBy,
} from "./utils.ts";
import {
assert,
assertEquals,
assertFalse,
assertThrows,
describe,
it,
} from "./_dev_deps.ts";
describe("isToken68", () => {
it("should return true", () => {
const table: string[] = [
"a",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"0123456789",
"~",
"+",
"/",
"-",
"_",
".",
"a=",
"a===========",
];
table.forEach((input) => {
assert(isToken68(input));
});
});
it("should return false", () => {
const table: string[] = [
"",
" ",
" a",
"=",
"あ",
":",
"`",
"a=a",
];
table.forEach((input) => {
assertFalse(isToken68(input));
});
});
});
describe("assertToken68", () => {
it("should return void if the input is valid token68", () => {
assertFalse(assertToken68("a=="));
});
it("should throw error if the input is invalid token68", () => {
assertThrows(() => assertToken68(""));
assertThrows(() => assertToken68("?"));
});
});
describe("assertToken", () => {
it("should return void if the input is valid token", () => {
assertFalse(assertToken("a"));
});
it("should throw error if the input is invalid token", () => {
assertThrows(() => assertToken(""));
assertThrows(() => assertToken("a=="));
});
});
describe("divideWhile", () => {
it("should return null if the input does not match", () => {
assertEquals(divideWhile("", () => false), null);
assertEquals(divideWhile("a", () => false), null);
assertEquals(divideWhile("abc", (str) => str === "b"), null);
});
it("should return null if the input does not match", () => {
assertEquals(divideWhile("abc", (str) => str === "a"), ["a", "bc"]);
assertEquals(divideWhile("abcCBA", (str) => /[a-z]/.test(str)), [
"abc",
"CBA",
]);
assertEquals(
divideWhile("あabc亜", (str) => /[a-z]/.test(str) || str === "あ"),
[
"あabc",
"亜",
],
);
assertEquals(divideWhile("abcCBA", (str) => /.*/.test(str)), [
"abcCBA",
"",
]);
});
});
describe("trimStartBy", () => {
it("should return trimmed string", () => {
const table: [string, string, string][] = [
["abc", "a", "bc"],
["abcdefg", "abc", "defg"],
["abcdefg", "aaa", "abcdefg"],
["\\\\abc", "\\", "abc"],
];
table.forEach(([input, separator, expected]) => {
assertEquals(trimStartBy(input, separator), expected);
});
});
});