Skip to content

Commit

Permalink
refactor: - Refactor to use SearchInfo.fromAllTasks(tasks)
Browse files Browse the repository at this point in the history
  • Loading branch information
claremacrae committed Oct 28, 2023
1 parent ffba698 commit aead521
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Query/QueryResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class QueryResult {
}

static fromError(message: string): QueryResult {
const result = new QueryResult(new TaskGroups([], [], new SearchInfo(undefined, [])), 0);
const result = new QueryResult(new TaskGroups([], [], SearchInfo.fromAllTasks([])), 0);
result._searchErrorMessage = message;
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Query/SearchInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class SearchInfo {
this.allTasks = [...allTasks];
}

public static fromAllTasks(tasks: Task[]) {
public static fromAllTasks(tasks: Task[]): SearchInfo {
return new SearchInfo(undefined, tasks);
}
}
2 changes: 1 addition & 1 deletion tests/CustomMatchers/CustomMatchersForFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('CustomMatchersForFilters', () => {
it('should check filter with supplied SearchInfo', () => {
// Arrange
const task = new TaskBuilder().build();
const initialSearchInfo = new SearchInfo(undefined, [task]);
const initialSearchInfo = SearchInfo.fromAllTasks([task]);
const checkSearchInfoPassedThrough = (_task: Task, searchInfo: SearchInfo) => {
return Object.is(initialSearchInfo, searchInfo);
};
Expand Down
2 changes: 1 addition & 1 deletion tests/CustomMatchers/CustomMatchersForFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ with filter: "${filter.instruction}"`,
}

export function toMatchTask(filter: FilterOrErrorMessage, task: Task) {
return toMatchTaskWithSearchInfo(filter, task, new SearchInfo(undefined, [task]));
return toMatchTaskWithSearchInfo(filter, task, SearchInfo.fromAllTasks([task]));
}

export function toMatchTaskFromLine(filter: FilterOrErrorMessage, line: string) {
Expand Down
4 changes: 2 additions & 2 deletions tests/CustomMatchers/CustomMatchersForGrouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function toSupportGroupingWithProperty(field: Field, property: string) {
* @param tasks
*/
export function groupHeadingsForTask(grouper: Grouper, tasks: Task[]) {
const groups = new TaskGroups([grouper], tasks, new SearchInfo(undefined, tasks));
const groups = new TaskGroups([grouper], tasks, SearchInfo.fromAllTasks(tasks));

const headings: string[] = [];
groups.groups.forEach((taskGroup) => {
Expand Down Expand Up @@ -99,7 +99,7 @@ export function toGroupTask(grouper: Grouper | null, task: Task, expectedGroupNa
};
}

expect(grouper!.grouper(task, new SearchInfo(undefined, [task]))).toEqual(expectedGroupNames);
expect(grouper!.grouper(task, SearchInfo.fromAllTasks([task]))).toEqual(expectedGroupNames);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/DocumentationSamples/DocsSamplesForStatuses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function verifyTransitionsAsMarkdownTable(statuses: Status[]) {

function filterAllStatuses(filter: FilterOrErrorMessage) {
const cells: string[] = [`Matches \`${filter!.instruction}\``];
const searchInfo = new SearchInfo(undefined, tasks);
const searchInfo = SearchInfo.fromAllTasks(tasks);
tasks.forEach((task) => {
const matchedText = filter!.filter?.filterFunction(task, searchInfo) ? 'YES' : 'no';
cells.push(matchedText);
Expand All @@ -251,7 +251,7 @@ function verifyTransitionsAsMarkdownTable(statuses: Status[]) {
function showGroupNamesForAllTasks(groupName: string, grouperFunction: GrouperFunction) {
const cells: string[] = ['Name for `group by ' + groupName + '`'];
tasks.forEach((task) => {
const groupNamesForTask = grouperFunction(task, new SearchInfo(undefined, [task]));
const groupNamesForTask = grouperFunction(task, SearchInfo.fromAllTasks([task]));
const names = groupNamesForTask.join(',');
cells.push(names);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('Query parsing', () => {
expect(query.filters.length).toEqual(1);
expect(query.filters[0]).toBeDefined();
// If the boolean query and its sub-query are parsed correctly, the expression should always be true
expect(query.filters[0].filterFunction(task, new SearchInfo(undefined, [task]))).toBeTruthy();
expect(query.filters[0].filterFunction(task, SearchInfo.fromAllTasks([task]))).toBeTruthy();
});
});

Expand Down Expand Up @@ -619,7 +619,7 @@ describe('Query', () => {

// Act
let filteredTasks = [...tasks];
const searchInfo = new SearchInfo(undefined, tasks);
const searchInfo = SearchInfo.fromAllTasks(tasks);
query.filters.forEach((filter) => {
filteredTasks = filteredTasks.filter((task) => filter.filterFunction(task, searchInfo));
});
Expand Down Expand Up @@ -1063,7 +1063,7 @@ describe('Query', () => {
);

// Act, Assert
const searchInfo = new SearchInfo(undefined, allTasks);
const searchInfo = SearchInfo.fromAllTasks(allTasks);
expect(filter).toMatchTaskWithSearchInfo(same1, searchInfo);
expect(filter).toMatchTaskWithSearchInfo(same2, searchInfo);
expect(filter).not.toMatchTaskWithSearchInfo(different, searchInfo);
Expand Down
2 changes: 1 addition & 1 deletion tests/Query/Filter/FunctionField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('FunctionField - filtering', () => {
expect(filter).toBeValid();
const t = () => {
const task = new TaskBuilder().build();
filter.filterFunction!(task, new SearchInfo(undefined, [task]));
filter.filterFunction!(task, SearchInfo.fromAllTasks([task]));
};
expect(t).toThrow(Error);
expect(t).toThrowError('filtering function must return true or false. This returned "undefined".');
Expand Down
2 changes: 1 addition & 1 deletion tests/Query/Filter/TagsField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ describe('grouping by tag', () => {

// Act
const grouping: Grouper[] = [new TagsField().createGrouperFromLine('group by tags reverse')!];
const groups = new TaskGroups(grouping, inputs, new SearchInfo(undefined, inputs));
const groups = new TaskGroups(grouping, inputs, SearchInfo.fromAllTasks(inputs));

// Assert
expect(groups.toString()).toMatchInlineSnapshot(`
Expand Down
4 changes: 2 additions & 2 deletions tests/Query/QueryResult.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ describe('QueryResult', () => {

function createUngroupedQueryResultWithLimit(tasks: Task[], totalTasksCountBeforeLimit: number) {
const groupers: Grouper[] = [];
const groups = new TaskGroups(groupers, tasks, new SearchInfo(undefined, tasks));
const groups = new TaskGroups(groupers, tasks, SearchInfo.fromAllTasks(tasks));
return new QueryResult(groups, totalTasksCountBeforeLimit);
}

it('should create a QueryResult from TaskGroups', () => {
// Arrange
const groupers: Grouper[] = [];
const tasks: Task[] = [];
const groups = new TaskGroups(groupers, tasks, new SearchInfo(undefined, tasks));
const groups = new TaskGroups(groupers, tasks, SearchInfo.fromAllTasks(tasks));

// Act
const queryResult = new QueryResult(groups, 0);
Expand Down
2 changes: 1 addition & 1 deletion tests/Query/SearchInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('SearchInfo', () => {

it('should not be able to modify SearchInfo.allTasks indirectly', () => {
const tasks = [new TaskBuilder().build()];
const searchInfo = new SearchInfo(undefined, tasks);
const searchInfo = SearchInfo.fromAllTasks(tasks);

// Check that updating the original list of tasks does not change the tasks saved in searchInfo.allTasks
tasks.push(new TaskBuilder().description('I should not be added to searchInfo.allTasks').build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function verifyFunctionFieldFilterSamplesOnTasks(filters: QueryInstructio

const filterFunction = filterOrErrorMessage.filterFunction!;
const matchingTasks: string[] = [];
const searchInfo = new SearchInfo(undefined, tasks);
const searchInfo = SearchInfo.fromAllTasks(tasks);
for (const task of tasks) {
const matches = filterFunction(task, searchInfo);
if (matches) {
Expand Down
2 changes: 1 addition & 1 deletion tests/TaskGroups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { fromLine } from './TestHelpers';
window.moment = moment;

function makeTasksGroups(grouping: Grouper[], inputs: Task[]): any {
return new TaskGroups(grouping, inputs, new SearchInfo(undefined, inputs));
return new TaskGroups(grouping, inputs, SearchInfo.fromAllTasks(inputs));
}

describe('Grouping tasks', () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/TestingTools/FilterTestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function testFilter(filter: FilterOrErrorMessage, taskBuilder: TaskBuilde
export function testTaskFilter(filter: FilterOrErrorMessage, task: Task, expected: boolean) {
expect(filter.filterFunction).toBeDefined();
expect(filter.error).toBeUndefined();
expect(filter.filterFunction!(task, new SearchInfo(undefined, [task]))).toEqual(expected);
expect(filter.filterFunction!(task, SearchInfo.fromAllTasks([task]))).toEqual(expected);
}

/**
Expand All @@ -50,7 +50,7 @@ export function testTaskFilterViaQuery(filter: string, task: Task, expected: boo

// Act
let filteredTasks = [...tasks];
const searchInfo = new SearchInfo(undefined, tasks);
const searchInfo = SearchInfo.fromAllTasks(tasks);
query.filters.forEach((filter) => {
filteredTasks = filteredTasks.filter((task) => filter.filterFunction(task, searchInfo));
});
Expand Down Expand Up @@ -85,7 +85,7 @@ export function shouldSupportFiltering(

// Act
let filteredTasks = [...tasks];
const searchInfo = new SearchInfo(undefined, tasks);
const searchInfo = SearchInfo.fromAllTasks(tasks);
query.filters.forEach((filter) => {
filteredTasks = filteredTasks.filter((task) => filter.filterFunction(task, searchInfo));
});
Expand Down

0 comments on commit aead521

Please sign in to comment.