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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
//// import
import {
assert,
assertEquals,
assertFalse,
assertThrows
} from "@std/assert";
//// util
import { ChronVer, ChronVerError } from "./mod.ts";
const validVersions = [
"2024.04.03",
"2024.04.03.1",
"2024.04.03.15",
"2024.12.31.999",
"2024.04.03-feature",
"2024.04.03.1-feature",
"2024.04.03-break",
"2024.04.03.1-break",
"2024.04.03-feature-name",
"2024.04.03-feature.5"
];
const invalidVersions = [
"invalid",
"2024",
"2024.4.3",
"2024.04.32",
"2024.13.15",
"2024.00.15",
"2024.04.00",
"2024.04.03.-1",
"2024.04.03.1.2",
"24.04.03",
"2024.4.03",
"2024.04.3"
];
//// program
Deno.test("ChronVer constructor", async(test) => {
await test.step("should create version with current date when no input", () => {
const version = new ChronVer();
const now = new Date();
assertEquals(version.year, now.getFullYear());
assertEquals(version.month, now.getMonth() + 1);
assertEquals(version.day, now.getDate());
assertEquals(version.changeset, 0);
assertEquals(version.isBreaking, false);
});
await test.step("should parse valid version strings correctly", () => {
const version = new ChronVer("2024.04.03.5");
assertEquals(version.year, 2024);
assertEquals(version.month, 4);
assertEquals(version.day, 3);
assertEquals(version.changeset, 5);
assertEquals(version.isBreaking, false);
});
await test.step("should parse feature versions", () => {
const version = new ChronVer("2024.04.03-feature");
assertEquals(version.feature, "feature");
assertEquals(version.changeset, 0);
assertEquals(version.isBreaking, false);
});
await test.step("should parse feature versions with changeset", () => {
const version = new ChronVer("2024.04.03-feature.5");
assertEquals(version.feature, "feature");
assertEquals(version.changeset, 5);
assertEquals(version.isBreaking, false);
});
await test.step("should parse breaking versions", () => {
const version = new ChronVer("2024.04.03.1-break");
assertEquals(version.isBreaking, true);
assertEquals(version.changeset, 1);
});
await test.step("should throw on invalid formats", () => {
for (const invalid of invalidVersions) {
assertThrows(() => new ChronVer(invalid), ChronVerError);
}
});
});
Deno.test("ChronVer validation", async(test) => {
await test.step("should validate leap years correctly", () => {
const leapYear = new ChronVer("2024.02.29");
assertEquals(leapYear.day, 29);
assertThrows(() => new ChronVer("2023.02.29"), ChronVerError);
});
await test.step("should validate month boundaries", () => {
assertThrows(() => new ChronVer("2024.13.01"), ChronVerError);
assertThrows(() => new ChronVer("2024.00.01"), ChronVerError);
});
await test.step("should validate day boundaries for different months", () => {
assertThrows(() => new ChronVer("2024.02.30"), ChronVerError);
assertThrows(() => new ChronVer("2024.04.31"), ChronVerError);
assertThrows(() => new ChronVer("2024.01.32"), ChronVerError);
});
await test.step("should validate negative changesets", () => {
assertThrows(() => new ChronVer("2024.04.03.-1"), ChronVerError);
});
});
Deno.test("ChronVer comparison", async(test) => {
await test.step("should compare dates correctly", () => {
const v1 = new ChronVer("2024.04.03");
const v2 = new ChronVer("2024.04.04");
const v3 = new ChronVer("2024.05.03");
const v4 = new ChronVer("2025.04.03");
assertEquals(v1.compare(v2), -1);
assertEquals(v2.compare(v1), 1);
assertEquals(v1.compare(v3), -1);
assertEquals(v1.compare(v4), -1);
assertEquals(v1.compare(v1), 0);
});
await test.step("should compare changesets correctly", () => {
const v1 = new ChronVer("2024.04.03.1");
const v2 = new ChronVer("2024.04.03.2");
assertEquals(v1.compare(v2), -1);
assertEquals(v2.compare(v1), 1);
});
await test.step("should handle breaking changes in comparison", () => {
const normal = new ChronVer("2024.04.03.1");
const breaking = new ChronVer("2024.04.03.1-break");
assertEquals(breaking.compare(normal), 1);
assertEquals(normal.compare(breaking), -1);
});
await test.step("should treat feature versions as equal when dates match", () => {
const v1 = new ChronVer("2024.04.03-feature1");
const v2 = new ChronVer("2024.04.03-feature2");
assertEquals(v1.compare(v2), 0);
});
});
Deno.test("ChronVer utility methods", async(test) => {
await test.step(`"isNewerThan" should work correctly`, () => {
const v1 = new ChronVer("2024.04.03");
const v2 = new ChronVer("2024.04.04");
assert(v2.isNewerThan(v1));
assertFalse(v1.isNewerThan(v2));
});
await test.step(`"isOlderThan" should work correctly`, () => {
const v1 = new ChronVer("2024.04.03");
const v2 = new ChronVer("2024.04.04");
assert(v1.isOlderThan(v2));
assertFalse(v2.isOlderThan(v1));
});
await test.step(`"equals" should work correctly`, () => {
const v1 = new ChronVer("2024.04.03.1");
const v2 = new ChronVer("2024.04.03.1");
const v3 = new ChronVer("2024.04.03.2");
assert(v1.equals(v2));
assertFalse(v1.equals(v3));
});
await test.step(`"getDateString" should return correct format`, () => {
const version = new ChronVer("2024.04.03.5");
assertEquals(version.getDateString(), "2024.04.03");
});
});
Deno.test("ChronVer string output", async(test) => {
await test.step("should format basic versions correctly", () => {
assertEquals(new ChronVer("2024.04.03").toString(), "2024.04.03");
assertEquals(new ChronVer("2024.04.03.1").toString(), "2024.04.03.1");
});
await test.step("should format feature versions correctly", () => {
assertEquals(new ChronVer("2024.04.03-feature").toString(), "2024.04.03-feature");
assertEquals(new ChronVer("2024.04.03-feature.5").toString(), "2024.04.03.5-feature");
});
await test.step("should format breaking versions correctly", () => {
assertEquals(new ChronVer("2024.04.03-break").toString(), "2024.04.03-break");
assertEquals(new ChronVer("2024.04.03.1-break").toString(), "2024.04.03.1-break");
});
await test.step("should pad months and days correctly", () => {
assertEquals(new ChronVer("2024.01.05").toString(), "2024.01.05");
assertEquals(new ChronVer("2024.12.25").toString(), "2024.12.25");
});
});
Deno.test("ChronVer increment", async(test) => {
await test.step("should increment changeset for same day", () => {
const today = new Date();
const todayStr = `${today.getFullYear()}.${(today.getMonth() + 1).toString().padStart(2, "0")}.${today.getDate().toString().padStart(2, "0")}`;
const version = new ChronVer(todayStr + ".5");
const incremented = version.increment();
assertEquals(incremented.toString(), todayStr + ".6");
});
await test.step("should create new version for different day", () => {
const version = new ChronVer("2023.01.01.5");
const incremented = version.increment();
const today = new Date();
const expectedDate = `${today.getFullYear()}.${(today.getMonth() + 1).toString().padStart(2, "0")}.${today.getDate().toString().padStart(2, "0")}`;
assertEquals(incremented.toString(), expectedDate);
});
});
Deno.test("ChronVer static methods", async(test) => {
await test.step(`"compare" should work with strings`, () => {
assertEquals(ChronVer.compare("2024.04.03", "2024.04.04"), -1);
assertEquals(ChronVer.compare("2024.04.04", "2024.04.03"), 1);
assertEquals(ChronVer.compare("2024.04.03", "2024.04.03"), 0);
});
await test.step(`"isValid" should validate correctly`, () => {
for (const valid of validVersions) {
assert(ChronVer.isValid(valid));
}
for (const invalid of invalidVersions) {
assertFalse(ChronVer.isValid(invalid));
}
});
await test.step(`"parseVersion" should return correct structure`, () => {
const parsed = ChronVer.parseVersion("2024.04.03.5-feature");
assertEquals(parsed?.changeset, 5);
assertEquals(parsed?.date, "2024.04.03");
assertEquals(parsed?.version, "2024.04.03.5-feature");
assertEquals(parsed?.isBreaking, false);
assertEquals(parsed?.feature, "feature");
});
await test.step(`"parseVersion" should return null for invalid versions`, () => {
assertEquals(ChronVer.parseVersion("invalid"), null);
});
await test.step(`"fromDate" should create version from Date object`, () => {
const date = new Date(2024, 3, 3); /*** month is 0-indexed ***/
const version = ChronVer.fromDate(date, 5);
assertEquals(version.year, 2024);
assertEquals(version.month, 4);
assertEquals(version.day, 3);
assertEquals(version.changeset, 5);
});
await test.step(`"sort" should order versions correctly`, () => {
const versions = [
"2024.04.05",
"2024.04.03.1",
"2024.04.03",
"2024.04.04"
];
const sorted = ChronVer.sort(versions);
assertEquals(sorted, [
"2024.04.03",
"2024.04.03.1",
"2024.04.04",
"2024.04.05"
]);
const sortedDesc = ChronVer.sort(versions, true);
assertEquals(sortedDesc, [
"2024.04.05",
"2024.04.04",
"2024.04.03.1",
"2024.04.03"
]);
});
});
Deno.test("ChronVer file operations", async(test) => {
const testFile = "test-package.json";
await test.step(`"incrementInFile" should create version if missing`, async() => {
const testData = { name: "test-package" };
await Deno.writeTextFile(testFile, JSON.stringify(testData));
const newVersion = await ChronVer.incrementInFile(testFile);
assert(ChronVer.isValid(newVersion));
const content = await Deno.readTextFile(testFile);
const json = JSON.parse(content);
assertEquals(json.version, newVersion);
await Deno.remove(testFile);
});
await test.step(`"incrementInFile" should increment existing version`, async() => {
const today = new Date();
const todayStr = `${today.getFullYear()}.${(today.getMonth() + 1).toString().padStart(2, "0")}.${today.getDate().toString().padStart(2, "0")}`;
const testData = { name: "test-package", version: `${todayStr}.1` };
await Deno.writeTextFile(testFile, JSON.stringify(testData));
const newVersion = await ChronVer.incrementInFile(testFile);
assertEquals(newVersion, `${todayStr}.2`);
await Deno.remove(testFile);
});
await test.step(`"incrementInFile" should handle file not found`, async() => {
try {
await ChronVer.incrementInFile("nonexistent.json");
assert(false, "Expected ChronVerError to be thrown");
} catch(error) {
assert(error instanceof ChronVerError, "Should throw ChronVerError");
assert(error.message.includes("File not found"), "Should mention file not found");
}
});
});
Deno.test("ChronVer edge cases", async(test) => {
await test.step("should handle year boundaries", () => {
const version = new ChronVer("0001.01.01");
assertEquals(version.year, 1);
assertThrows(() => new ChronVer("0000.01.01"), ChronVerError);
});
await test.step("should handle complex feature names", () => {
const version = new ChronVer("2024.04.03-feature-name-123");
assertEquals(version.feature, "feature-name-123");
});
await test.step("should handle large changesets", () => {
const version = new ChronVer("2024.04.03.999999");
assertEquals(version.changeset, 999999);
});
await test.step(`"compare" should throw on invalid versions`, () => {
assertThrows(() => ChronVer.compare("invalid", "2024.04.03"), ChronVerError);
});
});
|