addon-sdk/source/test/test-unit-test.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-unit-test.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,268 @@
     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 +const timer = require("sdk/timers");
     1.9 +const { Loader } = require("sdk/test/loader");
    1.10 +
    1.11 +var setupCalled = false, teardownCalled = false;
    1.12 +
    1.13 +exports.setup = function() {
    1.14 +    setupCalled = true;    
    1.15 +};
    1.16 +
    1.17 +exports.teardown = function() {
    1.18 +    teardownCalled = true;
    1.19 +    setupCalled = false;
    1.20 +};
    1.21 +
    1.22 +// Important note - unit tests are run in alphabetical order.  The following 
    1.23 +// unit tests for setup/teardown are order dependent, sometimes the result of 
    1.24 +// one test is checked in the next test (testing for teardown does this).  When 
    1.25 +// tests are cohesively a single unit, they are named <test_name> - partN where 
    1.26 +// N is their order in the sequence.  Secondly, because these tests should be
    1.27 +// run before all others, they start with an A.
    1.28 +exports.testASetupTeardownSyncTestPart1 = function(test) {
    1.29 +    test.assertEqual(true, setupCalled, 'setup function was called before this');
    1.30 +    test.assertEqual(false, teardownCalled, 'teardown function was not called before this');
    1.31 +};
    1.32 +
    1.33 +exports.testASetupTeardownSyncTestPart2 = function(test) {
    1.34 +    test.assertEqual(true, setupCalled, 'setup was re-called before this');
    1.35 +    test.assertEqual(true, teardownCalled, 'teardown was called after first function');
    1.36 +};
    1.37 +
    1.38 +exports.testATeardownAsyncTestPart1 = function(test) {
    1.39 +    teardownCalled = false;
    1.40 +
    1.41 +    timer.setTimeout(function() {
    1.42 +        test.assertEqual(false, teardownCalled, "teardown not called until done");
    1.43 +        test.done();
    1.44 +    }, 200);
    1.45 +    test.waitUntilDone();
    1.46 +};
    1.47 +
    1.48 +exports.testATeardownAsyncTestPart2 = function(test) {
    1.49 +    test.assertEqual(true, teardownCalled, "teardown called after done");
    1.50 +};
    1.51 +
    1.52 +exports.testWaitUntilInstant = function(test) {
    1.53 +  test.waitUntilDone();
    1.54 +  
    1.55 +  test.waitUntil(function () true, "waitUntil with instant true pass")
    1.56 +      .then(function () test.done());
    1.57 +}
    1.58 +
    1.59 +exports.testWaitUntil = function(test) {
    1.60 +  test.waitUntilDone();
    1.61 +  let succeed = false;
    1.62 +  
    1.63 +  test.waitUntil(function () succeed, "waitUntil pass")
    1.64 +      .then(function () test.done());
    1.65 +  
    1.66 +  timer.setTimeout(function () {
    1.67 +    succeed = true;
    1.68 +  }, 200);
    1.69 +}
    1.70 +
    1.71 +exports.testWaitUntilEqual = function(test) {
    1.72 +  test.waitUntilDone();
    1.73 +  let succeed = false;
    1.74 +  
    1.75 +  test.waitUntilEqual("foo", function () succeed ? "foo" : "bar", 
    1.76 +                      "waitUntilEqual pass")
    1.77 +      .then(function () test.done());
    1.78 +  
    1.79 +  timer.setTimeout(function () {
    1.80 +    succeed = true;
    1.81 +  }, 200);
    1.82 +}
    1.83 +
    1.84 +exports.testWaitUntilNotEqual = function(test) {
    1.85 +  test.waitUntilDone();
    1.86 +  let succeed = false;
    1.87 +  
    1.88 +  test.waitUntilNotEqual("foo", function () succeed ? "bar" : "foo",
    1.89 +                         "waitUntilNotEqual pass")
    1.90 +      .then(function () test.done());
    1.91 +  
    1.92 +  timer.setTimeout(function () {
    1.93 +    succeed = true;
    1.94 +  }, 200);
    1.95 +}
    1.96 +
    1.97 +exports.testWaitUntilMatches = function(test) {
    1.98 +  test.waitUntilDone();
    1.99 +  let succeed = false;
   1.100 +  
   1.101 +  test.waitUntilMatches(function () succeed ? "foo" : "bar",
   1.102 +                        /foo/, "waitUntilEqual pass")
   1.103 +      .then(function () test.done());
   1.104 +  
   1.105 +  timer.setTimeout(function () {
   1.106 +    succeed = true;
   1.107 +  }, 200);
   1.108 +}
   1.109 +
   1.110 +exports.testWaitUntilErrorInCallback = function(test) {
   1.111 +  test.waitUntilDone();
   1.112 +
   1.113 +  test.expectFail(function() {
   1.114 +    test.waitUntil(function () {throw "oops"}, "waitUntil pass")
   1.115 +        .then(function () test.done());
   1.116 +  });
   1.117 +}
   1.118 +
   1.119 +exports.testWaitUntilTimeoutInCallback = function(test) {
   1.120 +  test.waitUntilDone();
   1.121 +
   1.122 +  let expected = [];
   1.123 +  let message = 0;
   1.124 +  if (require("@test/options").parseable) {
   1.125 +    expected.push(["print", "TEST-START | wait4ever\n"]);
   1.126 +    expected.push(["error", "fail:", "Timed out"]);
   1.127 +    expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
   1.128 +    expected.push(["print", "TEST-END | wait4ever\n"]);
   1.129 +  }
   1.130 +  else {
   1.131 +    expected.push(["info",  "executing 'wait4ever'"]);
   1.132 +    expected.push(["error", "fail:", "Timed out"]);
   1.133 +    expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
   1.134 +  }
   1.135 +
   1.136 +  function checkExpected(name, args) {
   1.137 +    if (expected.length == 0 || expected[0][0] != name) {
   1.138 +      test.fail("Saw an unexpected console." + name + "() call " + args);
   1.139 +      return;
   1.140 +    }
   1.141 +
   1.142 +    message++;
   1.143 +    let expectedArgs = expected.shift().slice(1);
   1.144 +    for (let i = 0; i < expectedArgs.length; i++)
   1.145 +      test.assertEqual(args[i], expectedArgs[i], "Should have seen the right message in argument " + i + " of message " + message);
   1.146 +    if (expected.length == 0)
   1.147 +      test.done();
   1.148 +  }
   1.149 +
   1.150 +  let runner = new (require("sdk/deprecated/unit-test").TestRunner)({
   1.151 +    console: {
   1.152 +      error: function() {
   1.153 +        checkExpected("error", Array.slice(arguments));
   1.154 +      },
   1.155 +      info: function () {
   1.156 +        checkExpected("info", Array.slice(arguments));
   1.157 +      },
   1.158 +      trace: function () {},
   1.159 +      exception: function () {},
   1.160 +      print: function () {
   1.161 +        checkExpected("print", Array.slice(arguments));
   1.162 +      }
   1.163 +    }
   1.164 +  });
   1.165 +
   1.166 +  runner.start({
   1.167 +    test: {
   1.168 +      name: "wait4ever",
   1.169 +      testFunction: function(test) {
   1.170 +        test.waitUntilDone(100);
   1.171 +        test.waitUntil(function() false);
   1.172 +      }
   1.173 +    },
   1.174 +    onDone: function() {}
   1.175 +  });
   1.176 +};
   1.177 +
   1.178 +exports.testExpectFail = function(test) {
   1.179 +    test.expectFail(function() {
   1.180 +        test.fail('expectFail masking .fail');
   1.181 +    });
   1.182 +
   1.183 +    test.expectFail(function() {
   1.184 +        test.assert(false, 'expectFail masking .assert');
   1.185 +    });
   1.186 +
   1.187 +    test.assert(true, 'assert should pass with no expectFail');
   1.188 +/*
   1.189 +    test.expectFail(function() {
   1.190 +        test.expectFail(function() {
   1.191 +            test.fail('this should blow up');
   1.192 +        });
   1.193 +    });
   1.194 +*/
   1.195 +};
   1.196 +
   1.197 +exports.testAssertFunction = function(test) {
   1.198 +    test.assertFunction(function() {}, 'assertFunction with function');
   1.199 +    test.expectFail(function() {
   1.200 +        test.assertFunction(null, 'assertFunction with non-function');
   1.201 +    });    
   1.202 +};
   1.203 +
   1.204 +exports.testAssertUndefined = function(test) {
   1.205 +    test.assertUndefined(undefined, 'assertUndefined with undefined');
   1.206 +    test.expectFail(function() {
   1.207 +        test.assertUndefined(null, 'assertUndefined with null');
   1.208 +    });    
   1.209 +    test.expectFail(function() {
   1.210 +        test.assertUndefined(false, 'assertUndefined with false');
   1.211 +    });    
   1.212 +    test.expectFail(function() {
   1.213 +        test.assertUndefined(0, 'assertUndefined with 0');
   1.214 +    });    
   1.215 +};
   1.216 +
   1.217 +exports.testAssertNotUndefined = function(test) {
   1.218 +    test.expectFail(function() {
   1.219 +        test.assertNotUndefined(undefined, 'assertNotUndefined with undefined');
   1.220 +    });    
   1.221 +    test.assertNotUndefined(null, 'assertNotUndefined with null');
   1.222 +    test.assertNotUndefined(false, 'assertNotUndefined with false');
   1.223 +    test.assertNotUndefined(0, 'assertNotUndefined with 0');
   1.224 +};
   1.225 +
   1.226 +exports.testAssertNull = function(test) {
   1.227 +    test.assertNull(null, 'assertNull with null');
   1.228 +    test.expectFail(function() {
   1.229 +        test.assertNull(undefined, 'assertNull with undefined');
   1.230 +    });    
   1.231 +    test.expectFail(function() {
   1.232 +        test.assertNull(false, 'assertNull with false');
   1.233 +    });
   1.234 +    test.expectFail(function() {
   1.235 +        test.assertNull(0, 'assertNull with 0');
   1.236 +    });
   1.237 +};
   1.238 +
   1.239 +exports.testAssertNotNull = function(test) {
   1.240 +    test.assertNotNull(undefined, 'assertNotNull with undefined');
   1.241 +    test.assertNotNull(false, 'assertNotNull with false');
   1.242 +    test.assertNotNull(0, 'assertNotNull with 0');
   1.243 +
   1.244 +    test.expectFail(function() {
   1.245 +        test.assertNotNull(null, 'testAssertNotNull with null');
   1.246 +    });    
   1.247 +};
   1.248 +
   1.249 +exports.testAssertObject = function(test) {
   1.250 +    test.assertObject({}, 'assertObject with {}' );
   1.251 +    test.assertObject(new Object(), 'assertObject with new Object');
   1.252 +    test.expectFail(function() {
   1.253 +        test.assertObject('fail', 'assertObject with string');
   1.254 +    });
   1.255 +};
   1.256 +
   1.257 +exports.testAssertString = function(test) {
   1.258 +    test.assertString('', 'assertString with ""');
   1.259 +    test.assertString(new String(), 'assertString with new String');
   1.260 +};
   1.261 +
   1.262 +exports.testAssertArray = function(test) {
   1.263 +    test.assertArray([], 'assertArray with []');
   1.264 +    test.assertArray(new Array(), 'assertArray with new Array');
   1.265 +};
   1.266 +
   1.267 +exports.testNumber = function(test) {
   1.268 +    test.assertNumber(1, 'assertNumber with 1');
   1.269 +    test.assertNumber(new Number('2'), 'assertNumber with new Number("2")' );
   1.270 +};
   1.271 +

mercurial