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

Add Unit Test to Thoroughly Tests Mastodon's Undo Redo Mechanism #289

Merged
merged 9 commits into from
Mar 27, 2024

Conversation

maarzt
Copy link
Contributor

@maarzt maarzt commented Mar 7, 2024

There is a PR to fix a small problem in Mastodon's undo/redo mechanism: mastodon-sc/mastodon-collection#16

The goal of this PR is to ensure that Mastodons very important undo/redo mechanism works properly. I there for added a unit test ModelUndoRedoTest. The unit test uses the ModelUtils.dump(...) method to convert the current status of the Model into a string. Converting the model status into string allows me to test if the model is correctly rolled back into a specific (previously) recorded state.

It was necessary to extend the ModelUtils.dump(...) method to also print the tag sets. (The undo redo mechanism handle tags as well.)

@maarzt maarzt requested review from tinevez and stefanhahmann March 7, 2024 14:37
@maarzt maarzt changed the title Add Unit test that thoroughly tests Mastodon's undo redo mechanism Add Unit Test to Thoroughly Tests Mastodon's Undo Redo Mechanism Mar 7, 2024
@maarzt
Copy link
Contributor Author

maarzt commented Mar 7, 2024

The test currently fails because of a but in the Undo/Redo mechanism. That will be fixed by this PR mastodon-sc/mastodon-collection#16

Copy link
Collaborator

@stefanhahmann stefanhahmann left a comment

Choose a reason for hiding this comment

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

The main contribution of this PR is a unit test that checks if the undo/redo function works correctly on the Model. This is very valuable! Some parts of the test need some time to understand them, but overall the test is well written.
Also, this PR improves the dumping methods of ModelUtils a bit, which is also a good contribution.

  • I made some suggestion mainly regarding code formatting and also a bit regarding code structuring.
  • The unit tests for ModelUtils were failing on my machine due to a different default locale. However, this can be fixed with little effort. I made some suggestion regarding this.
  • testUndoRedoAfterModelImporter is failing in the CI environment. This should be handled.

src/main/java/org/mastodon/mamut/model/ModelUtils.java Outdated Show resolved Hide resolved
spotsTable.defineColumn( 9, "Label", "", Spot::getLabel );
spotsTable.defineColumn( 6, "Frame", "", spot -> Integer.toString( spot.getTimepoint() ) );
spotsTable.defineColumn( 9, "X", bracket( spaceUnits ), spot -> String.format( "%9.1f", spot.getDoublePosition( 0 ) ) );
spotsTable.defineColumn( 9, "Y", bracket( spaceUnits ), spot -> String.format( "%9.1f", spot.getDoublePosition( 1 ) ) );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
spotsTable.defineColumn( 9, "Y", bracket( spaceUnits ), spot -> String.format( "%9.1f", spot.getDoublePosition( 1 ) ) );
spotsTable.defineColumn( 9, "Y", bracket( spaceUnits ), spot -> String.format( Locale.US, "%9.1f", spot.getDoublePosition( 1 ) ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

src/main/java/org/mastodon/mamut/model/ModelUtils.java Outdated Show resolved Hide resolved
src/main/java/org/mastodon/mamut/model/ModelUtils.java Outdated Show resolved Hide resolved
src/main/java/org/mastodon/mamut/model/ModelUtils.java Outdated Show resolved Hide resolved
src/test/java/org/mastodon/mamut/model/ModelUtilsTest.java Outdated Show resolved Hide resolved
private static void testUndoRedo( Model model )
{
final List< String > states = new ArrayList<>();
makeVariousChangesAndRecordUndoPoints( model, states );
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find it confusing when reading code, where changes to the variables that are given to the method happen inside the method. I usually do not expect this to happen.
Could this method maybe return a Pair<Model, List> instead? That would confuse me less.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I introduced a UndoVerifier class to solve that problem.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is better now. Perhaps UndoRedoModifier would be a bit better suited as class name?

maarzt added 6 commits March 26, 2024 14:47
This increases the reproducibility of the dump method, and makes it easier
to test.
1. Broke large dump(...) method into smaller methods.
2. Merged redundant methods addLinkFeatureColumns and addSpotFeatureColumns
   into one method "addFeatureColumns".
Copy link
Contributor Author

@maarzt maarzt left a comment

Choose a reason for hiding this comment

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

I addressed all the reviewers comments

import org.mastodon.feature.FeatureSpec;
import org.mastodon.feature.IntFeatureProjection;
import org.mastodon.mamut.feature.SpotTrackIDFeature;
import org.mastodon.model.tag.ObjTagMap;
import org.mastodon.model.tag.ObjTags;
import org.mastodon.model.tag.TagSetModel;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* The tables by default contain all spot and link properties as well
* as the feature values.
* <p>
* There is also a method that {@link #dump(Model, DumpFlags...)}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* @param maxLines
* the max number of rows to print in the two tables.
* @return a String representation of the model.
* Which information to include in the table can be changed using
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* Which information to include in the table can be changed using
* {@link DumpFlags}.
*/
public static final String dump( final Model model, final DumpFlags... options )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Difficult topic. I will leave it as it is to be consistent with the old code. You may read about the topic here:
Intellij doesn't like it, stack overflow recommends "public static final".

private static void testUndoRedo( Model model )
{
final List< String > states = new ArrayList<>();
makeVariousChangesAndRecordUndoPoints( model, states );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I introduced a UndoVerifier class to solve that problem.

src/test/java/org/mastodon/mamut/model/ModelUtilsTest.java Outdated Show resolved Hide resolved
Copy link
Collaborator

@stefanhahmann stefanhahmann left a comment

Choose a reason for hiding this comment

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

Apart from a minor suggestion to rename UndoVerifier to UndoRedoVerifier, which I leave up to your decision, I have no further suggestions to this PR.

return String.format( "%" + width + "s", "unset" );
}

private static class TablePrinter< T >
Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay.

* Which information to include in the table can be changed using
* {@link DumpFlags}.
*/
public static final String dump( final Model model, final DumpFlags... options )
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. Thanks for the explanation. The link is an interesting read. There are pros and cons regarding this that I was not aware of.

@tinevez tinevez merged commit 25e041f into dev Mar 27, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants