Skip to content

Commit

Permalink
tests: use cleanups in old command tests (#1253)
Browse files Browse the repository at this point in the history
* tests: use cleanups in guzzlr command test

* tests: use cleanups in snapper command test
  • Loading branch information
midgleyc committed Nov 9, 2022
1 parent fc2c34b commit 4409975
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 117 deletions.
171 changes: 92 additions & 79 deletions test/net/sourceforge/kolmafia/textui/command/GuzzlrCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
package net.sourceforge.kolmafia.textui.command;

import static internal.helpers.Player.withItem;
import static internal.helpers.Player.withProperty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

import net.sourceforge.kolmafia.AdventureResult;
import internal.helpers.Cleanups;
import net.sourceforge.kolmafia.KoLCharacter;
import net.sourceforge.kolmafia.KoLConstants;
import net.sourceforge.kolmafia.objectpool.ItemPool;
import net.sourceforge.kolmafia.preferences.Preferences;
import net.sourceforge.kolmafia.request.GenericRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class GuzzlrCommandTest extends AbstractCommandTestBase {
@BeforeEach
public void initEach() {
@BeforeAll
public static void init() {
KoLCharacter.reset("testUser");
KoLCharacter.reset(true);
Preferences.resetToDefault("_guzzlrQuestAbandoned");
Preferences.resetToDefault("questGuzzlr");
Preferences.resetToDefault("guzzlrBronzeDeliveries");
Preferences.resetToDefault("guzzlrGoldDeliveries");

// Stop requests from actually running
GenericRequest.sessionId = null;
Preferences.reset("testUser");
}

public GuzzlrCommandTest() {
this.command = "guzzlr";
}

private static void playerHasGuzzlrTablet() {
AdventureResult.addResultToList(KoLConstants.inventory, ItemPool.get(ItemPool.GUZZLR_TABLET));
}

@Test
void mustHaveGuzzlrTablet() {
String output = execute("accept bronze");
Expand All @@ -44,109 +33,133 @@ void mustHaveGuzzlrTablet() {

@Test
void mustSpecifyParameters() {
playerHasGuzzlrTablet();
String output = execute("");
var cleanups = withItem(ItemPool.GUZZLR_TABLET);
try (cleanups) {
String output = execute("");

assertErrorState();
assertThat(output, containsString("Use command guzzlr"));
assertErrorState();
assertThat(output, containsString("Use command guzzlr"));
}
}

@Test
void abandonMustHaveQuest() {
playerHasGuzzlrTablet();
Preferences.setString("questGuzzlr", "unstarted");
String output = execute("abandon");

assertContinueState();
assertThat(output, containsString("You don't have a client"));
var cleanups =
new Cleanups(withItem(ItemPool.GUZZLR_TABLET), withProperty("questGuzzlr", "unstarted"));
try (cleanups) {
String output = execute("abandon");

assertContinueState();
assertThat(output, containsString("You don't have a client"));
}
}

@Test
void cannotAbandonTwiceInADay() {
playerHasGuzzlrTablet();
Preferences.setBoolean("_guzzlrQuestAbandoned", true);
Preferences.setString("questGuzzlr", "started");
String output = execute("abandon");

assertErrorState();
assertThat(output, containsString("already abandoned"));
var cleanups =
new Cleanups(
withItem(ItemPool.GUZZLR_TABLET),
withProperty("_guzzlrQuestAbandoned", true),
withProperty("questGuzzlr", "started"));
try (cleanups) {
String output = execute("abandon");

assertErrorState();
assertThat(output, containsString("already abandoned"));
}
}

@Test
void abandonAbandons() {
playerHasGuzzlrTablet();
Preferences.setString("questGuzzlr", "started");
String output = execute("abandon");

assertContinueState();
assertThat(output, containsString("Abandoning client"));
var cleanups =
new Cleanups(withItem(ItemPool.GUZZLR_TABLET), withProperty("questGuzzlr", "started"));
try (cleanups) {
String output = execute("abandon");

assertContinueState();
assertThat(output, containsString("Abandoning client"));
}
}

@Test
void acceptRequiresNoClient() {
playerHasGuzzlrTablet();
Preferences.setString("questGuzzlr", "started");
String output = execute("accept bronze");

assertErrorState();
assertThat(output, containsString("You already have a client"));
var cleanups =
new Cleanups(withItem(ItemPool.GUZZLR_TABLET), withProperty("questGuzzlr", "started"));
try (cleanups) {
String output = execute("accept bronze");

assertErrorState();
assertThat(output, containsString("You already have a client"));
}
}

@Test
void acceptRequiresValidClient() {
playerHasGuzzlrTablet();
String output = execute("accept silver");
var cleanups = withItem(ItemPool.GUZZLR_TABLET);
try (cleanups) {
String output = execute("accept silver");

assertErrorState();
assertThat(output, containsString("Use command 'guzzlr"));
assertErrorState();
assertThat(output, containsString("Use command 'guzzlr"));
}
}

@Test
void acceptBronze() {
playerHasGuzzlrTablet();
String output = execute("accept bronze");
var cleanups = withItem(ItemPool.GUZZLR_TABLET);
try (cleanups) {
String output = execute("accept bronze");

assertContinueState();
assertThat(output, containsString("Accepting a bronze client"));
assertContinueState();
assertThat(output, containsString("Accepting a bronze client"));
}
}

@Test
void acceptGoldRequiresFiveBronze() {
playerHasGuzzlrTablet();
String output = execute("accept gold");

assertErrorState();
assertThat(
output, containsString("You need to make 5 bronze deliveries to serve gold clients"));
var cleanups = withItem(ItemPool.GUZZLR_TABLET);
try (cleanups) {
String output = execute("accept gold");

assertErrorState();
assertThat(
output, containsString("You need to make 5 bronze deliveries to serve gold clients"));
}
}

@Test
void acceptGold() {
playerHasGuzzlrTablet();
Preferences.setInteger("guzzlrBronzeDeliveries", 5);
String output = execute("accept gold");

assertContinueState();
assertThat(output, containsString("Accepting a gold client"));
var cleanups =
new Cleanups(withItem(ItemPool.GUZZLR_TABLET), withProperty("guzzlrBronzeDeliveries", 5));
try (cleanups) {
String output = execute("accept gold");

assertContinueState();
assertThat(output, containsString("Accepting a gold client"));
}
}

@Test
void acceptPlatinumRequiresFiveGold() {
playerHasGuzzlrTablet();
String output = execute("accept platinum");

assertErrorState();
assertThat(
output, containsString("You need to make 5 gold deliveries to serve platinum clients"));
var cleanups = withItem(ItemPool.GUZZLR_TABLET);
try (cleanups) {
String output = execute("accept platinum");

assertErrorState();
assertThat(
output, containsString("You need to make 5 gold deliveries to serve platinum clients"));
}
}

@Test
void acceptPlatinum() {
playerHasGuzzlrTablet();
Preferences.setInteger("guzzlrGoldDeliveries", 5);
String output = execute("accept platinum");

assertContinueState();
assertThat(output, containsString("Accepting a platinum client"));
var cleanups =
new Cleanups(withItem(ItemPool.GUZZLR_TABLET), withProperty("guzzlrGoldDeliveries", 5));
try (cleanups) {
String output = execute("accept platinum");

assertContinueState();
assertThat(output, containsString("Accepting a platinum client"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,79 +1,74 @@
package net.sourceforge.kolmafia.textui.command;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static internal.helpers.Player.withFamiliar;
import static internal.helpers.Player.withProperty;
import static org.junit.jupiter.api.Assertions.assertTrue;

import net.sourceforge.kolmafia.FamiliarData;
import internal.helpers.Cleanups;
import net.sourceforge.kolmafia.KoLCharacter;
import net.sourceforge.kolmafia.objectpool.FamiliarPool;
import net.sourceforge.kolmafia.preferences.Preferences;
import net.sourceforge.kolmafia.request.GenericRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class SnapperCommandTest extends AbstractCommandTestBase {
@BeforeEach
public void initEach() {
@BeforeAll
public static void init() {
KoLCharacter.reset("testUser");
Preferences.resetToDefault("redSnapperPhylum");

// Stop requests from actually running
GenericRequest.sessionId = null;
Preferences.reset("testUser");
}

public SnapperCommandTest() {
this.command = "snapper";
}

static String getPhylum() {
return Preferences.getString("redSnapperPhylum");
}

static void setSnapper() {
var familiar = FamiliarData.registerFamiliar(FamiliarPool.RED_SNAPPER, 1);
KoLCharacter.setFamiliar(familiar);
}

@Test
void mustHaveSnapper() {
KoLCharacter.setFamiliar(FamiliarData.NO_FAMILIAR);
String output = execute("bug");

assertTrue(output.contains("You need to take your Red-Nosed Snapper with you"));
assertContinueState();
assertEquals("", getPhylum());
}

@Test
void mustSpecifyPhylum() {
setSnapper();
execute("");
assertErrorState();
assertEquals("", getPhylum());
var cleanups = withFamiliar(FamiliarPool.RED_SNAPPER);
try (cleanups) {
String output = execute("");
assertErrorState();
assertTrue(output.contains("Which monster phylum do you want?"));
}
}

@Test
void mustSpecifyValidPhylum() {
setSnapper();
execute("dog");
assertErrorState();
assertEquals("", getPhylum());
var cleanups = withFamiliar(FamiliarPool.RED_SNAPPER);
try (cleanups) {
String output = execute("dog");
assertErrorState();
assertTrue(output.contains("What kind of random monster is a dog?"));
}
}

@Test
void alreadyTrackingPhylum() {
Preferences.setString("redSnapperPhylum", "beast");
setSnapper();
String output = execute("beast");
assertContinueState();
assertTrue(output.contains("already hot on the tail"));
var cleanups =
new Cleanups(
withFamiliar(FamiliarPool.RED_SNAPPER), withProperty("redSnapperPhylum", "beast"));
try (cleanups) {
String output = execute("beast");
assertContinueState();
assertTrue(output.contains("already hot on the tail"));
}
}

@Test
void changesPhylum() {
setSnapper();
String output = execute("beast");
assertContinueState();
assertTrue(output.contains("guiding you towards beasts"));
var cleanups = withFamiliar(FamiliarPool.RED_SNAPPER);
try (cleanups) {
String output = execute("beast");
assertContinueState();
assertTrue(output.contains("guiding you towards beasts"));
}
}
}

0 comments on commit 4409975

Please sign in to comment.