addon-sdk/source/test/test-test-utils-async.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-test-utils-async.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +'use strict'
     1.9 +
    1.10 +const { defer: async } = require('sdk/lang/functional');
    1.11 +const { before, after } = require('sdk/test/utils');
    1.12 +
    1.13 +let AFTER_RUN = 0;
    1.14 +let BEFORE_RUN = 0;
    1.15 +
    1.16 +/*
    1.17 + * Tests are dependent on ordering, as the before and after functions
    1.18 + * are called outside of each test, and sometimes checked in the next test
    1.19 + * (like in the `after` tests)
    1.20 + */
    1.21 +exports.testABeforeAsync = function (assert, done) {
    1.22 +  assert.equal(BEFORE_RUN, 1, 'before function was called');
    1.23 +  BEFORE_RUN = 0;
    1.24 +  AFTER_RUN = 0;
    1.25 +  async(done)();
    1.26 +};
    1.27 +
    1.28 +exports.testABeforeNameAsync = function (assert, done) {
    1.29 +  assert.equal(BEFORE_RUN, 2, 'before function was called with name');
    1.30 +  BEFORE_RUN = 0;
    1.31 +  AFTER_RUN = 0;
    1.32 +  async(done)();
    1.33 +};
    1.34 +
    1.35 +exports.testAfterAsync = function (assert, done) {
    1.36 +  assert.equal(AFTER_RUN, 1, 'after function was called previously');
    1.37 +  BEFORE_RUN = 0;
    1.38 +  AFTER_RUN = 0;
    1.39 +  async(done)();
    1.40 +};
    1.41 +
    1.42 +exports.testAfterNameAsync = function (assert, done) {
    1.43 +  assert.equal(AFTER_RUN, 2, 'after function was called with name');
    1.44 +  BEFORE_RUN = 0;
    1.45 +  AFTER_RUN = 0;
    1.46 +  async(done)();
    1.47 +};
    1.48 +
    1.49 +exports.testSyncABefore = function (assert) {
    1.50 +  assert.equal(BEFORE_RUN, 1, 'before function was called for sync test');
    1.51 +  BEFORE_RUN = 0;
    1.52 +  AFTER_RUN = 0;
    1.53 +};
    1.54 +
    1.55 +exports.testSyncAfter = function (assert) {
    1.56 +  assert.equal(AFTER_RUN, 1, 'after function was called for sync test');
    1.57 +  BEFORE_RUN = 0;
    1.58 +  AFTER_RUN = 0;
    1.59 +};
    1.60 +
    1.61 +before(exports, (name, assert, done) => {
    1.62 +  if (name === 'testABeforeNameAsync')
    1.63 +    BEFORE_RUN = 2;
    1.64 +  else
    1.65 +    BEFORE_RUN = 1;
    1.66 +  assert.pass('assert passed into before function');
    1.67 +  async(done)();
    1.68 +});
    1.69 +
    1.70 +after(exports, (name, assert, done) => {
    1.71 +  // testAfterName runs after testAfter, which is where this
    1.72 +  // check occurs in the assertation
    1.73 +  if (name === 'testAfterAsync')
    1.74 +    AFTER_RUN = 2;
    1.75 +  else
    1.76 +    AFTER_RUN = 1;
    1.77 +  assert.pass('assert passed into after function');
    1.78 +  async(done)();
    1.79 +});
    1.80 +
    1.81 +require('sdk/test').run(exports);

mercurial