UNPKG

unexpected-magicpen

Version:

MagicPen plugin for the Unexpected assertion library

289 lines (279 loc) 8.81 kB
var name = "unexpected-magicpen"; var version = "3.0.0"; var description = "MagicPen plugin for the Unexpected assertion library"; var main = "lib/unexpected-magicpen.js"; var scripts = { lint: "eslint . && prettier --check '**/*.{js,md}'", test: "mocha", "test:ci": "npm run coverage", coverage: "NODE_ENV=development nyc --reporter=lcov --reporter=text -- mocha --reporter dot && echo google-chrome coverage/lcov-report/index.html", prepublishOnly: "rollup -c", preversion: "offline-github-changelog --next=${npm_package_version} > CHANGELOG.md && git add CHANGELOG.md" }; var files = [ "unexpected-magicpen.esm.js", "unexpected-magicpen.min.js", "lib" ]; var repository = { type: "git", url: "git+https://github.com/unexpectedjs/unexpected-magicpen.git" }; var keywords = [ "Unexpected", "magicpen", "assertion", "style" ]; var author = "Andreas Lind <andreaslindpetersen@gmail.com>"; var license = "BSD-3-Clause"; var bugs = { url: "https://github.com/unexpectedjs/unexpected-magicpen/issues" }; var homepage = "https://github.com/unexpectedjs/unexpected-magicpen#readme"; var devDependencies = { coveralls: "^3.0.0", eslint: "^7.14.0", "eslint-config-prettier": "^6.0.0", "eslint-config-standard": "^16.0.1", "eslint-plugin-import": "^2.17.3", "eslint-plugin-mocha": "^8.0.0", "eslint-plugin-node": "^11.0.0", "eslint-plugin-promise": "^4.0.1", "eslint-plugin-standard": "^4.0.0", mocha: "^8.2.1", nyc: "^15.0.0", "offline-github-changelog": "^1.7.0", prettier: "^2.0.5", rollup: "^2.0.3", "rollup-plugin-commonjs": "^10.0.2", "rollup-plugin-json": "^4.0.0", "rollup-plugin-node-resolve": "^5.2.0", unexpected: "^12.0.0" }; var _package = { name: name, version: version, description: description, main: main, scripts: scripts, files: files, repository: repository, keywords: keywords, author: author, license: license, bugs: bugs, homepage: homepage, devDependencies: devDependencies }; var _package$1 = /*#__PURE__*/Object.freeze({ __proto__: null, name: name, version: version, description: description, main: main, scripts: scripts, files: files, repository: repository, keywords: keywords, author: author, license: license, bugs: bugs, homepage: homepage, devDependencies: devDependencies, 'default': _package }); function getCjsExportFromNamespace (n) { return n && n['default'] || n; } var require$$0 = getCjsExportFromNamespace(_package$1); var unexpectedMagicpen = { name: 'unexpected-magicpen', version: require$$0.version, installInto: function unexpectedMagicPen(expect) { expect.addType({ name: 'magicpen', identify: function (obj) { return obj && obj.isMagicPen; }, inspect: function (pen, depth, output) { output.magicPen(pen); }, equal: function (a, b) { if (a.format !== b.format) { return false; } if (a.format) { // Both have the same format return a.toString() === b.toString(); } else { // Neither have a format, test all serializations return ( a.toString() === b.toString() && a.toString('ansi') === b.toString('ansi') && a.toString('html') === b.toString('html') ); } }, }); expect.addStyle('magicPenLine', function (line, pen) { line.forEach(function (lineEntry, j) { if (j > 0) { this.nl(); } if (lineEntry.style === 'text') { var styles = lineEntry.args.styles; if ( pen && styles.length === 1 && typeof pen[styles[0]] === 'function' ) { // Text with a single style also available as a method on the pen being inspected: this.text('.') .jsFunctionName(styles[0]) .text('(') .singleQuotedString(lineEntry.args.content) .text(')'); } else { this.text('.') .jsFunctionName('text') .text('(') .singleQuotedString(lineEntry.args.content); if (styles.length > 0) { this.text(', ').appendInspected( styles.length === 1 && Array.isArray(styles[0]) ? styles[0] : styles ); } this.text(')'); } } else if (lineEntry.style === 'raw') { this.text('.') .jsFunctionName('raw') .text('(') .appendInspected(lineEntry.args.content()) .text(')'); } else { // lineEntry.style === 'block' this.text('.') .jsFunctionName('block') .text('(') .jsKeyword('function') .text(' () {'); if ( lineEntry.args && lineEntry.args.length > 0 && lineEntry.args[0] && lineEntry.args[0].length > 0 ) { this.nl() .indentLines() .i() .magicPen(pen, lineEntry.args) .outdentLines() .nl(); } this.text('})'); } }, this); }); expect.addStyle('magicPen', function (pen, lines) { var isTopLevel = !lines; lines = lines || pen.output; this.block(function () { if (isTopLevel) { this.jsFunctionName('magicpen').text('('); if (pen.format) { this.singleQuotedString(pen.format); } this.text(')'); } else { this.jsKeyword('this'); } if (!pen.isEmpty()) { var inspectOnMultipleLines = lines.length > 1 || lines[0].length > 1; if (inspectOnMultipleLines) { this.nl().indentLines().i(); } this.block(function () { lines.forEach(function (line, i) { if (i > 0) { this.text('.').jsFunctionName('nl').text('()').nl(); } this.magicPenLine(line, pen); }, this); if (!isTopLevel) { this.text(';'); } }); if (inspectOnMultipleLines) { this.outdentLines(); } } }); // If we're at the top level of a non-empty pen compatible with the current output, // render the output of the pen in a comment: if ( isTopLevel && !pen.isEmpty() && (pen.format === this.format || !pen.format) ) { this.sp().commentBlock(pen); } }); expect.addAssertion( '<Error> to have (ansi|html|text|) (message|diff) <any>', function (expect, subject, value) { expect.errorMode = 'nested'; var format = expect.alternations[0] || 'text'; var useDiff = expect.alternations[1] === 'diff'; if (subject.isUnexpected) { var subjectPen; if (useDiff) { var diff = subject.getDiff({ format: format }); if (diff) { subjectPen = diff; } else { expect.fail('The UnexpectedError instance does not have a diff'); } } else { subjectPen = subject.getErrorMessage({ format: format }); } var valueType = expect.argTypes[0]; if (valueType.is('magicpen')) { expect(subjectPen, 'to equal', value); } else if (valueType.is('function') && !valueType.is('expect.it')) { var expectedOutput = expect.createOutput(format); var returnValue = value.call(expectedOutput, subjectPen.toString()); if (!expectedOutput.isEmpty()) { // If the function didn't generate any expected output, assume that it ran assertions based on the serialized message expect(subjectPen, 'to equal', expectedOutput); } return returnValue; } else { return expect(subjectPen.toString(), 'to satisfy', value); } } else { if (useDiff) { expect.fail('Cannot get the diff from a non-Unexpected error'); } if (format !== 'text') { expect.fail( 'Cannot get the ' + format + ' representation of non-Unexpected error' ); } else { return expect(subject.message, 'to satisfy', value); } } } ); }, }; var unexpectedMagicpen_1 = unexpectedMagicpen.name; var unexpectedMagicpen_2 = unexpectedMagicpen.version; var unexpectedMagicpen_3 = unexpectedMagicpen.installInto; export default unexpectedMagicpen; export { unexpectedMagicpen_3 as installInto, unexpectedMagicpen_1 as name, unexpectedMagicpen_2 as version };