From df8253dbf6cb09093018333f99dd9c19ba0ff02b Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Sat, 28 Nov 2020 21:24:40 -0500 Subject: Initial commit `Tokenizer` can _just barely_ parse a basic, well-formed move list. Initially, I wanted to provide the movetext as a `Stream` rather than a string, the idea being that it could be processed as it was being read from a file without having to read the entire file into memory first. I had difficulties with the stream being unreadable in `Tokenizer.ParseMoves()`, so I switched to a string in order to get the actual parsing logic down first. Because of the `yield return` strategy, the debug console output includes all of the expected halfmoves multiple times in various orders. After running a test, generally the full, in-order list seems to exist at the bottom of the output. --- DotnetPgn.Test/DotnetPgn.Test.csproj | 26 ++++++++++++ DotnetPgn.Test/PieceParserTest.cs | 17 ++++++++ DotnetPgn.Test/TokenizerTest.cs | 82 ++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 DotnetPgn.Test/DotnetPgn.Test.csproj create mode 100644 DotnetPgn.Test/PieceParserTest.cs create mode 100644 DotnetPgn.Test/TokenizerTest.cs (limited to 'DotnetPgn.Test') diff --git a/DotnetPgn.Test/DotnetPgn.Test.csproj b/DotnetPgn.Test/DotnetPgn.Test.csproj new file mode 100644 index 0000000..0e90980 --- /dev/null +++ b/DotnetPgn.Test/DotnetPgn.Test.csproj @@ -0,0 +1,26 @@ + + + + net5.0 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/DotnetPgn.Test/PieceParserTest.cs b/DotnetPgn.Test/PieceParserTest.cs new file mode 100644 index 0000000..e8aacf6 --- /dev/null +++ b/DotnetPgn.Test/PieceParserTest.cs @@ -0,0 +1,17 @@ +using System.Linq; +using DotnetPgn.Models; +using Xunit; + +namespace DotnetPgn.Test +{ + public class PieceParserTest + { + [Theory] + [InlineData("K", Piece.King)] + [InlineData("Q", Piece.Queen)] + public void ParsePieceTest(string input, Piece expectedOutput) + { + Assert.Equal(expectedOutput, PieceParser.ParsePiece(input)); + } + } +} diff --git a/DotnetPgn.Test/TokenizerTest.cs b/DotnetPgn.Test/TokenizerTest.cs new file mode 100644 index 0000000..cb9940b --- /dev/null +++ b/DotnetPgn.Test/TokenizerTest.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using DotnetPgn.Models; +using Xunit; + +namespace DotnetPgn.Test +{ + public class TokenizerTest + { + [Fact] + public void ParseMovesTest() + { + var moveText = "1. e4 e5 2. Nf3 Qe7 3. d3 h5\n"; + IEnumerable halfmoves; + + #region Expected + List expectedHalfMoves = new() + { + new HalfMove + { + MoveNumber = 1, + Player = Player.White, + Piece = Piece.Pawn, + TargetSquare = new Square('e', 4), + }, + new HalfMove + { + MoveNumber = 1, + Player = Player.Black, + Piece = Piece.Pawn, + TargetSquare = new Square('e', 5), + }, + new HalfMove + { + MoveNumber = 2, + Player = Player.White, + Piece = Piece.Knight, + TargetSquare = new Square('f', 3), + }, + new HalfMove + { + MoveNumber = 2, + Player = Player.Black, + Piece = Piece.Queen, + TargetSquare = new Square('e', 7), + }, + new HalfMove + { + MoveNumber = 3, + Player = Player.White, + Piece = Piece.Pawn, + TargetSquare = new Square('d', 3), + }, + new HalfMove + { + MoveNumber = 3, + Player = Player.Black, + Piece = Piece.Pawn, + TargetSquare = new Square('h', 5), + }, + }; + #endregion + + halfmoves = Tokenizer.ParseMoves(moveText); + + for (int i = 0; i < expectedHalfMoves.Count(); i++) + { + HalfMove generated = halfmoves.ElementAtOrDefault(i); + HalfMove expected = expectedHalfMoves.ElementAtOrDefault(i); + + Assert.NotNull(generated); + Assert.NotNull(expected); + Assert.Equal(expected.MoveNumber, generated.MoveNumber); + Assert.Equal(expected.Player, generated.Player); + Assert.Equal(expected.Piece, generated.Piece); + Assert.Equal(expected.TargetSquare, generated.TargetSquare); + } + } + } +} -- cgit v1.2.3