From db9177bb17b508ef4a6fa1d0707d3297fbd66c56 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 2 Nov 2016 21:13:56 +0100 Subject: TAP-based test harness --- gulpfile.js | 2 +- lib/wallet/cryptoApi.ts | 2 +- lib/wallet/cryptoLib.ts | 2 +- lib/wallet/emscriptif.ts | 2 +- scripts/prove | 3 ++ test/integration/tests.py | 21 ----------- test/run_tests.js | 56 ------------------------------ test/tests/taler.ts | 13 ------- testlib/talertest.ts | 88 +++++++++++++++++++++++++++++++++++++++++++++++ testlib/testruntime.js | 70 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 +- 11 files changed, 167 insertions(+), 95 deletions(-) create mode 100755 scripts/prove delete mode 100755 test/integration/tests.py delete mode 100644 test/run_tests.js delete mode 100644 test/tests/taler.ts create mode 100644 testlib/talertest.ts create mode 100644 testlib/testruntime.js diff --git a/gulpfile.js b/gulpfile.js index 7b17b6eed..b11debdc8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -62,7 +62,7 @@ const paths = { "lib/refs.d.ts", ], dev: [ - "test/tests/*.{ts,tsx}", + "testlib/**/.ts", ], }, dist: [ diff --git a/lib/wallet/cryptoApi.ts b/lib/wallet/cryptoApi.ts index 40be13732..4a507074a 100644 --- a/lib/wallet/cryptoApi.ts +++ b/lib/wallet/cryptoApi.ts @@ -257,4 +257,4 @@ export class CryptoApi { newCoinDenoms, meltFee); } -} \ No newline at end of file +} diff --git a/lib/wallet/cryptoLib.ts b/lib/wallet/cryptoLib.ts index 498e1cc53..d7d1905f9 100644 --- a/lib/wallet/cryptoLib.ts +++ b/lib/wallet/cryptoLib.ts @@ -351,4 +351,4 @@ namespace RpcFunctions { const b = native.ByteArray.fromStringWithNull(str); return b.hash().toCrock(); } -} \ No newline at end of file +} diff --git a/lib/wallet/emscriptif.ts b/lib/wallet/emscriptif.ts index 0be1c1c37..aad5a12a2 100644 --- a/lib/wallet/emscriptif.ts +++ b/lib/wallet/emscriptif.ts @@ -1281,4 +1281,4 @@ export function setupFreshCoin(secretSeed: TransferSecretP, return {priv, blindingKey}; -} \ No newline at end of file +} diff --git a/scripts/prove b/scripts/prove new file mode 100755 index 000000000..de9c8931a --- /dev/null +++ b/scripts/prove @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +exec prove -e "node ./testlib/testruntime.js" "$@" diff --git a/test/integration/tests.py b/test/integration/tests.py deleted file mode 100755 index 123f100ff..000000000 --- a/test/integration/tests.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/python3 - -import unittest -from selenium import webdriver -from selenium.webdriver.common.keys import Keys - -class PythonOrgSearch(unittest.TestCase): - - def setUp(self): - self.driver = webdriver.Chrome() - - def test_taler_reachable(self): - driver = self.driver - driver.get("https://bank.demo.taler.net") - - - def tearDown(self): - self.driver.close() - -if __name__ == "__main__": - unittest.main() diff --git a/test/run_tests.js b/test/run_tests.js deleted file mode 100644 index 0c7b2c4c1..000000000 --- a/test/run_tests.js +++ /dev/null @@ -1,56 +0,0 @@ - -/** - * Bridge between the mocha test runner / nodejs - * and the typescript / the wallet's module system. - * - * The test cases use better-assert as assert library - * with mocha's bdd UI. - */ - -"use strict"; - -let assert = require("better-assert"); -let vm = require("vm"); -let fs = require("fs"); -let instrument = require("typhonjs-istanbul-instrument-jspm").default; - -if ("function" !== typeof run) { - throw Error("test must be run with 'mocha --delay ...'"); -} - -let emsc = require("../lib/emscripten/libwrapper.js"); - -// Do it here, since it breaks 'require'' for libwrapper -let System = require("systemjs"); - - -// When instrumenting code with istanbul, -// automatic module type detection fails, -// thus we specify it here manually. -System.config({ - defaultJSExtensions: true, - meta: { - './test/tests/taler.js': { - format: 'register' - }, - './lib/wallet/*': { - format: 'register' - } - } -}); - -instrument(System); - -let mod = System.newModule({Module: emsc}); -let modName = System.normalizeSync(__dirname + "/../lib/emscripten/emsc"); -console.log("registering", modName); -System.set(modName, mod); - -System.import("./test/tests/taler.js") - .then((t) => { - t.declareTests(assert, context, it); - setTimeout(run, 1); - }) - .catch((e) => { - console.error("failed to load module", e.stack); - }); diff --git a/test/tests/taler.ts b/test/tests/taler.ts deleted file mode 100644 index 0ffb37329..000000000 --- a/test/tests/taler.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as Emsc from '../../lib/wallet/emscriptif'; - - -declare var HttpMockLib: any; - -export function declareTests(assert: any, context: any, it: any) { - - it("calls native emscripten code", function() { - let x = new Emsc.Amount({value: 42, fraction: 42, currency: "EUR"}); - let j = x.toJson(); - assert("value" in j); - }); -} diff --git a/testlib/talertest.ts b/testlib/talertest.ts new file mode 100644 index 000000000..5dcaff457 --- /dev/null +++ b/testlib/talertest.ts @@ -0,0 +1,88 @@ +/* + This file is part of TALER + (C) 2016 Inria + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +/** + * + * @author Florian Dold + */ + +type TestFn = (t: TestLib) => void; + +interface Test { + name: string; + testFn: TestFn; +} + +export interface TestLib { + pass(msg?: string): void; + fail(msg?: string): void; + assert(v: any, msg?: string): void; +} + +let tests: Test[] = []; +let testRunner: any; + + +/** + * Register a test case. + */ +export function test(name: string, testFn: TestFn) { + tests.push({name, testFn}); +} + + +/** + * Run all registered test case, producing a TAP stream. + */ +export async function run() { + console.log(`1..${tests.length}`); + for (let i in tests) { + let t = tests[i]; + let lastMsg: string|undefined = undefined; + let p = new Promise((resolve, reject) => { + let pass = (msg?: string) => { + lastMsg = msg; + resolve(); + }; + let fail = (msg?: string) => { + lastMsg = msg; + reject(); + throw Error("test failed"); + }; + let assert = (v: any, msg?: string) => { + if (!v) { + lastMsg = msg; + reject(); + throw Error("test failed"); + } + }; + t.testFn({pass,fail, assert}); + }); + + console.log(`# ${t.name}`); + + if (!lastMsg) { + lastMsg = "-"; + } + + try { + await p; + console.log(`ok ${Number(i) + 1} ${lastMsg}`); + } catch (e) { + console.log(`not ok ${Number(i) + 1} ${lastMsg}`); + } + } +} diff --git a/testlib/testruntime.js b/testlib/testruntime.js new file mode 100644 index 000000000..236807265 --- /dev/null +++ b/testlib/testruntime.js @@ -0,0 +1,70 @@ +/* + This file is part of TALER + (C) 2016 Inria + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +/** + * + * @author Florian Dold + */ + + +"use strict"; + +let vm = require("vm"); +let fs = require("fs"); +let process = require("process"); + +let emsc = require("../lib/emscripten/taler-emscripten-lib.js"); + +// Do it here, since it breaks 'require'' for libwrapper +let System = require("systemjs"); + + +// When instrumenting code with istanbul, +// automatic module type detection fails, +// thus we specify it here manually. +System.config({ + defaultJSExtensions: true, + //meta: { + // './test/tests/taler.js': { + // format: 'register' + // }, + // './lib/wallet/*': { + // format: 'register' + // } + //} +}); + +console.log("TAP version 13"); + +let mod = System.newModule({Module: emsc}); +let modName = System.normalizeSync(__dirname + "/../lib/emscripten/taler-emscripten-lib.js"); +System.set(modName, mod); + + +let testName = process.argv[2]; +System.import("./testlib/talertest") + .then(tt => { + SystemJS.import(testName) + .then(() => { + return tt.run(); + }) + .catch((e) => console.error(e)); + }) + .catch((e) => { + console.error("can't locate talertest"); + console.error(e); + }); + diff --git a/tsconfig.json b/tsconfig.json index de73f283d..4692feb82 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,16 +14,17 @@ }, "files": [ "lib/components.ts", - "test/tests/taler.ts", "lib/refs.d.ts", "lib/i18n.ts", "lib/taler-wallet-lib.ts", "lib/wallet/checkable.ts", "lib/wallet/chromeBadge.ts", + "lib/wallet/cryptoApi-test.ts", "lib/wallet/cryptoApi.ts", "lib/wallet/cryptoLib.ts", "lib/wallet/cryptoWorker.ts", "lib/wallet/db.ts", + "lib/wallet/emscriptif-test.ts", "lib/wallet/emscriptif.ts", "lib/wallet/helpers.ts", "lib/wallet/http.ts", -- cgit v1.2.3