-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.ts
More file actions
144 lines (131 loc) · 6.06 KB
/
index.spec.ts
File metadata and controls
144 lines (131 loc) · 6.06 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
142
143
144
import { expect, it, describe } from "bun:test";
import { readVariablePrecisionDate, showVariablePrecisionDate, UnshowableVariablePrecisionDate } from '.'
describe('showVariablePrecisionDate', () => {
it("can show day, month, or year", () => {
expect(showVariablePrecisionDate(
{ precision: 'day', year: 2025, month: 2, day: 4 }))
.toEqual('2025-02-04');
expect(showVariablePrecisionDate(
{ precision: 'month', year: 2025, month: 2 }))
.toEqual('2025-02');
expect(showVariablePrecisionDate(
{ precision: 'year', year: 2025 }))
.toEqual('2025');
});
it("can show weird or invalid dates", () => {
expect(showVariablePrecisionDate(
{ precision: 'day', year: 0, month: 0, day: 0 }))
.toEqual('0000-00-00');
expect(showVariablePrecisionDate(
{ precision: 'day', year: 123456, month: 345678, day: 567123 }))
.toEqual('123456-345678-567123');
expect(showVariablePrecisionDate(
{ precision: 'month', year: 0, month: 0 }))
.toEqual('0000-00');
expect(showVariablePrecisionDate(
{ precision: 'month', year: 123456, month: 345678 }))
.toEqual('123456-345678');
expect(showVariablePrecisionDate(
{ precision: 'year', year: 0 }))
.toEqual('0000');
expect(showVariablePrecisionDate(
{ precision: 'year', year: 123456 }))
.toEqual('123456');
})
it("rejects non-integer or negative numbers", () => {
expect(() => showVariablePrecisionDate(
{ precision: 'day', year: -1, month: 0, day: 0 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'day', year: 0, month: -1, day: 0 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'day', year: 0, month: -1, day: -1 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'day', year: 1.5, month: 0, day: 0 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'day', year: 0, month: 1.5, day: 0 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'day', year: 0, month: 0, day: 1.5 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'month', year: -1, month: 0 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'month', year: 0, month: -1 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'month', year: 1.5, month: 0 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'month', year: 0, month: 1.5 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'year', year: -1 }))
.toThrow(UnshowableVariablePrecisionDate)
expect(() => showVariablePrecisionDate(
{ precision: 'year', year: 1.5 }))
.toThrow(UnshowableVariablePrecisionDate)
})
})
describe('readVariablePrecisionDate', () => {
it("can read day, month, or year", () => {
expect(readVariablePrecisionDate('2025-02-04'))
.toEqual({ precision: 'day', year: 2025, month: 2, day: 4 });
expect(readVariablePrecisionDate('2025-02'))
.toEqual({ precision: 'month', year: 2025, month: 2 });
expect(readVariablePrecisionDate('2025'))
.toEqual({ precision: 'year', year: 2025 });
});
it("disregards leading zeroes", () => {
expect(readVariablePrecisionDate('02025-02-04'))
.toEqual(readVariablePrecisionDate('2025-2-4'))
expect(readVariablePrecisionDate('002025-002-004'))
.toEqual(readVariablePrecisionDate('2025-2-4'))
})
it("can read weird or invalid dates", () => {
expect(readVariablePrecisionDate('0'))
.toEqual({ precision: 'year', year: 0 });
expect(readVariablePrecisionDate('0-0'))
.toEqual({ precision: 'month', year: 0, month: 0 });
expect(readVariablePrecisionDate('0-0-0'))
.toEqual({ precision: 'day', year: 0, month: 0, day: 0 });
expect(readVariablePrecisionDate('1-2-3'))
.toEqual({ precision: 'day', year: 1, month: 2, day: 3 });
expect(readVariablePrecisionDate('00'))
.toEqual({ precision: 'year', year: 0 });
expect(readVariablePrecisionDate('00-00'))
.toEqual({ precision: 'month', year: 0, month: 0 });
expect(readVariablePrecisionDate('00-00-00'))
.toEqual({ precision: 'day', year: 0, month: 0, day: 0 });
expect(readVariablePrecisionDate('123456'))
.toEqual({ precision: 'year', year: 123456 });
expect(readVariablePrecisionDate('123456-345678'))
.toEqual({ precision: 'month', year: 123456, month: 345678 });
expect(readVariablePrecisionDate('123456-345678-567123'))
.toEqual({ precision: 'day', year: 123456, month: 345678, day: 567123 });
})
it("rejects malformed input", () => {
expect(readVariablePrecisionDate('')).toBeUndefined()
expect(readVariablePrecisionDate('a')).toBeUndefined()
expect(readVariablePrecisionDate('-')).toBeUndefined()
expect(readVariablePrecisionDate('-1')).toBeUndefined()
expect(readVariablePrecisionDate('a1')).toBeUndefined()
expect(readVariablePrecisionDate('a-1')).toBeUndefined()
expect(readVariablePrecisionDate('2025 ')).toBeUndefined()
expect(readVariablePrecisionDate(' 2025')).toBeUndefined()
expect(readVariablePrecisionDate('-2025')).toBeUndefined()
expect(readVariablePrecisionDate('2025-')).toBeUndefined()
expect(readVariablePrecisionDate('2025--1')).toBeUndefined()
expect(readVariablePrecisionDate('2025 1')).toBeUndefined()
expect(readVariablePrecisionDate('2025- 1')).toBeUndefined()
expect(readVariablePrecisionDate('2025 -1')).toBeUndefined()
expect(readVariablePrecisionDate('2025 - 1')).toBeUndefined()
expect(readVariablePrecisionDate('1\n')).toBeUndefined()
expect(readVariablePrecisionDate('1\n2')).toBeUndefined()
expect(readVariablePrecisionDate('\n1')).toBeUndefined()
})
})