UNPKG

4.39 kBPlain TextView Raw
1import { BufferedDebugHandler } from "../src/debugging";
2import { en, ParsedResult, ParsingOption, ParsingReference } from "../src";
3
4interface ChronoLike {
5 parse(text: string, ref?: ParsingReference | Date, option?: ParsingOption): ParsedResult[];
6}
7
8type CheckResult = (p: ParsedResult, text: string) => void;
9
10export function testSingleCase(chrono: ChronoLike, text: string, checkResult?: CheckResult);
11export function testSingleCase(
12 chrono: ChronoLike,
13 text: string,
14 refDateOrCheckResult?: ParsingReference | Date | CheckResult,
15 checkResult?: CheckResult
16);
17export function testSingleCase(
18 chrono: ChronoLike,
19 text: string,
20 refDateOrCheckResult?: ParsingReference | Date | CheckResult,
21 optionOrCheckResult?: ParsingOption | CheckResult,
22 checkResult?: CheckResult
23);
24export function testSingleCase(
25 chrono: ChronoLike,
26 text: string,
27 refDateOrCheckResult?: ParsingReference | Date | CheckResult,
28 optionOrCheckResult?: ParsingOption | CheckResult,
29 checkResult?: CheckResult
30) {
31 if (checkResult === undefined && typeof optionOrCheckResult === "function") {
32 checkResult = optionOrCheckResult;
33 optionOrCheckResult = undefined;
34 }
35
36 if (optionOrCheckResult === undefined && typeof refDateOrCheckResult === "function") {
37 checkResult = refDateOrCheckResult;
38 refDateOrCheckResult = undefined;
39 }
40
41 const debugHandler = new BufferedDebugHandler();
42 optionOrCheckResult = (optionOrCheckResult as ParsingOption) || {};
43 optionOrCheckResult.debug = debugHandler;
44
45 try {
46 const results = chrono.parse(text, refDateOrCheckResult as Date, optionOrCheckResult);
47 expect(results).toBeSingleOnText(text);
48 if (checkResult) {
49 checkResult(results[0], text);
50 }
51 } catch (e) {
52 debugHandler.executeBufferedBlocks();
53 e.stack = e.stack.replace(/[^\n]*at .*test_util.*\n/g, "");
54 throw e;
55 }
56}
57
58export function testWithExpectedDate(chrono: ChronoLike, text: string, expectedDate: Date) {
59 testSingleCase(chrono, text, (result) => {
60 expect(result.start).toBeDate(expectedDate);
61 });
62}
63
64export function testUnexpectedResult(chrono: ChronoLike, text: string, refDate?: Date) {
65 const debugHandler = new BufferedDebugHandler();
66 try {
67 const results = chrono.parse(text, refDate, { debug: debugHandler });
68 expect(results).toHaveLength(0);
69 } catch (e) {
70 debugHandler.executeBufferedBlocks();
71 e.stack = e.stack.replace(/[^\n]*at .*test_util.*\n/g, "");
72 throw e;
73 }
74}
75
76export function measureMilliSec(block: () => void): number {
77 const startTime = new Date().getMilliseconds();
78 block();
79 const endTime = new Date().getMilliseconds();
80 return endTime - startTime;
81}
82
83// --------------------------------------------------
84
85declare global {
86 // eslint-disable-next-line @typescript-eslint/no-namespace
87 namespace jest {
88 // noinspection JSUnusedGlobalSymbols
89 // eslint-disable-next-line @typescript-eslint/no-unused-vars
90 interface Matchers<R> {
91 toBeDate(date: Date): CustomMatcherResult;
92 toBeSingleOnText(text: string): CustomMatcherResult;
93 }
94 }
95}
96
97// noinspection JSUnusedGlobalSymbols
98expect.extend({
99 toBeDate(resultOrComponent, date) {
100 if (typeof resultOrComponent.date !== "function") {
101 return {
102 message: () => `${resultOrComponent} is not a ParsedResult or ParsedComponent`,
103 pass: false,
104 };
105 }
106
107 const actualDate = resultOrComponent.date();
108 const actualTime = actualDate.getTime();
109 const expectedTime = date.getTime();
110 return {
111 message: () => `Expected date to be: ${date} Received: ${actualDate} (${resultOrComponent})`,
112 pass: actualTime === expectedTime,
113 };
114 },
115
116 toBeSingleOnText(results, text) {
117 if (results.length === 1) {
118 return {
119 message: () => `Got single result from '${text}'`,
120 pass: true,
121 };
122 }
123
124 return {
125 message: () =>
126 `Got ${results.length} results from '${text}'\n${results
127 .map((result) => JSON.stringify(result))
128 .join("\n")}`,
129 pass: false,
130 };
131 },
132});