test.buffer.js 746 B

12345678910111213141516171819202122232425262728293031
  1. import test from "ava";
  2. import Chance from "../chance.js";
  3. import _ from "lodash";
  4. const chance = new Chance();
  5. // chance.buffer()
  6. test("buffer() returns a random buffer", t => {
  7. _.times(1000, () => {
  8. let buffer = chance.buffer();
  9. t.true(_.isBuffer(buffer));
  10. let len = buffer.byteLength;
  11. t.true(len >= 5);
  12. t.true(len <= 20);
  13. });
  14. });
  15. // chance.buffer()
  16. test("buffer() will obey bounds", t => {
  17. _.times(1000, () => {
  18. let buffer = chance.buffer({ length: 12 });
  19. t.true(_.isBuffer(buffer));
  20. t.is(buffer.byteLength, 12);
  21. });
  22. });
  23. // chance.buffer()
  24. test("buffer() throws if length < 0", t => {
  25. const fn = () => chance.buffer({ length: -3 });
  26. t.throws(fn, "Chance: Length cannot be less than zero.");
  27. });