test: add tests for simple pawn moves

This commit is contained in:
Cozma Rares
2023-04-16 22:23:17 +03:00
parent 9c296d3429
commit addd122887
6 changed files with 438 additions and 255 deletions
+241 -236
View File
File diff suppressed because it is too large Load Diff
+8 -8
View File
@@ -1,36 +1,36 @@
import { describe, expect, test } from "vitest";
import { expect, test } from "vitest";
import Chess, { DEFAULT_POSITION } from "../chess/engine";
test("starting position", () => {
expect(Chess.load(DEFAULT_POSITION).getFEN()).toBe(DEFAULT_POSITION);
expect(Chess.load(DEFAULT_POSITION).getFEN()).toEqual(DEFAULT_POSITION);
});
test("1. e4", () => {
const fen = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1";
expect(Chess.load(fen).getFEN()).toBe(fen);
expect(Chess.load(fen).getFEN()).toEqual(fen);
});
test("1. e4 2. e5", () => {
const fen = "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2";
expect(Chess.load(fen).getFEN()).toBe(fen);
expect(Chess.load(fen).getFEN()).toEqual(fen);
});
test("1. e4 2. e5 3. ke2", () => {
const fen = "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2";
expect(Chess.load(fen).getFEN()).toBe(fen);
expect(Chess.load(fen).getFEN()).toEqual(fen);
});
test("random position 1", () => {
const fen = "r1b1r1k1/pp4pp/3Bpp2/8/2q5/P5Q1/3R1PPP/R5K1 b - - 0 19";
expect(Chess.load(fen).getFEN()).toBe(fen);
expect(Chess.load(fen).getFEN()).toEqual(fen);
});
test("random position 2", () => {
const fen = "1k6/1pp5/p7/5B1p/PP6/6K1/4p2r/4R3 b - - 3 43";
expect(Chess.load(fen).getFEN()).toBe(fen);
expect(Chess.load(fen).getFEN()).toEqual(fen);
});
test("random position 3", () => {
const fen = "8/8/2k2Q2/8/5P2/8/1p6/6K1 b - - 1 48";
expect(Chess.load(fen).getFEN()).toBe(fen);
expect(Chess.load(fen).getFEN()).toEqual(fen);
});
@@ -0,0 +1,178 @@
import { describe, expect, test } from "vitest";
import Chess, { MOVE_FLAGS, Move, Square } from "../chess/engine";
type ExpectedMove = {
square: Square;
moves: Move[];
};
describe("simple moves", () => {
describe("pawn moves", () => {
const chess = Chess.load("7k/1P5p/5p2/4p3/3P4/2P5/P5p1/K7 w - - 0 1");
const expectedMoves: Record<string, ExpectedMove[]> = {
starting: [
{
square: "a2",
moves: [
{
from: "a2",
to: "a3",
flag: MOVE_FLAGS.NORMAL,
},
{
from: "a2",
to: "a4",
flag: MOVE_FLAGS.PAWN_JUMP,
},
],
},
{
square: "h7",
moves: [
{
from: "h7",
to: "h6",
flag: MOVE_FLAGS.NORMAL,
},
{
from: "h7",
to: "h5",
flag: MOVE_FLAGS.PAWN_JUMP,
},
],
},
],
promotion: [
{
square: "b7",
moves: [
{
from: "b7",
to: "b8",
flag: MOVE_FLAGS.PROMOTION,
promotion: "n",
},
{
from: "b7",
to: "b8",
flag: MOVE_FLAGS.PROMOTION,
promotion: "b",
},
{
from: "b7",
to: "b8",
flag: MOVE_FLAGS.PROMOTION,
promotion: "r",
},
{
from: "b7",
to: "b8",
flag: MOVE_FLAGS.PROMOTION,
promotion: "q",
},
],
},
{
square: "g2",
moves: [
{
from: "g2",
to: "g1",
flag: MOVE_FLAGS.PROMOTION,
promotion: "n",
},
{
from: "g2",
to: "g1",
flag: MOVE_FLAGS.PROMOTION,
promotion: "b",
},
{
from: "g2",
to: "g1",
flag: MOVE_FLAGS.PROMOTION,
promotion: "r",
},
{
from: "g2",
to: "g1",
flag: MOVE_FLAGS.PROMOTION,
promotion: "q",
},
],
},
],
regular: [
{
square: "c3",
moves: [
{
from: "c3",
to: "c4",
flag: MOVE_FLAGS.NORMAL,
},
],
},
{
square: "f6",
moves: [
{
from: "f6",
to: "f5",
flag: MOVE_FLAGS.NORMAL,
},
],
},
],
attack: [
{
square: "d4",
moves: [
{
from: "d4",
to: "d5",
flag: MOVE_FLAGS.NORMAL,
},
{
from: "d4",
to: "e5",
flag: MOVE_FLAGS.CAPTURE,
},
],
},
{
square: "e5",
moves: [
{
from: "e5",
to: "e4",
flag: MOVE_FLAGS.NORMAL,
},
{
from: "e5",
to: "d4",
flag: MOVE_FLAGS.CAPTURE,
},
],
},
],
};
Object.keys(expectedMoves).forEach(key =>
test(key, () =>
expectedMoves[key].forEach(({ square, moves: expectedMoves }) => {
const moves = chess.getMovesForSquare(square);
// console.log({ square, expectedMoves, moves });
// console.log();
expect(moves.length).toEqual(expectedMoves.length);
moves.forEach(move => expect(expectedMoves).toContain(move));
expectedMoves.forEach(move => expect(moves).toContain(move));
})
)
);
});
});
+9 -9
View File
@@ -3,13 +3,13 @@ import { DEFAULT_POSITION, validateFEN } from "../chess/engine";
describe("valid FEN strings", () => {
test("starting position", () => {
expect(validateFEN(DEFAULT_POSITION)).toBe(undefined);
expect(validateFEN(DEFAULT_POSITION)).toBeUndefined();
});
test("1. e4", () => {
expect(
validateFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1")
).toBe(undefined);
).toBeUndefined();
});
test("1. e4 2. e5", () => {
@@ -17,29 +17,29 @@ describe("valid FEN strings", () => {
validateFEN(
"rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2"
)
).toBe(undefined);
).toBeUndefined();
});
test("1. e4 2. e5 3. ke2", () => {
expect(
validateFEN("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2")
).toBe(undefined);
).toBeUndefined();
});
test("random position 1", () => {
expect(
validateFEN("r1b1r1k1/pp4pp/3Bpp2/8/2q5/P5Q1/3R1PPP/R5K1 b - - 0 19")
).toBe(undefined);
).toBeUndefined();
});
test("random position 2", () => {
expect(validateFEN("1k6/1pp5/p7/5B1p/PP6/6K1/4p2r/4R3 b - - 3 43")).toBe(
undefined
);
expect(
validateFEN("1k6/1pp5/p7/5B1p/PP6/6K1/4p2r/4R3 b - - 3 43")
).toBeUndefined();
});
test("random position 3", () => {
expect(validateFEN("8/8/2k2Q2/8/5P2/8/1p6/6K1 b - - 1 48")).toBe(undefined);
expect(validateFEN("8/8/2k2Q2/8/5P2/8/1p6/6K1 b - - 1 48")).toBeUndefined();
});
});
+1 -1
View File
@@ -388,7 +388,7 @@ function generatePawnMoves(
return moves;
}
function generatePieceMoves(
export function generatePieceMoves(
board: Readonly<Board>,
position: number,
piece: Piece
+1 -1
View File
@@ -47,7 +47,7 @@
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */