Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yo35 committed Mar 3, 2024
1 parent 5b96707 commit 4e94bd8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test/04_position_fen.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,18 @@ describe('FEN without XFEN if possible', () => {
test.value(position.fen({ regularFENIfPossible: true })).is(elem.fenOutWithoutXFEN === '' ? elem.fenOutDefault : elem.fenOutWithoutXFEN);
});
});


describe('Invalid FEN overloads', () => {

function itInvalidOverload(label, action) {
it(label, () => {
const position = new Position();
test.exception(() => action(position)).isInstanceOf(exception.IllegalArgument);
});
}

itInvalidOverload('Invalid key for getter', pos => pos.fen({ fiftyMoveClock: 'forty two' }));
itInvalidOverload('Non-string input for setter', pos => pos.fen(42));
itInvalidOverload('Non-boolean option for setter', pos => pos.fen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', 42));
});
45 changes: 45 additions & 0 deletions test/05_position_process_exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,51 @@ describe('Figurine notation', () => {
});


describe('King-take-rook flag for UCI notation generation', () => {

function itGenerateUCIWithKxR(label, variant, forceKxR, expectedUCI) {
it(label, () => {
const position = new Position(variant, 'r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1');
const moveDescriptor = position.notation('O-O');
test.value(position.uci(moveDescriptor, forceKxR)).is(expectedUCI);
});
}

itGenerateUCIWithKxR('Force KxR at regular chess', 'regular', true, 'e1h1');
itGenerateUCIWithKxR('Do not force KxR at regular chess', 'regular', false, 'e1g1');
itGenerateUCIWithKxR('Force KxR at Chess960', 'chess960', true, 'e1h1');
itGenerateUCIWithKxR('Do not force KxR at Chess960', 'chess960', false, 'e1h1');
});


describe('King-take-rook flag for UCI notation parsing', () => {

function itParseValidUCIWithKxR(label, variant, uciNotation, strict) {
it(label, () => {
const position = new Position(variant, 'r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1');
const moveDescriptor = position.uci(uciNotation, strict);
test.value(position.notation(moveDescriptor)).is('O-O');
});
}

function itParseInvalidUCIWithKxR(label, variant, uciNotation, strict) {
it(label + ' (invalid)', () => {
const position = new Position(variant, 'r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1');
test.exception(() => position.uci(uciNotation, strict)).isInstanceOf(exception.InvalidNotation);
});
}

itParseValidUCIWithKxR('Regular chess, king-move style, non-strict mode', 'regular', 'e1g1', false);
itParseValidUCIWithKxR('Regular chess, king-move style, strict mode', 'regular', 'e1g1', true);
itParseValidUCIWithKxR('Regular chess, KxR style, non-strict mode', 'regular', 'e1h1', false);
itParseInvalidUCIWithKxR('Regular chess, KxR style, strict mode', 'regular', 'e1h1', true);
itParseInvalidUCIWithKxR('Chess960, king-move style, non-strict mode', 'chess960', 'e1g1', false);
itParseInvalidUCIWithKxR('Chess960, king-move style, strict mode', 'chess960', 'e1g1', true);
itParseValidUCIWithKxR('Chess960, KxR style, non-strict mode', 'chess960', 'e1h1', false);
itParseValidUCIWithKxR('Chess960, KxR style, strict mode', 'chess960', 'e1h1', true);
});


describe('Parse invalid notation', () => {

function itInvalidNotation(label, parsingAction) {
Expand Down

0 comments on commit 4e94bd8

Please sign in to comment.