earley-sgf
Version:
Early algorithm used to parse SGF file
46 lines (45 loc) • 2 kB
JavaScript
import { assert, test } from 'vitest';
import { sgfGame, sgfHead } from './sgfGame.js';
test('Read small SGF file', () => {
const sgfFile = '(;DT[2024-04-21]PB[or2win]PW[Henry_Yuan]SZ[19]KM[6.5];B[pd];W[dq])';
const tree = sgfGame(sgfFile);
assert(3 === tree.nodes.length);
assert(5 === tree.nodes[0].props.length);
assert('player' in tree.nodes[0].props[1]);
assert('B' === tree.nodes[0].props[1].player);
assert('or2win' === tree.nodes[0].props[1].name);
assert('move' in tree.nodes[1].props[0]);
assert('pd' === tree.nodes[1].props[0].position);
assert('move' in tree.nodes[2].props[0]);
assert('W' === tree.nodes[2].props[0].move);
});
test('Read head of SGF file', () => {
const sgfFile = '(;DT[2024-04-21]PB[or2win]PW[Henry_Yuan]SZ[19]KM[6.5];B[pd];W[dq])';
const h = sgfHead(sgfFile);
assert(h.date === '2024-04-21', "date failed " + h.date);
assert(h.whiteName === 'Henry_Yuan', "no white player");
assert(h.blackName === 'or2win', "no black player");
});
test('Names with blanks', () => {
const sgfFile = '(;DT[2024-04-21]PB[or2win]PW[Henry Yuan]SZ[19]KM[6.5];B[pd];W[dq])';
const h = sgfHead(sgfFile);
assert(h.whiteName === 'Henry Yuan', "no white player");
});
test('Branch', () => {
const sgfFile = '(;;B[pd](;W[dq])(;W[eq]))';
const tree = sgfGame(sgfFile);
assert(2 === tree.nodes.length);
assert(2 === tree.children.length);
assert(1 === tree.children[1].nodes.length);
assert('move' in tree.children[1].nodes[0].props[0]);
assert('eq' === tree.children[1].nodes[0].props[0].position);
});
test('Deep', () => {
const sgfFile = '(;;B[pd](;W[dq](;W[eq])))';
const tree = sgfGame(sgfFile);
assert(2 === tree.nodes.length);
assert(1 === tree.children.length);
assert(1 === tree.children[0].children.length);
assert('move' in tree.children[0].children[0].nodes[0].props[0]);
assert('eq' === tree.children[0].children[0].nodes[0].props[0].position);
});