diff --git a/libs/tests/_locales/tests-jsdoc-strings.json b/libs/tests/_locales/tests-jsdoc-strings.json index 42e3dbca..26924ab6 100644 --- a/libs/tests/_locales/tests-jsdoc-strings.json +++ b/libs/tests/_locales/tests-jsdoc-strings.json @@ -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" } \ No newline at end of file diff --git a/libs/tests/_locales/tests-strings.json b/libs/tests/_locales/tests-strings.json index 1f0abcb8..62661802 100644 --- a/libs/tests/_locales/tests-strings.json +++ b/libs/tests/_locales/tests-strings.json @@ -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", diff --git a/libs/tests/platformoverrides.ts b/libs/tests/platformoverrides.ts new file mode 100644 index 00000000..1fc53915 --- /dev/null +++ b/libs/tests/platformoverrides.ts @@ -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(); +}) diff --git a/libs/tests/pxt.json b/libs/tests/pxt.json index f6b36b5e..b58c790b 100644 --- a/libs/tests/pxt.json +++ b/libs/tests/pxt.json @@ -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" } diff --git a/libs/tests/tests.ts b/libs/tests/tests.ts deleted file mode 100644 index 971cb88a..00000000 --- a/libs/tests/tests.ts +++ /dev/null @@ -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); - } -} diff --git a/package.json b/package.json index 545534fa..244ca603 100644 --- a/package.json +++ b/package.json @@ -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": {