Skip to content

Commit

Permalink
Fix cat operator
Browse files Browse the repository at this point in the history
  • Loading branch information
mrijk committed Jun 21, 2018
1 parent c202b70 commit 1e6f234
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
7 changes: 6 additions & 1 deletion lib/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const describe = require('./util/describe');
const generate = require('./util/generate');

function _conform(specs, values) {

if (_.isEmpty(specs)) {
return _.isEmpty(values) ? {} : invalidString
}

for (let [head, rest] of generate(values)) {
const [key, predicate] = _.head(specs);
const conformHead = conform(predicate, head, true);
Expand All @@ -24,7 +29,7 @@ function _conform(specs, values) {
}
}
}
return _.isEmpty(values) && _.isEmpty(specs) ? {} : invalidString;
return invalidString;
}

function cat(...predicates) {
Expand Down
4 changes: 2 additions & 2 deletions test/and.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash');
const {every} = require('lodash');

const {expect} = require('chai');

Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Test the and function', () => {

it('should implement a generator', () => {
expect(s.exercise('::even?', 7)).to.have.length(7)
.to.satisfy(sample => _.every(sample, ([v]) => isInteger(v) && isEven(v)));
.to.satisfy(sample => every(sample, ([v]) => isInteger(v) && isEven(v)));
});

it('should implement describe', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/cat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash');
const {every} = require('lodash');

const {expect} = require('chai');;

Expand Down Expand Up @@ -46,7 +46,7 @@ describe('Test the cat function', () => {
expect(s.isValid('::ingredient', [2, 13])).to.be.false;
});

xit('should fail when no predicate supplied', () => {
it('should fail when no predicate supplied', () => {
expect(s.isValid(s.cat(), [1])).to.be.false;
});

Expand Down Expand Up @@ -107,7 +107,7 @@ describe('Test the cat function', () => {
s.def('::ingredient2', s.cat(':quantity', isInteger, ':unit', isString));

expect(s.exercise('::ingredient2', 7)).to.have.length(7)
.to.satisfy(sample => _.every(sample, ([[v1, v2]]) => isInteger(v1) || isString(v2)));
.to.satisfy(sample => every(sample, ([[v1, v2]]) => isInteger(v1) || isString(v2)));
});

it('should implement describe', () => {
Expand Down
10 changes: 5 additions & 5 deletions test/exercise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash');
const {every} = require('lodash');

const {expect} = require('chai');;

Expand All @@ -12,7 +12,7 @@ describe('Test the exercise function', () => {
});

it('should generate 10 booleans', () => {
expect(s.exercise(isBoolean)).to.have.length(10).to.satisfy(sample => _.every(sample, ([b]) => isBoolean(b)));
expect(s.exercise(isBoolean)).to.have.length(10).to.satisfy(sample => every(sample, ([b]) => isBoolean(b)));
});

it('should generate 5 random values from a set', () => {
Expand All @@ -22,12 +22,12 @@ describe('Test the exercise function', () => {

it('should generate 7 random values from a named spec', () => {
s.def('::pairs', s.cat(':n', isInteger, ':s', isString));
expect(s.exercise('::pairs', 7)).to.have.length(7).to.satisfy(sample => _.every(sample, ([[n, s]]) =>
isInteger(n) && isString(s)));
expect(s.exercise('::pairs', 7)).to.have.length(7).to.satisfy(sample => every(sample, ([[n, s]]) =>
isInteger(n) && isString(s)));
});

it('should generate 10 random values from a spec object', () => {
expect(s.exercise(s.nilable(isString))).to.have.length(10).to.satisfy(sample => _.every(sample, ([s]) => isNull(s) || isString(s)));
expect(s.exercise(s.nilable(isString))).to.have.length(10).to.satisfy(sample => every(sample, ([s]) => isNull(s) || isString(s)));
});

it('should throw an exception', () => {
Expand Down
15 changes: 11 additions & 4 deletions test/or.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const _ = require('lodash');
const {every} = require('lodash');

const {expect} = require('chai');;

const gen = require('../lib/gen');
const s = require('../lib/spec');

const {exerciseFunc, idemPotent} = require('./utils');
Expand Down Expand Up @@ -65,9 +66,15 @@ describe('Test the or function', () => {
expect(() => s.unform('::name-or-id', [':ID', 13])).to.throw(Error, 'Key :ID does not exist in spec');
});

it('should implement a generator', () => {
expect(s.exercise('::name-or-id', 7)).to.have.length(7)
.to.satisfy(sample => _.every(sample, ([v]) => isInteger(v) || isString(v)));
describe('should implement a generator', () => {
it('output nothing on spec with zero predicates', () => {
expect(() => s.exercise(s.or(), 10)).to.throw(Error, /sampleFromSpec failed.*/);
});

it('output values if the generator is valid', () => {
expect(s.exercise('::name-or-id', 7)).to.have.length(7)
.to.satisfy(sample => every(sample, ([v]) => isInteger(v) || isString(v)));
});
});

it('should implement describe', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash');
const {map, forEach, isEqual} = require('lodash');

const s = require('../lib/spec');
const stest = require('../lib/test');
Expand All @@ -15,13 +15,13 @@ function exerciseFunc(func, specPath) {

s.fdef(func, specFile);

const specs = _.map(s.exerciseFn(func), ([, s]) => s);
const specs = map(s.exerciseFn(func), ([, s]) => s);

_.forEach(specs, s.exercise);
forEach(specs, s.exercise);
}

function idemPotent(spec, value) {
return _.isEqual(s.unform(spec, s.conform(spec, value)), value);
return isEqual(s.unform(spec, s.conform(spec, value)), value);
}

module.exports = {
Expand Down

0 comments on commit 1e6f234

Please sign in to comment.