summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-04-27 02:20:46 +0300
committerViljami Ilola <+@hix.fi>2024-04-27 02:20:46 +0300
commitd73fe4bb20e544443476ebb4c9fc593fa0a8ad4a (patch)
tree9a0669819d4f18cf1a118f0755d78dd95959dc7c
parentdb5967a52d9409842581276154a6fa9763a57a24 (diff)
fix tests
-rw-r--r--README.txt4
-rw-r--r--src/sliceitoff/settings/settings.py6
-rw-r--r--src/sliceitoff/sfx/sfx.py3
-rw-r--r--tests/test_field.py2
-rw-r--r--tests/test_highscores.py45
-rw-r--r--tests/test_sfx.py24
6 files changed, 62 insertions, 22 deletions
diff --git a/README.txt b/README.txt
index 6ec2e00..3cd6524 100644
--- a/README.txt
+++ b/README.txt
@@ -17,8 +17,8 @@ Installing:
License:
- This project uses GPL-2 license. Assets have their licenses listed
- on `src/sliceitoff/assets/COPYRIGHTS.txt`.
+ This project uses GPL-2 license. Assets have their licenses and
+ copyrights listed on `src/sliceitoff/assets/COPYRIGHTS.txt`.
Developement:
diff --git a/src/sliceitoff/settings/settings.py b/src/sliceitoff/settings/settings.py
index 19cca7d..2d4b28f 100644
--- a/src/sliceitoff/settings/settings.py
+++ b/src/sliceitoff/settings/settings.py
@@ -3,12 +3,14 @@ import os
from pathlib import Path
from .static import DEFAULT_SETTINGS
+TEST_CONFIG_FILE = os.getenv("TEST_CONFIG_FILE")
+
class Settings:
""" Handles loading and saving settings from config file"""
def __init__(self, filename = None):
self.settings=[]
- if filename:
- self.config_filename = filename
+ if TEST_CONFIG_FILE:
+ self.config_filename = Path(TEST_CONFIG_FILE)
else:
if os.name == 'nt':
self.config_filename = (Path.home().resolve()
diff --git a/src/sliceitoff/sfx/sfx.py b/src/sliceitoff/sfx/sfx.py
index 8f3371e..a72df0b 100644
--- a/src/sliceitoff/sfx/sfx.py
+++ b/src/sliceitoff/sfx/sfx.py
@@ -52,7 +52,8 @@ class Sfx:
self.music_volume += 1
self.music_volume %= 11
settings.replace_value("music_volume", self.music_volume)
- self.sound[self.bgm].set_volume(self.music_volume / 10)
+ if self.bgm:
+ self.sound[self.bgm].set_volume(self.music_volume / 10)
def play(self, sample):
""" Just plays named sample loaded from assets directory """
diff --git a/tests/test_field.py b/tests/test_field.py
index ac56184..e84c3fd 100644
--- a/tests/test_field.py
+++ b/tests/test_field.py
@@ -10,7 +10,7 @@ from sliceitoff.display import Scaling
class TestFieldSprite(unittest.TestCase):
def test_can_create(self):
- fs = FieldSprite((0,0,320_000,220_000))
+ fs = FieldSprite((0,0,320_000,220_000), (0,0,0,0))
self.assertNotEqual(None, fs)
diff --git a/tests/test_highscores.py b/tests/test_highscores.py
index 048a42c..366d60f 100644
--- a/tests/test_highscores.py
+++ b/tests/test_highscores.py
@@ -1,37 +1,50 @@
+import os
from pathlib import Path
import unittest
import pygame
-from sliceitoff.hiscores import HiScores
+os.environ["TEST_CONFIG_FILE"] = "/tmp/sliceitoff-hiscores-test"
+
+from sliceitoff.settings import settings
+from sliceitoff.hiscores import hi_scores
+
class TestHiScores(unittest.TestCase):
def setUp(self):
self.filename = Path("/tmp/sliceitoff-hiscores-test")
if self.filename.is_file():
self.filename.unlink()
- self.hiscores = HiScores(filename = self.filename)
def tearDown(self):
- try:
- del self.hiscores
- except AttributeError:
- pass
if self.filename.is_file():
self.filename.unlink()
def test_can_create(self):
- self.assertNotEqual(None, self.hiscores)
+ self.assertNotEqual(None, hi_scores)
- def test_config_file_is_created_on_exit(self):
- del self.hiscores
- self.assertTrue(self.filename.is_file())
+ def test_config_save_can_run(self):
+ hi_scores.save()
def test_scores_can_be_added(self):
- self.hiscores.add(500_000,"HUH")
+ hi_scores.add(500_000,"HUH")
def test_scores_can_be_saved(self):
- self.hiscores.add(230_000,"HAH")
- old_scores = str(self.hiscores)
- del self.hiscores
- self.hiscores = HiScores(filename = self.filename)
- self.assertEqual(old_scores, str(self.hiscores))
+ hi_scores.add(230_000,"ZAP")
+ hi_scores.save()
+ settings.save()
+ with open(self.filename, "r", encoding="utf-8") as cfile:
+ for line in cfile:
+ if line == "hiscore=230000!ZAP\n":
+ break
+ else:
+ self.fail("Problem saving hiscores.")
+
+ def test_high_enough(self):
+ for _ in range(20):
+ hi_scores.add(100_000,"LOL")
+ self.assertFalse( hi_scores.high_enough(99_999) )
+ self.assertFalse( hi_scores.high_enough(100_000) )
+ self.assertTrue( hi_scores.high_enough(100_001) )
+
+ def test_str_can_run(self):
+ str(hi_scores)
diff --git a/tests/test_sfx.py b/tests/test_sfx.py
index 1aaf53e..35483a5 100644
--- a/tests/test_sfx.py
+++ b/tests/test_sfx.py
@@ -18,3 +18,27 @@ class TestSfx(unittest.TestCase):
def test_play(self):
sfx.play("laser")
+
+ def test_music(self):
+ sfx.music("baby")
+ sfx.music("baby")
+ sfx.music("glass")
+ sfx.music(None)
+
+ def test_sfx_volume_control(self):
+ old_vol = sfx.sfx_volume
+ sfx.sfx_up()
+ self.assertNotEqual(old_vol, sfx.sfx_volume)
+ for _ in range(10):
+ sfx.sfx_up()
+ self.assertEqual(old_vol, sfx.sfx_volume)
+
+ def test_music_volume_control(self):
+ old_vol = sfx.music_volume
+ sfx.music(None)
+ sfx.music_up()
+ sfx.music("glass")
+ self.assertNotEqual(old_vol, sfx.music_volume)
+ for _ in range(10):
+ sfx.music_up()
+ self.assertEqual(old_vol, sfx.music_volume)