Name: js-handler/node_modules/nodeunit/lib/types.js 
1:
/*!
2:
 * Nodeunit
3:
 * Copyright (c) 2010 Caolan McMahon
4:
 * MIT Licensed
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, it's mostly to avoid requiring code
9:
 * that is node specific
10:
 */
11:
 
12:
/**
13:
 * Module dependencies
14:
 */
15:
 
16:
var assert = require('./assert'),     //@REMOVE_LINE_FOR_BROWSER
17:
    async = require('../deps/async'); //@REMOVE_LINE_FOR_BROWSER
18:
 
19:
 
20:
/**
21:
 * Creates assertion objects representing the result of an assert call.
22:
 * Accepts an object or AssertionError as its argument.
23:
 *
24:
 * @param {object} obj
25:
 * @api public
26:
 */
27:
 
28:
exports.assertion = function (obj) {
29:
    return {
30:
        method: obj.method || '',
31:
        message: obj.message || (obj.error && obj.error.message) || '',
32:
        error: obj.error,
33:
        passed: function () {
34:
            return !this.error;
35:
        },
36:
        failed: function () {
37:
            return Boolean(this.error);
38:
        }
39:
    };
40:
};
41:
 
42:
/**
43:
 * Creates an assertion list object representing a group of assertions.
44:
 * Accepts an array of assertion objects.
45:
 *
46:
 * @param {Array} arr
47:
 * @param {Number} duration
48:
 * @api public
49:
 */
50:
 
51:
exports.assertionList = function (arr, duration) {
52:
    var that = arr || [];
53:
    that.failures = function () {
54:
        var failures = 0;
55:
        for (var i = 0; i < this.length; i += 1) {
56:
            if (this[i].failed()) {
57:
                failures += 1;
58:
            }
59:
        }
60:
        return failures;
61:
    };
62:
    that.passes = function () {
63:
        return that.length - that.failures();
64:
    };
65:
    that.duration = duration || 0;
66:
    return that;
67:
};
68:
 
69:
/**
70:
 * Create a wrapper function for assert module methods. Executes a callback
71:
 * after it's complete with an assertion object representing the result.
72:
 *
73:
 * @param {Function} callback
74:
 * @api private
75:
 */
76:
 
77:
var assertWrapper = function (callback) {
78:
    return function (new_method, assert_method, arity) {
79:
        return function () {
80:
            var message = arguments[arity - 1];
81:
            var a = exports.assertion({method: new_method, message: message});
82:
            try {
83:
                assert[assert_method].apply(null, arguments);
84:
            }
85:
            catch (e) {
86:
                a.error = e;
87:
            }
88:
            callback(a);
89:
        };
90:
    };
91:
};
92:
 
93:
/**
94:
 * Creates the 'test' object that gets passed to every test function.
95:
 * Accepts the name of the test function as its first argument, followed by
96:
 * the start time in ms, the options object and a callback function.
97:
 *
98:
 * @param {String} name
99:
 * @param {Number} start
100:
 * @param {Object} options
101:
 * @param {Function} callback
102:
 * @api public
103:
 */
104:
 
105:
exports.test = function (name, start, options, callback) {
106:
    var expecting;
107:
    var a_list = [];
108:
 
109:
    var wrapAssert = assertWrapper(function (a) {
110:
        a_list.push(a);
111:
        if (options.log) {
112:
            async.nextTick(function () {
113:
                options.log(a);
114:
            });
115:
        }
116:
    });
117:
 
118:
    var test = {
119:
        done: function (err) {
120:
            if (expecting !== undefined && expecting !== a_list.length) {
121:
                var e = new Error(
122:
                    'Expected ' + expecting + ' assertions, ' +
123:
                    a_list.length + ' ran'
124:
                );
125:
                var a1 = exports.assertion({method: 'expect', error: e});
126:
                a_list.push(a1);
127:
                if (options.log) {
128:
                    async.nextTick(function () {
129:
                        options.log(a1);
130:
                    });
131:
                }
132:
            }
133:
            if (err) {
134:
                var a2 = exports.assertion({error: err});
135:
                a_list.push(a2);
136:
                if (options.log) {
137:
                    async.nextTick(function () {
138:
                        options.log(a2);
139:
                    });
140:
                }
141:
            }
142:
            var end = new Date().getTime();
143:
            async.nextTick(function () {
144:
                var assertion_list = exports.assertionList(a_list, end - start);
145:
                options.testDone(name, assertion_list);
146:
                callback(null, a_list);
147:
            });
148:
        },
149:
        ok: wrapAssert('ok', 'ok', 2),
150:
        same: wrapAssert('same', 'deepEqual', 3),
151:
        equals: wrapAssert('equals', 'equal', 3),
152:
        expect: function (num) {
153:
            expecting = num;
154:
        },
155:
        _assertion_list: a_list
156:
    };
157:
    // add all functions from the assert module
158:
    for (var k in assert) {
159:
        if (assert.hasOwnProperty(k)) {
160:
            test[k] = wrapAssert(k, k, assert[k].length);
161:
        }
162:
    }
163:
    return test;
164:
};
165:
 
166:
/**
167:
 * Ensures an options object has all callbacks, adding empty callback functions
168:
 * if any are missing.
169:
 *
170:
 * @param {Object} opt
171:
 * @return {Object}
172:
 * @api public
173:
 */
174:
 
175:
exports.options = function (opt) {
176:
    var optionalCallback = function (name) {
177:
        opt[name] = opt[name] || function () {};
178:
    };
179:
 
180:
    optionalCallback('moduleStart');
181:
    optionalCallback('moduleDone');
182:
    optionalCallback('testStart');
183:
    optionalCallback('testDone');
184:
    //optionalCallback('log');
185:
 
186:
    // 'done' callback is not optional.
187:
 
188:
    return opt;
189:
};