Skip to content

Commit

Permalink
added tests and corrected the number function
Browse files Browse the repository at this point in the history
  • Loading branch information
mkawalec committed Nov 28, 2014
1 parent 03e10c4 commit 89070a9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
9 changes: 4 additions & 5 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var random = {
number: function (options) {

if (typeof options === "number") {
var options = {
options = {
max: options
};
}
Expand All @@ -30,10 +30,9 @@ var random = {
max += options.precision;
}

var randomNumber = Math.floor(options.precision *
mersenne.rand(max / options.precision,
options.min / options.precision)
);
var randomNumber = options.precision * Math.floor(
mersenne.rand(max / options.precision, options.min / options.precision));

return randomNumber;

},
Expand Down
59 changes: 30 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"name": "faker",
"description": "Generate massive amounts of fake contextual data",
"version": "2.1.0",
"contributors": [
"Marak Squires <[email protected]>",
"Matthew Bergman <[email protected]>"
],
"repository": {
"type": "git",
"url": "http://github.com/Marak/Faker.js.git"
},
"scripts": {
"test": "node_modules/.bin/mocha test/*.*.js"
},
"devDependencies": {
"jshint": "0.9.0",
"istanbul": "0.1.25",
"mocha": "1.8.x",
"node-minify": "*",
"optimist" : "0.3.5",
"sinon": "1.4.2",
"gulp": "3.8.8",
"browserify": "5.11.1",
"gulp-uglify": "1.0.1",
"gulp-rename": "1.2.0",
"gulp-mustache": "0.4.0",
"vinyl-transform": "0.0.1"
},
"main": "index.js"
"name": "faker",
"description": "Generate massive amounts of fake contextual data",
"version": "2.1.0",
"contributors": [
"Marak Squires <[email protected]>",
"Matthew Bergman <[email protected]>"
],
"repository": {
"type": "git",
"url": "http://github.com/Marak/Faker.js.git"
},
"scripts": {
"test": "node_modules/.bin/mocha test/*.*.js"
},
"devDependencies": {
"browserify": "5.11.1",
"gulp": "3.8.8",
"gulp-mustache": "0.4.0",
"gulp-rename": "1.2.0",
"gulp-uglify": "1.0.1",
"istanbul": "0.1.25",
"jshint": "0.9.0",
"lodash": "^2.4.1",
"mocha": "1.8.x",
"node-minify": "*",
"optimist": "0.3.5",
"sinon": "1.4.2",
"vinyl-transform": "0.0.1"
},
"main": "index.js"
}
35 changes: 34 additions & 1 deletion test/random.unit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var _ = require('lodash');
var faker = require('../index');
}


describe("random.js", function () {
describe("number", function() {

Expand All @@ -26,6 +28,37 @@ describe("random.js", function () {
assert.ok(randomNumber <= options.max);
}
});
});

it("provides numbers with a given precision", function() {
var options = { min: 0, max: 1.5, precision: 0.5 };
var results = _.chain(_.range(50))
.map(function() {
return faker.random.number(options);
})
.uniq()
.value()
.sort();

assert.ok(_.contains(results, 0.5));
assert.ok(_.contains(results, 1.0));

assert.equal(results[0], 0);
assert.equal(_.last(results), 1.5);

});

it("should not modify the input object", function() {
var min = 1;
var max = 2;
var opts = {
min: min,
max: max
};

faker.random.number(opts);

assert.equal(opts.min, min);
assert.equal(opts.max, max);
});
});
});

0 comments on commit 89070a9

Please sign in to comment.