add tests for values functions

This commit is contained in:
Tom Hunkapiller
2021-05-24 09:03:18 +03:00
parent 097c6a5962
commit 56eecef1a8
2 changed files with 104 additions and 2 deletions

102
src/file/values.spec.ts Normal file
View File

@ -0,0 +1,102 @@
import { expect } from "chai";
import {
hexColorValue,
hpsMeasureValue,
positiveUniversalMeasureValue,
signedTwipsMeasureValue,
universalMeasureValue,
unsignedDecimalNumber,
} from "./values";
describe("values", () => {
describe("universalMeasureValue", () => {
it("should allow valid values", () => {
// "-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"
expect(universalMeasureValue("-9mm")).to.eq("-9mm");
expect(universalMeasureValue("-0.5in")).to.eq("-0.5in");
expect(universalMeasureValue("20.pt")).to.eq("20pt");
expect(universalMeasureValue("5.22pc")).to.eq("5.22pc");
expect(universalMeasureValue("100 pi")).to.eq("100pi");
});
it("should throw on invalid values", () => {
expect(() => universalMeasureValue("100pp")).to.throw();
expect(() => universalMeasureValue("foo")).to.throw();
expect(() => universalMeasureValue("--in")).to.throw();
expect(() => universalMeasureValue("NaNpc")).to.throw();
expect(() => universalMeasureValue("50")).to.throw();
});
});
describe("positiveUniversalMeasureValue", () => {
it("should allow valid values", () => {
// "[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"
expect(positiveUniversalMeasureValue("9mm")).to.eq("9mm");
expect(positiveUniversalMeasureValue("0.5in")).to.eq("0.5in");
expect(positiveUniversalMeasureValue("20.pt")).to.eq("20pt");
expect(positiveUniversalMeasureValue("5.22pc")).to.eq("5.22pc");
expect(positiveUniversalMeasureValue("100 pi")).to.eq("100pi");
});
it("should throw on invalid values", () => {
expect(() => positiveUniversalMeasureValue("-9mm")).to.throw();
expect(() => positiveUniversalMeasureValue("-0.5in")).to.throw();
expect(() => positiveUniversalMeasureValue("100pp")).to.throw();
expect(() => positiveUniversalMeasureValue("foo")).to.throw();
expect(() => positiveUniversalMeasureValue("--in")).to.throw();
expect(() => positiveUniversalMeasureValue("NaNpc")).to.throw();
expect(() => positiveUniversalMeasureValue("50")).to.throw();
});
});
describe("hexColorValue", () => {
it("should allow valid values", () => {
expect(hexColorValue("auto")).to.eq("auto");
expect(hexColorValue("FF0000")).to.eq("FF0000");
expect(hexColorValue("aabbcc")).to.eq("aabbcc");
expect(hexColorValue("#BEEFEE")).to.eq("BEEFEE");
expect(hexColorValue("abcdef")).to.eq("abcdef");
});
it("should throw on invalid values", () => {
expect(() => hexColorValue("foo")).to.throw();
expect(() => hexColorValue("fff")).to.throw();
expect(() => hexColorValue("a")).to.throw();
expect(() => hexColorValue("abcde")).to.throw();
expect(() => hexColorValue("---")).to.throw();
expect(() => hexColorValue("brown")).to.throw();
});
});
describe("unsignedDecimalNumber", () => {
it("should allow valid values", () => {
expect(unsignedDecimalNumber(1243)).to.eq(1243);
expect(unsignedDecimalNumber(12.43)).to.eq(12);
expect(unsignedDecimalNumber(1e10)).to.eq(1e10);
});
it("should throw on invalid values", () => {
expect(() => unsignedDecimalNumber(NaN)).to.throw();
expect(() => unsignedDecimalNumber(-10)).to.throw();
});
});
describe("signedTwipsMeasureValue", () => {
it("should allow valid values", () => {
expect(signedTwipsMeasureValue(1243)).to.eq(1243);
expect(signedTwipsMeasureValue("-5mm")).to.eq("-5mm");
expect(signedTwipsMeasureValue("10.in")).to.eq("10in");
});
it("should throw on invalid values", () => {
expect(() => signedTwipsMeasureValue(NaN)).to.throw();
expect(() => signedTwipsMeasureValue("foo")).to.throw();
});
});
describe("hpsMeasureValue", () => {
it("should allow valid values", () => {
expect(hpsMeasureValue(1243)).to.eq(1243);
expect(hpsMeasureValue("5mm")).to.eq("5mm");
});
it("should throw on invalid values", () => {
expect(() => hpsMeasureValue(NaN)).to.throw();
expect(() => hpsMeasureValue("-5mm")).to.throw();
});
});
});

View File

@ -15,7 +15,7 @@ export function universalMeasureValue(val: string): string {
if (isNaN(Number(amount))) {
throw new Error(`Invalid value '${amount}' specified. Expected a valid number.`);
}
return val;
return `${Number(amount)}${unit}`;
}
const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"];
@ -26,7 +26,7 @@ const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"];
// </xsd:simpleType>
export function positiveUniversalMeasureValue(val: string): string {
const value = universalMeasureValue(val);
if (parseInt(value, 10) < 0) {
if (parseFloat(value) < 0) {
throw new Error(`Invalid value '${value}' specified. Expected a positive number.`);
}
return value;