-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME-ts.test.ts
203 lines (168 loc) · 7.67 KB
/
README-ts.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { describe, test } from "node:test"
import assert, { fail } from "node:assert"
import { Level2Date as EdtfDate } from "./src/level2/date/index.mjs"
import { Level2Interval, Level2Interval as EdtfInterval } from "./src/level2/interval/index.mjs"
import { Level2Duration as Duration } from "./src/level2/duration/index.mjs"
import { level0Calendar } from "./src/calendar/index.mjs"
import { assertLevel2 } from "./src/level2/component/Level2TestUtil.mjs"
import { level2Calendar } from "./src/level2/Level2Calendar.mjs"
describe("Demo samples", () => {
describe("Dates", () => {
describe("instanciation", () => {
test("now", () => {
const now = new EdtfDate()
assert.ok(now.year.value >= 2025)
})
test("from date", () => {
const now = EdtfDate.fromDate(new Date(2025, 1, 28))
assert.ok(now instanceof EdtfDate)
assert.equal(now.year.value, 2025)
assert.equal(now.month.value, 2)
assert.equal(now.day.value, 28)
assert.equal(now.hour?.value, 0)
assert.equal(now.minute?.value, 0)
assert.equal(now.second?.value, 0)
})
test("instanciation", () => {
const date = new EdtfDate({year: 1972, month: 8, day: 12, hour: 16, minute: 45, second: 55})
assert.equal(date.year.value, 1972)
assert.equal(date.month.value, 8)
assert.equal(date.day.value, 12)
assert.equal(date.hour.value, 16)
assert.equal(date.minute.value, 45)
assert.equal(date.second.value, 55)
})
})
test("comparison", () => {
const maybeAugust = EdtfDate.fromString("2024-?08-15")
const aroundMarch2025 = EdtfDate.fromString("2025-03~")
assert.ok(!maybeAugust.isEqual(aroundMarch2025))
assert.ok(maybeAugust.isBefore(aroundMarch2025))
assert.ok(aroundMarch2025.isAfter(maybeAugust))
const delta = aroundMarch2025.delta(maybeAugust).toSpec()
assert.equal(delta.months, 6)
assert.equal(delta.days, 16)
})
test("uncertain", () => {
const maybeAugust = EdtfDate.fromString("2024-?08-25")
assert.equal(maybeAugust.month.value, 8)
assert.equal(maybeAugust.month.uncertainComponent, true)
assert.equal(maybeAugust.year.uncertain, true)
assert.equal(maybeAugust.year.uncertainComponent, false)
assert.equal(maybeAugust.uncertain, true)
})
test("approximate", () => {
const aroundMarch2025 = EdtfDate.fromString("2025-03~")
assert.equal(aroundMarch2025.month.approximate, true)
assert.equal(aroundMarch2025.month.value, 3)
assert.equal(aroundMarch2025.year.approximate, true)
assert.equal(aroundMarch2025.year.value, 2025)
assert.equal(aroundMarch2025.year.approximate, true)
assert.equal(aroundMarch2025.approximate, true)
})
})
describe("Durations", () => {
test("parsing", () => {
const aroundTenMinutes = Duration.fromString("P10M~")
assert.equal(aroundTenMinutes.value, 10 * level0Calendar.minute.duration)
const tenMnSpec = aroundTenMinutes.toSpec()
assert.equal(tenMnSpec.minutes, 10)
})
test("between", () => {
const start = EdtfDate.fromString("2024-01-01")
const now = new EdtfDate()
const duration = Duration.between(start, now)
assert.ok(duration.toSpec().years.value >= 1)
})
})
describe("Intervals", () => {
test("instanciation", () => {
const start = new EdtfDate({year: 1972, month: 8, day: 12, hour: 16, minute: 45, second: 55})
const end = new EdtfDate({year: 1972, month: 8, day: 12, hour: 16, minute: 45, second: 55})
const interval = new Level2Interval(start, end)
assert.equal(interval.start.year.value, 1972)
assert.equal(interval.start.month.value, 8)
assert.equal(interval.start.day.value, 12)
assert.equal(interval.start.hour.value, 16)
assert.equal(interval.start.minute.value, 45)
assert.equal(interval.start.second.value, 55)
})
test("from uncertain to approximate", {todo: true}, () => {
const maybeAugust = EdtfInterval.fromString("2023-12-?08/2024-12~")
assertLevel2(maybeAugust.start.day).to.equal(8).and.beUncertain().atTheComponentLevel().but.bePrecise()
assertLevel2(maybeAugust.end.month).to.equal(
12).and.beApproximate().atTheGroupLevel().but.beApproximate().atTheGroupLevel()
})
test("approximate", () => {
const aroundMarch2025 = EdtfDate.fromString("2025-03~")
assertLevel2(aroundMarch2025.month).to.equal(3).and.beApproximate().atTheGroupLevel().but.not.beUncertain()
assertLevel2(aroundMarch2025.year).to.equal(2025).and.beApproximate().atTheGroupLevel().but.not.beUncertain()
assert.equal(aroundMarch2025.approximate, true)
})
})
describe("prev", () => {
test("valid", () => {
const dateWithYear = EdtfDate.fromString("1985")
const nextDateWithYear = dateWithYear.previous()
assert.equal(nextDateWithYear.toString(), "1984")
const dateWithMonth = EdtfDate.fromString("1985-07")
const nextDateWithMonth = dateWithMonth.previous()
assert.equal(nextDateWithMonth.toString(), "1985-06")
const dateWithDay = EdtfDate.fromString("1985-07-25")
const nextDateWithDay = dateWithDay.previous()
assert.equal(nextDateWithDay.toString(), "1985-07-24")
const dateWithHour = EdtfDate.fromString("1985-07-25 16:00")
const nextDateWithHour = dateWithHour.previous()
assert.equal(nextDateWithHour.toString(), "1985-07-25T15:00")
const dateWithMinute = EdtfDate.fromString("1985-07-25 16:30")
const nextDateWithMinute = dateWithMinute.previous()
assert.equal(nextDateWithMinute.toString(), "1985-07-25T16:29")
const dateWithSecond = EdtfDate.fromString("1985-07-25 16:30:40")
const nextDateWithSecond = dateWithSecond.previous()
assert.equal(nextDateWithSecond.toString(), "1985-07-25T16:30:39")
})
test("overflow", () => {
const date = EdtfDate.fromString(level2Calendar.year.min.toString())
try {
date.previous()
fail("Should not allow next year before min")
} catch (e) {
assert.equal(e.message,
"year value must be >= -9007199254740991 and <= 9007199254740991, but was -9007199254740992")
}
})
})
describe("next", () => {
test("valid", () => {
const date = EdtfDate.fromString("1985")
const next = date.next()
assert.equal(next.toString(), "1986")
const dateWithMonth = EdtfDate.fromString("1985-07")
const nextDateWithMonth = dateWithMonth.next()
assert.equal(nextDateWithMonth.toString(), "1985-08")
const dateWithDay = EdtfDate.fromString("1985-07-25")
const nextDateWithDay = dateWithDay.next()
assert.equal(nextDateWithDay.toString(), "1985-07-26")
const dateWithHour = EdtfDate.fromString("1985-07-25T16:00")
dateWithHour.minute = undefined
const nextDateWithHour = dateWithHour.next()
assert.equal(nextDateWithHour.toString(), "1985-07-25T17")
const dateWithMinute = EdtfDate.fromString("1985-07-25 16:30")
const nextDateWithMinute = dateWithMinute.next()
assert.equal(nextDateWithMinute.toString(), "1985-07-25T16:31")
const dateWithSecond = EdtfDate.fromString("1985-07-25 16:30:40")
const nextDateWithSecond = dateWithSecond.next()
assert.equal(nextDateWithSecond.toString(), "1985-07-25T16:30:41")
})
test("overflow", () => {
const certainYear = EdtfDate.fromString(String(level2Calendar.year.max))
try {
certainYear.next()
fail("Should not allow next year after max")
} catch (e) {
assert.equal(e.message,
"year value must be >= -9007199254740991 and <= 9007199254740991, but was 9007199254740992")
}
})
})
})