test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var test = require("tape");
  3. var getLength = require("./index");
  4. var browserGetLength = require("./browser");
  5. function repeat(string, times) {
  6. return new Array(times + 1).join(string);
  7. }
  8. // Test writing files to the fs
  9. //
  10. try {
  11. var blns = require("./vendor/big-list-of-naughty-strings/blns.json");
  12. }
  13. catch (err) {
  14. console.error("Error: Cannot load file './vendor/big-list-of-naughty-strings/blns.json'");
  15. console.error();
  16. console.error("Make sure you've initialized git submodules by running");
  17. console.error();
  18. console.error(" git submodule update --init");
  19. console.error();
  20. process.exit(1);
  21. }
  22. // 8-byte, 4-character string
  23. var THUMB = "👍🏽";
  24. // Tests run against both implementations
  25. [getLength, browserGetLength].forEach(function(getLength) {
  26. // Strings with known lengths
  27. [
  28. ["", 0],
  29. ["a", 1],
  30. ["☃", 3],
  31. ["a☃", 4],
  32. [repeat("a", 250) + '\uD800\uDC00', 254],
  33. [repeat("a", 251) + '\uD800\uDC00', 255],
  34. [repeat("a", 252) + '\uD800\uDC00', 256],
  35. [THUMB, 8],
  36. [THUMB[0], 3],
  37. [THUMB[1], 3],
  38. [THUMB[2], 3],
  39. [THUMB[3], 3],
  40. [THUMB.slice(0, 2), 4],
  41. [THUMB.slice(2, 4), 4],
  42. [THUMB.slice(1, 3), 6],
  43. ].forEach(function(desc) {
  44. var string = desc[0];
  45. var length = desc[1];
  46. test(JSON.stringify(string) + "=" + length, function(t) {
  47. t.equal(getLength(string), length);
  48. t.end();
  49. });
  50. });
  51. // Make sure result matches Buffer.byteLength for various strings
  52. blns.forEach(function(str) {
  53. test(JSON.stringify(str), function(t) {
  54. t.equal(getLength(str), Buffer.byteLength(str));
  55. t.end();
  56. });
  57. });
  58. });