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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 const timer = require("sdk/timers");
michael@0 6 const { Loader } = require("sdk/test/loader");
michael@0 7
michael@0 8 var setupCalled = false, teardownCalled = false;
michael@0 9
michael@0 10 exports.setup = function() {
michael@0 11 setupCalled = true;
michael@0 12 };
michael@0 13
michael@0 14 exports.teardown = function() {
michael@0 15 teardownCalled = true;
michael@0 16 setupCalled = false;
michael@0 17 };
michael@0 18
michael@0 19 // Important note - unit tests are run in alphabetical order. The following
michael@0 20 // unit tests for setup/teardown are order dependent, sometimes the result of
michael@0 21 // one test is checked in the next test (testing for teardown does this). When
michael@0 22 // tests are cohesively a single unit, they are named <test_name> - partN where
michael@0 23 // N is their order in the sequence. Secondly, because these tests should be
michael@0 24 // run before all others, they start with an A.
michael@0 25 exports.testASetupTeardownSyncTestPart1 = function(test) {
michael@0 26 test.assertEqual(true, setupCalled, 'setup function was called before this');
michael@0 27 test.assertEqual(false, teardownCalled, 'teardown function was not called before this');
michael@0 28 };
michael@0 29
michael@0 30 exports.testASetupTeardownSyncTestPart2 = function(test) {
michael@0 31 test.assertEqual(true, setupCalled, 'setup was re-called before this');
michael@0 32 test.assertEqual(true, teardownCalled, 'teardown was called after first function');
michael@0 33 };
michael@0 34
michael@0 35 exports.testATeardownAsyncTestPart1 = function(test) {
michael@0 36 teardownCalled = false;
michael@0 37
michael@0 38 timer.setTimeout(function() {
michael@0 39 test.assertEqual(false, teardownCalled, "teardown not called until done");
michael@0 40 test.done();
michael@0 41 }, 200);
michael@0 42 test.waitUntilDone();
michael@0 43 };
michael@0 44
michael@0 45 exports.testATeardownAsyncTestPart2 = function(test) {
michael@0 46 test.assertEqual(true, teardownCalled, "teardown called after done");
michael@0 47 };
michael@0 48
michael@0 49 exports.testWaitUntilInstant = function(test) {
michael@0 50 test.waitUntilDone();
michael@0 51
michael@0 52 test.waitUntil(function () true, "waitUntil with instant true pass")
michael@0 53 .then(function () test.done());
michael@0 54 }
michael@0 55
michael@0 56 exports.testWaitUntil = function(test) {
michael@0 57 test.waitUntilDone();
michael@0 58 let succeed = false;
michael@0 59
michael@0 60 test.waitUntil(function () succeed, "waitUntil pass")
michael@0 61 .then(function () test.done());
michael@0 62
michael@0 63 timer.setTimeout(function () {
michael@0 64 succeed = true;
michael@0 65 }, 200);
michael@0 66 }
michael@0 67
michael@0 68 exports.testWaitUntilEqual = function(test) {
michael@0 69 test.waitUntilDone();
michael@0 70 let succeed = false;
michael@0 71
michael@0 72 test.waitUntilEqual("foo", function () succeed ? "foo" : "bar",
michael@0 73 "waitUntilEqual pass")
michael@0 74 .then(function () test.done());
michael@0 75
michael@0 76 timer.setTimeout(function () {
michael@0 77 succeed = true;
michael@0 78 }, 200);
michael@0 79 }
michael@0 80
michael@0 81 exports.testWaitUntilNotEqual = function(test) {
michael@0 82 test.waitUntilDone();
michael@0 83 let succeed = false;
michael@0 84
michael@0 85 test.waitUntilNotEqual("foo", function () succeed ? "bar" : "foo",
michael@0 86 "waitUntilNotEqual pass")
michael@0 87 .then(function () test.done());
michael@0 88
michael@0 89 timer.setTimeout(function () {
michael@0 90 succeed = true;
michael@0 91 }, 200);
michael@0 92 }
michael@0 93
michael@0 94 exports.testWaitUntilMatches = function(test) {
michael@0 95 test.waitUntilDone();
michael@0 96 let succeed = false;
michael@0 97
michael@0 98 test.waitUntilMatches(function () succeed ? "foo" : "bar",
michael@0 99 /foo/, "waitUntilEqual pass")
michael@0 100 .then(function () test.done());
michael@0 101
michael@0 102 timer.setTimeout(function () {
michael@0 103 succeed = true;
michael@0 104 }, 200);
michael@0 105 }
michael@0 106
michael@0 107 exports.testWaitUntilErrorInCallback = function(test) {
michael@0 108 test.waitUntilDone();
michael@0 109
michael@0 110 test.expectFail(function() {
michael@0 111 test.waitUntil(function () {throw "oops"}, "waitUntil pass")
michael@0 112 .then(function () test.done());
michael@0 113 });
michael@0 114 }
michael@0 115
michael@0 116 exports.testWaitUntilTimeoutInCallback = function(test) {
michael@0 117 test.waitUntilDone();
michael@0 118
michael@0 119 let expected = [];
michael@0 120 let message = 0;
michael@0 121 if (require("@test/options").parseable) {
michael@0 122 expected.push(["print", "TEST-START | wait4ever\n"]);
michael@0 123 expected.push(["error", "fail:", "Timed out"]);
michael@0 124 expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
michael@0 125 expected.push(["print", "TEST-END | wait4ever\n"]);
michael@0 126 }
michael@0 127 else {
michael@0 128 expected.push(["info", "executing 'wait4ever'"]);
michael@0 129 expected.push(["error", "fail:", "Timed out"]);
michael@0 130 expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
michael@0 131 }
michael@0 132
michael@0 133 function checkExpected(name, args) {
michael@0 134 if (expected.length == 0 || expected[0][0] != name) {
michael@0 135 test.fail("Saw an unexpected console." + name + "() call " + args);
michael@0 136 return;
michael@0 137 }
michael@0 138
michael@0 139 message++;
michael@0 140 let expectedArgs = expected.shift().slice(1);
michael@0 141 for (let i = 0; i < expectedArgs.length; i++)
michael@0 142 test.assertEqual(args[i], expectedArgs[i], "Should have seen the right message in argument " + i + " of message " + message);
michael@0 143 if (expected.length == 0)
michael@0 144 test.done();
michael@0 145 }
michael@0 146
michael@0 147 let runner = new (require("sdk/deprecated/unit-test").TestRunner)({
michael@0 148 console: {
michael@0 149 error: function() {
michael@0 150 checkExpected("error", Array.slice(arguments));
michael@0 151 },
michael@0 152 info: function () {
michael@0 153 checkExpected("info", Array.slice(arguments));
michael@0 154 },
michael@0 155 trace: function () {},
michael@0 156 exception: function () {},
michael@0 157 print: function () {
michael@0 158 checkExpected("print", Array.slice(arguments));
michael@0 159 }
michael@0 160 }
michael@0 161 });
michael@0 162
michael@0 163 runner.start({
michael@0 164 test: {
michael@0 165 name: "wait4ever",
michael@0 166 testFunction: function(test) {
michael@0 167 test.waitUntilDone(100);
michael@0 168 test.waitUntil(function() false);
michael@0 169 }
michael@0 170 },
michael@0 171 onDone: function() {}
michael@0 172 });
michael@0 173 };
michael@0 174
michael@0 175 exports.testExpectFail = function(test) {
michael@0 176 test.expectFail(function() {
michael@0 177 test.fail('expectFail masking .fail');
michael@0 178 });
michael@0 179
michael@0 180 test.expectFail(function() {
michael@0 181 test.assert(false, 'expectFail masking .assert');
michael@0 182 });
michael@0 183
michael@0 184 test.assert(true, 'assert should pass with no expectFail');
michael@0 185 /*
michael@0 186 test.expectFail(function() {
michael@0 187 test.expectFail(function() {
michael@0 188 test.fail('this should blow up');
michael@0 189 });
michael@0 190 });
michael@0 191 */
michael@0 192 };
michael@0 193
michael@0 194 exports.testAssertFunction = function(test) {
michael@0 195 test.assertFunction(function() {}, 'assertFunction with function');
michael@0 196 test.expectFail(function() {
michael@0 197 test.assertFunction(null, 'assertFunction with non-function');
michael@0 198 });
michael@0 199 };
michael@0 200
michael@0 201 exports.testAssertUndefined = function(test) {
michael@0 202 test.assertUndefined(undefined, 'assertUndefined with undefined');
michael@0 203 test.expectFail(function() {
michael@0 204 test.assertUndefined(null, 'assertUndefined with null');
michael@0 205 });
michael@0 206 test.expectFail(function() {
michael@0 207 test.assertUndefined(false, 'assertUndefined with false');
michael@0 208 });
michael@0 209 test.expectFail(function() {
michael@0 210 test.assertUndefined(0, 'assertUndefined with 0');
michael@0 211 });
michael@0 212 };
michael@0 213
michael@0 214 exports.testAssertNotUndefined = function(test) {
michael@0 215 test.expectFail(function() {
michael@0 216 test.assertNotUndefined(undefined, 'assertNotUndefined with undefined');
michael@0 217 });
michael@0 218 test.assertNotUndefined(null, 'assertNotUndefined with null');
michael@0 219 test.assertNotUndefined(false, 'assertNotUndefined with false');
michael@0 220 test.assertNotUndefined(0, 'assertNotUndefined with 0');
michael@0 221 };
michael@0 222
michael@0 223 exports.testAssertNull = function(test) {
michael@0 224 test.assertNull(null, 'assertNull with null');
michael@0 225 test.expectFail(function() {
michael@0 226 test.assertNull(undefined, 'assertNull with undefined');
michael@0 227 });
michael@0 228 test.expectFail(function() {
michael@0 229 test.assertNull(false, 'assertNull with false');
michael@0 230 });
michael@0 231 test.expectFail(function() {
michael@0 232 test.assertNull(0, 'assertNull with 0');
michael@0 233 });
michael@0 234 };
michael@0 235
michael@0 236 exports.testAssertNotNull = function(test) {
michael@0 237 test.assertNotNull(undefined, 'assertNotNull with undefined');
michael@0 238 test.assertNotNull(false, 'assertNotNull with false');
michael@0 239 test.assertNotNull(0, 'assertNotNull with 0');
michael@0 240
michael@0 241 test.expectFail(function() {
michael@0 242 test.assertNotNull(null, 'testAssertNotNull with null');
michael@0 243 });
michael@0 244 };
michael@0 245
michael@0 246 exports.testAssertObject = function(test) {
michael@0 247 test.assertObject({}, 'assertObject with {}' );
michael@0 248 test.assertObject(new Object(), 'assertObject with new Object');
michael@0 249 test.expectFail(function() {
michael@0 250 test.assertObject('fail', 'assertObject with string');
michael@0 251 });
michael@0 252 };
michael@0 253
michael@0 254 exports.testAssertString = function(test) {
michael@0 255 test.assertString('', 'assertString with ""');
michael@0 256 test.assertString(new String(), 'assertString with new String');
michael@0 257 };
michael@0 258
michael@0 259 exports.testAssertArray = function(test) {
michael@0 260 test.assertArray([], 'assertArray with []');
michael@0 261 test.assertArray(new Array(), 'assertArray with new Array');
michael@0 262 };
michael@0 263
michael@0 264 exports.testNumber = function(test) {
michael@0 265 test.assertNumber(1, 'assertNumber with 1');
michael@0 266 test.assertNumber(new Number('2'), 'assertNumber with new Number("2")' );
michael@0 267 };
michael@0 268

mercurial