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

@ -5,9 +5,9 @@ import { FooterWrapper } from "@file/footer-wrapper";
import { HeaderWrapper } from "@file/header-wrapper";
import { VerticalAlign, VerticalAlignElement } from "@file/vertical-align";
import { OnOffElement, XmlComponent } from "@file/xml-components";
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
import { HeaderFooterReference, HeaderFooterReferenceType, HeaderFooterType } from "./properties/header-footer-reference";
import { Columns, IColumnsAttributes } from "./properties/columns";
import { DocumentGrid, IDocGridAttributesProperties } from "./properties/doc-grid";
import { ILineNumberAttributes, LineNumberType } from "./properties/line-number";
@ -17,7 +17,6 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./properties/page-num
import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size";
import { PageTextDirection, PageTextDirectionType } from "./properties/page-text-direction";
import { SectionType, Type } from "./properties/section-type";
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
export interface IHeaderFooterGroup<T> {
readonly default?: T;

View File

@ -1,5 +1,5 @@
import { StringEnumValueElement, XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values";
import { PositiveUniversalMeasure, signedTwipsMeasureValue, twipsMeasureValue, UniversalMeasure } from "@util/values";
export enum TableAnchorType {
MARGIN = "margin",
@ -55,7 +55,7 @@ export interface ITableFloatOptions {
* If relativeHorizontalPosition is also specified, then the absoluteHorizontalPosition attribute is ignored.
* If the attribute is omitted, the value is assumed to be zero.
*/
readonly absoluteHorizontalPosition?: number | string;
readonly absoluteHorizontalPosition?: number | UniversalMeasure;
/**
* Specifies a relative horizontal position for the table, relative to the horizontalAnchor attribute.
@ -86,7 +86,7 @@ export interface ITableFloatOptions {
* If relativeVerticalPosition is also specified, then the absoluteVerticalPosition attribute is ignored.
* If the attribute is omitted, the value is assumed to be zero.
*/
readonly absoluteVerticalPosition?: number | string;
readonly absoluteVerticalPosition?: number | UniversalMeasure;
/**
* Specifies a relative vertical position for the table, relative to the verticalAnchor attribute.
@ -104,25 +104,25 @@ export interface ITableFloatOptions {
* Specifies the minimum distance to be maintained between the table and the top of text in the paragraph
* below the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero.
*/
readonly bottomFromText?: number | string;
readonly bottomFromText?: number | PositiveUniversalMeasure;
/**
* Specifies the minimum distance to be maintained between the table and the bottom edge of text in the paragraph
* above the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero.
*/
readonly topFromText?: number | string;
readonly topFromText?: number | PositiveUniversalMeasure;
/**
* Specifies the minimum distance to be maintained between the table and the edge of text in the paragraph
* to the left of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero.
*/
readonly leftFromText?: number | string;
readonly leftFromText?: number | PositiveUniversalMeasure;
/**
* Specifies the minimum distance to be maintained between the table and the edge of text in the paragraph
* to the right of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero.
*/
readonly rightFromText?: number | string;
readonly rightFromText?: number | PositiveUniversalMeasure;
readonly overlap?: OverlapType;
}

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { twipsMeasureValue } from "@util/values";
import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
// <xsd:complexType name="CT_Height">
// <xsd:attribute name="val" type="s:ST_TwipsMeasure"/>
@ -30,7 +30,7 @@ export class TableRowHeightAttributes extends XmlAttributeComponent<{
}
export class TableRowHeight extends XmlComponent {
public constructor(value: number | string, rule: HeightRule) {
public constructor(value: number | PositiveUniversalMeasure, rule: HeightRule) {
super("w:trHeight");
this.root.push(

View File

@ -28,6 +28,7 @@
// </xsd:complexContent>
// </xsd:complexType>
import { IgnoreIfEmptyXmlComponent, OnOffElement } from "@file/xml-components";
import { PositiveUniversalMeasure } from "@util/values";
import { HeightRule, TableRowHeight } from "./table-row-height";
@ -35,7 +36,7 @@ export interface ITableRowPropertiesOptions {
readonly cantSplit?: boolean;
readonly tableHeader?: boolean;
readonly height?: {
readonly value: number | string;
readonly value: number | PositiveUniversalMeasure;
readonly rule: HeightRule;
};
}

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">