-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlainDate_test.ts
177 lines (145 loc) · 5.31 KB
/
PlainDate_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { ComPlainDate, PlainDate } from "./PlainDate.ts";
import { assert, assertEquals, assertFalse, assertThrows } from "./dev_deps.ts";
import { PlainDateMapFn } from "./support/function-signatures.ts";
Deno.test("factory accepts number date parts", () => {
const plainDate = PlainDate({ year: 2022, month: 2, day: 2 });
assertEquals(String(plainDate), "2022-02-02");
});
Deno.test("factory accepts string date parts", () => {
const plainDate = PlainDate({ year: "2022", month: "02", day: "02" });
assertEquals(String(plainDate), "2022-02-02");
});
Deno.test("factory throws error when date is invalid", () => {
assertThrows(() => {
PlainDate({ year: NaN });
});
});
Deno.test("enumerable properties can not be set", async (t) => {
const plainDate = PlainDate({ year: "2022", month: "12", day: "22" });
for (const property in plainDate) {
await t.step(`property '${property}'`, () => {
assertThrows(() => {
// @ts-ignore: Bypass readonly
plainDate[property] = 1;
});
});
}
});
Deno.test("main string representations are the same ISO standard string", () => {
const plainDate = PlainDate({ year: 2022, month: 2, day: 2 });
const iso = "2022-02-02";
assertEquals(plainDate.iso, iso);
assertEquals(plainDate.valueOf(), iso);
assertEquals(plainDate.toString(), iso);
assertEquals(plainDate.toJSON(), iso);
assertEquals(String(plainDate), iso);
});
Deno.test("up to 6-digit years can be represented in ISO string", () => {
const plainDate = PlainDate({ year: 100000, month: 1, day: 1 });
assertEquals(plainDate.iso, "+100000-01-01");
});
Deno.test("negative years can be represented in ISO string", () => {
const plainDate = PlainDate({ year: -1, month: 1, day: 1 });
assertEquals(plainDate.iso, "-000001-01-01");
});
Deno.test("can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 6, day: 13 });
assertEquals(
plainDate.toLocaleString("sv", { dateStyle: "full" }),
"lördag 13 juni 2020",
);
});
Deno.test("can be converted to instant in UTC", () => {
const plainDate = PlainDate({ year: 2022, month: 2, day: 2 });
const time = { hour: 23, minute: 59, second: 59, millisecond: 999 };
assertEquals(
plainDate.toUtcInstant(time).toISOString(),
"2022-02-02T23:59:59.999Z",
);
});
Deno.test("converted instant in UTC is a fresh object", () => {
const plainDate = PlainDate({ year: 2022, month: 2, day: 2 });
assert(plainDate.toUtcInstant() != plainDate.toUtcInstant());
});
Deno.test("functor obeys identity law", () => {
const plainDate = PlainDate({ year: "2022", month: "12", day: "22" });
const identityFunction = <T>(x: T): T => x;
assertEquals(
String(plainDate),
String(plainDate.map(identityFunction)),
);
});
Deno.test("functor obeys composition law", () => {
const plainDate = PlainDate({ year: "2022", month: "12", day: "22" });
const addOneYear = (plainDate: ComPlainDate) => ({
...plainDate,
year: plainDate.year + 1,
});
const doubleYear = (plainDate: ComPlainDate) => ({
...plainDate,
year: plainDate.year * 2,
});
assertEquals(
String(plainDate.map(addOneYear).map(doubleYear)),
String(plainDate.map((x) => doubleYear(addOneYear(x)))),
);
});
Deno.test("can be passed through pipeline of functions", () => {
const plainDate = PlainDate({ year: "2022", month: "12", day: "22" });
const addOneYear: PlainDateMapFn = (plainDate) =>
plainDate.constructor({
...plainDate,
year: plainDate.year + 1,
});
const doubleYear: PlainDateMapFn = (plainDate) =>
plainDate.constructor({
...plainDate,
year: plainDate.year * 2,
});
assertEquals(String(plainDate.pipe(addOneYear, doubleYear)), "4046-12-22");
});
Deno.test("can be compared as primitives", () => {
assert(PlainDate({ year: 2023, month: 2 }) > PlainDate({ year: 2023 }));
assert(PlainDate({ year: 2023 }) >= PlainDate({ year: 2023 }));
assertFalse(PlainDate({ year: 2022 }) > PlainDate({ year: 2022 }));
});
Deno.test("can be compared for full equality", () => {
assert(PlainDate({ year: 2023 }).is(PlainDate({ year: 2023 })));
assert(
PlainDate({ year: 2023, month: 2, day: 3 })
.is({ year: 2023, month: 2, day: 3 }),
);
assert(
PlainDate({ year: 2023, month: 2, day: 3 })
.is({ year: "2023", month: "02", day: "03" }),
);
assertFalse(
PlainDate({ year: 2023, month: 2, day: 3 })
.is({ year: 2023, month: 2, day: 4 }),
);
assertFalse(
PlainDate({ year: 2023, month: 2, day: 3 })
.is({ year: 2023, month: 3, day: 3 }),
);
assertFalse(
PlainDate({ year: 2023, month: 2, day: 3 })
.is({ year: 2022, month: 2, day: 3 }),
);
});
Deno.test("can be compared for partial equality", () => {
assert(PlainDate({ year: 2023 }).is({ year: 2023 }));
assert(PlainDate({ year: 2023 }).is({ day: 1 }));
assert(PlainDate({ year: 2023 }).is({ month: 1 }));
assert(PlainDate({ year: 2023 }).is({ month: 1, day: 1 }));
assert(PlainDate({ year: 2023 }).is({ year: 2023, month: 1 }));
assertFalse(PlainDate({ year: 2023 }).is({ year: 2022 }));
assertFalse(PlainDate({ year: 2023 }).is({ day: 2 }));
assertFalse(PlainDate({ year: 2023 }).is({ month: 2 }));
// Empty comparison object matches, although not allowed by type
assert(
PlainDate({ year: 2023 }).is(
// @ts-ignore Force passing empty object
{},
),
);
});