summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-04-10 18:04:27 +0300
committerViljami Ilola <+@hix.fi>2024-04-10 18:04:27 +0300
commit6f2eeab32b08c75399c971a52238bdcdc16085b2 (patch)
tree6972e845a3351023d56e8e7e4882262722041b9b
parent7b312aab1af3b2c67bc29df1faf46e39947b51fe (diff)
tests for highscores
-rw-r--r--pyproject.toml6
-rw-r--r--src/sliceitoff/hiscores/hiscores.py19
-rw-r--r--src/sliceitoff/hiscores/static.py2
-rw-r--r--tests/test_highscores.py39
4 files changed, 56 insertions, 10 deletions
diff --git a/pyproject.toml b/pyproject.toml
index 42ca912..16e0c2c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -31,8 +31,10 @@ build-backend = "poetry.core.masonry.api"
[tool.coverage.run]
branch = true
source = ["src/sliceitoff/"]
-omit = ["src/sliceitoff/game/*"] # Game logic and input-needing parts
-
+omit = [
+ "**/__????__.py", # __init__.py and __main__.py files
+ "src/sliceitoff/game/*" # Needs input and graphics.
+]
[tool.pylint.main]
recursive = true
diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py
index 372db31..627c3f7 100644
--- a/src/sliceitoff/hiscores/hiscores.py
+++ b/src/sliceitoff/hiscores/hiscores.py
@@ -6,16 +6,21 @@ from .static import INITIAL_HIGHSCORES, MAX_HIGHSCORES
class HiScores:
""" Keeps track of high scores """
- def __init__(self):
+ def __init__(self, filename = None):
""" On creation load high scores from config file """
self.table=[]
- if os.name == 'nt':
- self.config_filename = os.path.join(Path.home(), "sliceitoff.cfg")
+ if filename:
+ self.config_filename = filename
else:
- self.config_filename = os.path.join(
- Path.home(),
- ".config",
- "sliceitoffrc")
+ if os.name == 'nt':
+ self.config_filename = os.path.join(
+ Path.home(),
+ "sliceitoff.cfg")
+ else:
+ self.config_filename = os.path.join(
+ Path.home(),
+ ".config",
+ "sliceitoffrc")
if not os.path.isfile(self.config_filename):
self.table=INITIAL_HIGHSCORES[:]
return
diff --git a/src/sliceitoff/hiscores/static.py b/src/sliceitoff/hiscores/static.py
index cf17ba8..65a032e 100644
--- a/src/sliceitoff/hiscores/static.py
+++ b/src/sliceitoff/hiscores/static.py
@@ -19,7 +19,7 @@ INITIAL_HIGHSCORES = [
(10000, "-O-"),
(8000, "-F-"),
(6000, "-F-"),
- (4000, "-!-"),
+ (4000, "---"),
(2000, "---"),
(0, "---")
]
diff --git a/tests/test_highscores.py b/tests/test_highscores.py
new file mode 100644
index 0000000..26545f0
--- /dev/null
+++ b/tests/test_highscores.py
@@ -0,0 +1,39 @@
+import os
+import unittest
+import pygame
+
+from pathlib import Path
+
+from sliceitoff.hiscores import HiScores
+
+class TestHiScores(unittest.TestCase):
+ def setUp(self):
+ self.filename = "/tmp/sliceitoff-hiscores-test"
+ if os.path.isfile(self.filename):
+ os.remove(self.filename)
+ self.hiscores = HiScores(filename = self.filename)
+
+ def tearDown(self):
+ try:
+ del self.hiscores
+ except AttributeError:
+ pass
+ if os.path.isfile(self.filename):
+ os.remove(self.filename)
+
+ def test_can_create(self):
+ self.assertNotEqual(None, self.hiscores)
+
+ def test_config_file_is_created_on_exit(self):
+ del self.hiscores
+ self.assertTrue(os.path.isfile(self.filename))
+
+ def test_scores_can_be_added(self):
+ self.hiscores.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))