@dmail/expect
Advanced tools
Comparing version 2.7.0 to 2.8.0
@@ -6,4 +6,6 @@ "use strict"; | ||
}); | ||
exports.expectTypeError = exports.expectError = exports.matchTypeError = exports.matchError = exports.expectConstructedBy = exports.matchConstructor = void 0; | ||
exports.expectTypeErrorWith = exports.matchTypeErrorWith = exports.expectErrorWith = exports.matchErrorWith = exports.matchConstructorWith = exports.expectTypeError = exports.matchTypeError = exports.expectError = exports.matchError = exports.expectConstructedBy = exports.matchConstructor = void 0; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _action = require("@dmail/action"); | ||
@@ -13,2 +15,4 @@ | ||
var _index = require("../index"); | ||
// const firstLetterLowerCase = name => name[0].toLowerCase() + name.slice(1) | ||
@@ -39,2 +43,16 @@ // const objectTagPrefixLength = "[object ".length | ||
// } | ||
var curry = function curry(fn) { | ||
for (var _len = arguments.length, curriedArgs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
curriedArgs[_key - 1] = arguments[_key]; | ||
} | ||
return function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return fn.apply(void 0, [].concat(curriedArgs, args)); | ||
}; | ||
}; | ||
var getConstructorName = function getConstructorName(value) { | ||
@@ -48,9 +66,4 @@ return value.constructor.name; | ||
var matchConstructor = exports.matchConstructor = function matchConstructor() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var matchConstructor = exports.matchConstructor = function matchConstructor(constructor) { | ||
return (0, _expectMatch.createMatcher)(function (value) { | ||
var constructor = args[0]; | ||
var constructorName = constructor.name; | ||
@@ -63,6 +76,2 @@ var actualConstructorName = getConstructorName(value); | ||
if (args.length > 1) { | ||
return (0, _expectMatch.expectMatch)(value, args[1]); | ||
} | ||
return (0, _action.passed)(); | ||
@@ -73,21 +82,33 @@ }); | ||
var expectConstructedBy = exports.expectConstructedBy = (0, _expectMatch.createExpectFromMatcherFactory)(matchConstructor); | ||
var matchError = exports.matchError = curry(matchConstructor, Error); | ||
var expectError = exports.expectError = (0, _expectMatch.createExpectFromMatcherFactory)(matchError); | ||
var matchTypeError = exports.matchTypeError = curry(matchConstructor, TypeError); | ||
var expectTypeError = exports.expectTypeError = (0, _expectMatch.createExpectFromMatcherFactory)(matchTypeError); | ||
var curry = function curry(fn) { | ||
for (var _len2 = arguments.length, curriedArgs = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
curriedArgs[_key2 - 1] = arguments[_key2]; | ||
var firstLetterToLowerCase = function firstLetterToLowerCase(string) { | ||
return string[0].toLowerCase() + string.slice(1); | ||
}; | ||
var augmentMismatchMessageForConstructedValue = function augmentMismatchMessageForConstructedValue(value, constructor, mismatch) { | ||
return "".concat(firstLetterToLowerCase(constructor.name), " mismatch: ").concat(mismatch); | ||
}; | ||
var matchConstructorWith = exports.matchConstructorWith = function matchConstructorWith(constructor, expected) { | ||
if (expected && _typeof(expected) === "object" && (0, _expectMatch.isMatcher)(expected) === false) { | ||
expected = (0, _index.matchProperties)(expected); | ||
} | ||
return function () { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
return fn.apply(void 0, [].concat(curriedArgs, args)); | ||
}; | ||
return (0, _expectMatch.createMatcher)(function (actual) { | ||
return expectConstructedBy(actual, constructor).then(function () { | ||
return (0, _expectMatch.expectMatch)(actual, expected).then(null, function (failure) { | ||
return augmentMismatchMessageForConstructedValue(actual, constructor, failure); | ||
}); | ||
}); | ||
}); | ||
}; | ||
var matchError = exports.matchError = curry(matchConstructor, Error); | ||
var matchTypeError = exports.matchTypeError = curry(matchConstructor, TypeError); | ||
var expectError = exports.expectError = (0, _expectMatch.createExpectFromMatcherFactory)(matchError); | ||
var expectTypeError = exports.expectTypeError = (0, _expectMatch.createExpectFromMatcherFactory)(matchTypeError); | ||
var matchErrorWith = exports.matchErrorWith = curry(matchConstructorWith, Error); | ||
var expectErrorWith = exports.expectErrorWith = (0, _expectMatch.createExpectFromMatcherFactory)(matchErrorWith); | ||
var matchTypeErrorWith = exports.matchTypeErrorWith = curry(matchConstructorWith, TypeError); | ||
var expectTypeErrorWith = exports.expectTypeErrorWith = (0, _expectMatch.createExpectFromMatcherFactory)(matchTypeErrorWith); | ||
//# sourceMappingURL=expectConstructedBy.js.map |
@@ -9,2 +9,4 @@ "use strict"; | ||
var _expectMatch = require("../expectMatch.js"); | ||
var _test = require("@dmail/test"); | ||
@@ -16,14 +18,16 @@ | ||
var _action = require("@dmail/action"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var assertPassedWith = function assertPassedWith(action, value) { | ||
_assert2.default.deepEqual(action.getResult(), value); | ||
_assert2.default.equal(action.getState(), "passed"); | ||
_assert2.default.equal(action.getResult(), value); | ||
}; | ||
var assertFailedWith = function assertFailedWith(action, value) { | ||
_assert2.default.deepEqual(action.getResult(), value); | ||
_assert2.default.equal(action.getState(), "failed"); | ||
_assert2.default.equal(action.getResult(), value); | ||
}; | ||
@@ -42,20 +46,32 @@ | ||
}, | ||
"expectError matching": function expectErrorMatching(_ref3) { | ||
"expectTypeError on new TypeError()": function expectTypeErrorOnNewTypeError(_ref3) { | ||
var pass = _ref3.pass; | ||
var error = new Error(); | ||
assertPassedWith((0, _expectConstructedBy.expectError)(error, error)); | ||
assertPassedWith((0, _expectConstructedBy.expectTypeError)(new TypeError())); | ||
pass(); | ||
}, | ||
"expectError not matching": function expectErrorNotMatching(_ref4) { | ||
"expectErrorWith() matching": function expectErrorWithMatching(_ref4) { | ||
var pass = _ref4.pass; | ||
var error = new Error(); | ||
assertFailedWith((0, _expectConstructedBy.expectError)(error, null), "Error(\"\") does not match null"); | ||
var message = "foo"; | ||
assertPassedWith((0, _expectConstructedBy.expectErrorWith)(new Error(message), { | ||
message: message | ||
}), [undefined]); | ||
pass(); | ||
}, | ||
"expectTypeError on new TypeError()": function expectTypeErrorOnNewTypeError(_ref5) { | ||
"expectErrorWith() with custom matcher": function expectErrorWithWithCustomMatcher(_ref5) { | ||
var pass = _ref5.pass; | ||
assertPassedWith((0, _expectConstructedBy.expectTypeError)(new TypeError())); | ||
var matcher = (0, _expectMatch.createMatcher)(function () { | ||
return (0, _action.failed)(10); | ||
}); | ||
assertFailedWith((0, _expectConstructedBy.expectErrorWith)(new Error(), matcher), "error mismatch: 10"); | ||
pass(); | ||
}, | ||
"expectErrorWith() not matching": function expectErrorWithNotMatching(_ref6) { | ||
var pass = _ref6.pass; | ||
var message = "foo"; | ||
assertFailedWith((0, _expectConstructedBy.expectErrorWith)(new Error(message), { | ||
message: "bar" | ||
}), "error mismatch: message property mismatch: \"foo\" does not match \"bar\""); | ||
pass(); | ||
} | ||
}); | ||
//# sourceMappingURL=expectConstructedBy.test.js.map |
{ | ||
"name": "@dmail/expect", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
import { failed, passed } from "@dmail/action" | ||
import { expectMatch, createMatcher, createExpectFromMatcherFactory } from "../expectMatch.js" | ||
import { | ||
expectMatch, | ||
createMatcher, | ||
createExpectFromMatcherFactory, | ||
isMatcher | ||
} from "../expectMatch.js" | ||
import { matchProperties } from "../index" | ||
@@ -30,9 +36,11 @@ // const firstLetterLowerCase = name => name[0].toLowerCase() + name.slice(1) | ||
const curry = (fn, ...curriedArgs) => (...args) => fn(...[...curriedArgs, ...args]) | ||
const getConstructorName = value => value.constructor.name | ||
const createFailedConstructorMessage = (actual, expected) => | ||
`expect value constructed by ${expected} but got ${actual}` | ||
const createFailedConstructorMessage = (actual, expected) => { | ||
return `expect value constructed by ${expected} but got ${actual}` | ||
} | ||
export const matchConstructor = (...args) => | ||
export const matchConstructor = constructor => | ||
createMatcher(value => { | ||
const [constructor] = args | ||
const constructorName = constructor.name | ||
@@ -43,5 +51,2 @@ const actualConstructorName = getConstructorName(value) | ||
} | ||
if (args.length > 1) { | ||
return expectMatch(value, args[1]) | ||
} | ||
return passed() | ||
@@ -51,8 +56,29 @@ }) | ||
const curry = (fn, ...curriedArgs) => (...args) => fn(...[...curriedArgs, ...args]) | ||
export const matchError = curry(matchConstructor, Error) | ||
export const expectError = createExpectFromMatcherFactory(matchError) | ||
export const matchError = curry(matchConstructor, Error) | ||
export const matchTypeError = curry(matchConstructor, TypeError) | ||
export const expectTypeError = createExpectFromMatcherFactory(matchTypeError) | ||
export const expectError = createExpectFromMatcherFactory(matchError) | ||
export const expectTypeError = createExpectFromMatcherFactory(matchTypeError) | ||
const firstLetterToLowerCase = string => string[0].toLowerCase() + string.slice(1) | ||
const augmentMismatchMessageForConstructedValue = (value, constructor, mismatch) => { | ||
return `${firstLetterToLowerCase(constructor.name)} mismatch: ${mismatch}` | ||
} | ||
export const matchConstructorWith = (constructor, expected) => { | ||
if (expected && typeof expected === "object" && isMatcher(expected) === false) { | ||
expected = matchProperties(expected) | ||
} | ||
return createMatcher(actual => | ||
expectConstructedBy(actual, constructor).then(() => | ||
expectMatch(actual, expected).then(null, failure => | ||
augmentMismatchMessageForConstructedValue(actual, constructor, failure) | ||
) | ||
) | ||
) | ||
} | ||
export const matchErrorWith = curry(matchConstructorWith, Error) | ||
export const expectErrorWith = createExpectFromMatcherFactory(matchErrorWith) | ||
export const matchTypeErrorWith = curry(matchConstructorWith, TypeError) | ||
export const expectTypeErrorWith = createExpectFromMatcherFactory(matchTypeErrorWith) |
@@ -1,13 +0,15 @@ | ||
import { expectError, expectTypeError } from "./expectConstructedBy.js" | ||
import { expectError, expectTypeError, expectErrorWith } from "./expectConstructedBy.js" | ||
import { createMatcher } from "../expectMatch.js" | ||
import { createTest } from "@dmail/test" | ||
import assert from "assert" | ||
import { failed } from "@dmail/action" | ||
const assertPassedWith = (action, value) => { | ||
assert.deepEqual(action.getResult(), value) | ||
assert.equal(action.getState(), "passed") | ||
assert.equal(action.getResult(), value) | ||
} | ||
const assertFailedWith = (action, value) => { | ||
assert.deepEqual(action.getResult(), value) | ||
assert.equal(action.getState(), "failed") | ||
assert.equal(action.getResult(), value) | ||
} | ||
@@ -27,16 +29,31 @@ | ||
}, | ||
"expectError matching": ({ pass }) => { | ||
const error = new Error() | ||
assertPassedWith(expectError(error, error)) | ||
"expectTypeError on new TypeError()": ({ pass }) => { | ||
assertPassedWith(expectTypeError(new TypeError())) | ||
pass() | ||
}, | ||
"expectError not matching": ({ pass }) => { | ||
const error = new Error() | ||
assertFailedWith(expectError(error, null), `Error("") does not match null`) | ||
"expectErrorWith() matching": ({ pass }) => { | ||
const message = "foo" | ||
assertPassedWith( | ||
expectErrorWith(new Error(message), { | ||
message | ||
}), | ||
[undefined] | ||
) | ||
pass() | ||
}, | ||
"expectTypeError on new TypeError()": ({ pass }) => { | ||
assertPassedWith(expectTypeError(new TypeError())) | ||
"expectErrorWith() with custom matcher": ({ pass }) => { | ||
const matcher = createMatcher(() => failed(10)) | ||
assertFailedWith(expectErrorWith(new Error(), matcher), "error mismatch: 10") | ||
pass() | ||
}, | ||
"expectErrorWith() not matching": ({ pass }) => { | ||
const message = "foo" | ||
assertFailedWith( | ||
expectErrorWith(new Error(message), { | ||
message: "bar" | ||
}), | ||
`error mismatch: message property mismatch: "foo" does not match "bar"` | ||
) | ||
pass() | ||
} | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
265506
3843