Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixing the handling of ranges so values are actually in the range

  • Loading branch information...
commit 17dfc7a6b9aa12eb6e3dfcdd56793a670acac548 1 parent 0e44682
@aaronpowell authored
Showing with 19 additions and 7 deletions.
  1. +1 −1  lib/tbd.js
  2. +1 −1  package.json
  3. +17 −5 tests/util-range-spec.js
View
2  lib/tbd.js
@@ -120,7 +120,7 @@
utils.range = function (min, max) {
return function () {
var random = Math.random();
- var val = Math.floor(random * (max-min));
+ var val = Math.floor(random * (max - min + 1) + min);
if(min.constructor === Date || max === Date) {
val = Math.floor(random * (max.getTime() - min.getTime()));
return new Date(max.getTime() - val);
View
2  package.json
@@ -2,7 +2,7 @@
"name":"tbd",
"description":"tbd is a test data building library, allowing you to quickly spin up large amounts of fake data to be pumped into tests",
"keywords": ["testing", "unit-testing", "data", "generator"],
- "version":"0.6.1",
+ "version":"0.6.2",
"maintainers": [{
"name": "Aaron Powell",
"email": "me@aaron-powell.com",
View
22 tests/util-range-spec.js
@@ -6,6 +6,10 @@ describe('tbd-util-range', function() {
toBeInDateRange: function(min, max) {
var actual = this.actual.getTime();
return actual <= max.getTime() && actual >= min.getTime();
+ },
+ toBeInNumericalRange: function (min, max) {
+ var actual = this.actual;
+ return actual <= max && actual >= min;
}
});
});
@@ -15,17 +19,15 @@ describe('tbd-util-range', function() {
.prop('foo').use(tbd.utils.range(0, 10))
.make(1);
expect(data.length).toBe(1);
- expect(data[0].foo).toBeGreaterThan(-1);
- expect(data[0].foo).toBeLessThan(11);
+ expect(data[0].foo).toBeInNumericalRange(0, 10);
});
it('should allow negative ranges', function () {
var data = tbd.from({})
- .prop('foo').use(tbd.utils.range(-1, -10))
+ .prop('foo').use(tbd.utils.range(-10, -1))
.make(1);
expect(data.length).toBe(1);
- expect(data[0].foo).toBeGreaterThan(-11);
- expect(data[0].foo).toBeLessThan(0);
+ expect(data[0].foo).toBeInNumericalRange(-10, -1);
});
it('should handle date ranges', function () {
@@ -38,4 +40,14 @@ describe('tbd-util-range', function() {
expect(data.length).toBe(1);
expect(data[0].foo).toBeInDateRange(min, max);
});
+
+ it('should handle large number ranges', function () {
+ var data = tbd.from({})
+ .prop('foo').use(tbd.utils.range(100, 500))
+ .make(100);
+
+ for (var i = 0, il = data.length; i < il; i++) {
+ expect(data[i].foo).toBeInNumericalRange(100, 500);
+ }
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.