tst.basic.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * tst.basic.js: tests various valid invocation
  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': 'empty string',
  10. 'args': [ '' ],
  11. 'result': ''
  12. }, {
  13. 'name': '%s: basic',
  14. 'args': [ '%s', 'foo' ],
  15. 'result': 'foo'
  16. }, {
  17. 'name': '%s: not first',
  18. 'args': [ 'hello %s\n', 'world' ],
  19. 'result': 'hello world\n'
  20. }, {
  21. 'name': '%s: right-aligned',
  22. 'args': [ 'hello %10s\n', 'world' ],
  23. 'result': 'hello world\n'
  24. }, {
  25. 'name': '%s: left-aligned',
  26. 'args': [ 'hello %-10sagain\n', 'world' ],
  27. 'result': 'hello world again\n'
  28. }, {
  29. 'name': '%d: basic, positive',
  30. 'args': [ '%d', 17 ],
  31. 'result': '17'
  32. }, {
  33. 'name': '%d: basic, zero',
  34. 'args': [ '%d', 0 ],
  35. 'result': '0'
  36. }, {
  37. 'name': '%d: basic, floating point value',
  38. 'args': [ '%d', 17.3 ],
  39. 'result': '17'
  40. }, {
  41. 'name': '%d: basic, negative',
  42. 'args': [ '%d', -3 ],
  43. 'result': '-3'
  44. }, {
  45. 'name': '%d: right-aligned',
  46. 'args': [ '%4d', 17 ],
  47. 'result': ' 17'
  48. }, {
  49. 'name': '%d: right-aligned, zero-padded',
  50. 'args': [ '%04d', 17 ],
  51. 'result': '0017'
  52. }, {
  53. 'name': '%d: left-aligned',
  54. 'args': [ '%-4d', 17 ],
  55. 'result': '17 '
  56. }, {
  57. 'name': '%x: basic',
  58. 'args': [ '%x', 18],
  59. 'result': '12'
  60. }, {
  61. 'name': '%x: zero-padded, right-aligned',
  62. 'args': [ '%08x', 0xfeedface ],
  63. 'result': 'feedface'
  64. }, {
  65. 'name': '%d: with plus sign',
  66. 'args': [ '%+d', 17 ],
  67. 'result': '+17'
  68. }, {
  69. 'name': '%f: basic',
  70. 'args': [ '%f', 3.2 ],
  71. 'result': '3.2'
  72. }, {
  73. 'name': '%f: right-aligned',
  74. 'args': [ '%5f', 3.2 ],
  75. 'result': ' 3.2'
  76. }, {
  77. 'name': '%%: basic',
  78. 'args': [ '%%' ],
  79. 'result': '%'
  80. }, {
  81. 'name': 'complex',
  82. 'args': [ 'one %s %8s %-3d bytes past 0x%04x, which was %6f%%%s%5s',
  83. 'program', 'wrote', -2, 0x30, 3.7, ' plus', 'over' ],
  84. 'result': 'one program wrote -2 bytes past 0x0030, which was ' +
  85. '3.7% plus over'
  86. } ];
  87. function main(verbose) {
  88. /*
  89. * Create one test case with a very large input string.
  90. */
  91. var input = '1234';
  92. while (input.length < 100 * 1024) {
  93. input += input;
  94. }
  95. testcases.push({
  96. 'name': 'long string argument (' + input.length + ' characters)',
  97. 'args': [ '%s', input ],
  98. 'result': input
  99. });
  100. testcases.forEach(function (tc) {
  101. var result;
  102. console.error('test case: %s', tc.name);
  103. result = sprintf.apply(null, tc.args);
  104. if (verbose) {
  105. console.error(' args: %s', JSON.stringify(tc.args));
  106. console.error(' result: %s', result);
  107. }
  108. mod_assert.equal(tc.result, result);
  109. });
  110. console.log('%s tests passed', mod_path.basename(__filename));
  111. }
  112. main(process.argv.length > 2 && process.argv[2] == '-v');