Skip to content

Commit

Permalink
Merge branch 'wolframkriesing-allow-pure-values-in-assertThat'
Browse files Browse the repository at this point in the history
  • Loading branch information
rluba committed Aug 21, 2017
2 parents 1448771 + 94e06d6 commit d5aef8c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
36 changes: 28 additions & 8 deletions dist/hamjest.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/hamjest.min.js

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions lib/assertThat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
const _ = require('lodash');
const AssertionError = require('assertion-error');
const Description = require('./Description');
const asMatcher = require('./utils/asMatcher');

function assertThat(reason, actual, matcher) {
if (arguments.length === 2) {
matcher = actual;
actual = reason;
reason = '';
}
const processArgs = (args) => {
const hasThreeArgs = args.length === 3;
const [reason, actual, maybeMatcher] = hasThreeArgs ? args : ['', ...args];
return {reason, actual, matcher: asMatcher(maybeMatcher)};
};

const assertThat = (...args) => {
const {reason, matcher, actual} = processArgs(args);
const matches = matcher.matches(actual);

if (matches && _.isFunction(matches.then)) {
Expand All @@ -37,6 +39,6 @@ function assertThat(reason, actual, matcher) {

throw new AssertionError(description.get(), errorProperties, assertThat);
}
}
};

module.exports = assertThat;
16 changes: 16 additions & 0 deletions test/node/assertThatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ describe('assertThat', () => {
assert.ok(passedValue === input, 'Received: ' + passedValue);
});

it('should wrap non-matcher values in `equalTo()` (without message)', () => {

assert.doesNotThrow(() => __.assertThat(true, true));
assert.doesNotThrow(() => __.assertThat('some string', 'some string'));
assert.doesNotThrow(() => __.assertThat({a: 'value'}, {a: 'value'}));
__.assertThat(() => __.assertThat('some value', 'other value'), __.throws(__.instanceOf(AssertionError)));
});

it('should wrap non-matcher values in `equalTo()` (with a message)', () => {

assert.doesNotThrow(() => __.assertThat('Boolean', true, true));
assert.doesNotThrow(() => __.assertThat('String', 'some string', 'some string'));
assert.doesNotThrow(() => __.assertThat('Objects', {a: 'value'}, {a: 'value'}));
__.assertThat(() => __.assertThat('Mismatching strings', 'some value', 'other value'), __.throws(__.instanceOf(AssertionError)));
});

it('should format assertion message if matcher fails', () => {
let thrown;

Expand Down

0 comments on commit d5aef8c

Please sign in to comment.