|
| 1 | +import type { Task } from '../../src/Task'; |
1 | 2 | import type { FilterOrErrorMessage } from '../../src/Query/Filter/Filter';
|
2 | 3 | import { fromLine } from '../TestHelpers';
|
3 | 4 | import { TaskBuilder } from '../TestingTools/TaskBuilder';
|
@@ -56,19 +57,25 @@ declare global {
|
56 | 57 | namespace jest {
|
57 | 58 | interface Matchers<R> {
|
58 | 59 | toBeValid(): R;
|
| 60 | + toMatchTask(task: Task): R; |
59 | 61 | toMatchTaskFromLine(line: string): R;
|
| 62 | + toMatchTaskWithHeading(heading: string | null): R; |
60 | 63 | toMatchTaskWithPath(path: string): R;
|
61 | 64 | }
|
62 | 65 |
|
63 | 66 | interface Expect {
|
64 | 67 | toBeValid(): any;
|
| 68 | + toMatchTask(task: Task): any; |
65 | 69 | toMatchTaskFromLine(line: string): any;
|
| 70 | + toMatchTaskWithHeading(heading: string | null): any; |
66 | 71 | toMatchTaskWithPath(path: string): any;
|
67 | 72 | }
|
68 | 73 |
|
69 | 74 | interface InverseAsymmetricMatchers {
|
70 | 75 | toBeValid(): any;
|
| 76 | + toMatchTask(task: Task): any; |
71 | 77 | toMatchTaskFromLine(line: string): any;
|
| 78 | + toMatchTaskWithHeading(heading: string | null): any; |
72 | 79 | toMatchTaskWithPath(path: string): any;
|
73 | 80 | }
|
74 | 81 | }
|
@@ -97,45 +104,47 @@ export function toBeValid(filter: FilterOrErrorMessage) {
|
97 | 104 | };
|
98 | 105 | }
|
99 | 106 |
|
100 |
| -export function toMatchTaskFromLine( |
101 |
| - filter: FilterOrErrorMessage, |
102 |
| - line: string, |
103 |
| -) { |
104 |
| - const task = fromLine({ |
105 |
| - line: line, |
106 |
| - }); |
107 |
| - |
| 107 | +export function toMatchTask(filter: FilterOrErrorMessage, task: Task) { |
108 | 108 | const matches = filter.filter!(task);
|
109 | 109 | if (!matches) {
|
110 | 110 | return {
|
111 |
| - message: () => `unexpected failure to match task: ${line}`, |
| 111 | + message: () => |
| 112 | + `unexpected failure to match task: ${task.toFileLineString()}`, |
112 | 113 | pass: false,
|
113 | 114 | };
|
114 | 115 | }
|
115 | 116 |
|
116 | 117 | return {
|
117 |
| - message: () => `filter should not have matched task: ${line}`, |
| 118 | + message: () => |
| 119 | + `filter should not have matched task: ${task.toFileLineString()}`, |
118 | 120 | pass: true,
|
119 | 121 | };
|
120 | 122 | }
|
121 | 123 |
|
| 124 | +export function toMatchTaskFromLine( |
| 125 | + filter: FilterOrErrorMessage, |
| 126 | + line: string, |
| 127 | +) { |
| 128 | + const task = fromLine({ |
| 129 | + line: line, |
| 130 | + }); |
| 131 | + return toMatchTask(filter, task); |
| 132 | +} |
| 133 | + |
| 134 | +export function toMatchTaskWithHeading( |
| 135 | + filter: FilterOrErrorMessage, |
| 136 | + heading: string, |
| 137 | +) { |
| 138 | + const builder = new TaskBuilder(); |
| 139 | + const task = builder.precedingHeader(heading).build(); |
| 140 | + return toMatchTask(filter, task); |
| 141 | +} |
| 142 | + |
122 | 143 | export function toMatchTaskWithPath(
|
123 | 144 | filter: FilterOrErrorMessage,
|
124 | 145 | path: string,
|
125 | 146 | ) {
|
126 | 147 | const builder = new TaskBuilder();
|
127 | 148 | const task = builder.path(path).build();
|
128 |
| - |
129 |
| - const matches = filter.filter!(task); |
130 |
| - if (!matches) { |
131 |
| - return { |
132 |
| - message: () => `unexpected failure to match task: ${path}`, |
133 |
| - pass: false, |
134 |
| - }; |
135 |
| - } |
136 |
| - |
137 |
| - return { |
138 |
| - message: () => `filter should not have matched task: ${path}`, |
139 |
| - pass: true, |
140 |
| - }; |
| 149 | + return toMatchTask(filter, task); |
141 | 150 | }
|
0 commit comments