Skip to content

Commit 0506f60

Browse files
authored
test: Test heading with regex, and 2 new custom matchers (#1045)
* test: Test regex searches on heading * refactor: Extract method toMatchTask() * test: Improve output of toMatchTask() on failure * test: Simplify remaining custom matchers for filter tests * test: Export toMatchTask() custom matcher
1 parent 882b7e0 commit 0506f60

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

tests/CustomMatchers/CustomMatchersForFilters.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Task } from '../../src/Task';
12
import type { FilterOrErrorMessage } from '../../src/Query/Filter/Filter';
23
import { fromLine } from '../TestHelpers';
34
import { TaskBuilder } from '../TestingTools/TaskBuilder';
@@ -56,19 +57,25 @@ declare global {
5657
namespace jest {
5758
interface Matchers<R> {
5859
toBeValid(): R;
60+
toMatchTask(task: Task): R;
5961
toMatchTaskFromLine(line: string): R;
62+
toMatchTaskWithHeading(heading: string | null): R;
6063
toMatchTaskWithPath(path: string): R;
6164
}
6265

6366
interface Expect {
6467
toBeValid(): any;
68+
toMatchTask(task: Task): any;
6569
toMatchTaskFromLine(line: string): any;
70+
toMatchTaskWithHeading(heading: string | null): any;
6671
toMatchTaskWithPath(path: string): any;
6772
}
6873

6974
interface InverseAsymmetricMatchers {
7075
toBeValid(): any;
76+
toMatchTask(task: Task): any;
7177
toMatchTaskFromLine(line: string): any;
78+
toMatchTaskWithHeading(heading: string | null): any;
7279
toMatchTaskWithPath(path: string): any;
7380
}
7481
}
@@ -97,45 +104,47 @@ export function toBeValid(filter: FilterOrErrorMessage) {
97104
};
98105
}
99106

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) {
108108
const matches = filter.filter!(task);
109109
if (!matches) {
110110
return {
111-
message: () => `unexpected failure to match task: ${line}`,
111+
message: () =>
112+
`unexpected failure to match task: ${task.toFileLineString()}`,
112113
pass: false,
113114
};
114115
}
115116

116117
return {
117-
message: () => `filter should not have matched task: ${line}`,
118+
message: () =>
119+
`filter should not have matched task: ${task.toFileLineString()}`,
118120
pass: true,
119121
};
120122
}
121123

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+
122143
export function toMatchTaskWithPath(
123144
filter: FilterOrErrorMessage,
124145
path: string,
125146
) {
126147
const builder = new TaskBuilder();
127148
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);
141150
}

tests/Query/Filter/HeadingField.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { HeadingField } from '../../../src/Query/Filter/HeadingField';
22
import type { FilterOrErrorMessage } from '../../../src/Query/Filter/Filter';
33
import { TaskBuilder } from '../../TestingTools/TaskBuilder';
44
import { testFilter } from '../../TestingTools/FilterTestHelpers';
5+
import {
6+
toBeValid,
7+
toMatchTaskWithHeading,
8+
} from '../../CustomMatchers/CustomMatchersForFilters';
59

610
function testTaskFilterForHeading(
711
filter: FilterOrErrorMessage,
@@ -12,6 +16,14 @@ function testTaskFilterForHeading(
1216
testFilter(filter, builder.precedingHeader(precedingHeader), expected);
1317
}
1418

19+
expect.extend({
20+
toBeValid,
21+
});
22+
23+
expect.extend({
24+
toMatchTaskWithHeading,
25+
});
26+
1527
describe('heading', () => {
1628
it('by heading (includes)', () => {
1729
// Arrange
@@ -36,4 +48,30 @@ describe('heading', () => {
3648
testTaskFilterForHeading(filter, 'SoMe InteResting HeaDing', false);
3749
testTaskFilterForHeading(filter, 'Other Heading', true);
3850
});
51+
52+
it('by heading (regex matches - case sensitive)', () => {
53+
// Arrange
54+
const filter = new HeadingField().createFilterOrErrorMessage(
55+
'heading regex matches /[Ii]nteresting Head.ng/',
56+
);
57+
58+
// Act, Assert
59+
expect(filter).toBeValid();
60+
expect(filter).toMatchTaskWithHeading('Interesting Heading');
61+
expect(filter).not.toMatchTaskWithHeading(null);
62+
expect(filter).not.toMatchTaskWithHeading('SoMe InteResting HeaDing');
63+
});
64+
65+
it('by heading (regex does not match - case in-sensitive)', () => {
66+
// Arrange
67+
const filter = new HeadingField().createFilterOrErrorMessage(
68+
'heading regex does not match /[Ii]nteresting Head.ng/i',
69+
);
70+
71+
// Act, Assert
72+
expect(filter).toBeValid();
73+
expect(filter).toMatchTaskWithHeading(null);
74+
expect(filter).not.toMatchTaskWithHeading('Interesting Heading');
75+
expect(filter).not.toMatchTaskWithHeading('SoMe InteResting HeaDing');
76+
});
3977
});

0 commit comments

Comments
 (0)