first commit
This commit is contained in:
118
node_modules/extsprintf/test/tst.basic.js
generated
vendored
Normal file
118
node_modules/extsprintf/test/tst.basic.js
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* tst.basic.js: tests various valid invocation
|
||||
*/
|
||||
|
||||
var mod_assert = require('assert');
|
||||
var mod_extsprintf = require('../lib/extsprintf');
|
||||
var mod_path = require('path');
|
||||
var sprintf = mod_extsprintf.sprintf;
|
||||
|
||||
var testcases = [ {
|
||||
'name': 'empty string',
|
||||
'args': [ '' ],
|
||||
'result': ''
|
||||
}, {
|
||||
'name': '%s: basic',
|
||||
'args': [ '%s', 'foo' ],
|
||||
'result': 'foo'
|
||||
}, {
|
||||
'name': '%s: not first',
|
||||
'args': [ 'hello %s\n', 'world' ],
|
||||
'result': 'hello world\n'
|
||||
}, {
|
||||
'name': '%s: right-aligned',
|
||||
'args': [ 'hello %10s\n', 'world' ],
|
||||
'result': 'hello world\n'
|
||||
}, {
|
||||
'name': '%s: left-aligned',
|
||||
'args': [ 'hello %-10sagain\n', 'world' ],
|
||||
'result': 'hello world again\n'
|
||||
}, {
|
||||
'name': '%d: basic, positive',
|
||||
'args': [ '%d', 17 ],
|
||||
'result': '17'
|
||||
}, {
|
||||
'name': '%d: basic, zero',
|
||||
'args': [ '%d', 0 ],
|
||||
'result': '0'
|
||||
}, {
|
||||
'name': '%d: basic, floating point value',
|
||||
'args': [ '%d', 17.3 ],
|
||||
'result': '17'
|
||||
}, {
|
||||
'name': '%d: basic, negative',
|
||||
'args': [ '%d', -3 ],
|
||||
'result': '-3'
|
||||
}, {
|
||||
'name': '%d: right-aligned',
|
||||
'args': [ '%4d', 17 ],
|
||||
'result': ' 17'
|
||||
}, {
|
||||
'name': '%d: right-aligned, zero-padded',
|
||||
'args': [ '%04d', 17 ],
|
||||
'result': '0017'
|
||||
}, {
|
||||
'name': '%d: left-aligned',
|
||||
'args': [ '%-4d', 17 ],
|
||||
'result': '17 '
|
||||
}, {
|
||||
'name': '%x: basic',
|
||||
'args': [ '%x', 18],
|
||||
'result': '12'
|
||||
}, {
|
||||
'name': '%x: zero-padded, right-aligned',
|
||||
'args': [ '%08x', 0xfeedface ],
|
||||
'result': 'feedface'
|
||||
}, {
|
||||
'name': '%d: with plus sign',
|
||||
'args': [ '%+d', 17 ],
|
||||
'result': '+17'
|
||||
}, {
|
||||
'name': '%f: basic',
|
||||
'args': [ '%f', 3.2 ],
|
||||
'result': '3.2'
|
||||
}, {
|
||||
'name': '%f: right-aligned',
|
||||
'args': [ '%5f', 3.2 ],
|
||||
'result': ' 3.2'
|
||||
}, {
|
||||
'name': '%%: basic',
|
||||
'args': [ '%%' ],
|
||||
'result': '%'
|
||||
}, {
|
||||
'name': 'complex',
|
||||
'args': [ 'one %s %8s %-3d bytes past 0x%04x, which was %6f%%%s%5s',
|
||||
'program', 'wrote', -2, 0x30, 3.7, ' plus', 'over' ],
|
||||
'result': 'one program wrote -2 bytes past 0x0030, which was ' +
|
||||
'3.7% plus over'
|
||||
} ];
|
||||
|
||||
function main(verbose) {
|
||||
/*
|
||||
* Create one test case with a very large input string.
|
||||
*/
|
||||
var input = '1234';
|
||||
while (input.length < 100 * 1024) {
|
||||
input += input;
|
||||
}
|
||||
testcases.push({
|
||||
'name': 'long string argument (' + input.length + ' characters)',
|
||||
'args': [ '%s', input ],
|
||||
'result': input
|
||||
});
|
||||
|
||||
testcases.forEach(function (tc) {
|
||||
var result;
|
||||
console.error('test case: %s', tc.name);
|
||||
result = sprintf.apply(null, tc.args);
|
||||
if (verbose) {
|
||||
console.error(' args: %s', JSON.stringify(tc.args));
|
||||
console.error(' result: %s', result);
|
||||
}
|
||||
mod_assert.equal(tc.result, result);
|
||||
});
|
||||
|
||||
console.log('%s tests passed', mod_path.basename(__filename));
|
||||
}
|
||||
|
||||
main(process.argv.length > 2 && process.argv[2] == '-v');
|
78
node_modules/extsprintf/test/tst.invalid.js
generated
vendored
Normal file
78
node_modules/extsprintf/test/tst.invalid.js
generated
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* tst.invalid.js: tests invalid invocations
|
||||
*/
|
||||
|
||||
var mod_assert = require('assert');
|
||||
var mod_extsprintf = require('../lib/extsprintf');
|
||||
var mod_path = require('path');
|
||||
var sprintf = mod_extsprintf.sprintf;
|
||||
|
||||
var testcases = [ {
|
||||
'name': 'missing all arguments',
|
||||
'args': [],
|
||||
'errmsg': /first argument must be a format string$/
|
||||
}, {
|
||||
'name': 'missing argument for format specifier (first char and specifier)',
|
||||
'args': [ '%s' ],
|
||||
'errmsg': new RegExp(
|
||||
'format string "%s": conversion specifier "%s" at character 1 ' +
|
||||
'has no matching argument \\(too few arguments passed\\)')
|
||||
}, {
|
||||
'name': 'missing argument for format specifier (later in string)',
|
||||
'args': [ 'hello %s world %13d', 'big' ],
|
||||
'errmsg': new RegExp(
|
||||
'format string "hello %s world %13d": conversion specifier "%13d" at ' +
|
||||
'character 16 has no matching argument \\(too few arguments passed\\)')
|
||||
}, {
|
||||
'name': 'printing null as string',
|
||||
'args': [ '%d cookies %3s', 15, null ],
|
||||
'errmsg': new RegExp(
|
||||
'format string "%d cookies %3s": conversion specifier "%3s" at ' +
|
||||
'character 12 attempted to print undefined or null as a string ' +
|
||||
'\\(argument 3 to sprintf\\)')
|
||||
}, {
|
||||
'name': 'printing undefined as string',
|
||||
'args': [ '%d cookies %3s ah %d', 15, undefined, 7 ],
|
||||
'errmsg': new RegExp(
|
||||
'format string "%d cookies %3s ah %d": conversion specifier "%3s" at ' +
|
||||
'character 12 attempted to print undefined or null as a string ' +
|
||||
'\\(argument 3 to sprintf\\)')
|
||||
}, {
|
||||
'name': 'unsupported format character',
|
||||
'args': [ 'do not use %X', 13 ],
|
||||
'errmsg': new RegExp(
|
||||
'format string "do not use %X": conversion ' +
|
||||
'specifier "%X" at character 12 is not supported$')
|
||||
}, {
|
||||
'name': 'unsupported flags',
|
||||
'args': [ '%#x', 13 ],
|
||||
'errmsg': new RegExp(
|
||||
'format string "%#x": conversion ' +
|
||||
'specifier "%#x" at character 1 uses unsupported flags$')
|
||||
} ];
|
||||
|
||||
function main(verbose) {
|
||||
testcases.forEach(function (tc) {
|
||||
var error;
|
||||
console.error('test case: %s', tc.name);
|
||||
if (verbose) {
|
||||
console.error(' args: %s', JSON.stringify(tc.args));
|
||||
}
|
||||
mod_assert.throws(function () {
|
||||
try {
|
||||
sprintf.apply(null, tc.args);
|
||||
} catch (ex) {
|
||||
error = ex;
|
||||
throw (ex);
|
||||
}
|
||||
}, tc.errmsg);
|
||||
|
||||
if (verbose && error) {
|
||||
console.error(' error: %s', error.message);
|
||||
}
|
||||
});
|
||||
|
||||
console.log('%s tests passed', mod_path.basename(__filename));
|
||||
}
|
||||
|
||||
main(process.argv.length > 2 && process.argv[2] == '-v');
|
Reference in New Issue
Block a user