Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

create-matcher.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. "use strict";
  2. var arrayProto = require("@sinonjs/commons").prototypes.array;
  3. var deepEqual = require("./deep-equal").use(createMatcher); // eslint-disable-line no-use-before-define
  4. var every = require("@sinonjs/commons").every;
  5. var functionName = require("@sinonjs/commons").functionName;
  6. var get = require("lodash.get");
  7. var iterableToString = require("./iterable-to-string");
  8. var objectProto = require("@sinonjs/commons").prototypes.object;
  9. var typeOf = require("@sinonjs/commons").typeOf;
  10. var valueToString = require("@sinonjs/commons").valueToString;
  11. var assertMatcher = require("./create-matcher/assert-matcher");
  12. var assertMethodExists = require("./create-matcher/assert-method-exists");
  13. var assertType = require("./create-matcher/assert-type");
  14. var isIterable = require("./create-matcher/is-iterable");
  15. var isMatcher = require("./create-matcher/is-matcher");
  16. var matcherPrototype = require("./create-matcher/matcher-prototype");
  17. var arrayIndexOf = arrayProto.indexOf;
  18. var some = arrayProto.some;
  19. var hasOwnProperty = objectProto.hasOwnProperty;
  20. var objectToString = objectProto.toString;
  21. var TYPE_MAP = require("./create-matcher/type-map")(createMatcher); // eslint-disable-line no-use-before-define
  22. /**
  23. * Creates a matcher object for the passed expectation
  24. *
  25. * @alias module:samsam.createMatcher
  26. * @param {*} expectation An expecttation
  27. * @param {string} message A message for the expectation
  28. * @returns {object} A matcher object
  29. */
  30. function createMatcher(expectation, message) {
  31. var m = Object.create(matcherPrototype);
  32. var type = typeOf(expectation);
  33. if (message !== undefined && typeof message !== "string") {
  34. throw new TypeError("Message should be a string");
  35. }
  36. if (arguments.length > 2) {
  37. throw new TypeError(
  38. `Expected 1 or 2 arguments, received ${arguments.length}`
  39. );
  40. }
  41. if (type in TYPE_MAP) {
  42. TYPE_MAP[type](m, expectation, message);
  43. } else {
  44. m.test = function (actual) {
  45. return deepEqual(actual, expectation);
  46. };
  47. }
  48. if (!m.message) {
  49. m.message = `match(${valueToString(expectation)})`;
  50. }
  51. return m;
  52. }
  53. createMatcher.isMatcher = isMatcher;
  54. createMatcher.any = createMatcher(function () {
  55. return true;
  56. }, "any");
  57. createMatcher.defined = createMatcher(function (actual) {
  58. return actual !== null && actual !== undefined;
  59. }, "defined");
  60. createMatcher.truthy = createMatcher(function (actual) {
  61. return Boolean(actual);
  62. }, "truthy");
  63. createMatcher.falsy = createMatcher(function (actual) {
  64. return !actual;
  65. }, "falsy");
  66. createMatcher.same = function (expectation) {
  67. return createMatcher(function (actual) {
  68. return expectation === actual;
  69. }, `same(${valueToString(expectation)})`);
  70. };
  71. createMatcher.in = function (arrayOfExpectations) {
  72. if (typeOf(arrayOfExpectations) !== "array") {
  73. throw new TypeError("array expected");
  74. }
  75. return createMatcher(function (actual) {
  76. return some(arrayOfExpectations, function (expectation) {
  77. return expectation === actual;
  78. });
  79. }, `in(${valueToString(arrayOfExpectations)})`);
  80. };
  81. createMatcher.typeOf = function (type) {
  82. assertType(type, "string", "type");
  83. return createMatcher(function (actual) {
  84. return typeOf(actual) === type;
  85. }, `typeOf("${type}")`);
  86. };
  87. createMatcher.instanceOf = function (type) {
  88. /* istanbul ignore if */
  89. if (
  90. typeof Symbol === "undefined" ||
  91. typeof Symbol.hasInstance === "undefined"
  92. ) {
  93. assertType(type, "function", "type");
  94. } else {
  95. assertMethodExists(
  96. type,
  97. Symbol.hasInstance,
  98. "type",
  99. "[Symbol.hasInstance]"
  100. );
  101. }
  102. return createMatcher(function (actual) {
  103. return actual instanceof type;
  104. }, `instanceOf(${functionName(type) || objectToString(type)})`);
  105. };
  106. /**
  107. * Creates a property matcher
  108. *
  109. * @private
  110. * @param {Function} propertyTest A function to test the property against a value
  111. * @param {string} messagePrefix A prefix to use for messages generated by the matcher
  112. * @returns {object} A matcher
  113. */
  114. function createPropertyMatcher(propertyTest, messagePrefix) {
  115. return function (property, value) {
  116. assertType(property, "string", "property");
  117. var onlyProperty = arguments.length === 1;
  118. var message = `${messagePrefix}("${property}"`;
  119. if (!onlyProperty) {
  120. message += `, ${valueToString(value)}`;
  121. }
  122. message += ")";
  123. return createMatcher(function (actual) {
  124. if (
  125. actual === undefined ||
  126. actual === null ||
  127. !propertyTest(actual, property)
  128. ) {
  129. return false;
  130. }
  131. return onlyProperty || deepEqual(actual[property], value);
  132. }, message);
  133. };
  134. }
  135. createMatcher.has = createPropertyMatcher(function (actual, property) {
  136. if (typeof actual === "object") {
  137. return property in actual;
  138. }
  139. return actual[property] !== undefined;
  140. }, "has");
  141. createMatcher.hasOwn = createPropertyMatcher(function (actual, property) {
  142. return hasOwnProperty(actual, property);
  143. }, "hasOwn");
  144. createMatcher.hasNested = function (property, value) {
  145. assertType(property, "string", "property");
  146. var onlyProperty = arguments.length === 1;
  147. var message = `hasNested("${property}"`;
  148. if (!onlyProperty) {
  149. message += `, ${valueToString(value)}`;
  150. }
  151. message += ")";
  152. return createMatcher(function (actual) {
  153. if (
  154. actual === undefined ||
  155. actual === null ||
  156. get(actual, property) === undefined
  157. ) {
  158. return false;
  159. }
  160. return onlyProperty || deepEqual(get(actual, property), value);
  161. }, message);
  162. };
  163. var jsonParseResultTypes = {
  164. null: true,
  165. boolean: true,
  166. number: true,
  167. string: true,
  168. object: true,
  169. array: true,
  170. };
  171. createMatcher.json = function (value) {
  172. if (!jsonParseResultTypes[typeOf(value)]) {
  173. throw new TypeError("Value cannot be the result of JSON.parse");
  174. }
  175. var message = `json(${JSON.stringify(value, null, " ")})`;
  176. return createMatcher(function (actual) {
  177. var parsed;
  178. try {
  179. parsed = JSON.parse(actual);
  180. } catch (e) {
  181. return false;
  182. }
  183. return deepEqual(parsed, value);
  184. }, message);
  185. };
  186. createMatcher.every = function (predicate) {
  187. assertMatcher(predicate);
  188. return createMatcher(function (actual) {
  189. if (typeOf(actual) === "object") {
  190. return every(Object.keys(actual), function (key) {
  191. return predicate.test(actual[key]);
  192. });
  193. }
  194. return (
  195. isIterable(actual) &&
  196. every(actual, function (element) {
  197. return predicate.test(element);
  198. })
  199. );
  200. }, `every(${predicate.message})`);
  201. };
  202. createMatcher.some = function (predicate) {
  203. assertMatcher(predicate);
  204. return createMatcher(function (actual) {
  205. if (typeOf(actual) === "object") {
  206. return !every(Object.keys(actual), function (key) {
  207. return !predicate.test(actual[key]);
  208. });
  209. }
  210. return (
  211. isIterable(actual) &&
  212. !every(actual, function (element) {
  213. return !predicate.test(element);
  214. })
  215. );
  216. }, `some(${predicate.message})`);
  217. };
  218. createMatcher.array = createMatcher.typeOf("array");
  219. createMatcher.array.deepEquals = function (expectation) {
  220. return createMatcher(function (actual) {
  221. // Comparing lengths is the fastest way to spot a difference before iterating through every item
  222. var sameLength = actual.length === expectation.length;
  223. return (
  224. typeOf(actual) === "array" &&
  225. sameLength &&
  226. every(actual, function (element, index) {
  227. var expected = expectation[index];
  228. return typeOf(expected) === "array" &&
  229. typeOf(element) === "array"
  230. ? createMatcher.array.deepEquals(expected).test(element)
  231. : deepEqual(expected, element);
  232. })
  233. );
  234. }, `deepEquals([${iterableToString(expectation)}])`);
  235. };
  236. createMatcher.array.startsWith = function (expectation) {
  237. return createMatcher(function (actual) {
  238. return (
  239. typeOf(actual) === "array" &&
  240. every(expectation, function (expectedElement, index) {
  241. return actual[index] === expectedElement;
  242. })
  243. );
  244. }, `startsWith([${iterableToString(expectation)}])`);
  245. };
  246. createMatcher.array.endsWith = function (expectation) {
  247. return createMatcher(function (actual) {
  248. // This indicates the index in which we should start matching
  249. var offset = actual.length - expectation.length;
  250. return (
  251. typeOf(actual) === "array" &&
  252. every(expectation, function (expectedElement, index) {
  253. return actual[offset + index] === expectedElement;
  254. })
  255. );
  256. }, `endsWith([${iterableToString(expectation)}])`);
  257. };
  258. createMatcher.array.contains = function (expectation) {
  259. return createMatcher(function (actual) {
  260. return (
  261. typeOf(actual) === "array" &&
  262. every(expectation, function (expectedElement) {
  263. return arrayIndexOf(actual, expectedElement) !== -1;
  264. })
  265. );
  266. }, `contains([${iterableToString(expectation)}])`);
  267. };
  268. createMatcher.map = createMatcher.typeOf("map");
  269. createMatcher.map.deepEquals = function mapDeepEquals(expectation) {
  270. return createMatcher(function (actual) {
  271. // Comparing lengths is the fastest way to spot a difference before iterating through every item
  272. var sameLength = actual.size === expectation.size;
  273. return (
  274. typeOf(actual) === "map" &&
  275. sameLength &&
  276. every(actual, function (element, key) {
  277. return expectation.has(key) && expectation.get(key) === element;
  278. })
  279. );
  280. }, `deepEquals(Map[${iterableToString(expectation)}])`);
  281. };
  282. createMatcher.map.contains = function mapContains(expectation) {
  283. return createMatcher(function (actual) {
  284. return (
  285. typeOf(actual) === "map" &&
  286. every(expectation, function (element, key) {
  287. return actual.has(key) && actual.get(key) === element;
  288. })
  289. );
  290. }, `contains(Map[${iterableToString(expectation)}])`);
  291. };
  292. createMatcher.set = createMatcher.typeOf("set");
  293. createMatcher.set.deepEquals = function setDeepEquals(expectation) {
  294. return createMatcher(function (actual) {
  295. // Comparing lengths is the fastest way to spot a difference before iterating through every item
  296. var sameLength = actual.size === expectation.size;
  297. return (
  298. typeOf(actual) === "set" &&
  299. sameLength &&
  300. every(actual, function (element) {
  301. return expectation.has(element);
  302. })
  303. );
  304. }, `deepEquals(Set[${iterableToString(expectation)}])`);
  305. };
  306. createMatcher.set.contains = function setContains(expectation) {
  307. return createMatcher(function (actual) {
  308. return (
  309. typeOf(actual) === "set" &&
  310. every(expectation, function (element) {
  311. return actual.has(element);
  312. })
  313. );
  314. }, `contains(Set[${iterableToString(expectation)}])`);
  315. };
  316. createMatcher.bool = createMatcher.typeOf("boolean");
  317. createMatcher.number = createMatcher.typeOf("number");
  318. createMatcher.string = createMatcher.typeOf("string");
  319. createMatcher.object = createMatcher.typeOf("object");
  320. createMatcher.func = createMatcher.typeOf("function");
  321. createMatcher.regexp = createMatcher.typeOf("regexp");
  322. createMatcher.date = createMatcher.typeOf("date");
  323. createMatcher.symbol = createMatcher.typeOf("symbol");
  324. module.exports = createMatcher;