Skip to content

Commit 5ac061e

Browse files
committed
Decent output
1 parent a4a9dcc commit 5ac061e

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

functions/math/lcg_value.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function lcg_value () {
22
// From: http://phpjs.org/functions
33
// + original by: Onno Marsman
4+
// * example 1: lcg_value()
5+
// * returns 1: 1
6+
47
return Math.random();
58
}

functions/strings/explode.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
function explode (delimiter, string, limit) {
2+
// From: http://phpjs.org/functions
3+
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4+
// * example 1: explode(' ', 'Kevin van Zonneveld');
5+
// * returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
26

37
if ( arguments.length < 2 || typeof delimiter === 'undefined' || typeof string === 'undefined' ) return null;
48
if ( delimiter === '' || delimiter === false || delimiter === null) return false;

tests/cli.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ PhpjsUtil.opener = function (name, cb) {
3131
});
3232
};
3333

34+
// --debug works out of the box. See -h
3435
cli.parse({
3536
name: ['name', 'Function name to test', 'path', '*'],
36-
category: ['c', 'Category to test', 'path', '*']
37+
category: ['c', 'Category to test', 'path', '*'],
38+
abort: ['a', 'Abort on first failure']
3739
});
3840

3941
cli.pad = function(str, pad, chr, dir) {
@@ -55,8 +57,18 @@ cli.pad = function(str, pad, chr, dir) {
5557
var width = 120;
5658

5759
cli.main(function(args, options) {
60+
var self = this;
5861
var globpath = __dirname + '/../functions/' + options.category + '/' + options.name + '.js';
5962

63+
process.on('exit', function (){
64+
var msg = self.pass_cnt + ' passed / ' + self.fail_cnt + ' failed / ' + self.skip_cnt + ' skipped';
65+
if (self.fail_cnt) {
66+
cli.fatal(msg);
67+
} else {
68+
cli.ok(msg);
69+
}
70+
});
71+
6072
glob(globpath, {}, function (err, files) {
6173
var names = [];
6274
for (var i in files) {
@@ -66,41 +78,32 @@ cli.main(function(args, options) {
6678
}
6779
}
6880

69-
// cli.spinner('Working..');
70-
var processed = 0;
81+
self.pass_cnt = 0;
82+
self.fail_cnt = 0;
83+
self.skip_cnt = 0;
84+
7185
names.forEach(function(name) {
7286
PhpjsUtil.load(name, function (err, params) {
7387
if (err) {
7488
return cli.fatal(err);
7589
}
7690

7791
if (params['headKeys']['test'] && params['headKeys']['test'][0] === 'skip') {
78-
return cli.ok('Skipped ' + params['name']);
92+
self.skip_cnt++;
93+
return cli.info('--> ' + params['name'] + ' skipped as instructed. ');
7994
}
8095

8196
PhpjsUtil.test(params, function(err, test, params) {
82-
var testline = cli.pad(params['name'] + '#' + test['number'], (width * 0.4), ' ', 'right');
83-
testline += ' ' + cli.pad(test['example'], (width * 0.6 -7));
84-
85-
if (err) {
86-
if ('expected' in test) {
87-
testline += '\n expected' + cli.pad(JSON.stringify(test['expected'], undefined, 2).replace(/\n/g, ''), width - 8);
88-
} else {
89-
testline += '\n expected' + cli.pad('undefined', width - 8);
90-
}
91-
if ('result' in test) {
92-
testline += '\n result ' + cli.pad(JSON.stringify(test['result'], undefined, 2).replace(/\n/g, ''), width - 8);
93-
} else {
94-
testline += '\n result ' + cli.pad('undefined', width - 8);
95-
}
96-
cli.error(testline + '');
97-
// cli.error(err);
97+
if (!err) {
98+
self.pass_cnt++;
99+
cli.debug('--> ' + params['name'] + '#' + test['number'] + ' passed. ');
98100
} else {
99-
cli.ok(' ' + testline + '');
100-
}
101-
102-
if (++processed === names.length) {
103-
cli.spinner('Working.. done!', true); //End the spinner
101+
self.fail_cnt++;
102+
cli.error('--> ' + params['name'] + '#' + test['number'] + ' failed. ');
103+
cli.error(err);
104+
if (options.abort) {
105+
cli.fatal('Aborting on first failure as instructed. ');
106+
}
104107
}
105108
});
106109
});

tests/phpjsutil.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ if (typeof require !== 'undefined') {
66
}
77

88
function PhpjsUtil(config) {
9+
this.injectDependencies = [];
10+
11+
// Overwrite properties with config
912
for (var k in config) {
1013
this[k] = config[k];
1114
}
@@ -221,7 +224,7 @@ PhpjsUtil.prototype.test = function(params, cb) {
221224
var codez = [];
222225

223226

224-
self.loadMultiple(['ini_set', 'ini_get'], function(err, paramsMultiple) {
227+
self.loadMultiple(self.injectDependencies, function(err, paramsMultiple) {
225228
for (var name in paramsMultiple) {
226229
codez.unshift(paramsMultiple[name]['code']);
227230
}
@@ -242,9 +245,6 @@ PhpjsUtil.prototype.test = function(params, cb) {
242245
'};' +
243246
'');
244247

245-
// console.log(params);
246-
console.log(params['name']);
247-
248248
// Load code
249249
eval(
250250
codez.join(';\n')
@@ -278,7 +278,6 @@ PhpjsUtil.prototype.test = function(params, cb) {
278278
// Let's do something evil. Execute line by line (see date.js why)
279279
// We need test.reslult be the last result of the example code
280280
for (var j in params['headKeys']['example'][i]) {
281-
// console.log(params['headKeys']['example'][i]);
282281
if (+j === params['headKeys']['example'][i].length-1) {
283282
eval('test.result = ' + params['headKeys']['example'][i][j] + '');
284283
} else {
@@ -290,8 +289,8 @@ PhpjsUtil.prototype.test = function(params, cb) {
290289
var jsonResult = JSON.stringify(test.result, undefined, 2);
291290

292291
if (jsonExpected !== jsonResult) {
293-
err = 'Expected: ' + jsonExpected +
294-
' but returned: ' + jsonResult;
292+
err = 'expected: \n' + jsonExpected + '\n\n' +
293+
'returned: \n' + jsonResult + '\n';
295294
cb(err, test, params);
296295
continue;
297296
}

0 commit comments

Comments
 (0)