Skip to content

Commit

Permalink
chore: add mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy committed Oct 17, 2024
1 parent 6eba574 commit 98ac2f5
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 45 deletions.
63 changes: 63 additions & 0 deletions src/test/java/dinkplugin/util/AbstractRarityServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dinkplugin.util;

import com.google.gson.Gson;
import com.google.inject.testing.fieldbinder.Bind;
import dinkplugin.MockedTestBase;
import net.runelite.api.ItemComposition;
import net.runelite.client.game.ItemManager;
import net.runelite.http.api.RuneLiteAPI;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;

import java.util.OptionalDouble;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

abstract class AbstractRarityServiceTest extends MockedTestBase {
private static final double DELTA = MathUtils.EPSILON;

@Bind
protected final Gson gson = RuneLiteAPI.GSON;

@Bind
protected final ItemManager itemManager = Mockito.mock(ItemManager.class);

protected abstract AbstractRarityService getService();

@Override
@BeforeEach
protected void setUp() {
super.setUp();

// default item mock
Mockito.doAnswer(invocation -> {
ItemComposition comp = mock(ItemComposition.class);
when(comp.getMembersName()).thenReturn("?");
when(comp.getNote()).thenReturn(-1);
return comp;
}).when(itemManager).getItemComposition(anyInt());
}

protected void test(String npcName, int itemId, int quantity, double expectedProbability) {
OptionalDouble rarity = getService().getRarity(npcName, itemId, quantity);
assertTrue(rarity.isPresent());
assertEquals(expectedProbability, rarity.getAsDouble(), DELTA);
}

protected void mockItem(int id, String name, boolean noted) {
ItemComposition item = mock(ItemComposition.class);
when(item.getName()).thenReturn(name);
when(item.getMembersName()).thenReturn(name);
when(item.getNote()).thenReturn(noted ? 799 : -1);
when(item.getLinkedNoteId()).thenReturn(noted ? id - 1 : id + 1);
when(itemManager.getItemComposition(id)).thenReturn(item);
}

protected void mockItem(int id, String name) {
this.mockItem(id, name, false);
}
}
48 changes: 3 additions & 45 deletions src/test/java/dinkplugin/util/RarityServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
package dinkplugin.util;

import com.google.gson.Gson;
import com.google.inject.testing.fieldbinder.Bind;
import dinkplugin.MockedTestBase;
import net.runelite.api.ItemComposition;
import lombok.Getter;
import net.runelite.api.ItemID;
import net.runelite.client.game.ItemManager;
import net.runelite.http.api.RuneLiteAPI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.OptionalDouble;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class RarityServiceTest extends MockedTestBase {

private static final double DELTA = MathUtils.EPSILON;

@Bind
private final Gson gson = RuneLiteAPI.GSON;

@Bind
private final ItemManager itemManager = Mockito.mock(ItemManager.class);
class RarityServiceTest extends AbstractRarityServiceTest {

@Bind
@Getter
private final RarityService service = Mockito.spy(new RarityService(gson, itemManager));

@Override
@BeforeEach
protected void setUp() {
super.setUp();

// default item mock
Mockito.doAnswer(invocation -> {
ItemComposition comp = mock(ItemComposition.class);
when(comp.getMembersName()).thenReturn("?");
when(comp.getNote()).thenReturn(-1);
return comp;
}).when(itemManager).getItemComposition(anyInt());

// actual item mocks
mockItem(ItemID.DRAGON_SPEAR, "Dragon spear");
mockItem(ItemID.DRAGON_AXE, "Dragon axe");
Expand Down Expand Up @@ -265,22 +241,4 @@ void testVariations() {
test("Tribesman", ItemID.SUPERANTIPOISON3, 1, 1.0 / 138);
}

private void test(String npcName, int itemId, int quantity, double expectedProbability) {
OptionalDouble rarity = service.getRarity(npcName, itemId, quantity);
assertTrue(rarity.isPresent());
assertEquals(expectedProbability, rarity.getAsDouble(), DELTA);
}

private void mockItem(int id, String name, boolean noted) {
ItemComposition item = mock(ItemComposition.class);
when(item.getName()).thenReturn(name);
when(item.getMembersName()).thenReturn(name);
when(item.getNote()).thenReturn(noted ? 799 : -1);
when(item.getLinkedNoteId()).thenReturn(noted ? id - 1 : id + 1);
when(itemManager.getItemComposition(id)).thenReturn(item);
}

private void mockItem(int id, String name) {
this.mockItem(id, name, false);
}
}
92 changes: 92 additions & 0 deletions src/test/java/dinkplugin/util/ThievingServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package dinkplugin.util;

import com.google.inject.testing.fieldbinder.Bind;
import lombok.Getter;
import net.runelite.api.ItemID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class ThievingServiceTest extends AbstractRarityServiceTest {

@Bind
@Getter
private final ThievingService service = Mockito.spy(new ThievingService(gson, itemManager));

@Override
@BeforeEach
protected void setUp() {
super.setUp();

// item mocks
mockItem(ItemID.BLOOD_SHARD, "Blood shard");
mockItem(ItemID.ENHANCED_CRYSTAL_TELEPORT_SEED, "Enhanced crystal teleport seed");
mockItem(ItemID.UNCUT_DIAMOND, "Uncut diamond");
mockItem(ItemID.CLUE_SCROLL_EASY, "Clue scroll (easy)");
mockItem(ItemID.CLUE_SCROLL_EASY_2711, "Clue scroll (easy)");
mockItem(ItemID.CLUE_SCROLL_MEDIUM, "Clue scroll (medium)");
mockItem(ItemID.CLUE_SCROLL_MEDIUM_2809, "Clue scroll (medium)");
mockItem(ItemID.CLUE_SCROLL_HARD, "Clue scroll (hard)");
mockItem(ItemID.CLUE_SCROLL_HARD_3560, "Clue scroll (hard)");
mockItem(ItemID.CLUE_SCROLL_ELITE, "Clue scroll (elite)");
mockItem(ItemID.CLUE_SCROLL_ELITE_12157, "Clue scroll (elite)");
mockItem(ItemID.CLUE_SCROLL_ELITE_12075, "Clue scroll (elite)");
mockItem(ItemID.HAM_CLOAK, "Ham cloak");
mockItem(ItemID.HAM_BOOTS, "Ham boots");
mockItem(ItemID.SNAPE_GRASS_SEED, "Snape grass seed");
mockItem(ItemID.SNAPDRAGON_SEED, "Snapdragon seed");
}

@Test
void testFarmer() {
test("Master Farmer", ItemID.SNAPE_GRASS_SEED, 1, 1.0 / 260);
test("Master Farmer", ItemID.SNAPDRAGON_SEED, 1, 1.0 / 2083);
}

@Test
void testHam() {
test("H.A.M. Member", ItemID.HAM_CLOAK, 1, 1.0 / 100);
test("H.A.M. Member", ItemID.HAM_BOOTS, 1, 1.0 / 100);
}

@Test
void testCitizen() {
test("Wealthy citizen", ItemID.CLUE_SCROLL_EASY, 1, 1.0 / 85);
}

@Test
void testPaladin() {
test("Paladin", ItemID.CLUE_SCROLL_HARD, 1, 1.0 / 1000);
}

@Test
void testGnome() {
test("Gnome", ItemID.CLUE_SCROLL_MEDIUM, 1, 1.0 / 150);
}

@Test
void testHero() {
test("Hero", ItemID.CLUE_SCROLL_ELITE, 1, 1.0 / 1400);
test("Hero", ItemID.CLUE_SCROLL_ELITE_12075, 1, 1.0 / 1400);
test("Hero", ItemID.CLUE_SCROLL_ELITE_12157, 1, 1.0 / 1400);
}

@Test
void testVyre() {
test("Caninelle Draynar", ItemID.BLOOD_SHARD, 1, 1.0 / 5000);
test("Grigor Rasputin", ItemID.BLOOD_SHARD, 1, 1.0 / 5000);
test("Valentina Diaemus", ItemID.BLOOD_SHARD, 1, 1.0 / 5000);
}

@Test
void testElf() {
test("Arvel", ItemID.ENHANCED_CRYSTAL_TELEPORT_SEED, 1, 1.0 / 1024);
test("Indis", ItemID.ENHANCED_CRYSTAL_TELEPORT_SEED, 1, 1.0 / 1024);
}

@Test
void testHur() {
test("TzHaar-Hur", ItemID.UNCUT_DIAMOND, 1, 1.0 / 195);
}

}

0 comments on commit 98ac2f5

Please sign in to comment.