Basic mechanical unit tests for the separating axis algorithm.

This commit is contained in:
Elf M. Sternberg 2016-12-18 16:35:47 -08:00
parent 9e740bb702
commit 83a7fe7610
4 changed files with 67 additions and 3 deletions

View File

@ -2,6 +2,7 @@
HAML=/usr/bin/haml
COFFEE = coffee
TSC=tsc
LESSCSS=lessc
SED=sed
COMPILER=uglifyjs
@ -17,8 +18,14 @@ js/wordlist.js: src/wordlist.coffee
$(COFFEE) --compile --no-header --bare --output js/ $<
$(SED) -i -e '$$ s/;$$//' $@
js/sat.js: src/sat.coffee
$(COFFEE) --compile --output js/ $<
js/sat.js: src/sat.ts
$(TSC) $< --outDir js/ --module "commonjs"
js/test_sat.js: src/test_sat.ts
$(TSC) $< --outDir js/ --module "commonjs"
test: js/test_sat.js js/sat.js
node_modules/nodeunit/bin/nodeunit js/test_sat.js
style.css: src/style.less
$(LESSCSS) $< $@

View File

@ -113,7 +113,10 @@ var colliding = (shape1, shape2) => {
return true;
}
module.exports = {
Vector: Vector,
colliding: colliding
}

40
src/test_sat.ts Normal file
View File

@ -0,0 +1,40 @@
var testCase = require('nodeunit').testCase;
var Vector = require('./sat').Vector;
module.exports = testCase({
"TestAddition": (test) => {
var m = (new Vector(1, 1)).add(new Vector(-1, -1));
test.ok(m.x == 0 && m.y == 0);
var n = (new Vector(1, 1)).add(new Vector(1, 1));
test.ok(n.x == 2 && n.y == 2);
test.done();
},
"TestScalar": (test) => {
var m = (new Vector(2, 2)).scalar(2);
test.ok(m.x == 4 && m.y == 4);
test.done();
},
"TestMag2": (test) => {
var m = (new Vector(2, 2)).magnitude2();
test.ok(m == 8);
test.done();
},
"TestMag": (test) => {
var m = (new Vector(2, 2)).magnitude();
test.ok(m == Math.sqrt(8));
test.done();
},
"TestNormalize": (test) => {
var m = (new Vector(5, 0)).normalize();
test.ok(m.x == 1 && m.y == 0);
m = (new Vector(0, 5)).normalize();
test.ok(m.x == 0 && m.y == 1);
m = (new Vector(4, 3)).normalize().magnitude2();
test.ok(m == 1);
test.done();
}
});

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
// types option has been previously configured
"types": [
// add node as an option
"node"
],
// typeroots option has been previously configured
"typeroots": [
// add path to @types
"../node_modules/@types"
]
}
}