Skip to content

Commit

Permalink
Add test for wrong lenght
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurits Rijk committed Jul 5, 2017
1 parent fc02b54 commit a988806
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@ function alt(...predicates) {
}),
describe: () => [alt.name, ...describe(predicates)],
explain: function*(value, {via}) {
yield* explainLength(predicates, value, via);
}
};
}

function* explainLength(predicates, value, via) {
if (_.isEmpty(value)) {
yield {
path: [],
reason: 'Insufficient input',
pred: ['alt', ...describe(predicates)],
val: value,
via,
'in': []
};
}
}

module.exports = alt;
15 changes: 15 additions & 0 deletions test/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ describe('Test the alt function', () => {
expect(s.conform('::bool-or-string', [true])).to.eql([':b', true]);
expect(s.conform('::bool-or-string', ['foo'])).to.eql([':s', 'foo']);
});

it('explainData should reject invalid length', () => {
expect(s.explainData('::bool-or-string', [])).to.be.eql({
problems: [
{
path: [],
reason: 'Insufficient input',
pred: ['alt', ':s', 'isString', ':b', 'isBoolean'],
val: [],
via: ['::bool-or-string'],
'in': []
}
]
});
});
});

it('should handle list of values', () => {
Expand Down

0 comments on commit a988806

Please sign in to comment.