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, options?: ParsingOption) {
|
65 | const debugHandler = new BufferedDebugHandler();
|
66 | options = options || {};
|
67 | options.debug = debugHandler;
|
68 |
|
69 | try {
|
70 | const results = chrono.parse(text, refDate, options);
|
71 | expect(results).toHaveLength(0);
|
72 | } catch (e) {
|
73 | debugHandler.executeBufferedBlocks();
|
74 | e.stack = e.stack.replace(/[^\n]*at .*test_util.*\n/g, "");
|
75 | throw e;
|
76 | }
|
77 | }
|
78 |
|
79 | export function measureMilliSec(block: () => void): number {
|
80 | const startTime = new Date().getMilliseconds();
|
81 | block();
|
82 | const endTime = new Date().getMilliseconds();
|
83 | return endTime - startTime;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 | declare global {
|
89 |
|
90 | namespace jest {
|
91 |
|
92 |
|
93 | interface Matchers<R> {
|
94 | toBeDate(date: Date): CustomMatcherResult;
|
95 | toBeSingleOnText(text: string): CustomMatcherResult;
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 | expect.extend({
|
102 | toBeDate(resultOrComponent, date) {
|
103 | if (typeof resultOrComponent.date !== "function") {
|
104 | return {
|
105 | message: () => `${resultOrComponent} is not a ParsedResult or ParsedComponent`,
|
106 | pass: false,
|
107 | };
|
108 | }
|
109 |
|
110 | const actualDate = resultOrComponent.date();
|
111 | const actualTime = actualDate.getTime();
|
112 | const expectedTime = date.getTime();
|
113 | return {
|
114 | message: () => `Expected date to be: ${date} Received: ${actualDate} (${resultOrComponent})`,
|
115 | pass: actualTime === expectedTime,
|
116 | };
|
117 | },
|
118 |
|
119 | toBeSingleOnText(results, text) {
|
120 | if (results.length === 1) {
|
121 | return {
|
122 | message: () => `Got single result from '${text}'`,
|
123 | pass: true,
|
124 | };
|
125 | }
|
126 |
|
127 | return {
|
128 | message: () =>
|
129 | `Got ${results.length} results from '${text}'\n${results
|
130 | .map((result) => JSON.stringify(result))
|
131 | .join("\n")}`,
|
132 | pass: false,
|
133 | };
|
134 | },
|
135 | });
|