aboutsummaryrefslogtreecommitdiff
path: root/test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test.ts')
-rw-r--r--test.ts127
1 files changed, 122 insertions, 5 deletions
diff --git a/test.ts b/test.ts
index c149137..75bbced 100644
--- a/test.ts
+++ b/test.ts
@@ -10,9 +10,12 @@ import {
assertThrows
} from "@std/assert";
+import { parseArgs } from "@std/cli";
+
//// util
import { ChronVer, ChronVerError } from "./mod.ts";
+import { preserveNegativeNumbers } from "./cli.ts";
const validVersions = [
"2024.04.03",
@@ -24,7 +27,13 @@ const validVersions = [
"2024.04.03-break",
"2024.04.03.1-break",
"2024.04.03-feature-name",
- "2024.04.03-feature.5"
+ "2024.04.03-feature-break",
+ "2024.04.03-breaking",
+ "2024.04.03--ui",
+ "2024.02.29",
+ "0001.01.01",
+ "9999.12.31",
+ "2024.04.03.9007199254740991"
];
const invalidVersions = [
@@ -39,7 +48,21 @@ const invalidVersions = [
"2024.04.03.1.2",
"24.04.03",
"2024.4.03",
- "2024.04.3"
+ "2024.04.3",
+ "",
+ "0000.01.01",
+ "2023.02.29",
+ "2024.04.31",
+ "2024.04.03.0",
+ "2024.04.03.01",
+ "2024.04.03.0-break",
+ "2024.04.03-feature.5",
+ "2024.04.03.1-feature.5",
+ "2024.04.03-break.5",
+ "2024.04.03--",
+ "2024.04.03---",
+ "2024.04.03-feature_name",
+ "2024.04.03.9007199254740992"
];
@@ -77,13 +100,29 @@ Deno.test("ChronVer constructor", async(test) => {
});
await test.step("should parse feature versions with changeset", () => {
- const version = new ChronVer("2024.04.03-feature.5");
+ const version = new ChronVer("2024.04.03.5-feature");
assertEquals(version.feature, "feature");
assertEquals(version.changeset, 5);
assertEquals(version.isBreaking, false);
});
+ await test.step("should reject a changeset after the label", () => {
+ assertThrows(() => new ChronVer("2024.04.03-feature.5"), ChronVerError);
+ assertThrows(() => new ChronVer("2024.04.03-break.5"), ChronVerError);
+ });
+
+ await test.step("should treat a feature containing break as a feature", () => {
+ const version = new ChronVer("2024.04.03-feature-break");
+
+ assertEquals(version.feature, "feature-break");
+ assertEquals(version.isBreaking, false);
+ });
+
+ await test.step("should reject an empty string (only undefined means today)", () => {
+ assertThrows(() => new ChronVer(""), ChronVerError);
+ });
+
await test.step("should parse breaking versions", () => {
const version = new ChronVer("2024.04.03.1-break");
@@ -120,6 +159,20 @@ Deno.test("ChronVer validation", async(test) => {
await test.step("should validate negative changesets", () => {
assertThrows(() => new ChronVer("2024.04.03.-1"), ChronVerError);
});
+
+ await test.step("should reject an explicit 0 and leading zeros in the changeset", () => {
+ assertThrows(() => new ChronVer("2024.04.03.0"), ChronVerError);
+ assertThrows(() => new ChronVer("2024.04.03.01"), ChronVerError);
+ });
+
+ await test.step("should reject changesets above Number.MAX_SAFE_INTEGER", () => {
+ assertEquals(new ChronVer(`2024.04.03.${Number.MAX_SAFE_INTEGER}`).changeset, Number.MAX_SAFE_INTEGER);
+ assertThrows(() => new ChronVer("2024.04.03.9007199254740992"), ChronVerError);
+ });
+
+ await test.step("should reject features made only of hyphens", () => {
+ assertThrows(() => new ChronVer("2024.04.03--"), ChronVerError);
+ });
});
Deno.test("ChronVer comparison", async(test) => {
@@ -200,7 +253,7 @@ Deno.test("ChronVer string output", async(test) => {
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");
+ assertEquals(new ChronVer("2024.04.03.5-feature").toString(), "2024.04.03.5-feature");
});
await test.step("should format breaking versions correctly", () => {
@@ -212,6 +265,17 @@ Deno.test("ChronVer string output", async(test) => {
assertEquals(new ChronVer("2024.01.05").toString(), "2024.01.05");
assertEquals(new ChronVer("2024.12.25").toString(), "2024.12.25");
});
+
+ await test.step("should pad the year to 4 digits", () => {
+ assertEquals(new ChronVer("0001.01.01").toString(), "0001.01.01");
+ assertEquals(new ChronVer("0999.12.31").getDateString(), "0999.12.31");
+ });
+
+ await test.step("should return the parsed string for every valid version", () => {
+ for (const valid of validVersions) {
+ assertEquals(new ChronVer(valid).toString(), valid);
+ }
+ });
});
Deno.test("ChronVer increment", async(test) => {
@@ -232,6 +296,18 @@ Deno.test("ChronVer increment", async(test) => {
assertEquals(incremented.toString(), expectedDate);
});
+
+ await test.step("should drop the feature and -break on the same day", () => {
+ const today = new Date();
+ const todayStr = `${today.getFullYear()}.${(today.getMonth() + 1).toString().padStart(2, "0")}.${today.getDate().toString().padStart(2, "0")}`;
+
+ assertEquals(new ChronVer(todayStr + ".1-break").increment().toString(), todayStr + ".2");
+ assertEquals(new ChronVer(todayStr + "-feature").increment().toString(), todayStr + ".1");
+ });
+
+ await test.step("should throw when the version's date is after today", () => {
+ assertThrows(() => new ChronVer("9999.12.31").increment(), ChronVerError, "is after today");
+ });
});
Deno.test("ChronVer static methods", async(test) => {
@@ -247,7 +323,7 @@ Deno.test("ChronVer static methods", async(test) => {
}
for (const invalid of invalidVersions) {
- assertFalse(ChronVer.isValid(invalid));
+ assertFalse(ChronVer.isValid(invalid), invalid);
}
});
@@ -275,6 +351,29 @@ Deno.test("ChronVer static methods", async(test) => {
assertEquals(version.changeset, 5);
});
+ await test.step(`"fromDate" should pad the year`, () => {
+ const date = new Date(2024, 0, 1);
+ date.setFullYear(999);
+
+ assertEquals(ChronVer.fromDate(date).toString(), "0999.01.01");
+ });
+
+ await test.step(`"fromDate" should throw on a negative or non-integer changeset`, () => {
+ const date = new Date(2024, 3, 3);
+
+ assertThrows(() => ChronVer.fromDate(date, -1), ChronVerError);
+ assertThrows(() => ChronVer.fromDate(date, 1.5), ChronVerError);
+ assertThrows(() => ChronVer.fromDate(date, NaN), ChronVerError);
+ });
+
+ await test.step(`"fromDate" should throw on a year outside 1 to 9999 or an invalid date`, () => {
+ const date = new Date(2024, 0, 1);
+ date.setFullYear(10000);
+
+ assertThrows(() => ChronVer.fromDate(date), ChronVerError);
+ assertThrows(() => ChronVer.fromDate(new Date(NaN)), ChronVerError);
+ });
+
await test.step(`"sort" should order versions correctly`, () => {
const versions = [
"2024.04.05",
@@ -301,6 +400,11 @@ Deno.test("ChronVer static methods", async(test) => {
"2024.04.03"
]);
});
+
+ await test.step(`"sort" should validate every input up front`, () => {
+ assertThrows(() => ChronVer.sort(["invalid"]), ChronVerError);
+ assertThrows(() => ChronVer.sort(["2024.04.03", "2024.04.04", "invalid"]), ChronVerError);
+ });
});
Deno.test("ChronVer file operations", async(test) => {
@@ -350,6 +454,7 @@ Deno.test("ChronVer edge cases", async(test) => {
assertEquals(version.year, 1);
assertThrows(() => new ChronVer("0000.01.01"), ChronVerError);
+ assertEquals(new ChronVer("9999.12.31").year, 9999);
});
await test.step("should handle complex feature names", () => {
@@ -366,3 +471,15 @@ Deno.test("ChronVer edge cases", async(test) => {
assertThrows(() => ChronVer.compare("invalid", "2024.04.03"), ChronVerError);
});
});
+
+Deno.test("CLI argument handling", async(test) => {
+ await test.step("should keep a negative changeset as a value, not a flag", () => {
+ assertEquals(parseArgs(preserveNegativeNumbers(["format", "2024-04-03", "-1"]))._, ["format", "2024-04-03", "-1"]);
+ assertEquals(parseArgs(preserveNegativeNumbers(["--format", "2024-04-03", "-1"]), { string: ["format"] })._, ["-1"]);
+ });
+
+ await test.step("should leave arguments without negative numbers alone", () => {
+ const args = ["format", "2024-04-03", "5", "--sort-desc"];
+ assertEquals(preserveNegativeNumbers(args), args);
+ });
+});