Name: js-handler/node_modules/nodeunit/test/test-base.js 
1:
/*
2:
 *  This module is not a plain nodeunit test suite, but instead uses the
3:
 *  assert module to ensure a basic level of functionality is present,
4:
 *  allowing the rest of the tests to be written using nodeunit itself.
5:
 *
6:
 *  THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
7:
 *  You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
8:
 *  Only code on that line will be removed, its mostly to avoid requiring code
9:
 *  that is node specific
10:
 */
11:
 
12:
var assert = require('assert'),             // @REMOVE_LINE_FOR_BROWSER
13:
    async = require('../deps/async'),       // @REMOVE_LINE_FOR_BROWSER
14:
    nodeunit = require('../lib/nodeunit');  // @REMOVE_LINE_FOR_BROWSER
15:
 
16:
 
17:
// NOT A TEST - util function to make testing faster.
18:
// retries the assertion until it passes or the timeout is reached,
19:
// at which point it throws the assertion error
20:
var waitFor = function (fn, timeout, callback, start) {
21:
    start = start || new Date().getTime();
22:
    callback = callback || function () {};
23:
    try {
24:
        fn();
25:
        callback();
26:
    }
27:
    catch (e) {
28:
        if (e instanceof assert.AssertionError) {
29:
            var now = new Date().getTime();
30:
            if (now - start >= timeout) {
31:
                throw e;
32:
            }
33:
            else {
34:
                async.nextTick(function () {
35:
                    waitFor(fn, timeout, callback, start);
36:
                });
37:
            }
38:
        }
39:
        else {
40:
            throw e;
41:
        }
42:
    }
43:
};
44:
 
45:
 
46:
// TESTS:
47:
 
48:
// Are exported tests actually run? - store completed tests in this variable
49:
// for checking later
50:
var tests_called = {};
51:
 
52:
// most basic test that should run, the tests_called object is tested
53:
// at the end of this module to ensure the tests were actually run by nodeunit
54:
exports.testCalled = function (test) {
55:
    tests_called.testCalled = true;
56:
    test.done();
57:
};
58:
 
59:
// generates test functions for nodeunit assertions
60:
var makeTest = function (method, args_pass, args_fail) {
61:
    return function (test) {
62:
        var test1_called = false;
63:
        var test2_called = false;
64:
 
65:
        // test pass
66:
        nodeunit.runTest(
67:
            'testname',
68:
            function (test) {
69:
                test[method].apply(test, args_pass);
70:
                test.done();
71:
            },
72:
            {testDone: function (name, assertions) {
73:
                assert.equal(assertions.length, 1);
74:
                assert.equal(assertions.failures(), 0);
75:
            }},
76:
            function () {
77:
                test1_called = true;
78:
            }
79:
        );
80:
 
81:
        // test failure
82:
        nodeunit.runTest(
83:
            'testname',
84:
            function (test) {
85:
                test[method].apply(test, args_fail);
86:
                test.done();
87:
            },
88:
            {testDone: function (name, assertions) {
89:
                assert.equal(assertions.length, 1);
90:
                assert.equal(assertions.failures(), 1);
91:
            }},
92:
            function () {
93:
                test2_called = true;
94:
            }
95:
        );
96:
 
97:
        // ensure tests were run
98:
        waitFor(function () {
99:
            assert.ok(test1_called);
100:
            assert.ok(test2_called);
101:
            tests_called[method] = true;
102:
        }, 500, test.done);
103:
    };
104:
};
105:
 
106:
// ensure basic assertions are working:
107:
exports.testOk = makeTest('ok', [true], [false]);
108:
exports.testEquals = makeTest('equals', [1, 1], [1, 2]);
109:
exports.testSame = makeTest('same',
110:
    [{test: 'test'}, {test: 'test'}],
111:
    [{test: 'test'}, {monkey: 'penguin'}]
112:
);
113:
 
114:
// from the assert module:
115:
exports.testEqual = makeTest('equal', [1, 1], [1, 2]);
116:
exports.testNotEqual = makeTest('notEqual', [1, 2], [1, 1]);
117:
exports.testDeepEqual = makeTest('deepEqual',
118:
    [{one: 1}, {one: 1}], [{one: 1}, {two: 2}]
119:
);
120:
exports.testNotDeepEqual = makeTest('notDeepEqual',
121:
    [{one: 1}, {two: 2}], [{one: 1}, {one: 1}]
122:
);
123:
exports.testStrictEqual = makeTest('strictEqual', [1, 1], [1, true]);
124:
exports.testNotStrictEqual = makeTest('notStrictEqual', [true, 1], [1, 1]);
125:
exports.testThrows = makeTest('throws',
126:
    [function () {
127:
        throw new Error('test');
128:
    }],
129:
    [function () {
130:
        return;
131:
    }]
132:
);
133:
exports.testThrowsWithReGex = makeTest('throws',
134:
    [function () {
135:
        throw new Error('test');
136:
    }, /test/],
137:
    [function () {
138:
        throw new Error('test');
139:
    }, /fail/]
140:
);
141:
exports.testThrowsWithErrorValidation = makeTest('throws',
142:
    [function () {
143:
        throw new Error('test');
144:
    }, function(err) {
145:
        return true;
146:
    }],
147:
    [function () {
148:
        throw new Error('test');
149:
    }, function(err) {
150:
        return false;
151:
    }]
152:
);
153:
exports.testDoesNotThrows = makeTest('doesNotThrow',
154:
    [function () {
155:
        return;
156:
    }],
157:
    [function () {
158:
        throw new Error('test');
159:
    }]
160:
);
161:
exports.testIfError = makeTest('ifError', [false], [new Error('test')]);
162:
 
163:
 
164:
exports.testExpect = function (test) {
165:
    var test1_called = false,
166:
        test2_called = false,
167:
        test3_called = false;
168:
 
169:
    // correct number of tests run
170:
    nodeunit.runTest(
171:
        'testname',
172:
        function (test) {
173:
            test.expect(2);
174:
            test.ok(true);
175:
            test.ok(true);
176:
            test.done();
177:
        },
178:
        {testDone: function (name, assertions) {
179:
            test.equals(assertions.length, 2);
180:
            test.equals(assertions.failures(), 0);
181:
        }},
182:
        function () {
183:
            test1_called = true;
184:
        }
185:
    );
186:
 
187:
    // no tests run
188:
    nodeunit.runTest(
189:
        'testname',
190:
        function (test) {
191:
            test.expect(2);
192:
            test.done();
193:
        },
194:
        {testDone: function (name, assertions) {
195:
            test.equals(assertions.length, 1);
196:
            test.equals(assertions.failures(), 1);
197:
        }},
198:
        function () {
199:
            test2_called = true;
200:
        }
201:
    );
202:
 
203:
    // incorrect number of tests run
204:
    nodeunit.runTest(
205:
        'testname',
206:
        function (test) {
207:
            test.expect(2);
208:
            test.ok(true);
209:
            test.ok(true);
210:
            test.ok(true);
211:
            test.done();
212:
        },
213:
        {testDone: function (name, assertions) {
214:
            test.equals(assertions.length, 4);
215:
            test.equals(assertions.failures(), 1);
216:
        }},
217:
        function () {
218:
            test3_called = true;
219:
        }
220:
    );
221:
 
222:
    // ensure callbacks fired
223:
    waitFor(function () {
224:
        assert.ok(test1_called);
225:
        assert.ok(test2_called);
226:
        assert.ok(test3_called);
227:
        tests_called.expect = true;
228:
    }, 1000, test.done);
229:
};
230:
 
231:
 
232:
// tests are async, so wait for them to be called
233:
waitFor(function () {
234:
    assert.ok(tests_called.testCalled);
235:
    assert.ok(tests_called.ok);
236:
    assert.ok(tests_called.equals);
237:
    assert.ok(tests_called.same);
238:
    assert.ok(tests_called.expect);
239:
}, 10000);