Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move default comparator to Grouper #2018

Conversation

ilandikov
Copy link
Collaborator

Description

Move the default comparator from TaskGroups to Grouper.

Motivation and Context

Preparing for fixes group ordering of UrgencyField and others. Next step will be the fix.

Split of #1966

How has this been tested?

Unit test, some new test added.

Screenshots (if appropriate)

Types of changes

Internal changes:

  • Refactor (prefix: refactor - non-breaking change which only improves the design or structure of existing code, and making no changes to its external behaviour)

Checklist

Terms

@claremacrae
Copy link
Collaborator

Motivation and Context

Preparing for fixes group ordering of UrgencyField and others. Next step will be the fix.

Which of the steps listed in this comment in #1966 is this?

I'm just conscious I rushed you to a partial fix of step 1, and so it is incomplete....

PR 1 On the current code, add an expressive test for every field, showing current headings order

this.property = property;
this.grouper = grouper;
this.reverse = reverse;
this.comparator = (a: Task, b: Task) => {
const result = comparator(a, b);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something but we talked elsewhere about comparator() throwing if sorting is unimplemented in the field...

Copy link
Collaborator Author

@ilandikov ilandikov Jun 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but this is just refactoring - moving the comparator from TaskGroups.sortTaskGroups into Field.defaultComparator. For now Field.comparator() is never called, so no throwing when sorting is not supported

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - right. Now I see what's going on.

I need to actually test this out and think about it some more.

I would be a lot more comfortable, and the review would be a lot quicker, if there were already the thorough group-sorting tests that we have talked about previously.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correctly, you said we don't need to have extensive tests till the end to start changing the code. I may have misunderstood.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can quite believe I said that, but it wasn't my intention.

Once it became possible to change the sort order of groups, it revealed a gaping hole in the grouping tests, which had previously only tested the names of groups and not the order.

I would like that hole to be fixed for all fields before the grouping code is changed please.

@claremacrae
Copy link
Collaborator

I am not sure that this is a refactor, but I think we don't yet have good enough test coverage to be able to prove that anyway....

(Caveat: I have only read the code and not run it...)

@@ -673,4 +673,12 @@ describe('grouping by tag', () => {
"
`);
});

it('should sort groups for TaskField', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be TagsField

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, fixed, thanks

Comment on lines 679 to 691
const taskLines = ['- [ ] a #tag1', '- [ ] a #tag2', '- [ ] a #tag1 #tag2', '- [ ] a'];
const tasks = taskLines.map((taskLine) => fromLine({ line: taskLine }));

expect({ grouper, tasks }).groupHeadingsToBe(['(No tags)', '#tag1', '#tag2']);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we stick with the idea of having a helper function that creates a selection of tasks with a wide range of values, I think it would be good for this to also include some nested tags to see how they sort...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right! I had a feeling that I need to add more to this test! Thanks =)

Done.

@ilandikov
Copy link
Collaborator Author

Motivation and Context

Preparing for fixes group ordering of UrgencyField and others. Next step will be the fix.

Which of the steps listed in this comment in #1966 is this?

I'm just conscious I rushed you to a partial fix of step 1, and so it is incomplete....

PR 1 On the current code, add an expressive test for every field, showing current headings order

This is making the change is easy for the PR2

Comment on lines 315 to 316
const groupA = this.grouper()(a)[0];
const groupB = this.grouper()(b)[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when you test this on a field that does not support grouping?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like description, for example. But better to create a fake field in the test that is guaranteed to not support grouping.

@claremacrae
Copy link
Collaborator

We should probably chat about this next time we pair - it will be a lot more efficient - sorry for not thinking about that earlier! 😄

@ilandikov ilandikov force-pushed the refactor-move-default-comparator-to-grouper branch 2 times, most recently from ddda591 to 489241d Compare June 8, 2023 16:58
@ilandikov
Copy link
Collaborator Author

@claremacrae I have re-pushed this branch after the tests have been done in #2024

I'm still disturbed by the fact that group order test in TagsField.test is failing randomly (Since we are scrambling the tasks in the input).

This probably shall be coming from that change of the comparison in TaskGroups.sortTaskGroups(). Before the comparison was done between group names:

const result = groupNames1[i].localeCompare(groupNames2[i], undefined, { numeric: true });

and now it is done between the first tasks of each group:

const result = grouper.comparator(group1.tasks[0], group2.tasks[0]);

On the other hand, the comparison shall be finally done between two tasks since Comparator will be called:

export type Comparator = (a: Task, b: Task) => number;

If you have a quick clue for me - I'm buying. Otherwise let's discuss this during our next pairing session =)

@claremacrae
Copy link
Collaborator

I'm sorry I don't have time to look at this right now and for a little while. I am desperate to finish off a big new feature I've been working on for a couple of weeks.

@ilandikov ilandikov force-pushed the refactor-move-default-comparator-to-grouper branch from 489241d to 25501ad Compare June 10, 2023 12:32
@ilandikov
Copy link
Collaborator Author

Closing this PR as we just discussed "local" fixes for Urgency, Priority and StatusType fields.

@ilandikov ilandikov closed this Jun 15, 2023
@claremacrae claremacrae added the type: internal Only regards development or contributing label Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: internal Only regards development or contributing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants