No Description

tst.invalid.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * tst.invalid.js: tests invalid invocations
  3. */
  4. var mod_assert = require('assert');
  5. var mod_extsprintf = require('../lib/extsprintf');
  6. var mod_path = require('path');
  7. var sprintf = mod_extsprintf.sprintf;
  8. var testcases = [ {
  9. 'name': 'missing all arguments',
  10. 'args': [],
  11. 'errmsg': /first argument must be a format string$/
  12. }, {
  13. 'name': 'missing argument for format specifier (first char and specifier)',
  14. 'args': [ '%s' ],
  15. 'errmsg': new RegExp(
  16. 'format string "%s": conversion specifier "%s" at character 1 ' +
  17. 'has no matching argument \\(too few arguments passed\\)')
  18. }, {
  19. 'name': 'missing argument for format specifier (later in string)',
  20. 'args': [ 'hello %s world %13d', 'big' ],
  21. 'errmsg': new RegExp(
  22. 'format string "hello %s world %13d": conversion specifier "%13d" at ' +
  23. 'character 16 has no matching argument \\(too few arguments passed\\)')
  24. }, {
  25. 'name': 'printing null as string',
  26. 'args': [ '%d cookies %3s', 15, null ],
  27. 'errmsg': new RegExp(
  28. 'format string "%d cookies %3s": conversion specifier "%3s" at ' +
  29. 'character 12 attempted to print undefined or null as a string ' +
  30. '\\(argument 3 to sprintf\\)')
  31. }, {
  32. 'name': 'printing undefined as string',
  33. 'args': [ '%d cookies %3s ah %d', 15, undefined, 7 ],
  34. 'errmsg': new RegExp(
  35. 'format string "%d cookies %3s ah %d": conversion specifier "%3s" at ' +
  36. 'character 12 attempted to print undefined or null as a string ' +
  37. '\\(argument 3 to sprintf\\)')
  38. }, {
  39. 'name': 'unsupported format character',
  40. 'args': [ 'do not use %X', 13 ],
  41. 'errmsg': new RegExp(
  42. 'format string "do not use %X": conversion ' +
  43. 'specifier "%X" at character 12 is not supported$')
  44. }, {
  45. 'name': 'unsupported flags',
  46. 'args': [ '%#x', 13 ],
  47. 'errmsg': new RegExp(
  48. 'format string "%#x": conversion ' +
  49. 'specifier "%#x" at character 1 uses unsupported flags$')
  50. } ];
  51. function main(verbose) {
  52. testcases.forEach(function (tc) {
  53. var error;
  54. console.error('test case: %s', tc.name);
  55. if (verbose) {
  56. console.error(' args: %s', JSON.stringify(tc.args));
  57. }
  58. mod_assert.throws(function () {
  59. try {
  60. sprintf.apply(null, tc.args);
  61. } catch (ex) {
  62. error = ex;
  63. throw (ex);
  64. }
  65. }, tc.errmsg);
  66. if (verbose && error) {
  67. console.error(' error: %s', error.message);
  68. }
  69. });
  70. console.log('%s tests passed', mod_path.basename(__filename));
  71. }
  72. main(process.argv.length > 2 && process.argv[2] == '-v');