continued unittesting
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
71
tests/test_minimax.py
Normal file
71
tests/test_minimax.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import unittest
|
||||
from minimax import Minimax
|
||||
|
||||
class TestMinimax(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.minimax = Minimax() # Create an instance of your Minimax class
|
||||
|
||||
def test_minimax(self):
|
||||
current_field = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", "X", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", "O", "|", " "]] # Set the current field state
|
||||
player1_char = 'X'
|
||||
player2_char = 'O'
|
||||
|
||||
score, x, y = self.minimax.minimax(current_field, True, "X", "O") # Call the minimax method
|
||||
expected_score = 1 # Adjust the expected score based on the game rules and the field state
|
||||
expected_x = 4 # Adjust the expected x-coordinate of the best move
|
||||
expected_y = 4 # Adjust the expected y-coordinate of the best move
|
||||
|
||||
self.assertEqual(score, expected_score) # Check if the score matches the expected value
|
||||
self.assertEqual(x, expected_x) # Check if the x-coordinate of the best move matches the expected value
|
||||
self.assertEqual(y, expected_y) # Check if the y-coordinate of the best move matches the expected value
|
||||
|
||||
def test_check_win(self) -> None:
|
||||
player1_char = 'X'
|
||||
player2_char = 'O'
|
||||
|
||||
# Testing player 1 win condition
|
||||
field = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["X", "|", " ", "|", " "]]
|
||||
|
||||
result = self.minimax.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (1, 0))
|
||||
|
||||
# Testing player 2 win condition
|
||||
field = [["O", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", " ", "|", " "]]
|
||||
|
||||
result = self.minimax.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (0, 1))
|
||||
|
||||
# Testing draw condition
|
||||
field = [["X", "|", "O", "|", "X"],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["X", "|", "O", "|", "O"],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", "X", "|", "X"]]
|
||||
|
||||
result = self.minimax.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (1, 1))
|
||||
|
||||
# Testing no win condition
|
||||
field = [[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]]
|
||||
|
||||
result = self.minimax.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (0, 0))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
97
tests/test_model.py
Normal file
97
tests/test_model.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import unittest
|
||||
from model import Model
|
||||
import os.path
|
||||
|
||||
|
||||
class TestModel(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
# self.model = model.Model()
|
||||
self.field = [[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]]
|
||||
self.field2 = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]]
|
||||
self.model = Model() # Create an instance of your Model class
|
||||
|
||||
def tearDown(self):
|
||||
if os.path.isfile('./savestate.py'):
|
||||
os.remove("savestate.py") # Remove the savestate file if it exists
|
||||
|
||||
def test_loadsavestate(self):
|
||||
f = open("savestate.py", "w") # Create a savestate file
|
||||
f.write('save = [["X", "|", " ", "|", " "],'
|
||||
'["—", "+", "—", "+", "—"],'
|
||||
'[" ", "|", " ", "|", " "],'
|
||||
'["—", "+", "—", "+", "—"],'
|
||||
'[" ", "|", " ", "|", " "]]')
|
||||
f.close()
|
||||
|
||||
self.model.loadsavestate() # Call the loadsavestate method
|
||||
expected_field = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]]
|
||||
self.assertEqual(self.model.field, expected_field) # Check if the field matches the expected state
|
||||
|
||||
def test_checksavestate(self):
|
||||
self.assertFalse(self.model.checksavestate()) # Check if savestate does not exist
|
||||
|
||||
f = open("savestate.py", "w") # Create a savestate file
|
||||
f.write('save = [["X", "|", " ", "|", " "],'
|
||||
'["—", "+", "—", "+", "—"],'
|
||||
'[" ", "|", " ", "|", " "],'
|
||||
'["—", "+", "—", "+", "—"],'
|
||||
'[" ", "|", " ", "|", " "]]')
|
||||
f.close()
|
||||
|
||||
self.assertTrue(self.model.checksavestate()) # Check if savestate exists
|
||||
|
||||
def test_savetosavestate(self):
|
||||
self.model.field = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]] # Set the field to a specific state
|
||||
|
||||
self.model.savetosavestate() # Call the savetosavestate method
|
||||
|
||||
f = open("savestate.py", "r") # Read the content of the savestate file
|
||||
content = f.read()
|
||||
f.close()
|
||||
|
||||
expected_content = "save = [['X', '|', ' ', '|', ' ']," \
|
||||
" ['—', '+', '—', '+', '—'], [' ', '|', ' ', '|', ' ']," \
|
||||
" ['—', '+', '—', '+', '—'], [' ', '|', ' ', '|', ' ']]"
|
||||
self.assertEqual(content, expected_content) # Check if the content matches the expected value
|
||||
|
||||
def test_deletesavestate(self):
|
||||
f = open("savestate.py", "w") # Create a savestate file
|
||||
f.close()
|
||||
|
||||
self.model.deletesavestate() # Call the deletesavestate method
|
||||
|
||||
self.assertFalse(os.path.isfile('./savestate.py')) # Check if the savestate file does not exist
|
||||
|
||||
def test_check_move(self):
|
||||
model = Model() # Create an instance of your Model class
|
||||
self.assertEqual(model.check_move(0, 0, True), True)
|
||||
|
||||
def test_do_move(self):
|
||||
model = Model() # Create an instance of your Model class
|
||||
model.do_move(0, 0, True, "X", "O") # Call the do_move method
|
||||
expected_field = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]]
|
||||
self.assertEqual(model.field, expected_field)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
102
tests/test_tictactoe.py
Normal file
102
tests/test_tictactoe.py
Normal file
@@ -0,0 +1,102 @@
|
||||
from unittest.mock import Mock
|
||||
import unittest
|
||||
import tictactoe
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class MockPrinter:
|
||||
# Define mock methods here that mimic the behavior of the Printer class...
|
||||
def printfield(self, ifield):
|
||||
pass # Mock methods do nothing
|
||||
|
||||
def occupied(self):
|
||||
pass
|
||||
|
||||
def aicalc(self):
|
||||
pass
|
||||
|
||||
def aimoved(self, best_moev):
|
||||
pass
|
||||
|
||||
def playermove(self, player1, player1_char, player2_char):
|
||||
next_move = (0, 1)
|
||||
return next_move
|
||||
|
||||
class TestTicTacToe(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.game = tictactoe.Tictactoe()
|
||||
self.game.printer = MockPrinter()
|
||||
'''
|
||||
@patch('builtins.input', side_effect=[0, 1]) # Mock user inputs 0 for x and 1 for y
|
||||
def test_turn(self, mock_input):
|
||||
# Initialize the field with empty spots
|
||||
self.game.ifield = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " mock_input"]]
|
||||
|
||||
|
||||
|
||||
player1 = True
|
||||
player1_char = "X"
|
||||
player2_char = "O"
|
||||
calling_1 = mock_input()
|
||||
expected_result = (0, 1)
|
||||
#result = self.game.turn(player1, player1_char, player2_char)
|
||||
result = self.game.turn()
|
||||
|
||||
self.assertEqual(result, expected_result)'''
|
||||
|
||||
def test_check_win(self) -> None:
|
||||
player1_char = 'X'
|
||||
player2_char = 'O'
|
||||
|
||||
# Testing player 1 win condition
|
||||
field = [["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["X", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["X", "|", " ", "|", " "]]
|
||||
|
||||
result = self.game.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (1, 0))
|
||||
|
||||
# Testing player 2 win condition
|
||||
field = [["O", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", " ", "|", " "]]
|
||||
|
||||
result = self.game.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (0, 1))
|
||||
|
||||
# Testing draw condition
|
||||
field = [["X", "|", "O", "|", "X"],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["X", "|", "O", "|", "O"],
|
||||
["—", "+", "—", "+", "—"],
|
||||
["O", "|", "X", "|", "X"]]
|
||||
|
||||
result = self.game.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (1, 1))
|
||||
|
||||
# Testing no win condition
|
||||
field = [[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "],
|
||||
["—", "+", "—", "+", "—"],
|
||||
[" ", "|", " ", "|", " "]]
|
||||
|
||||
result = self.game.check_win(field, player1_char, player2_char)
|
||||
self.assertEqual(result, (0, 0))
|
||||
|
||||
|
||||
def test_play_game(self):
|
||||
expected_result = True
|
||||
# Call the method you want to test
|
||||
result = self.tictactoe.play_game()
|
||||
# Add assertions to check the result
|
||||
self.assertEqual(result, expected_result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user