From 89070a9b5a544801eeacd6f3c5fea83794a42570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kawalec?= Date: Fri, 28 Nov 2014 22:27:30 +0100 Subject: [PATCH] added tests and corrected the number function --- lib/random.js | 9 +++---- package.json | 59 +++++++++++++++++++++++---------------------- test/random.unit.js | 35 ++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 35 deletions(-) diff --git a/lib/random.js b/lib/random.js index 5606622c4..a4bc18585 100644 --- a/lib/random.js +++ b/lib/random.js @@ -6,7 +6,7 @@ var random = { number: function (options) { if (typeof options === "number") { - var options = { + options = { max: options }; } @@ -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; }, diff --git a/package.json b/package.json index ba0503293..4bdf20fa4 100644 --- a/package.json +++ b/package.json @@ -1,31 +1,32 @@ { - "name": "faker", - "description": "Generate massive amounts of fake contextual data", - "version": "2.1.0", - "contributors": [ - "Marak Squires ", - "Matthew Bergman " - ], - "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 ", + "Matthew Bergman " + ], + "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" } diff --git a/test/random.unit.js b/test/random.unit.js index d1112bcd9..6071767b3 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -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() { @@ -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); + }); + }); });