Name: js-handler/node_modules/nodeunit/lib/reporters/nested.js 
1:
/*!
2:
 * Nodeunit
3:
 * Copyright (c) 2010 Caolan McMahon
4:
 * MIT Licensed
5:
 */
6:
 
7:
/**
8:
 * Module dependencies
9:
 */
10:
 
11:
var nodeunit = require('../nodeunit'),
12:
    utils = require('../utils'),
13:
    fs = require('fs'),
14:
    track = require('../track'),
15:
    path = require('path'),
16:
    AssertionError = require('../assert').AssertionError;
17:
 
18:
/**
19:
 * Reporter info string
20:
 */
21:
 
22:
exports.info = "Nested test reporter";
23:
 
24:
 
25:
/**
26:
 * Run all tests within each module, reporting the results to the command-line.
27:
 *
28:
 * @param {Array} files
29:
 * @api public
30:
 */
31:
 
32:
exports.run = function (files, options) {
33:
 
34:
    if (!options) {
35:
        // load default options
36:
        var content = fs.readFileSync(
37:
            __dirname + '/../../bin/nodeunit.json',
38:
            'utf8'
39:
        );
40:
        options = JSON.parse(content);
41:
    }
42:
 
43:
    var error = function (str) {
44:
        return options.error_prefix + str + options.error_suffix;
45:
    };
46:
    var ok    = function (str) {
47:
        return options.ok_prefix + str + options.ok_suffix;
48:
    };
49:
    var bold  = function (str) {
50:
        return options.bold_prefix + str + options.bold_suffix;
51:
    };
52:
    var assertion_message = function (str) {
53:
        return options.assertion_prefix + str + options.assertion_suffix;
54:
    };
55:
 
56:
    var spaces_per_indent = options.spaces_per_indent || 4;
57:
 
58:
    var start = new Date().getTime();
59:
    var paths = files.map(function (p) {
60:
        return path.join(process.cwd(), p);
61:
    });
62:
    var tracker = track.createTracker(function (tracker) {
63:
        var i, names;
64:
        if (tracker.unfinished()) {
65:
            console.log('');
66:
            console.log(error(bold(
67:
                'FAILURES: Undone tests (or their setups/teardowns): '
68:
            )));
69:
            names = tracker.names();
70:
            for (i = 0; i < names.length; i += 1) {
71:
                console.log('- ' + names[i]);
72:
            }
73:
            console.log('');
74:
            console.log('To fix this, make sure all tests call test.done()');
75:
            process.reallyExit(tracker.unfinished());
76:
        }
77:
    });
78:
 
79:
    // Object to hold status of each 'part' of the testCase/name array,
80:
    // i.e., whether this part has been printed yet.
81:
    tracker.already_printed = {};
82:
 
83:
    var pass_text = function (txt) {
84:
        // Print in bold green.
85:
        return bold(ok(txt + " (pass)"));
86:
    };
87:
 
88:
    var fail_text = function (txt) {
89:
        return bold(error(txt + " (fail) ✖ "));
90:
    };
91:
 
92:
    var status_text = function (txt, status) {
93:
        if (status === 'pass') {
94:
            return pass_text(txt);
95:
        } else {
96:
            return fail_text(txt);
97:
        }
98:
    };
99:
 
100:
    /**
101:
     *  Slices an array, returns a string by joining the sliced elements.
102:
     *  @example
103:
     *   > name_slice(['TC1', 'TC1.1', 'mytest'], 1);
104:
     *   "TC1,TC1.1"
105:
     */
106:
    var name_slice = function (name_arr, end_index) {
107:
        return name_arr.slice(0, end_index + 1).join(",");
108:
    };
109:
 
110:
    var indent = (function () {
111:
        var txt = '';
112:
        var i;
113:
        for (i = 0; i < spaces_per_indent; i++) {
114:
            txt += ' ';
115:
        }
116:
        return txt;
117:
    }());
118:
 
119:
    // Indent once for each indent_level
120:
    var add_indent = function (txt, indent_level) {
121:
        var k;
122:
        for (k = 0; k < indent_level; k++) {
123:
            txt += indent;
124:
        }
125:
        return txt;
126:
    };
127:
 
128:
    // If it's not the last element of the name_arr, it's a testCase.
129:
    var is_testCase = function (name_arr, index) {
130:
        return index === name_arr.length - 1 ? false : true;
131:
    };
132:
 
133:
    var testCase_line = function (txt) {
134:
        return txt + "\n";
135:
    };
136:
 
137:
    /**
138:
     * Prints (console.log) the nested test status line(s).
139:
     *
140:
     * @param {Array} name_arr - Array of name elements.
141:
     * @param {String} status - either 'pass' or 'fail'.
142:
     * @example
143:
     *   > print_status(['TC1', 'TC1.1', 'mytest'], 'pass');
144:
     *   TC1
145:
     *      TC1.1
146:
     *         mytest (pass)
147:
     */
148:
    var print_status = function (name_arr, status) {
149:
        var txt = '';
150:
        var _name_slice, part, i;
151:
        for (i = 0; i < name_arr.length; i++) {
152:
            _name_slice = name_slice(name_arr, i);
153:
            part = name_arr[i];
154:
            if (!tracker.already_printed[_name_slice]) {
155:
                txt = add_indent(txt, i);
156:
                if (is_testCase(name_arr, i)) {
157:
                    txt += testCase_line(part);
158:
                } else {
159:
                    txt += status_text(part, status);
160:
                }
161:
                tracker.already_printed[_name_slice] = true;
162:
            }
163:
        }
164:
        console.log(txt);
165:
    };
166:
 
167:
    nodeunit.runFiles(paths, {
168:
        testspec: options.testspec,
169:
        testFullSpec: options.testFullSpec,
170:
        moduleStart: function (name) {
171:
            console.log('\n' + bold(name));
172:
        },
173:
        testDone: function (name, assertions) {
174:
            tracker.remove(name);
175:
 
176:
            if (!assertions.failures()) {
177:
                print_status(name, 'pass');
178:
            } else {
179:
                print_status(name, 'fail');
180:
                assertions.forEach(function (a) {
181:
                    if (a.failed()) {
182:
                        a = utils.betterErrors(a);
183:
                        if (a.error instanceof AssertionError && a.message) {
184:
                            console.log(
185:
                                'Assertion Message: ' +
186:
                                    assertion_message(a.message)
187:
                            );
188:
                        }
189:
                        console.log(a.error.stack + '\n');
190:
                    }
191:
                });
192:
            }
193:
        },
194:
        done: function (assertions, end) {
195:
            end = end || new Date().getTime();
196:
            var duration = end - start;
197:
            if (assertions.failures()) {
198:
                console.log(
199:
                    '\n' + bold(error('FAILURES: ')) + assertions.failures() +
200:
                        '/' + assertions.length + ' assertions failed (' +
201:
                        assertions.duration + 'ms)'
202:
                );
203:
            } else {
204:
                console.log(
205:
                    '\n' + bold(ok('OK: ')) + assertions.length +
206:
                        ' assertions (' + assertions.duration + 'ms)'
207:
                );
208:
            }
209:
        },
210:
        testStart: function (name) {
211:
            tracker.put(name);
212:
        }
213:
    });
214:
};