Skip to content

Commit

Permalink
Mocha.seed() returns a hex value
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Sep 19, 2016
1 parent 3ff40dd commit 75b84c5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,12 @@ Mocha.prototype.randomize = function randomize(seed) {

/**
* Generate a random seed.
* @returns {number} Unsigned 32-bit integer
* @returns {string} Hexadecimal string
*/
Mocha.seed = function seed() {
return Random.integer(0, utils.MAX_SAFE_INTEGER)(Random.engines.mt19937()
.autoSeed());
var engine = Random.engines.mt19937()
.autoSeed();
return utils.toHex(Random.integer(0, utils.MAX_SAFE_INTEGER)(engine));
};

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,15 @@ exports.isPromise = function isPromise(value) {
return typeof value === 'object' && typeof value.then === 'function';
};

/**
* Given integer `int`, try to convert it to a hex string.
* @param {number} int Integer
* @returns {string} Hexadecimal representation thereof
*/
exports.toHex = function toHex(int) {
return '0x' + int.toString(16).toUpperCase();
};

/**
* Returns `true` if `value` is a "safe" 32-bit unsigned int.
* @param {*} [value] Value to test
Expand Down
12 changes: 5 additions & 7 deletions test/acceptance/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
var mocha = require('../..');
// yuk
var Mocha = mocha.Mocha || mocha;
var utils = require('../../lib/utils');

describe('random option', function() {
describe('seed', function() {
it('should return a number', function() {
it('should return a hex string', function() {
expect(Mocha.seed())
.to
.be
.a('number');
.match(/^0x[0-9A-F]+/);
});

it('should return a positive number', function() {
expect(Mocha.seed() > 0)
.to
.be(true);
it('should be a valid seed', function() {
expect(utils.toSafeUnsignedInteger(Mocha.seed())).to.be.a('number');
});

it('should return a finite number', function() {
Expand Down
6 changes: 6 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ describe('utils', function() {
});
});

describe('.toHex', function() {
it('should convert an integer to hex', function() {
utils.toHex(0xDEADBEEF).should.equal('0xDEADBEEF');
});
});

describe('.isPromise', function() {
it('should return true if the value is Promise-ish', function() {
utils.isPromise({then: function() {}}).should.be.true;
Expand Down

0 comments on commit 75b84c5

Please sign in to comment.