use common packages tests implementation (#171)

This commit is contained in:
Peli de Halleux 2018-01-04 09:23:28 -08:00 committed by GitHub
parent 9a4ed45797
commit ba0eb93b0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 98 deletions

View File

@ -1,9 +1,11 @@
{
"tests": "Unit tests framework",
"TestEvent": "Various test event in the execution cycle",
"tests": "A Unit tests framework",
"tests.assert": "Checks a boolean condition",
"tests.assertClose": "Checks that 2 values are close to each other",
"tests.assertClose|param|actual": "what the value was",
"tests.assertClose|param|expected": "what the value should be",
"tests.assertClose|param|tolerance": "the acceptable error margin",
"tests.assertClose|param|tolerance": "the acceptable error margin, eg: 5",
"tests.onEvent": "Registers code to be called at various points in the test execution",
"tests.test": "Registers a test to run"
}

View File

@ -1,4 +1,9 @@
{
"TestEvent.RunSetUp|block": "run setup",
"TestEvent.RunTearDown|block": "run teardown",
"TestEvent.TestSetUp|block": "test setup",
"TestEvent.TestTearDown|block": "test teardown",
"tests.assertClose|block": "assert %message|%expected|close to %actual|by %tolerance",
"tests.assert|block": "assert %message|%condition",
"tests.test|block": "test %name",
"tests|block": "tests",

View File

@ -0,0 +1,12 @@
// EV3 specific test functions
tests.onEvent(TestEvent.RunSetUp, function() {
console.sendToScreen();
})
tests.onEvent(TestEvent.TestSetUp, function() {
motors.stopAllMotors();
motors.resetAllMotors();
})
tests.onEvent(TestEvent.TestTearDown, function() {
motors.stopAllMotors();
motors.resetAllMotors();
})

View File

@ -3,11 +3,13 @@
"description": "A unit test library",
"files": [
"README.md",
"tests.ts"
"tests.ts",
"platformoverrides.ts"
],
"testFiles": [
],
"public": true,
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/tests",
"dependencies": {
"core": "file:../core"
}

View File

@ -1,94 +0,0 @@
/**
* Unit tests framework
*/
//% weight=100 color=#0fbc11 icon=""
namespace tests {
class Test {
name: string;
handler: () => void;
errors: string[];
constructor(name: string, handler: () => void) {
this.name = name;
this.handler = handler;
this.errors = [];
}
reset() {
motors.stopAllMotors();
motors.resetAllMotors();
}
run() {
// clear state
this.reset();
console.log(`> ${this.name}`)
this.handler()
if (this.errors.length)
console.log('')
// ensure clean state after test
this.reset();
}
}
let _tests: Test[] = undefined;
let _currentTest: Test = undefined;
function run() {
if (!_tests) return;
const start = control.millis();
console.sendToScreen();
console.log(`${_tests.length} tests found`)
console.log(` `)
for (let i = 0; i < _tests.length; ++i) {
const t = _currentTest = _tests[i];
t.run();
_currentTest = undefined;
}
console.log(` `)
console.log(`${_tests.length} tests, ${_tests.map(t => t.errors.length).reduce((p, c) => p + c, 0)} errs in ${Math.ceil((control.millis() - start) / 1000)}s`)
}
/**
* Registers a test to run
*/
//% blockId=testtest block="test %name"
export function test(name: string, handler: () => void): void {
if (!name || !handler) return;
if (!_tests) {
_tests = [];
control.runInBackground(function () {
// should run after on start
loops.pause(100)
run()
})
}
_tests.push(new Test(name, handler));
}
/**
* Checks a boolean condition
*/
//% blockId=testAssert block="assert %message|%condition"
export function assert(message: string, condition: boolean) {
if (!condition) {
console.log(`!!! ${message || ''}`)
if (_currentTest)
_currentTest.errors.push(message);
}
}
/**
* Checks that 2 values are close to each other
* @param expected what the value should be
* @param actual what the value was
* @param tolerance the acceptable error margin
*/
export function assertClose(name: string, expected: number, actual: number, tolerance: number) {
assert(`${name} ${expected} != ${actual} +-${tolerance}`, Math.abs(expected - actual) <= tolerance);
}
}

View File

@ -44,7 +44,7 @@
"webfonts-generator": "^0.4.0"
},
"dependencies": {
"pxt-common-packages": "0.14.13",
"pxt-common-packages": "0.15.1",
"pxt-core": "3.0.5"
},
"scripts": {