-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mts
98 lines (89 loc) · 2.29 KB
/
test.mts
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
/* eslint-env: node */
import { equal } from "node:assert/strict";
import {
isWorkZoneAwarenessMonth,
isWorkZoneAwarenessWeek,
defaultWorkZoneAwarenessMonth,
getWeekOfTheMonth,
defaultWorkZoneAwarenessWeek
} from "./index.js";
/**
* Enumerates through a range of numbers from {@link start} to {@link end}, inclusively.
* @param start - Start value
* @param end - End value
* @param step - Increment value
*/
function* getAllNumbersInRange(start: number, end: number, step = 1) {
for (let index = start; index <= end; index += step) {
yield index;
}
}
/**
* These are all of the dates that are in NWZAW.
*/
const nwzaw2023Dates = [...getAllNumbersInRange(17, 21)].map(
(d) => new Date(2023, 4 - 1, d)
);
const failDates = [
new Date(2023, 1 - 1, 1),
new Date(2023, 4 - 1, 1),
new Date(2023, 4 - 1, 4),
new Date(2023, 3 - 1, 21),
]
interface TestResult {
date: Date;
formattedDate: string;
monthResult: boolean;
weekResult: boolean;
monthExpected: boolean;
weekExpected: boolean;
}
function* testItems(
datesToTest: Iterable<Date>
): Generator<TestResult, void, unknown> {
for (const currentTestDate of datesToTest) {
const monthExpected = currentTestDate.getMonth() === defaultWorkZoneAwarenessMonth - 1;
const weekExpected = monthExpected && getWeekOfTheMonth(currentTestDate) === defaultWorkZoneAwarenessWeek;
yield {
date: currentTestDate,
formattedDate: currentTestDate.toLocaleDateString("en-US", {
dateStyle: "full",
}),
monthResult: isWorkZoneAwarenessMonth(currentTestDate),
weekResult: isWorkZoneAwarenessWeek(currentTestDate),
monthExpected,
weekExpected,
};
}
}
const testResults = testItems(nwzaw2023Dates.concat(...failDates));
// Write out to console as a table.
console.table(
[...testResults],
[
"formattedDate",
"monthResult",
"weekResult",
"monthExpected",
"weekExpected",
]
);
for (const {
// date,
formattedDate,
monthResult,
weekResult,
weekExpected,
monthExpected,
} of testResults) {
equal(
monthResult,
monthExpected,
`${formattedDate} should be detected as being in National Work Zone Awareness Month`
);
equal(
weekResult,
weekExpected,
`${formattedDate} should be detected as being in National Work Zone Awareness Week`
);
}