Skip to content

Commit

Permalink
Add AVA test runner and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joakim committed Nov 5, 2019
1 parent f5e401a commit 553a70d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "node test/test.js",
"test": "ava",
"pretest": "yarn build",
"release": "node -r dotenv/config ./node_modules/.bin/release-it"
},
Expand All @@ -36,14 +36,23 @@
"devDependencies": {
"@rollup/plugin-buble": "^0.20.0",
"@rollup/plugin-replace": "^2.2.0",
"ava": "^2.4.0",
"dotenv": "^8.2.0",
"eslint": "^6.6.0",
"esm": "^3.2.25",
"prettier": "joakim/prettiest",
"release-it": "^12.4.3",
"rollup": "^1.26.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0"
},
"ava": {
"babel": false,
"compileEnhancements": false,
"require": [
"esm"
]
},
"release-it": {
"github": {
"release": true
Expand Down
69 changes: 69 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import test from 'ava'

import {
generate,
encode,
decode,
validArrangement,
} from '../src/main.js'

import { POSITIONS } from '../src/lookup.js'

test('validArrangement() actually validates valid arrangements', t => {
t.plan(960)

// Validate all arrangements in the lookup table
for (let id = 0; id < 960; id++) {
t.true(validArrangement(POSITIONS[id]))
}
})

test('validArrangement() actually invalidates invalid arrangements', t => {
t.plan(3)
t.false(validArrangement('NONONONO')) // Obviously wrong
t.false(validArrangement('KQRNNBBR')) // Both rooks on one side of the king
t.false(validArrangement('BNBQRNKR')) // Bishops on same colored squares
})

test('generate() returns only correct arrangements', t => {
t.plan(960)

// Check all 960 starting positions at random
let cache = new Set()
do {
let sp = generate()
if (cache.has(sp)) continue

// Check against the lookup table
t.not(POSITIONS.indexOf(sp.join('')), -1)

cache.add(sp)
}
while (cache.size < 960)
})

test('decode() returns correct arrangements', t => {
t.plan(960)

// Check against the lookup table
for (let id = 0; id < 960; id++) {
let sp = decode(id)
t.is(POSITIONS.indexOf(sp.join('')), id)
}
})

test('encode() returns correct IDs', t => {
t.plan(960)

// Check against the lookup table
for (let id = 0; id < 960; id++) {
t.is(encode(POSITIONS[id]), id)
}
})

test.todo('toString()')
test.todo('toArray()')
test.todo('toLowerCase()')
test.todo('toUpperCase()')
test.todo('toMirror()')
test.todo('toUnicode()')

0 comments on commit 553a70d

Please sign in to comment.