Files
snake-python/tests/tests.py
T
daniel156161 3a9af3f54d feat(snake): modularize engine and add tournament tools
- Split active strategies, reusable engine code, core classes, and legacy snakes.
- Replace implicit snake imports with explicit module registrations.
- Extract Prism duel, spatial, and survival behavior into focused mixins.
- Improve duel scoring with food races, pressure, caches, and depth metrics.
- Add deterministic arena scenarios and paired seeded engine tournaments.
- Expand benchmark telemetry and bump Prism to version 1.3.0.
- Update documentation and tests for the new package layout and tooling.
2026-08-01 20:25:07 +02:00

97 lines
2.9 KiB
Python

"""
Starter Unit Tests using the built-in Python unittest library.
See https://docs.python.org/3/library/unittest.html
You can expand these to cover more cases!
To run the unit tests, use the following command in your terminal,
in the folder where this file exists:
python tests.py -v
"""
import unittest
from snakes.legacy.LogicSnake import avoid_my_neck
class AvoidNeckTest(unittest.TestCase):
def test_avoid_neck_all(self):
"""
The possible move set should be all moves.
In the starter position, a Battlesnake body is 'stacked' in a
single place, and thus all directions are valid.
"""
# Arrange
test_head = {"x": 5, "y": 5}
test_body = [{"x": 5, "y": 5}, {"x": 5, "y": 5}, {"x": 5, "y": 5}]
possible_moves = ["up", "down", "left", "right"]
# Act
result_moves = avoid_my_neck(test_head, test_body, possible_moves)
# Assert
self.assertEqual(len(result_moves), 4)
self.assertEqual(possible_moves, result_moves)
def test_avoid_neck_left(self):
# Arrange
test_head = {"x": 5, "y": 5}
test_body = [{"x": 5, "y": 5}, {"x": 4, "y": 5}, {"x": 3, "y": 5}]
possible_moves = ["up", "down", "left", "right"]
expected = ["up", "down", "right"]
# Act
result_moves = avoid_my_neck(test_head, test_body, possible_moves)
# Assert
self.assertEqual(len(result_moves), 3)
self.assertEqual(expected, result_moves)
def test_avoid_neck_right(self):
# Arrange
test_head = {"x": 5, "y": 5}
test_body = [{"x": 5, "y": 5}, {"x": 6, "y": 5}, {"x": 7, "y": 5}]
possible_moves = ["up", "down", "left", "right"]
expected = ["up", "down", "left"]
# Act
result_moves = avoid_my_neck(test_head, test_body, possible_moves)
# Assert
self.assertEqual(len(result_moves), 3)
self.assertEqual(expected, result_moves)
def test_avoid_neck_up(self):
# Arrange
test_head = {"x": 5, "y": 5}
test_body = [{"x": 5, "y": 5}, {"x": 5, "y": 6}, {"x": 5, "y": 7}]
possible_moves = ["up", "down", "left", "right"]
expected = ["down", "left", "right"]
# Act
result_moves = avoid_my_neck(test_head, test_body, possible_moves)
# Assert
self.assertEqual(len(result_moves), 3)
self.assertEqual(expected, result_moves)
def test_avoid_neck_down(self):
# Arrange
test_head = {"x": 5, "y": 5}
test_body = [{"x": 5, "y": 5}, {"x": 5, "y": 4}, {"x": 5, "y": 3}]
possible_moves = ["up", "down", "left", "right"]
expected = ["up", "left", "right"]
# Act
result_moves = avoid_my_neck(test_head, test_body, possible_moves)
# Assert
self.assertEqual(len(result_moves), 3)
self.assertEqual(expected, result_moves)
if __name__ == "__main__":
unittest.main()