Skip to content

Commit

Permalink
test: wrap test strings in "def project" block
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 20, 2023
1 parent d7c4e2f commit 0f6b4da
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
39 changes: 29 additions & 10 deletions lib/helpers/regexes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,44 @@ import {
invalidSemVers,
validSemVers,
} from "../../tests/fixtures/regexes.fixture.js";
import { DEF_P_1, DEF_P_2 } from "../../tests/helpers/test.constants.js";
import { replaceVersionInContent, versionRegex } from "./regexes.js";

describe("replaceVersionInContent", () => {
it("should replace the version part of a match", () => {
expect(replaceVersionInContent(`version: "0.0.0-dev"`, "1.0.0")).toBe(
`version: "1.0.0"`,
expect(
replaceVersionInContent(
`${DEF_P_1}version:"0.0.0-dev"${DEF_P_2}`,
"1.0.0",
),
).toBe(`${DEF_P_1}version:"1.0.0"${DEF_P_2}`);

expect(replaceVersionInContent('@version "0.0.0-dev"', "1.0.0")).toBe(
'@version "1.0.0"',
);
expect(replaceVersionInContent(`@version "0.0.0-dev"`, "1.0.0")).toBe(
`@version "1.0.0"`,
});

it("should not replace the version outside of the project definition", () => {
expect(replaceVersionInContent('version: "0.0.0-dev"', "1.0.0")).toBe(
'version: "1.0.0"',
);

expect(replaceVersionInContent('@version "0.0.0-dev"', "1.0.0")).toBe(
'@version "1.0.0"',
);
});

it("should preserve indentation and newline", () => {
expect(
replaceVersionInContent(`\n version: "0.0.0-dev" \n`, "1.0.0"),
).toBe(`\n version: "1.0.0" \n`);
replaceVersionInContent(
`\n ${DEF_P_1}\n version: "0.0.0-dev"\n ${DEF_P_2} \n`,
"1.0.0",
),
).toBe(`\n ${DEF_P_1}\n version: "1.0.0"\n ${DEF_P_2} \n`);

expect(
replaceVersionInContent(`\n @version "0.0.0-dev" \n`, "1.0.0"),
).toBe(`\n @version "1.0.0" \n`);
replaceVersionInContent('\n @version "0.0.0-dev" \n', "1.0.0"),
).toBe('\n @version "1.0.0" \n');
});

it("should throw if no match is found", () => {
Expand Down Expand Up @@ -49,13 +68,13 @@ describe("versionRegex", () => {
expect.assertions(validSemVers.length * 10 + invalidSemVers.length * 2);

for (let semVer of validSemVers) {
expect(`version: ${semVer}`).not.toMatch(versionRegex);
expect("version: ${semVer}").not.toMatch(versionRegex);
expect(`version "${semVer}"`).not.toMatch(versionRegex);
expect(`versin: "${semVer}",`).not.toMatch(versionRegex);
expect(`"~> ${semVer}"`).not.toMatch(versionRegex);
expect(`tag: "${semVer}",`).not.toMatch(versionRegex);

expect(`@version ${semVer}`).not.toMatch(versionRegex);
expect("@version ${semVer}").not.toMatch(versionRegex);
expect(`@version"${semVer}"`).not.toMatch(versionRegex);
expect(`@versin "${semVer}"`).not.toMatch(versionRegex);
expect(`"~> ${semVer}"`).not.toMatch(versionRegex);
Expand Down
11 changes: 7 additions & 4 deletions tests/helpers/read-project-version.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { readProjectVersion } from "./read-project-version.js";
import { DEF_P_1, DEF_P_2 } from "./test.constants.js";

describe("readProjectVersion", () => {
it("should return version and subparts when match", () => {
const simple = readProjectVersion('version: "0.0.4"');
const simple = readProjectVersion(`${DEF_P_1}version: "0.0.4"${DEF_P_2}`);
expect(simple.version).toBe("0.0.4");
expect(simple.major).toBe("0");
expect(simple.minor).toBe("0");
expect(simple.patch).toBe("4");
expect(simple.prerelease).toBeUndefined();
expect(simple.metadata).toBeUndefined();

const prerelease = readProjectVersion('version: "1.0.0-alpha"');
const prerelease = readProjectVersion(
`${DEF_P_1}version: "1.0.0-alpha"${DEF_P_2}`,
);
expect(prerelease.version).toBe("1.0.0-alpha");
expect(prerelease.major).toBe("1");
expect(prerelease.minor).toBe("0");
expect(prerelease.patch).toBe("0");
expect(prerelease.prerelease).toBe("alpha");
expect(prerelease.metadata).toBeUndefined();

const metadata = readProjectVersion('@version "1.1.2+meta"');
const metadata = readProjectVersion(`@version "1.1.2+meta"`);
expect(metadata.version).toBe("1.1.2+meta");
expect(metadata.major).toBe("1");
expect(metadata.minor).toBe("1");
Expand All @@ -27,7 +30,7 @@ describe("readProjectVersion", () => {
expect(metadata.metadata).toBe("meta");

const preleaseAndMetadata = readProjectVersion(
'@version "1.1.2-prerelease+meta"',
`@version "1.1.2-prerelease+meta"`,
);
expect(preleaseAndMetadata.version).toBe("1.1.2-prerelease+meta");
expect(preleaseAndMetadata.major).toBe("1");
Expand Down
2 changes: 2 additions & 0 deletions tests/helpers/test.constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEF_P_1 = "def project do\n[";
export const DEF_P_2 = "]\nend";

0 comments on commit 0f6b4da

Please sign in to comment.