Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.diamonddagger590.mccore.gui;

import com.diamonddagger590.mccore.CorePlugin;
import com.diamonddagger590.mccore.gui.slot.pagination.NextPageSlot;
import com.diamonddagger590.mccore.gui.slot.pagination.PreviousPageSlot;
import com.diamonddagger590.mccore.player.CorePlayer;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

class PaginatedGuiAdditionalBranchCoverageTest {

private static class TestCorePlayer extends CorePlayer {
public TestCorePlayer(@NotNull UUID uuid, @NotNull CorePlugin corePlugin) {
super(uuid, corePlugin);
}

@Override
public boolean useMutex() {
return false;
}
}

private static class ZeroMaxPageGui extends PaginatedGui<TestCorePlayer> {

private final Inventory mockInventory;

public ZeroMaxPageGui(@NotNull TestCorePlayer player, @NotNull Inventory mockInventory) {
super(player);
this.mockInventory = mockInventory;
}

@Override
public int getMaximumPage() {
return 0;
}

@NotNull
@Override
public PreviousPageSlot<TestCorePlayer> getPreviousPageSlot() {
return mock(PreviousPageSlot.class);
}

@NotNull
@Override
public NextPageSlot<TestCorePlayer> getNextPageSlot() {
return mock(NextPageSlot.class);
}

@NotNull
@Override
protected Inventory getInventoryForPage(int page) {
return mockInventory;
}

@Override
protected void paintInventoryForPage(@NotNull Inventory inventory, int page) {
}

@Override
public void paintInventory() {
super.paintInventory();
}

@Override
public void registerListeners() {
}

@Override
public void unregisterListeners() {
}
}

private TestCorePlayer player;
private Inventory mockInventory;

@BeforeEach
void setUp() {
CorePlugin plugin = mock(CorePlugin.class);
player = new TestCorePlayer(UUID.randomUUID(), plugin);
mockInventory = mock(Inventory.class);
}

@Test
@DisplayName("Given maximum page is 0, when setPage called, then throws IllegalArgumentException for max page < 1 precondition")
void setPage_throwsForMaxPageLessThanOne_whenMaximumPageIsZero() {
ZeroMaxPageGui gui = new ZeroMaxPageGui(player, mockInventory);
gui.getInventory();

assertThrows(IllegalArgumentException.class, () -> gui.setPage(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.diamonddagger590.mccore.parser;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ParserAdditionalBranchCoverageTest {

private static final double DELTA = 1e-9;

@Test
@DisplayName("Given variable starting with underscore, when evaluating, then isText recognizes underscore as first char")
void getValue_recognizesUnderscore_whenVariableStartsWithUnderscore() {
Parser parser = new Parser("_myVar + 1");
parser.setVariable("_myVar", 5.0);
assertEquals(6.0, parser.getValue(), DELTA);
}

@Test
@DisplayName("Given variable that is only underscore, when evaluating, then resolves correctly")
void getValue_recognizesUnderscore_whenVariableIsSingleUnderscore() {
Parser parser = new Parser("_ * 2");
parser.setVariable("_", 3.0);
assertEquals(6.0, parser.getValue(), DELTA);
}

@Test
@DisplayName("Given just a number, when getInputString called, then returns the number")
void getInputString_returnsNumber_whenInputIsSimpleNumber() {
assertEquals("42", new Parser("42").getInputString());
}

@Test
@DisplayName("Given built-in constant pi, when evaluating, then returns Math.PI")
void getValue_returnsMathPI_whenPiUsed() {
assertEquals(Math.PI, new Parser("pi").getValue(), DELTA);
}

@Test
@DisplayName("Given built-in constant e, when evaluating, then returns Math.E")
void getValue_returnsMathE_whenEUsed() {
assertEquals(Math.E, new Parser("e").getValue(), DELTA);
}

@Test
@DisplayName("Given unrecognized characters in input, when convertInput strips them, then expression evaluates without them")
void getValue_stripsUnrecognizedCharacters_whenInputContainsInvalidChars() {
assertEquals(23.0, new Parser("2@3").getValue(), DELTA);
}

@Test
@DisplayName("Given expression with implicit multiplication, when evaluating, then handles correctly")
void getValue_handlesImplicitMultiplication_whenConstantFollowedByVariable() {
Parser parser = new Parser("2x");
parser.setVariable("x", 5.0);
assertEquals(10.0, parser.getValue(), DELTA);
}

@Test
@DisplayName("Given expression with implicit multiplication before function, when evaluating, then handles correctly")
void getValue_handlesImplicitMultiplication_whenConstantFollowedByFunction() {
assertEquals(2.0 * Math.sin(1.0), new Parser("2sin(1)").getValue(), DELTA);
}

@Test
@DisplayName("Given error=true and unset variable, when evaluating via Parser, then throws EvaluationException")
void getValue_throwsEvaluationException_whenErrorModeAndVariableUnset() {
Parser parser = new Parser("x + 1", true);
assertThrows(EvaluationException.class, parser::getValue);
}

@Test
@DisplayName("Given error=true and variable is set, when evaluating via Parser, then returns correct value")
void getValue_returnsCorrectValue_whenErrorModeAndVariableIsSet() {
Parser parser = new Parser("x + 1", true);
parser.setVariable("x", 4.0);
assertEquals(5.0, parser.getValue(), DELTA);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.diamonddagger590.mccore.parser;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SfunAdditionalBranchCoverageTest {

private static final double DELTA = 1e-9;

@Test
@DisplayName("Given x = 0.0, when computing cot, then returns positive infinity (y == 0.0 branch)")
void cot_returnsPositiveInfinity_whenXIsZero() {
assertEquals(Double.POSITIVE_INFINITY, Sfun.cot(0.0));
}

@Test
@DisplayName("Given x = pi/4, when computing cot, then x != 0 and sign branch is exercised")
void cot_exercisesSignBranch_whenXIsNonZero() {
double result = Sfun.cot(Math.PI / 4);
assertEquals(1.0, result, 1e-6);
}

@Test
@DisplayName("Given negative integer -11, when computing gamma, then returns NaN (|x| > 10 negative integer branch)")
void gamma_returnsNaN_whenXIsLargeNegativeInteger() {
assertTrue(Double.isNaN(Sfun.gamma(-11.0)));
}

@Test
@DisplayName("Given x = -170.7, when computing gamma, then returns 0 (underflow branch)")
void gamma_returnsZero_whenXIsVeryLargeNegative() {
assertEquals(0.0, Sfun.gamma(-170.7), DELTA);
}

@Test
@DisplayName("Given x = 172, when computing gamma, then returns positive infinity (overflow branch)")
void gamma_returnsInfinity_whenXExceedsOverflowThreshold() {
assertEquals(Double.POSITIVE_INFINITY, Sfun.gamma(172.0));
}

@Test
@DisplayName("Given large negative integer -11, when computing logGamma, then returns NaN (sinpiy == 0 branch)")
void logGamma_returnsNaN_whenXIsLargeNegativeInteger() {
assertTrue(Double.isNaN(Sfun.logGamma(-11.0)));
}

@Test
@DisplayName("Given very small p and q, when computing logBeta, then exercises dlnrel path")
void logBeta_exercisesDlnrelPath_whenBothArgsVerySmall() {
double result = Sfun.logBeta(0.001, 0.001);
assertEquals(7.6009008, result, 1e-4);
}

@Test
@DisplayName("Given negative n, when computing fact, then returns NaN")
void fact_returnsNaN_whenNIsNegative() {
assertTrue(Double.isNaN(Sfun.fact(-1)));
}

@Test
@DisplayName("Given n > 170, when computing fact, then returns positive infinity")
void fact_returnsInfinity_whenNExceeds170() {
assertEquals(Double.POSITIVE_INFINITY, Sfun.fact(171));
}

@Test
@DisplayName("Given n = 0, when computing fact, then returns 1")
void fact_returnsOne_whenNIsZero() {
assertEquals(1.0, Sfun.fact(0), DELTA);
}
}
Loading