Skip to content

Commit

Permalink
Add settings combination example
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Feb 20, 2018
1 parent 9de313b commit 18f7546
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
13 changes: 13 additions & 0 deletions example/optional-settings-combination/createArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const createArray = function(gen, settings) {
/**
* Produces a array filled by calling gen
* settings can provide additonal parameters:
* - minimum_size: number
* - maximum_size: number
*/
const minSize = settings != null && settings.minimum_size != null ? settings.minimum_size : 0;
const maxSize = settings != null && settings.minimum_size != null ? settings.maximum_size : minSize + 10;
return [...Array(minSize + Math.floor((maxSize - minSize +1) * Math.random()))].map(gen);
};

module.exports = { createArray };
20 changes: 20 additions & 0 deletions example/optional-settings-combination/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const assert = require('assert');
const fc = require('fast-check');
const { createArray } = require('./createArray');

describe('createArray', () => {
it('should always produce an array taking care of settings', () => fc.assert(
fc.property(
fc.record({
minimum_size: fc.nat(100),
maximum_size: fc.nat(100)
}),
(settings) => {
const out = createArray(() => 0, settings);
if (settings.minimum_size != null)
assert.ok(out.length >= settings.minimum_size);
if (settings.maximum_size != null)
assert.ok(out.length <= settings.maximum_size);
})
));
});
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"test:contains": "mocha contains/test.js",
"test:knight": "mocha shadows-of-the-knight-codingame/test.js",
"test:settings": "mocha optional-settings-combination/test.js",
"test": "npm run test:contains && npm run test:knight"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion example/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ npm install

status=0

for testUnit in "contains:6:0" "knight:15:1"
for testUnit in "contains:6:0" "knight:15:1" "settings:0:1"
do
name=`echo "${testUnit}" | cut -d: -f1`
success=`echo "${testUnit}" | cut -d: -f2`
Expand Down

0 comments on commit 18f7546

Please sign in to comment.