Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | 'stability': 'unstable' |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | const { defer } = require('../core/promise'); |
michael@0 | 12 | const { setInterval, clearInterval } = require('../timers'); |
michael@0 | 13 | |
michael@0 | 14 | function getTestNames (exports) |
michael@0 | 15 | Object.keys(exports).filter(name => /^test/.test(name)) |
michael@0 | 16 | |
michael@0 | 17 | function isTestAsync (fn) fn.length > 1 |
michael@0 | 18 | function isHelperAsync (fn) fn.length > 2 |
michael@0 | 19 | |
michael@0 | 20 | /* |
michael@0 | 21 | * Takes an `exports` object of a test file and a function `beforeFn` |
michael@0 | 22 | * to be run before each test. `beforeFn` is called with a `name` string |
michael@0 | 23 | * as the first argument of the test name, and may specify a second |
michael@0 | 24 | * argument function `done` to indicate that this function should |
michael@0 | 25 | * resolve asynchronously |
michael@0 | 26 | */ |
michael@0 | 27 | function before (exports, beforeFn) { |
michael@0 | 28 | getTestNames(exports).map(name => { |
michael@0 | 29 | let testFn = exports[name]; |
michael@0 | 30 | if (!isTestAsync(testFn) && !isHelperAsync(beforeFn)) { |
michael@0 | 31 | exports[name] = function (assert) { |
michael@0 | 32 | beforeFn(name, assert); |
michael@0 | 33 | testFn(assert); |
michael@0 | 34 | }; |
michael@0 | 35 | } |
michael@0 | 36 | else if (isTestAsync(testFn) && !isHelperAsync(beforeFn)) { |
michael@0 | 37 | exports[name] = function (assert, done) { |
michael@0 | 38 | beforeFn(name, assert); |
michael@0 | 39 | testFn(assert, done); |
michael@0 | 40 | }; |
michael@0 | 41 | } |
michael@0 | 42 | else if (!isTestAsync(testFn) && isHelperAsync(beforeFn)) { |
michael@0 | 43 | exports[name] = function (assert, done) { |
michael@0 | 44 | beforeFn(name, assert, () => { |
michael@0 | 45 | testFn(assert); |
michael@0 | 46 | done(); |
michael@0 | 47 | }); |
michael@0 | 48 | }; |
michael@0 | 49 | } else if (isTestAsync(testFn) && isHelperAsync(beforeFn)) { |
michael@0 | 50 | exports[name] = function (assert, done) { |
michael@0 | 51 | beforeFn(name, assert, () => { |
michael@0 | 52 | testFn(assert, done); |
michael@0 | 53 | }); |
michael@0 | 54 | }; |
michael@0 | 55 | } |
michael@0 | 56 | }); |
michael@0 | 57 | } |
michael@0 | 58 | exports.before = before; |
michael@0 | 59 | |
michael@0 | 60 | /* |
michael@0 | 61 | * Takes an `exports` object of a test file and a function `afterFn` |
michael@0 | 62 | * to be run after each test. `afterFn` is called with a `name` string |
michael@0 | 63 | * as the first argument of the test name, and may specify a second |
michael@0 | 64 | * argument function `done` to indicate that this function should |
michael@0 | 65 | * resolve asynchronously |
michael@0 | 66 | */ |
michael@0 | 67 | function after (exports, afterFn) { |
michael@0 | 68 | getTestNames(exports).map(name => { |
michael@0 | 69 | let testFn = exports[name]; |
michael@0 | 70 | if (!isTestAsync(testFn) && !isHelperAsync(afterFn)) { |
michael@0 | 71 | exports[name] = function (assert) { |
michael@0 | 72 | testFn(assert); |
michael@0 | 73 | afterFn(name, assert); |
michael@0 | 74 | }; |
michael@0 | 75 | } |
michael@0 | 76 | else if (isTestAsync(testFn) && !isHelperAsync(afterFn)) { |
michael@0 | 77 | exports[name] = function (assert, done) { |
michael@0 | 78 | testFn(assert, () => { |
michael@0 | 79 | afterFn(name, assert); |
michael@0 | 80 | done(); |
michael@0 | 81 | }); |
michael@0 | 82 | }; |
michael@0 | 83 | } |
michael@0 | 84 | else if (!isTestAsync(testFn) && isHelperAsync(afterFn)) { |
michael@0 | 85 | exports[name] = function (assert, done) { |
michael@0 | 86 | testFn(assert); |
michael@0 | 87 | afterFn(name, assert, done); |
michael@0 | 88 | }; |
michael@0 | 89 | } else if (isTestAsync(testFn) && isHelperAsync(afterFn)) { |
michael@0 | 90 | exports[name] = function (assert, done) { |
michael@0 | 91 | testFn(assert, () => { |
michael@0 | 92 | afterFn(name, assert, done); |
michael@0 | 93 | }); |
michael@0 | 94 | }; |
michael@0 | 95 | } |
michael@0 | 96 | }); |
michael@0 | 97 | } |
michael@0 | 98 | exports.after = after; |
michael@0 | 99 | |
michael@0 | 100 | function waitUntil (predicate, delay) { |
michael@0 | 101 | let { promise, resolve } = defer(); |
michael@0 | 102 | let interval = setInterval(() => { |
michael@0 | 103 | if (!predicate()) return; |
michael@0 | 104 | clearInterval(interval); |
michael@0 | 105 | resolve(); |
michael@0 | 106 | }, delay || 10); |
michael@0 | 107 | return promise; |
michael@0 | 108 | } |
michael@0 | 109 | exports.waitUntil = waitUntil; |