|
| 1 | +package com.backend.util; |
| 2 | + |
| 3 | +import com.backend.domain.ChessGame; |
| 4 | +import com.backend.models.GameState; |
| 5 | +import com.backend.models.Position; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests for PGN export functionality. |
| 12 | + */ |
| 13 | +public class PGNExporterTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void testBasicPGNExport() { |
| 17 | + ChessGame game = new ChessGame(); |
| 18 | + |
| 19 | + // Make a few moves (opening moves) |
| 20 | + game.MoveController(new Position(2, 5), new Position(3, 5)); // e2-e3 (white) |
| 21 | + game.MoveController(new Position(7, 5), new Position(6, 5)); // e7-e6 (black) |
| 22 | + game.MoveController(new Position(2, 4), new Position(4, 4)); // d2-d4 (white) |
| 23 | + game.MoveController(new Position(7, 4), new Position(5, 4)); // d7-d5 (black) |
| 24 | + |
| 25 | + String pgn = game.exportToPGN(); |
| 26 | + |
| 27 | + assertNotNull(pgn, "PGN should not be null"); |
| 28 | + assertTrue(pgn.contains("[Event"), "PGN should contain Event header"); |
| 29 | + assertTrue(pgn.contains("[Result"), "PGN should contain Result header"); |
| 30 | + assertTrue(pgn.contains("1."), "PGN should contain move numbers"); |
| 31 | + |
| 32 | + // Game should still be ongoing |
| 33 | + assertTrue(pgn.contains("*"), "PGN should show game ongoing with *"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testResultStringForCheckmate() { |
| 38 | + String result = PGNExporter.getResultString(GameState.Checkmate, com.backend.models.Color.White); |
| 39 | + assertEquals("0-1", result, "White turn at checkmate means black won"); |
| 40 | + |
| 41 | + result = PGNExporter.getResultString(GameState.Checkmate, com.backend.models.Color.Black); |
| 42 | + assertEquals("1-0", result, "Black turn at checkmate means white won"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testResultStringForDraw() { |
| 47 | + String result = PGNExporter.getResultString(GameState.DrawByStalemate, com.backend.models.Color.White); |
| 48 | + assertEquals("1/2-1/2", result, "Stalemate should be a draw"); |
| 49 | + |
| 50 | + result = PGNExporter.getResultString(GameState.DrawByRepetition, com.backend.models.Color.Black); |
| 51 | + assertEquals("1/2-1/2", result, "Threefold repetition should be a draw"); |
| 52 | + |
| 53 | + result = PGNExporter.getResultString(GameState.DrawByFiftyMove, com.backend.models.Color.White); |
| 54 | + assertEquals("1/2-1/2", result, "Fifty-move rule should be a draw"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testResultStringForOngoingGame() { |
| 59 | + String result = PGNExporter.getResultString(GameState.Free, com.backend.models.Color.White); |
| 60 | + assertEquals("*", result, "Ongoing game should show *"); |
| 61 | + |
| 62 | + result = PGNExporter.getResultString(GameState.Check, com.backend.models.Color.Black); |
| 63 | + assertEquals("*", result, "Check state should still show game ongoing"); |
| 64 | + } |
| 65 | +} |
0 commit comments