Skip to content
Draft
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
101 changes: 101 additions & 0 deletions tests/location/test_tavern.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,104 @@ def mock_showOptions(prompt, options):
# Verify the message shows the actual bet amount won, not $0
assert "You won $50!" in tavernInstance.currentPrompt.text
assert "You won $0!" not in tavernInstance.currentPrompt.text


def test_gamble_loss():
# prepare
tavernInstance = createTavern()
tavernInstance.player.money = 100
tavernInstance.currentBet = 50

# Test the loss logic directly
input_value = 1
tavernInstance.diceThrow = 2 # Different from player choice

# Execute the loss condition logic
tavernInstance.player.money -= tavernInstance.currentBet
tavernInstance.stats.moneyLostFromGambling += tavernInstance.currentBet
tavernInstance.currentBet = 0
tavernInstance.currentPrompt.text = (
"The dice rolled a %d! You lost your money! Care to try again? Current Bet: $%d"
% (tavernInstance.diceThrow, tavernInstance.currentBet)
)

# check
assert tavernInstance.player.money == 50 # Lost 50
assert tavernInstance.stats.moneyLostFromGambling == 50
assert tavernInstance.currentBet == 0
assert "You lost your money!" in tavernInstance.currentPrompt.text


def test_changeBet_insufficient_money():
# prepare
tavernInstance = createTavern()
tavernInstance.userInterface.lotsOfSpace = MagicMock()
tavernInstance.userInterface.divider = MagicMock()
tavernInstance.player.money = 50

# Mock input to simulate user entering more than they have
import builtins

original_input = builtins.input
builtins.input = MagicMock(return_value="100")

try:
# call
tavernInstance.changeBet("How much money would you like to bet? Money: $50")

# check
# Bet should not be set since player doesn't have enough money
assert tavernInstance.currentBet == 0
# Verify error message
assert "You don't have that much money" in tavernInstance.currentPrompt.text
finally:
# Restore original input function
builtins.input = original_input


def test_changeBet_invalid_input():
# prepare
tavernInstance = createTavern()
tavernInstance.userInterface.lotsOfSpace = MagicMock()
tavernInstance.userInterface.divider = MagicMock()
tavernInstance.player.money = 100

# Mock input to simulate user entering invalid input
import builtins

original_input = builtins.input
builtins.input = MagicMock(return_value="not a number")

try:
# call
tavernInstance.changeBet("How much money would you like to bet? Money: $100")

# check
# Bet should remain 0
assert tavernInstance.currentBet == 0
# Verify error message
assert "Try again" in tavernInstance.currentPrompt.text
finally:
# Restore original input function
builtins.input = original_input


def test_getDrunk_updates_stats():
# prepare
tavernInstance = createTavern()
tavernInstance.userInterface.lotsOfSpace = MagicMock()
tavernInstance.userInterface.divider = MagicMock()
tavernInstance.player.money = 20
tavernInstance.stats.timesGottenDrunk = 0
tavern.print = MagicMock()
tavern.sys.stdout.flush = MagicMock()
tavern.time.sleep = MagicMock()
tavernInstance.timeService.increaseDay = MagicMock()

# call
tavernInstance.getDrunk()

# check
assert tavernInstance.player.money == 10 # Lost $10
assert tavernInstance.stats.timesGottenDrunk == 1
assert tavernInstance.currentPrompt.text == "You have a headache."
66 changes: 66 additions & 0 deletions tests/player/test_playerJsonReaderWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,69 @@ def test_createPlayerFromJson_backwards_compatibility():
assert player.money == playerJson["money"]
assert player.moneyInBank == playerJson["moneyInBank"]
assert player.energy == 100 # Should default to 100


def test_writePlayerToFile():
# prepare
import tempfile

playerJsonReaderWriter = createPlayerJsonReaderWriter()
player = createPlayer()
player.fishCount = 10
player.money = 500

# call
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as f:
playerJsonReaderWriter.writePlayerToFile(player, f)
temp_file_path = f.name

# check - read back the file and verify
with open(temp_file_path, "r") as f:
playerJson = json.load(f)

assert playerJson["fishCount"] == 10
assert playerJson["money"] == 500

# cleanup
import os

os.remove(temp_file_path)


def test_readPlayerFromFile():
# prepare
import tempfile

playerJson = {
"fishCount": 15,
"fishMultiplier": 3,
"money": 250,
"moneyInBank": 100,
"priceForBait": 60,
"energy": 80,
}

# Write test data to temp file
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".json"
) as f:
json.dump(playerJson, f)
temp_file_path = f.name

# call
playerJsonReaderWriter = createPlayerJsonReaderWriter()
with open(temp_file_path, "r") as f:
player = playerJsonReaderWriter.readPlayerFromFile(f)

# check
assert player.fishCount == 15
assert player.fishMultiplier == 3
assert player.money == 250
assert player.moneyInBank == 100
assert player.priceForBait == 60
assert player.energy == 80

# cleanup
import os

os.remove(temp_file_path)
20 changes: 20 additions & 0 deletions tests/prompt/test_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from src.prompt.prompt import Prompt


def test_initialization():
# call
prompt = Prompt("Test prompt")

# check
assert prompt.text == "Test prompt"


def test_text_can_be_modified():
# prepare
prompt = Prompt("Initial text")

# call
prompt.text = "Modified text"

# check
assert prompt.text == "Modified text"
66 changes: 66 additions & 0 deletions tests/stats/test_statsJsonReaderWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,69 @@ def test_createStatsFromJson():
assert statsFromJson.moneyMadeFromInterest == 2
assert statsFromJson.timesGottenDrunk == 2
assert statsFromJson.moneyLostFromGambling == 2


def test_writeStatsToFile():
# prepare
import tempfile

statsJsonReaderWriter = createStatsJsonReaderWriter()
stats = createStats()
stats.totalFishCaught = 50
stats.totalMoneyMade = 1000

# call
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as f:
statsJsonReaderWriter.writeStatsToFile(stats, f)
temp_file_path = f.name

# check - read back the file and verify
with open(temp_file_path, "r") as f:
statsJson = json.load(f)

assert statsJson["totalFishCaught"] == 50
assert statsJson["totalMoneyMade"] == 1000

# cleanup
import os

os.remove(temp_file_path)


def test_readStatsFromFile():
# prepare
import tempfile

statsJson = {
"totalFishCaught": 75,
"totalMoneyMade": 1500,
"hoursSpentFishing": 20,
"moneyMadeFromInterest": 100,
"timesGottenDrunk": 5,
"moneyLostFromGambling": 200,
}

# Write test data to temp file
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".json"
) as f:
json.dump(statsJson, f)
temp_file_path = f.name

# call
statsJsonReaderWriter = createStatsJsonReaderWriter()
with open(temp_file_path, "r") as f:
stats = statsJsonReaderWriter.readStatsFromFile(f)

# check
assert stats.totalFishCaught == 75
assert stats.totalMoneyMade == 1500
assert stats.hoursSpentFishing == 20
assert stats.moneyMadeFromInterest == 100
assert stats.timesGottenDrunk == 5
assert stats.moneyLostFromGambling == 200

# cleanup
import os

os.remove(temp_file_path)
59 changes: 59 additions & 0 deletions tests/world/test_timeServiceJsonReaderWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,62 @@ def test_createTimeServiceFromJson():
timeServiceJson, player, stats
)
assert timeServiceFromJson != None


def test_writeTimeServiceToFile():
# prepare
import tempfile

timeServiceJsonReaderWriter = createTimeServiceJsonReaderWriter()
timeService = createTimeService()
timeService.time = 15
timeService.day = 10

# call
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as f:
timeServiceJsonReaderWriter.writeTimeServiceToFile(timeService, f)
temp_file_path = f.name

# check - read back the file and verify
with open(temp_file_path, "r") as f:
timeServiceJson = json.load(f)

assert timeServiceJson["time"] == 15
assert timeServiceJson["day"] == 10

# cleanup
import os

os.remove(temp_file_path)


def test_readTimeServiceFromFile():
# prepare
import tempfile

timeServiceJson = {"time": 12, "day": 5}

# Write test data to temp file
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".json"
) as f:
json.dump(timeServiceJson, f)
temp_file_path = f.name

# call
timeServiceJsonReaderWriter = createTimeServiceJsonReaderWriter()
player = Player()
stats = Stats()
with open(temp_file_path, "r") as f:
timeService = timeServiceJsonReaderWriter.readTimeServiceFromFile(
f, player, stats
)

# check
assert timeService.time == 12
assert timeService.day == 5

# cleanup
import os

os.remove(temp_file_path)