Repository navigation
Lock a law group for 3 days after it changes #44
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/test/java/net/tfminecraft/simplefactions/laws/LawSwitchLockTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package net.tfminecraft.simplefactions.laws; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.bukkit.configuration.file.YamlConfiguration; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import net.tfminecraft.simplefactions.Cache; | ||
| import net.tfminecraft.simplefactions.objects.Faction; | ||
| import net.tfminecraft.simplefactions.objects.handler.LawHandler; | ||
|
|
||
| class LawSwitchLockTest { | ||
|
|
||
| private static final long DAY = 86_400_000L; | ||
| private static final long NOW = 1_000L * DAY; | ||
|
|
||
| private final double savedDays = Cache.lawSwitchLockDays; | ||
|
|
||
| @AfterEach | ||
| void restore() { | ||
| Cache.lawSwitchLockDays = savedDays; | ||
| } | ||
|
|
||
| @Test | ||
| void neverSwitched_notLocked() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| assertEquals(0, lawGroup().lockRemaining(NOW)); | ||
| } | ||
|
|
||
| @Test | ||
| void switching_startsLockForConfiguredDays() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| LawGroup group = lawGroup(); | ||
| group.switchTo(group.getLaw("high"), NOW); | ||
|
|
||
| assertEquals(3 * DAY, group.lockRemaining(NOW)); | ||
| assertEquals(DAY, group.lockRemaining(NOW + 2 * DAY)); | ||
| assertEquals(0, group.lockRemaining(NOW + 3 * DAY)); | ||
| } | ||
|
|
||
| @Test | ||
| void reapplyingSameLaw_doesNotRestartLock() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| LawGroup group = lawGroup(); | ||
| group.switchTo(group.getLaw("high"), NOW); | ||
| group.switchTo(group.getLaw("high"), NOW + 2 * DAY); | ||
|
|
||
| assertEquals(NOW, group.getChangedAt()); | ||
| } | ||
|
|
||
| @Test | ||
| void setCurrent_doesNotStartLock() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| LawGroup group = lawGroup(); | ||
| group.setCurrent(group.getLaw("high")); | ||
|
|
||
| assertEquals(0, group.lockRemaining(NOW)); | ||
| } | ||
|
|
||
| @Test | ||
| void zeroDays_disablesLock() { | ||
| Cache.lawSwitchLockDays = 0; | ||
| LawGroup group = lawGroup(); | ||
| group.switchTo(group.getLaw("high"), NOW); | ||
|
|
||
| assertEquals(0, group.lockRemaining(NOW)); | ||
| } | ||
|
|
||
| @Test | ||
| void lockReason_blocksOtherLawsInLockedGroup() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| LawGroup group = lawGroup(); | ||
| group.switchTo(group.getLaw("high"), NOW); | ||
| Faction faction = factionWith(group); | ||
|
|
||
| String reason = CanHaveLaw.lockReason(faction, group.getLaw("low"), NOW + DAY); | ||
| assertNotNull(reason); | ||
| assertTrue(reason.contains("2d 0h"), reason); | ||
| assertNull(CanHaveLaw.lockReason(faction, group.getLaw("low"), NOW + 3 * DAY)); | ||
| } | ||
|
|
||
| @Test | ||
| void lockReason_exemptsCurrentLaw() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| LawGroup group = lawGroup(); | ||
| group.switchTo(group.getLaw("high"), NOW); | ||
|
|
||
| assertNull(CanHaveLaw.lockReason(factionWith(group), group.getLaw("high"), NOW + DAY)); | ||
| } | ||
|
|
||
| @Test | ||
| void blockReason_currentLawStillAvailableWhileLocked() { | ||
| Cache.lawSwitchLockDays = 3; | ||
| LawGroup group = lawGroup(); | ||
| group.switchTo(group.getLaw("high"), System.currentTimeMillis()); | ||
| Faction faction = factionWith(group); | ||
|
|
||
| assertNull(CanHaveLaw.blockReason(faction, group.getLaw("high"))); | ||
| assertNotNull(CanHaveLaw.blockReason(faction, group.getLaw("low"))); | ||
| } | ||
|
|
||
| @Test | ||
| void formatRemaining_roundsUpToMinutes() { | ||
| assertEquals("2d 5h", CanHaveLaw.formatRemaining(2 * DAY + 5 * 3_600_000L + 1)); | ||
| assertEquals("3h 1m", CanHaveLaw.formatRemaining(3 * 3_600_000L + 1)); | ||
| assertEquals("1m", CanHaveLaw.formatRemaining(1)); | ||
| } | ||
|
|
||
| private static Faction factionWith(LawGroup group) { | ||
| Faction faction = mock(Faction.class); | ||
| LawHandler handler = mock(LawHandler.class); | ||
| when(faction.getLawHandler()).thenReturn(handler); | ||
| when(handler.getGroup("taxes")).thenReturn(group); | ||
| when(handler.getCurrentLaws()).thenReturn(List.of(group.getCurrent())); | ||
| return faction; | ||
| } | ||
|
|
||
| private static LawGroup lawGroup() { | ||
| YamlConfiguration config = new YamlConfiguration(); | ||
| config.set("name", "Taxes"); | ||
| for (String id : List.of("low", "high")) { | ||
| config.createSection("laws." + id); | ||
| config.set("laws." + id + ".name", id); | ||
| } | ||
| LawGroup group = new LawGroup("taxes", config); | ||
| group.setCurrent(group.getLaw("low")); | ||
| return group; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.