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

Refactored Nag Dialogs, Split UnableToAffordExpensesNagDialog into Two Dialogs, Added Unit Tests #4837

Merged
merged 32 commits into from
Sep 25, 2024

Conversation

IllianiCBT
Copy link
Collaborator

@IllianiCBT IllianiCBT commented Sep 14, 2024

Refactored a majority of our existing nag dialogs to standardize the code used within.

Modified the UnableToAffordExpensesNagDialog to specify that only monthly expenses are being checked. Updated the Java method to exclude loan data.

Added a new nag that triggers specifically if the campaign is unable to afford any loan payments due the next day.

Moved the StratCon outstanding contact nag logic into the relevant nag dialog class.

Added Unit Tests for every nag dialog, barring the single dialog that already had Unit Tests.

Closes #4834

Modified the `UnableToAffordExpensesNagDialog` to specify that only monthly expenses are being checked. Updated the Java method to exclude loan data.
Introduced a new nag dialog to warn users when loan payments due the next day cannot be afforded. Updated constants, properties, and options dialog to integrate this feature.
@codecov-commenter
Copy link

codecov-commenter commented Sep 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 10.45%. Comparing base (752cc96) to head (1ea5472).
Report is 70 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #4837      +/-   ##
============================================
+ Coverage     10.30%   10.45%   +0.14%     
- Complexity     5871     5957      +86     
============================================
  Files           944      945       +1     
  Lines        131394   131538     +144     
  Branches      19063    19079      +16     
============================================
+ Hits          13545    13748     +203     
+ Misses       116532   116466      -66     
- Partials       1317     1324       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@IllianiCBT IllianiCBT marked this pull request as draft September 14, 2024 18:26
@IllianiCBT
Copy link
Collaborator Author

Flipping to draft while I write some Unit Tests

Separated 'overdue loans' and 'scenarios due' checks into distinct methods for clearer logic flow. Updated localization for overdue loan messages.
Added tests to verify DayEndingEvent can be canceled and to check various unit states in UnmaintainedUnitsNagDialog.
@IllianiCBT IllianiCBT requested a review from Sleet01 September 14, 2024 19:47
@@ -2580,6 +2539,52 @@ public void handleDayEnding(DayEndingEvent evt) {
}
}

/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved these functions into their own discrete methods, as it made sense to isolate them in case we ever want to move them into their own class (like the other nag dialogs).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just an FYI, not requesting any changes:

Private members of a class are only visible to instances of that class, so will not be available for unit testing.
It is possible to write less-protected wrapper functions that then call the private methods just for testing purposes - that is, they can then be called in unit tests, but the private methods themselves will be invisible to unit testing code.

For that reason, it is common to see code that has unit tests get switched from private to protected - the Junit unit test code needs at least this much access to call the methods.
Package-private is another option iff the unit tests can be placed in the package hierarchy of the code under test, but I don't think we have that option with Junit 5.

Included missing GPLv3 licenses in test and main classes for compliance. Enhanced JavaDoc comments across relevant classes for better clarity and maintainability.
Included missing GPLv3 licenses in test and main classes for compliance. Enhanced JavaDoc comments across relevant classes for better clarity and maintainability.
# Conflicts:
#	MekHQ/unittests/mekhq/gui/dialog/nagDialogs/UnmaintainedUnitsNagDialogTest.java
// and its response is checked against expected behavior

@Test
void unmaintainedUnitExistsUnit1() {
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 all really good! I have a single recommendation, not necessary to implement here but something to think about going forward:

Check out "parameterization", a method for reducing copy-and-pasting a bunch of similar tests (q.v. https://www.baeldung.com/parameterized-tests-junit-5).
When you have a set of tests that are identical except for some parameters that they consume, you can have Junit run the same test function with different combinations of parameters, thusly:

@ParameterizedTest
@ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE})
void isOdd_ShouldReturnTrueForOddNumbers(int number) {
    assertTrue(Numbers.isOdd(number));
}

So this allows for running a test function multiple times with different inputs, cutting down on repetitive code.
There are drawbacks, though:

One of the limitations of value sources is that they only support these types:
short (with the shorts attribute)
byte (bytes attribute)
int (ints attribute)
long (longs attribute)
float (floats attribute)
double (doubles attribute)
char (chars attribute)
java.lang.String (strings attribute)
java.lang.Class (classes attribute)
Also, we can only pass one argument to the test method each time.

Other things to note:
This only works if the assert is the same for every combo of parameters or if you can pass in the expected value as an additional parameter... which is usually too much hassle.
So in this case, you could parameterize all the assertTrue tests into one function, and all the assertFalse tests into another, if initializeUnits() could be set up to take a single value rather than 4.

Again, not requesting any changes here, just something you might want to look at for future work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is very cool, thank you for the reading suggestion. I might be able to get it to work here if I can work out an algorithm that will allow us to test all the possible combinations, but I think in this instance what we would gain in brevity we'd lose in readability.

I know you said you weren't suggesting change, but I wanted to comment my thought process.

Copy link
Collaborator

@Sleet01 Sleet01 left a comment

Choose a reason for hiding this comment

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

Looks very good, outside of already-identified changes and one minor typo.

Introduced new unit tests for the EndContractNagDialog class to cover various contract expiration scenarios. Refactored the isContractEnded method for better performance and readability, and standardized documentation for nag dialog classes.
Created unit tests for the InsufficientAstechsNagDialog class in MekHQ. These tests mock the Campaign class to validate the dialog's behavior under different astech requirements, ensuring accurate nagging functionality.
Updated comments in the unit tests to directly describe the method being tested and its expected behavior. This cleanup improves readability and clarity, ensuring the comments are concise and to the point.
Updated nag dialog methods to provide clearer functionality and improved readability. Refactored InsufficientMedicsNagDialog with new methods and added tests to ensure accurate behavior checks. Additionally, adjusted GUI properties to reflect changes in dialog text for better user guidance.
Refactored InvalidFactionNagDialog to improve readability and maintainability by separating special handlers for factions. Updated the class to use constants for dialog parameters. Added unit tests to ensure proper functionality and prevent regression.
Removed unnecessary `@BeforeAll` setup methods within multiple unit test classes to streamline the testing process. Additionally, refined `NoCommanderNagDialog.java` by introducing constant strings for dialog names and added proper comments to maintain clarity and consistency.
Enhanced the OutstandingScenariosNagDialog by refactoring and adding helper methods for checking outstanding scenarios. Introduced a new unit test class to cover various scenarios and ensure the correctness of the new logic.
Refactored the PregnantCombatantNagDialog to enhance readability and reduce stream usage. Introduced new constants for dialog properties. Added comprehensive unit test coverage for PregnantCombatantNagDialog functionality.
Revised PrisonersNagDialog to use constants for dialog parameters and added JavaDoc comments for better readability. Introduced a new unit test class, PrisonersNagDialogTest, covering various scenarios for the hasPrisoners method to ensure proper functionality.
Refactored ShortDeploymentNagDialog to extract deployment requirement checks into a separate method for better readability and reuse. Added extensive unit tests for various scenarios to ensure consistent behavior. This improves code organization and testability.
Updated the 'UnableToAffordExpensesNagDialog' text to specify the exact amount of expenses and added unit tests. This provides clearer information to the user and ensures the dialog functions correctly under various conditions.
Updated the UnableToAffordJumpNagDialog to provide more detailed information about the inability to afford the next jump. Added unit tests for the UnableToAffordJumpNagDialog to ensure the functionality of checking if the campaign can afford the next jump. Refined the documentation and improved the structure and readability of the existing dialog-related code.
Refactored the `UnableToAffordLoanPaymentNagDialog` class to enhance readability and maintainability. Added unit tests to cover scenarios for the loan payment checks, ensuring better test coverage and reliability. Updated dialog text for dynamic payment amount display.
Added the 2024 copyright and licensing information to the following test files: OutstandingScenariosNagDialogTest, PregnantCombatantNagDialogTest, PrisonersNagDialogTest, ShortDeploymentNagDialogTest, UnableToAffordExpensesNagDialogTest, UnableToAffordJumpNagDialogTest, UnableToAffordLoanPaymentNagDialogTest, InvalidFactionNagDialogTest, NoCommanderNagDialogTest. This ensures compliance with the GNU General Public License.
Removed the nagUnresolvedContacts method from StratconRulesManager and integrated it into UnresolvedStratConContactsNagDialog. Added comprehensive unit tests to ensure proper functioning and validation of unresolved Stratcon scenarios in campaigns.
@IllianiCBT IllianiCBT changed the title Updated Monthly Expenses Nag Dialog & Added Unable to Afford Loan Payments Nag Refactored Nag Dialogs, Split UnableToAffordExpensesNagDialog into Two Dialogs, Added Unit Tests Sep 16, 2024
Removed the nagUnresolvedContacts method from StratconRulesManager and integrated it into UnresolvedStratConContactsNagDialog. Added comprehensive unit tests to ensure proper functioning and validation of unresolved Stratcon scenarios in campaigns.
@IllianiCBT IllianiCBT marked this pull request as ready for review September 16, 2024 03:04
@IllianiCBT
Copy link
Collaborator Author

Flipping this to 'live'. Description and title have been updated to reflect changes.

@Sleet01 there have been a bunch more Unit Tests added since your earlier review. I don't know if you want to take another look before confirming it has your seal of approval.

@rjhancock
Copy link
Collaborator

Other than the JavaDoc stuff I mentioned, (yes I know, minor stuff but we all need to get better at it), it looks good to me.

@IllianiCBT
Copy link
Collaborator Author

That should all be handled now - was there one I missed?

@rjhancock
Copy link
Collaborator

Half a dozen or so. I tagged them in the review changes section.

IllianiCBT and others added 2 commits September 18, 2024 13:24
Standardized Javadoc comments to use {@code} and {@link} annotations for better clarity and consistency. Enhanced descriptions in test classes to specify the methods and classes being tested, facilitating easier understanding and maintenance.
@IllianiCBT
Copy link
Collaborator Author

I believe I've caught the last remaining JavaDoc issues.

@rjhancock
Copy link
Collaborator

To help find the issues, run ./gradlew javadoc and it'll output a report of warnings and errors. Errors MUST be fixed, warnings RELATED to your code SHOULD be but NOT required.

Fixed the spelling of "TO&E" to "TOE" in the class documentation. This improves the readability and accuracy of the comment.
@IllianiCBT
Copy link
Collaborator Author

That command is invaluable, thank you

@rjhancock
Copy link
Collaborator

I'll be adding that to the Actions soon as part of a requirement of things passing. And eventually doing one for GH Pages of JavaDoc so be mindful and descriptive of your documentation. Write it for people who DON'T code.

@IllianiCBT
Copy link
Collaborator Author

Sounds good. I've been getting into the habit of writing JavaDocs for just about everything.

@HammerGS HammerGS merged commit ddbe027 into MegaMek:master Sep 25, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to afford expenses nag appears when current funds are below loan collateral.
5 participants