Skip to content

Commit

Permalink
Fix one test, minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurits Rijk committed Apr 13, 2017
1 parent 6cfd9dc commit 2b9f82f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
11 changes: 6 additions & 5 deletions lib/alt.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require('lodash');

const testcheck = require('testcheck');
const tcg = testcheck.gen;

const {gen} = require('./gen');
const isValid = require('./isValid');
Expand All @@ -9,13 +10,13 @@ function alt(...predicates) {
const pairs = _.chunk(predicates, 2);

return {
conform: values => {
const found = _.find(pairs, ([, predicate]) => isValid(predicate, values[0]));
return _.isUndefined(found) ? null : [found[0], values[0]];
conform: ([value]) => {
const found = _.find(pairs, ([, predicate]) => isValid(predicate, value));
return _.isUndefined(found) ? null : [found[0], value];
},
gen: () => testcheck.gen.bind(testcheck.gen.null, () => {
gen: () => tcg.bind(tcg.null, () => {
const result = gen(_.sample(pairs)[1]);
return testcheck.gen.array(result, 1);
return tcg.array(result, 1);
})
};
}
Expand Down
4 changes: 3 additions & 1 deletion lib/conform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const {getSpec} = require('./def');
const invalidString = ':node.spec/invalid';

function conform(spec, value) {

if (_.isFunction(spec)) {
const result = spec(value);
return result ? value : invalidString;
} else {
const predicate = getSpec(spec).conform;

const result = predicate(value);

if (_.isBoolean(result)) {
return result ? value : invalidString;
} else {
Expand Down
10 changes: 4 additions & 6 deletions test/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ describe('Test the alt function', () => {
expect(s.conform('::bool-or-string', ['foo'])).to.eql([':s', 'foo']);
});

xit('should handle list of values', () => {
s.def('::opt', s.cat(':prop', isString,
':val', s.alt(':s', isString, ':b', isBoolean)));
expect(s.conform('::opt', ['-verbose', true])).to.eql({':prop': '-verbose', ':val': [':b', true]});
it('should handle list of values', () => {
s.def('::opt', s.cat(':prop', isString, ':val', '::bool-or-string'));
expect(s.conform('::opt', ['-verbose', [true]])).to.eql({':prop': '-verbose', ':val': [':b', true]});
});

xit('should handle list of values', () => {
s.def('::config', s.star(s.cat(':prop', isString,
':val', s.alt(':s', isString, ':b', isBoolean))));
s.def('::config', s.star(s.cat(':prop', isString,':val', '::bool-or-string')));
expect(s.conform('::config', ['-server', 'foo', '-verbose', true, 'user', 'joe'])).to.eql([]);
});

Expand Down
5 changes: 5 additions & 0 deletions test/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('Test the cat function', () => {
expect(s.conform('::ingredient', [2, ':teaspoon'])).to.eql({':quantity': 2, ':unit': ':teaspoon'});
});

it('should handle nested concatenation', () => {
s.def('::named-ingredient', s.cat(':name', isString, ':ingredient', '::ingredient'));
expect(s.isValid('::named-ingredient', ['salt', [2, 'teaspoon']])).to.be.true;
});

it('should implement a generator', () => {
s.def('::ingredient', s.cat(':quantity', isInteger, ':unit', isString));

Expand Down

0 comments on commit 2b9f82f

Please sign in to comment.