1 | import { BufferedDebugHandler } from "../src/debugging";
|
2 | import { en, ParsedResult, ParsingOption, ParsingReference } from "../src";
|
3 |
|
4 | interface ChronoLike {
|
5 | parse(text: string, ref?: ParsingReference | Date, option?: ParsingOption): ParsedResult[];
|
6 | }
|
7 |
|
8 | type CheckResult = (p: ParsedResult, text: string) => void;
|
9 |
|
10 | export function testSingleCase(chrono: ChronoLike, text: string, checkResult?: CheckResult);
|
11 | export function testSingleCase(
|
12 | chrono: ChronoLike,
|
13 | text: string,
|
14 | refDateOrCheckResult?: ParsingReference | Date | CheckResult,
|
15 | checkResult?: CheckResult
|
16 | );
|
17 | export function testSingleCase(
|
18 | chrono: ChronoLike,
|
19 | text: string,
|
20 | refDateOrCheckResult?: ParsingReference | Date | CheckResult,
|
21 | optionOrCheckResult?: ParsingOption | CheckResult,
|
22 | checkResult?: CheckResult
|
23 | );
|
24 | export 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 |
|
58 | export function testWithExpectedDate(chrono: ChronoLike, text: string, expectedDate: Date) {
|
59 | testSingleCase(chrono, text, (result) => {
|
60 | expect(result.start).toBeDate(expectedDate);
|
61 | });
|
62 | }
|
63 |
|
64 | export 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 |
|
76 | export 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 |
|
85 | declare global {
|
86 |
|
87 | namespace jest {
|
88 |
|
89 |
|
90 | interface Matchers<R> {
|
91 | toBeDate(date: Date): CustomMatcherResult;
|
92 | toBeSingleOnText(text: string): CustomMatcherResult;
|
93 | }
|
94 | }
|
95 | }
|
96 |
|
97 |
|
98 | expect.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 | });
|