Fix tests

This commit is contained in:
Dolan Miu
2022-12-29 12:12:53 +00:00
parent 4513bb529b
commit f255d4c141
6 changed files with 19 additions and 40 deletions

View File

@ -25,13 +25,6 @@ describe("values", () => {
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", () => {
@ -46,11 +39,6 @@ describe("values", () => {
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();
});
});
@ -128,7 +116,6 @@ describe("values", () => {
it("should throw on invalid values", () => {
expect(() => twipsMeasureValue(-12)).to.throw();
expect(() => twipsMeasureValue(NaN)).to.throw();
expect(() => twipsMeasureValue("foo")).to.throw();
expect(() => twipsMeasureValue("-5mm")).to.throw();
});
});
@ -153,7 +140,6 @@ describe("values", () => {
});
it("should throw on invalid values", () => {
expect(() => hpsMeasureValue(NaN)).to.throw();
expect(() => hpsMeasureValue("5FF")).to.throw();
});
});
@ -164,11 +150,6 @@ describe("values", () => {
expect(percentageValue("100%")).to.eq("100%");
expect(percentageValue("1000%")).to.eq("1000%");
});
it("should throw on invalid values", () => {
expect(() => percentageValue("0%%")).to.throw();
expect(() => percentageValue("20")).to.throw();
expect(() => percentageValue("FF%")).to.throw();
});
});
describe("measurementOrPercentValue", () => {
@ -180,8 +161,6 @@ describe("values", () => {
});
it("should throw on invalid values", () => {
expect(() => measurementOrPercentValue(NaN)).to.throw();
expect(() => measurementOrPercentValue("10%%")).to.throw();
expect(() => measurementOrPercentValue("10F")).to.throw();
});
});

View File

@ -87,7 +87,7 @@ export const uCharHexNumber = (val: string): string => hexBinary(val, 1);
// <xsd:pattern value="-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
// </xsd:restriction>
// </xsd:simpleType>
export const universalMeasureValue = (val: UniversalMeasure): string => {
export const universalMeasureValue = (val: UniversalMeasure): UniversalMeasure => {
const unit = val.slice(-2);
if (!universalMeasureUnits.includes(unit)) {
throw new Error(`Invalid unit '${unit}' specified. Valid units are ${universalMeasureUnits.join(", ")}`);
@ -96,7 +96,7 @@ export const universalMeasureValue = (val: UniversalMeasure): string => {
if (isNaN(Number(amount))) {
throw new Error(`Invalid value '${amount}' specified. Expected a valid number.`);
}
return `${Number(amount)}${unit}`;
return `${Number(amount)}${unit}` as UniversalMeasure;
};
const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"];
@ -105,12 +105,12 @@ const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"];
// <xsd:pattern value="[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
// </xsd:restriction>
// </xsd:simpleType>
export const positiveUniversalMeasureValue = (val: PositiveUniversalMeasure): string => {
export const positiveUniversalMeasureValue = (val: PositiveUniversalMeasure): PositiveUniversalMeasure => {
const value = universalMeasureValue(val);
if (parseFloat(value) < 0) {
throw new Error(`Invalid value '${value}' specified. Expected a positive number.`);
}
return value;
return value as PositiveUniversalMeasure;
};
// <xsd:simpleType name="ST_HexColor">
@ -141,7 +141,7 @@ export const hexColorValue = (val: string): string => {
// <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/>
// </xsd:simpleType>
export const signedTwipsMeasureValue = (val: UniversalMeasure | number): UniversalMeasure | number =>
typeof val === "string" ? val : decimalNumber(val);
typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val);
// <xsd:simpleType name="ST_HpsMeasure">
// <xsd:union memberTypes="s:ST_UnsignedDecimalNumber s:ST_PositiveUniversalMeasure"/>
@ -159,7 +159,7 @@ export const signedHpsMeasureValue = (val: UniversalMeasure | number): string |
// <xsd:union memberTypes="ST_UnsignedDecimalNumber ST_PositiveUniversalMeasure"/>
// </xsd:simpleType>
export const twipsMeasureValue = (val: PositiveUniversalMeasure | number): PositiveUniversalMeasure | number =>
typeof val === "string" ? val : unsignedDecimalNumber(val);
typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val);
// <xsd:simpleType name="ST_Percentage">
// <xsd:restriction base="xsd:string">
@ -196,7 +196,7 @@ export const measurementOrPercentValue = (val: number | Percentage | UniversalMe
if (val.slice(-1) === "%") {
return percentageValue(val as Percentage);
}
return val as UniversalMeasure;
return universalMeasureValue(val as UniversalMeasure);
};
// <xsd:simpleType name="ST_EighthPointMeasure">