-
Notifications
You must be signed in to change notification settings - Fork 721
/
units.test.js
300 lines (263 loc) · 8.59 KB
/
units.test.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* global test expect */
import { Duration } from "../../src/luxon";
//------
// #shiftTo()
//-------
test("Duration#shiftTo rolls milliseconds up hours and minutes", () => {
const dur = Duration.fromMillis(5760000);
expect(dur.shiftTo("hours").hours).toBe(1.6);
const mod = dur.shiftTo("hours", "minutes");
expect(mod.toObject()).toEqual({ hours: 1, minutes: 36 });
});
test("Duration#shiftTo boils hours down milliseconds", () => {
const dur = Duration.fromObject({ hours: 1 }).shiftTo("milliseconds");
expect(dur.milliseconds).toBe(3600000);
});
test("Duration boils hours down shiftTo minutes and milliseconds", () => {
const dur = Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo("minutes", "milliseconds");
expect(dur.toObject()).toEqual({ minutes: 60, milliseconds: 30000 });
});
test("Duration#shiftTo boils down and then rolls up", () => {
const dur = Duration.fromObject({ years: 2, hours: 5000 }).shiftTo("months", "days", "minutes");
expect(dur.toObject()).toEqual({ months: 30, days: 28, minutes: 8 * 60 });
});
test("Duration#shiftTo throws on invalid units", () => {
expect(() => {
Duration.fromObject({ years: 2, hours: 5000 }).shiftTo("months", "glorp");
}).toThrow();
});
test("Duration#shiftTo tacks decimals onto the end", () => {
const dur = Duration.fromObject({ minutes: 73 }).shiftTo("hours");
expect(dur.isValid).toBe(true);
expect(dur.hours).toBeCloseTo(1.2167, 4);
});
test("Duration#shiftTo deconstructs decimal inputs", () => {
const dur = Duration.fromObject({ hours: 2.3 }).shiftTo("hours", "minutes");
expect(dur.isValid).toBe(true);
expect(dur.hours).toBe(2);
expect(dur.minutes).toBeCloseTo(18, 8);
});
test("Duration#shiftTo deconstructs in cascade and tacks decimal onto the end", () => {
const dur = Duration.fromObject({ hours: 1.17 }).shiftTo("hours", "minutes", "seconds");
expect(dur.isValid).toBe(true);
expect(dur.hours).toBe(1);
expect(dur.minutes).toBe(10);
expect(dur.seconds).toBeCloseTo(12, 8);
});
test("Duration#shiftTo maintains invalidity", () => {
const dur = Duration.invalid("because").shiftTo("years");
expect(dur.isValid).toBe(false);
expect(dur.invalidReason).toBe("because");
});
test("Duration#shiftTo without any units no-ops", () => {
const dur = Duration.fromObject({ years: 3 }).shiftTo();
expect(dur.isValid).toBe(true);
expect(dur.toObject()).toEqual({ years: 3 });
});
test("Duration#shiftTo accumulates when rolling up", () => {
expect(
Duration.fromObject({ minutes: 59, seconds: 183 })
.shiftTo("hours", "minutes", "seconds")
.toObject()
).toEqual({ hours: 1, minutes: 2, seconds: 3 });
});
test("Duration#shiftTo keeps unnecessary higher-order negative units 0", () => {
expect(
Duration.fromObject({ milliseconds: -100 }).shiftTo("hours", "minutes", "seconds").toObject()
).toEqual({ hours: 0, minutes: 0, seconds: -0.1 });
});
test("Duration#shiftTo does not normalize values", () => {
// Normalizing would convert to { quarters: 4, months: 1, days: 10 }
// which would be converted back to 404 days instead
expect(
Duration.fromObject({ quarters: 0, months: 0, days: 400 }).shiftTo("days").toObject()
).toEqual({ days: 400 });
});
test("Duration#shiftTo boils hours down to hours and minutes", () => {
const dur = Duration.fromObject({ hour: 2.4 });
expect(dur.shiftTo("hours", "minutes").toObject()).toEqual({
hours: 2,
minutes: 24,
});
});
test("Duration#shiftTo handles mixed units", () => {
const dur = Duration.fromObject({ weeks: -1, days: 14 });
expect(dur.shiftTo("years", "months", "weeks").toObject()).toEqual({
years: 0,
months: 0,
weeks: 1,
});
});
//------
// #shiftToAll()
//-------
test("Duration#shiftToAll shifts to all available units", () => {
const dur = Duration.fromMillis(5760000).shiftToAll();
expect(dur.toObject()).toEqual({
years: 0,
months: 0,
weeks: 0,
days: 0,
hours: 1,
minutes: 36,
seconds: 0,
milliseconds: 0,
});
});
test("Duration#shiftToAll maintains invalidity", () => {
const dur = Duration.invalid("because").shiftToAll();
expect(dur.isValid).toBe(false);
expect(dur.invalidReason).toBe("because");
});
//------
// #normalize()
//-------
test("Duration#normalize rebalances negative units", () => {
const dur = Duration.fromObject({ years: 2, days: -2 }).normalize();
expect(dur.toObject()).toEqual({ years: 1, days: 363 });
});
test("Duration#normalize de-overflows", () => {
const dur = Duration.fromObject({ years: 2, days: 5000 }).normalize();
expect(dur.years).toBe(15);
expect(dur.days).toBe(255);
expect(dur.toObject()).toEqual({ years: 15, days: 255 });
});
test("Duration#normalize handles fully negative durations", () => {
const dur = Duration.fromObject({ years: -2, days: -5000 }).normalize();
expect(dur.toObject()).toEqual({ years: -15, days: -255 });
});
test("Duration#normalize handles the full grid partially negative durations", () => {
const sets = [
[
{ months: 1, days: 32 },
{ months: 2, days: 2 },
],
[
{ months: 1, days: 28 },
{ months: 1, days: 28 },
],
[
{ months: 1, days: -32 },
{ months: 0, days: -2 },
],
[
{ months: 1, days: -28 },
{ months: 0, days: 2 },
],
[
{ months: -1, days: 32 },
{ months: 0, days: 2 },
],
[
{ months: -1, days: 28 },
{ months: 0, days: -2 },
],
[
{ months: -1, days: -32 },
{ months: -2, days: -2 },
],
[
{ months: -1, days: -28 },
{ months: -1, days: -28 },
],
[
{ months: 0, days: 32 },
{ months: 1, days: 2 },
],
[
{ months: 0, days: 28 },
{ months: 0, days: 28 },
],
[
{ months: 0, days: -32 },
{ months: -1, days: -2 },
],
[
{ months: 0, days: -28 },
{ months: 0, days: -28 },
],
[
{ hours: 96, minutes: 0, seconds: -10 },
{ hours: 95, minutes: 59, seconds: 50 },
],
];
sets.forEach(([from, to]) => {
expect(Duration.fromObject(from).normalize().toObject()).toEqual(to);
});
});
test("Duration#normalize maintains invalidity", () => {
const dur = Duration.invalid("because").normalize();
expect(dur.isValid).toBe(false);
expect(dur.invalidReason).toBe("because");
});
test("Duration#normalize can convert all unit pairs", () => {
const units = [
"years",
"quarters",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
];
for (let i = 0; i < units.length; i++) {
for (let j = i + 1; j < units.length; j++) {
const duration = Duration.fromObject({ [units[i]]: 1, [units[j]]: 2 });
const normalizedDuration = duration.normalize().toObject();
expect(normalizedDuration[units[i]]).not.toBe(NaN);
expect(normalizedDuration[units[j]]).not.toBe(NaN);
const accurateDuration = duration.reconfigure({ conversionAccuracy: "longterm" });
const normalizedAccurateDuration = accurateDuration.normalize().toObject();
expect(normalizedAccurateDuration[units[i]]).not.toBe(NaN);
expect(normalizedAccurateDuration[units[j]]).not.toBe(NaN);
}
}
});
//------
// #rescale()
//-------
test("Duration#rescale normalizes, shifts to all units and remove units with a value of 0", () => {
const sets = [
[{ milliseconds: 90000 }, { minutes: 1, seconds: 30 }],
[
{ minutes: 70, milliseconds: 12100 },
{ hours: 1, minutes: 10, seconds: 12, milliseconds: 100 },
],
[{ months: 2, days: -30 }, { months: 1 }],
];
sets.forEach(([from, to]) => {
expect(Duration.fromObject(from).rescale().toObject()).toEqual(to);
});
});
test("Duration#rescale maintains invalidity", () => {
const dur = Duration.invalid("because").rescale();
expect(dur.isValid).toBe(false);
expect(dur.invalidReason).toBe("because");
});
//------
// #as()
//-------
test("Duration#as shifts to one unit and returns it", () => {
const dur = Duration.fromMillis(5760000);
expect(dur.as("hours")).toBe(1.6);
});
test("Duration#as returns null for invalid durations", () => {
expect(Duration.invalid("because").as("hours")).toBeFalsy();
});
//------
// #valueOf()
//-------
test("Duration#valueOf value of zero duration", () => {
const dur = Duration.fromObject({});
expect(dur.valueOf()).toBe(0);
});
test("Duration#valueOf returns as millisecond value (lower order units)", () => {
const dur = Duration.fromObject({ hours: 1, minutes: 36, seconds: 0 });
expect(dur.valueOf()).toBe(5760000);
});
test("Duration#valueOf value of the duration with lower and higher order units", () => {
const dur = Duration.fromObject({ days: 2, seconds: 1 });
expect(dur.valueOf()).toBe(172801000);
});