random.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. var assert = require("chai").assert
  3. , random = require("../../string/random");
  4. var isValidFormat = RegExp.prototype.test.bind(/^[a-z0-9]+$/);
  5. describe("string/random", function () {
  6. it("Should return string", function () { assert.equal(typeof random(), "string"); });
  7. it("Should return by default string of length 10", function () {
  8. assert.equal(random().length, 10);
  9. });
  10. it("Should support custom charset", function () {
  11. var charset = "abc";
  12. var result = random({ charset: charset });
  13. assert.equal(result.length, 10);
  14. for (var i = 0; i < result.length; ++i) {
  15. assert.isAtLeast(charset.indexOf(result.charAt(i)), 0);
  16. }
  17. });
  18. it("Should ensure unique string with `isUnique` option", function () {
  19. assert.notEqual(random({ isUnique: true }), random({ isUnique: true }));
  20. });
  21. it("Should contain only ascii chars", function () { assert(isValidFormat(random())); });
  22. it("Should support `length` option", function () {
  23. assert.equal(random({ length: 4 }).length, 4);
  24. assert.equal(random({ length: 100 }).length, 100);
  25. assert.equal(random({ length: 0 }).length, 0);
  26. });
  27. it("Should crash if unable to generate unique string with `isUnique` optin", function () {
  28. random({ length: 0, isUnique: true });
  29. assert["throws"](function () {
  30. random({ length: 0, isUnique: true });
  31. }, "Cannot generate random string");
  32. });
  33. });