Skip to content

Commit

Permalink
fixing the handling of ranges so values are actually in the range
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell committed Dec 7, 2011
1 parent 0e44682 commit 17dfc7a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/tbd.js
Expand Up @@ -120,7 +120,7 @@
utils.range = function (min, max) { utils.range = function (min, max) {
return function () { return function () {
var random = Math.random(); 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) { if(min.constructor === Date || max === Date) {
val = Math.floor(random * (max.getTime() - min.getTime())); val = Math.floor(random * (max.getTime() - min.getTime()));
return new Date(max.getTime() - val); return new Date(max.getTime() - val);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name":"tbd", "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", "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"], "keywords": ["testing", "unit-testing", "data", "generator"],
"version":"0.6.1", "version":"0.6.2",
"maintainers": [{ "maintainers": [{
"name": "Aaron Powell", "name": "Aaron Powell",
"email": "me@aaron-powell.com", "email": "me@aaron-powell.com",
Expand Down
22 changes: 17 additions & 5 deletions tests/util-range-spec.js
Expand Up @@ -6,6 +6,10 @@ describe('tbd-util-range', function() {
toBeInDateRange: function(min, max) { toBeInDateRange: function(min, max) {
var actual = this.actual.getTime(); var actual = this.actual.getTime();
return actual <= max.getTime() && actual >= min.getTime(); return actual <= max.getTime() && actual >= min.getTime();
},
toBeInNumericalRange: function (min, max) {
var actual = this.actual;
return actual <= max && actual >= min;
} }
}); });
}); });
Expand All @@ -15,17 +19,15 @@ describe('tbd-util-range', function() {
.prop('foo').use(tbd.utils.range(0, 10)) .prop('foo').use(tbd.utils.range(0, 10))
.make(1); .make(1);
expect(data.length).toBe(1); expect(data.length).toBe(1);
expect(data[0].foo).toBeGreaterThan(-1); expect(data[0].foo).toBeInNumericalRange(0, 10);
expect(data[0].foo).toBeLessThan(11);
}); });


it('should allow negative ranges', function () { it('should allow negative ranges', function () {
var data = tbd.from({}) var data = tbd.from({})
.prop('foo').use(tbd.utils.range(-1, -10)) .prop('foo').use(tbd.utils.range(-10, -1))
.make(1); .make(1);
expect(data.length).toBe(1); expect(data.length).toBe(1);
expect(data[0].foo).toBeGreaterThan(-11); expect(data[0].foo).toBeInNumericalRange(-10, -1);
expect(data[0].foo).toBeLessThan(0);
}); });


it('should handle date ranges', function () { it('should handle date ranges', function () {
Expand All @@ -38,4 +40,14 @@ describe('tbd-util-range', function() {
expect(data.length).toBe(1); expect(data.length).toBe(1);
expect(data[0].foo).toBeInDateRange(min, max); 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);
}
});
}); });

0 comments on commit 17dfc7a

Please sign in to comment.