UNPKG

@plugjs/expect5

Version:

Unit Testing for the PlugJS Build System ========================================

168 lines (166 loc) 5.81 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // expectation/include.ts var include_exports = {}; __export(include_exports, { toInclude: () => toInclude, toMatchContents: () => toMatchContents }); module.exports = __toCommonJS(include_exports); var import_logging = require("@plugjs/plug/logging"); var import_diff = require("./diff.cjs"); var import_types = require("./types.cjs"); function toInclude(expectations, negative, contents) { if (contents instanceof Map) return includesMappings(expectations, negative, contents); if (contents instanceof Set) return includesValues(expectations, negative, contents); if (Array.isArray(contents)) return includesValues(expectations, negative, new Set(contents)); if (contents instanceof Object) return includesProps(expectations, negative, contents); throw new TypeError(`Invalid type for "toInclude(...)": ${(0, import_types.stringifyValue)(contents)}`); } function toMatchContents(expectations, contents) { let actual; let expected; try { actual = new Set(expectations.value); expected = new Set(contents); } catch { throw new import_types.ExpectationError(expectations, "to be an iterable object"); } const result = (0, import_diff.diff)(actual, expected); delete result.error; if (!result.diff) return; throw new import_types.ExpectationError( expectations, `to match contents of ${(0, import_types.stringifyObjectType)(contents)}`, { ...result, value: expectations.value } ); } function includesProps(expectations, negative, expected) { if (expectations.value instanceof Map) { return includesMappings(expectations, negative, new Map(Object.entries(expected))); } expectations.toBeInstanceOf(Object); const actual = expectations.value; const keys = new Set(Object.keys(expected)); const props = {}; if (negative) { for (const key of keys) { if (actual[key] !== void 0 || key in actual) { props[key] = { diff: true, extra: actual[key] }; } } } else { for (const key of keys) { const act = actual[key]; const exp = expected[key]; const result = (0, import_diff.diff)(act, exp); if (!result.diff) continue; if (act === void 0 && !(key in actual)) { props[key] = { diff: true, missing: exp }; } else { props[key] = result; } } } const count = Object.keys(props).length; if (count === 0) return; const type = (0, import_logging.$plur)(count, "property", "properties", false); const not = negative ? "not " : ""; throw new import_types.ExpectationError(expectations, `${not}to include ${type}`, { diff: true, value: actual, props }); } function includesValues(expectations, negative, expected) { expectations.toBeInstanceOf(Object); if (typeof expectations.value[Symbol.iterator] !== "function") { throw new import_types.ExpectationError(expectations, "to be an iterable object"); } const actual = new Set(expectations.value); const values = []; if (negative) { for (const exp of expected) { for (const act of actual) { const result = (0, import_diff.diff)(act, exp); if (result.diff) continue; values.push({ diff: true, extra: act }); break; } } } else { for (const exp of expected) { let found = false; for (const act of actual) { const result = (0, import_diff.diff)(act, exp); if (result.diff) continue; found = true; break; } if (!found) { values.push({ diff: true, missing: exp }); } } } const count = values.length; if (count === 0) return; const type = (0, import_logging.$plur)(count, "value", "values", false); const not = negative ? "not " : ""; throw new import_types.ExpectationError(expectations, `${not}to include ${type}`, { diff: true, value: expectations.value, values }); } function includesMappings(expectations, negative, expected) { const actual = expectations.toBeInstanceOf(Map).value; const keys = new Set(expected.keys()); const mappings = []; if (negative) { for (const key of keys) { if (actual.has(key)) { mappings.push([key, { diff: true, extra: actual.get(key) }]); } } } else { for (const key of keys) { if (!actual.has(key)) { mappings.push([key, { diff: true, missing: expected.get(key) }]); } else { const result = (0, import_diff.diff)(actual.get(key), expected.get(key)); if (result.diff) mappings.push([key, result]); } } } const count = mappings.length; if (count === 0) return; const type = (0, import_logging.$plur)(count, "mapping", "mappings", false); const not = negative ? "not " : ""; throw new import_types.ExpectationError(expectations, `${not}to include ${type}`, { diff: true, value: expectations.value, mappings }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { toInclude, toMatchContents }); //# sourceMappingURL=include.cjs.map