Files
functional-programming-snake/js/test/m05-game.test.js
T

27 lines
1.0 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { update, replay, history } from '../src/game.js'
import { loadFixture, freeze } from './helpers.js'
const fixtureTests = (name, f) => {
const fx = loadFixture(name)
fx.cases.forEach((c) =>
test(`m05 ${fx.function}: ${c.name}`, () => assert.deepStrictEqual(f(...freeze(c.args)), c.expected)))
}
fixtureTests('m05-update', update)
fixtureTests('m05-replay', replay)
fixtureTests('m05-history', history)
test('m05 history has one more state than there are actions', () => {
const [state, actions] = loadFixture('m05-replay').cases[0].args
assert.equal(history(freeze(state), freeze(actions)).length, actions.length + 1)
})
test('m05 history starts at the start state and ends where replay ends', () => {
const [state, actions] = freeze(loadFixture('m05-replay').cases[0].args)
const states = history(state, actions)
assert.deepStrictEqual(states[0], state)
assert.deepStrictEqual(states[states.length - 1], replay(state, actions))
})