test.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This program is free software. It comes without any warranty, to
  2. * the extent permitted by applicable law. You can redistribute it
  3. * and/or modify it under the terms of the Do What The Fuck You Want
  4. * To Public License, Version 2, as published by Sam Hocevar. See
  5. * http://www.wtfpl.net/ for more details. */
  6. var leftPad = require("./");
  7. var test = require("tape");
  8. var fc = require("fast-check");
  9. test('edge cases', function (assert) {
  10. assert.plan(12);
  11. assert.strictEqual(leftPad('foobar', 6), 'foobar');
  12. assert.strictEqual(leftPad('foobar', 5), 'foobar');
  13. assert.strictEqual(leftPad('foobar', -1), 'foobar');
  14. assert.strictEqual(leftPad('foobar', 6, '1'), 'foobar');
  15. assert.strictEqual(leftPad('foobar', 5, '1'), 'foobar');
  16. assert.strictEqual(leftPad('foobar', -1, '1'), 'foobar');
  17. assert.strictEqual(leftPad('foobar', 8, ''), ' foobar');
  18. assert.strictEqual(leftPad('foobar', 8, false), ' foobar', 'false default to space');
  19. assert.strictEqual(leftPad('foobar', 8, 0), '00foobar', '0 is treated as 0');
  20. assert.strictEqual(leftPad(0, 3, 1), '110', 'integer for str is converted to string');
  21. assert.strictEqual(leftPad(true, 7), ' true', 'boolean for str is converted to string');
  22. assert.strictEqual(leftPad('', 2), ' ', 'empty str for str');
  23. });
  24. test('spaces for ch', function (assert) {
  25. assert.plan(12);
  26. // default to space if not specified
  27. assert.strictEqual(leftPad('foo', 2), 'foo');
  28. assert.strictEqual(leftPad('foo', 3), 'foo');
  29. assert.strictEqual(leftPad('foo', 4), ' foo');
  30. assert.strictEqual(leftPad('foo', 5), ' foo');
  31. assert.strictEqual(leftPad('foo', 12), ' foo');
  32. assert.strictEqual(leftPad('foo', 13), ' foo');
  33. // explicit space param
  34. assert.strictEqual(leftPad('foo', 2, ' '), 'foo');
  35. assert.strictEqual(leftPad('foo', 3, ' '), 'foo');
  36. assert.strictEqual(leftPad('foo', 4, ' '), ' foo');
  37. assert.strictEqual(leftPad('foo', 5, ' '), ' foo');
  38. assert.strictEqual(leftPad('foo', 12, ' '), ' foo');
  39. assert.strictEqual(leftPad('foo', 13, ' '), ' foo');
  40. });
  41. test('non spaces for ch', function (assert) {
  42. assert.plan(7);
  43. assert.strictEqual(leftPad(1, 2, 0), '01');
  44. assert.strictEqual(leftPad(1, 2, '-'), '-1');
  45. assert.strictEqual(leftPad('foo', 4, '*'), '*foo', '0b1 len');
  46. assert.strictEqual(leftPad('foo', 5, '*'), '**foo', '0b10 len');
  47. assert.strictEqual(leftPad('foo', 6, '*'), '***foo', '0b11 len');
  48. assert.strictEqual(leftPad('foo', 7, '*'), '****foo', '0b001 len');
  49. assert.strictEqual(leftPad('foo', 103, '*'), '****************************************************************************************************foo', '100 pad');
  50. });
  51. var runProperty = function(assert, name, checkFn) {
  52. var prop = fc.property(fc.string(), fc.nat(1000), fc.char(), checkFn);
  53. var result = fc.check(prop);
  54. var message = '';
  55. if (result.failed) {
  56. message = 'Property "' + name + '" failed on counterexample ' + JSON.stringify(result.counterexample) + ' (seed: ' + result.seed + ')';
  57. }
  58. assert.strictEqual(message, '', name);
  59. };
  60. test('properties', function (assert) {
  61. assert.plan(4);
  62. runProperty(assert, 'starts by ch', function(str, len, ch) {
  63. var beg = leftPad(str, len, ch).substr(0, len -str.length);
  64. for (var idx = 0 ; idx != beg.length ; ++idx)
  65. if (beg[idx] !== ch)
  66. return false;
  67. return true;
  68. });
  69. runProperty(assert, 'ends by str', function(str, len, ch) {
  70. var out = leftPad(str, len, ch);
  71. for (var idx = 0 ; idx != str.length ; ++idx)
  72. if (str[str.length -idx -1] !== out[out.length -idx -1])
  73. return false;
  74. return true;
  75. });
  76. runProperty(assert, 'len char long if padded (unchanged otherwise)', function(str, len, ch) {
  77. var out = leftPad(str, len, ch);
  78. return str.length < len ? out.length === len : str === out;
  79. });
  80. runProperty(assert, 'no ch equivalent to space', function(str, len) {
  81. return leftPad(str, len) === leftPad(str, len, ' ');
  82. });
  83. });