Greasy Fork is available in English.

NH_xunit

xUnit style testing.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/478188/1332408/NH_xunit.js

  1. // ==UserScript==
  2. // ==UserLibrary==
  3. // @name NH_xunit
  4. // @description xUnit style testing.
  5. // @version 53
  6. // @license GPL-3.0-or-later; https://www.gnu.org/licenses/gpl-3.0-standalone.html
  7. // @homepageURL https://github.com/nexushoratio/userscripts
  8. // @supportURL https://github.com/nexushoratio/userscripts/issues
  9. // @match https://www.example.com/*
  10. // ==/UserLibrary==
  11. // ==/UserScript==
  12.  
  13. window.NexusHoratio ??= {};
  14.  
  15. window.NexusHoratio.xunit = (function xunit() {
  16. 'use strict';
  17.  
  18. /** @type {number} - Bumped per release. */
  19. const version = 53;
  20.  
  21. /**
  22. * @type {object} - For testing support.
  23. */
  24. const testing = {
  25. enabled: false,
  26. missingDescriptionsAreErrors: true,
  27. testCases: [],
  28. };
  29.  
  30. /** Data about a test execution. */
  31. class TestExecution {
  32.  
  33. start = 0;
  34. stop = 0;
  35.  
  36. }
  37.  
  38. /** Accumulated results from running a TestCase. */
  39. class TestResult {
  40.  
  41. /** Unexpected exceptions. */
  42. errors = [];
  43.  
  44. /** Explicit test failures (typically failed asserts). */
  45. failures = [];
  46.  
  47. /** Skipped tests. */
  48. skipped = [];
  49.  
  50. /** Successes. */
  51. successes = [];
  52.  
  53. /** All test executions. */
  54. tests = new Map();
  55.  
  56. /**
  57. * Record an unexpected exception from a execution.
  58. * @param {string} name - Name of the TestCase.testMethod.
  59. * @param {Error} exception - Exception caught.
  60. */
  61. addError(name, exception) {
  62. this.errors.push({
  63. name: name,
  64. error: exception.name,
  65. message: exception.message,
  66. });
  67. }
  68.  
  69. /**
  70. * Record a test failure.
  71. * @param {string} name - Name of the TestCase.testMethod.
  72. * @param {string} message - Message from the test or framework.
  73. */
  74. addFailure(name, message) {
  75. this.failures.push({
  76. name: name,
  77. message: message,
  78. });
  79. }
  80.  
  81. /**
  82. * Record a test skipped.
  83. * @param {string} name - Name of the TestCase.testMethod.
  84. * @param {string} message - Reason the test was skipped.
  85. */
  86. addSkip(name, message) {
  87. this.skipped.push({
  88. name: name,
  89. message: message,
  90. });
  91. }
  92.  
  93. /**
  94. * Record a successful execution.
  95. * @param {string} name - Name of the TestCase.testMethod.
  96. */
  97. addSuccess(name) {
  98. this.successes.push(name);
  99. }
  100.  
  101. /**
  102. * Record the start of a test execution.
  103. * @param {string} name - Name of the TestCase.testMethod.
  104. */
  105. startTest(name) {
  106. const execution = new TestExecution();
  107. execution.start = Date.now();
  108. this.tests.set(name, execution);
  109. }
  110.  
  111. /**
  112. * Record the stop of a test execution.
  113. * @param {string} name - Name of the TestCase.testMethod.
  114. */
  115. stopTest(name) {
  116. this.tests.get(name).stop = Date.now();
  117. }
  118.  
  119. /** @returns {boolean} - Indicates success so far. */
  120. wasSuccessful() {
  121. return this.errors.length === 0 && this.failures.length === 0;
  122. }
  123.  
  124. /**
  125. * Text summary of the results.
  126. *
  127. * Useful for test runners.
  128. * @param {boolean} [formatted=false] - Try to line things up columns.
  129. * @returns {string[]} - Summary, one line per entry in the array.
  130. */
  131. summary(formatted = false) {
  132. const fields = ['total', 'successes', 'skipped', 'errors', 'failures'];
  133. const numbers = new Map();
  134. const results = [];
  135. let maxFieldLength = 0;
  136. let maxCountLength = 0;
  137. for (const field of fields) {
  138. if (field === 'total') {
  139. // Double duty: renaming 'tests' to 'total', and using '.size'
  140. numbers.set(field, this.tests.size);
  141. } else {
  142. numbers.set(field, this[field].length);
  143. }
  144. }
  145.  
  146. if (formatted) {
  147. maxFieldLength = Math.max(...Array.from(numbers.keys())
  148. .map(x => x.length));
  149. maxCountLength = String(Math.max(...numbers.values())).length;
  150. }
  151.  
  152. for (const field of fields) {
  153. const f = field.padEnd(maxFieldLength);
  154. const v = `${numbers.get(field)}`.padStart(maxCountLength);
  155. results.push(`${f} : ${v}`);
  156. }
  157. return results;
  158. }
  159.  
  160. }
  161.  
  162. /**
  163. * Attempt to get the type of item.
  164. *
  165. * This is internal to xunit, so no need to make it equivalent to the
  166. * built-in `typeof` operator. Hence, results are explicitly NOT
  167. * lower-cased in order to reduce chances of conflicts.
  168. *
  169. * This just needs to be good enough to find a comparator function.
  170. * @param {*} item - Item to inspect.
  171. * @returns {string} - The likely type of item.
  172. */
  173. function getType(item) {
  174. const builtInClasses = [
  175. 'Array',
  176. 'Date',
  177. 'Error',
  178. 'Map',
  179. 'Set',
  180. ];
  181. let type = Object.prototype.toString.call(item)
  182. .replace(/^\[object (?<type>.*)\]$/u, '$<type>');
  183. if (type === 'Function') {
  184. if (String(item)
  185. .startsWith('class ')) {
  186. type = 'class';
  187. } else if (builtInClasses.includes(item.name)) {
  188. type = 'class';
  189. }
  190. }
  191. if (type === 'Object') {
  192. if (typeof item.constructor.name === 'string') {
  193. type = item.constructor.name;
  194. }
  195. }
  196. return type;
  197. }
  198.  
  199. /**
  200. * An xUnit style test framework.
  201. *
  202. * Many expected methods exist, such as setUp, setUpClass, addCleanup,
  203. * addClassCleanup, etc. No tearDown methods, however; use addCleanup.
  204. *
  205. * Assertion methods should always take a plain text string, typically named
  206. * `msg`, as the last parameter. This string should be added to the
  207. * assertion specific error message in case of a failure.
  208. *
  209. * JavaScript does not have portable access to things like line numbers and
  210. * stack traces (and in the case of userscripts, those may be inaccurate
  211. * anyway). So it can be difficult to track down a particular test failure.
  212. * The failure messages do include the name of the test class and test
  213. * method, but, if the method happens to have several assertions in it, it
  214. * may not be obvious which one failed. These extra descriptive messages
  215. * can help with differentiation. This system will emit a debug message if
  216. * any test method calls more than one assert method without a descriptive
  217. * message.
  218. *
  219. * While the *assertEqual()* method will handle many cases by looking up
  220. * special functions comparing by type. There may be times when what it can
  221. * handle needs to be enhanced. There are currently two ways to make such
  222. * enhancements.
  223. *
  224. * First, the method *addEqualFunc()* will allow the test method to register
  225. * an additional function for comparing two identical instances.
  226. *
  227. * Second, the property *defaultEqual* points to whatever *equalX()*
  228. * function should be used if one cannot be found, or if instances differ by
  229. * type. This fallback defaults to *equalEqEqEq()* which uses the strict
  230. * equality (`===`) operator. This can be explicitly set in the test
  231. * method. The method *equalValueOf()* will use the instance's *valueOf()*
  232. * method to get comparable values, and may be useful in such cases.
  233. *
  234. * In many languages, order does not matter for some built-in container
  235. * types (e.g., Map, Set). The JavaScript standard explicitly specifies
  236. * that order DOES matter for these types. However, for this test library,
  237. * the default *equalX()* functions explicitly IGNORE order.
  238. *
  239. * Some built-in types (e.g., Map, Set), do not have good string
  240. * representations when showing up in error messages. While user classes
  241. * can provide a *toString()* method, sometimes they may not be available.
  242. * To help with this situation, this class provides a registration system
  243. * similar to the one used for equality functions.
  244. *
  245. * The property *defaultRepr* points to *String()*, but may be overridden
  246. * for an invocation.
  247. *
  248. * The method *addReprFunc()* can allow users to register their own.
  249. *
  250. * Implementations for built-in types will be added as needed.
  251. *
  252. * All *assertX()* and *equalX()* methods should use *this.repr()* to turn
  253. * values into strings for these error messages.
  254. *
  255. * TestCases should run only one test method per instance. The name of the
  256. * method is registered during instantiation and invoked by calling
  257. * *instance.run().*. Generally, a system, like {@link TestRunner} is used
  258. * to register a number of TestCases, discover the test methods, and invoke
  259. * all of them in turn.
  260. *
  261. * @example
  262. * class FooTestCase extends TestCase {
  263. * testMethod() {
  264. * // Assemble - Act
  265. *
  266. * // Assert
  267. * this.assertEqual(actual, expected, 'extra message');
  268. * }
  269. * }
  270. *
  271. * const test = new FooTestCase('testMethod');
  272. * const result = test.run();
  273. */
  274. class TestCase {
  275.  
  276. /**
  277. * Instantiate a TestCase.
  278. * @param {string} methodName - The method to run on this instantiation.
  279. */
  280. constructor(methodName) {
  281. if (new.target === TestCase) {
  282. throw new TypeError('Abstract class; do not instantiate directly.');
  283. }
  284.  
  285. this.#methodName = methodName;
  286.  
  287. this.defaultRepr = String;
  288. this.addReprFunc('String', this.reprString);
  289. this.addReprFunc('Array', this.reprArray);
  290. this.addReprFunc('Object', this.reprObject);
  291. this.addReprFunc('Map', this.reprMap);
  292. this.addReprFunc('Set', this.reprSet);
  293.  
  294. this.defaultEqual = this.equalEqEqEq;
  295. this.addEqualFunc('String', this.equalString);
  296. this.addEqualFunc('Array', this.equalArray);
  297. this.addEqualFunc('Object', this.equalObject);
  298. this.addEqualFunc('Map', this.equalMap);
  299. this.addEqualFunc('Set', this.equalSet);
  300.  
  301. this.addCleanup(this.#checkAssertionCounts);
  302. }
  303.  
  304. static Error = class extends Error {
  305.  
  306. /** @inheritdoc */
  307. constructor(...rest) {
  308. super(...rest);
  309. this.name = `TestCase.${this.constructor.name}`;
  310. }
  311.  
  312. };
  313.  
  314. static Fail = class extends this.Error {}
  315. static Skip = class extends this.Error {}
  316.  
  317. static classCleanups = [];
  318.  
  319. /** Called once before any instances are created. */
  320. static setUpClass() {
  321. // Empty.
  322. }
  323.  
  324. /**
  325. * Register a function with arguments to run after all tests in the class
  326. * have ran.
  327. * @param {function} func - Function to call.
  328. * @param {...*} rest - Arbitrary arguments to func.
  329. */
  330. static addClassCleanup(func, ...rest) {
  331. this.classCleanups.push([func, rest]);
  332. }
  333.  
  334. /** Execute all functions registered with addClassCleanup. */
  335. static doClassCleanups() {
  336. while (this.classCleanups.length) {
  337. const [func, rest] = this.classCleanups.pop();
  338. func.call(this, ...rest);
  339. }
  340. }
  341.  
  342. /** @type {string} */
  343. get id() {
  344. const methodName = this.#methodName;
  345. return `${this.constructor.name}.${methodName}`;
  346. }
  347.  
  348. /**
  349. * Execute the test method registered upon instantiation.
  350. * @param {TestResult} [result] - Instance for accumulating results.
  351. * Typically, a test runner will pass in one of these to gather results
  352. * across multiple tests.
  353. * @returns {TestResult} - Accumulated results (one is created if not
  354. * passed in).
  355. */
  356. run(result) {
  357. const localResult = result ?? new TestResult();
  358. const klass = this.constructor.name;
  359.  
  360. localResult.startTest(this.id);
  361.  
  362. let stage = null;
  363. try {
  364. stage = `${klass}.setUp`;
  365. this.setUp();
  366.  
  367. stage = this.id;
  368. this[this.#methodName]();
  369.  
  370. stage = `${klass}.doCleanups`;
  371. this.doCleanups();
  372.  
  373. localResult.addSuccess(this.id);
  374. } catch (e) {
  375. const inCleanup = stage.includes('.doCleanups');
  376. if (e instanceof TestCase.Skip && !inCleanup) {
  377. localResult.addSkip(stage, e.message);
  378. } else if (e instanceof TestCase.Fail && !inCleanup) {
  379. localResult.addFailure(stage, e.message);
  380. } else {
  381. localResult.addError(stage, e);
  382. }
  383. }
  384.  
  385. localResult.stopTest(this.id);
  386.  
  387. return localResult;
  388. }
  389.  
  390. /** Called once before each test method. */
  391. setUp() { // eslint-disable-line class-methods-use-this
  392. // Empty.
  393. }
  394.  
  395. /**
  396. * Register a function with arguments to run after a test.
  397. * @param {function} func - Function to call.
  398. * @param {...*} rest - Arbitrary arguments to func.
  399. */
  400. addCleanup(func, ...rest) {
  401. this.#cleanups.push([func, rest]);
  402. }
  403.  
  404. /** Execute all functions registered with addCleanup. */
  405. doCleanups() {
  406. while (this.#cleanups.length) {
  407. const [func, rest] = this.#cleanups.pop();
  408. func.call(this, ...rest);
  409. }
  410. }
  411.  
  412. /**
  413. * Immediately skips a test method.
  414. * @param {string} [msg=''] - Reason for skipping.
  415. * @throws {TestCase.Skip}
  416. */
  417. skip(msg = '') {
  418. throw new this.constructor.Skip(msg);
  419. }
  420.  
  421. /**
  422. * Immediately fail a test method.
  423. * @param {string} [msg=''] - Reason for the failure.
  424. * @throws {TestCase.Fail}
  425. */
  426. fail(msg = '') {
  427. throw new this.constructor.Fail(msg);
  428. }
  429.  
  430. /**
  431. * Asserts that two arguments are equal.
  432. * @param {*} first - First argument.
  433. * @param {*} second - Second argument.
  434. * @param {string} [msg=''] - Text to complement the failure message.
  435. */
  436. assertEqual(first, second, msg = '') {
  437. this.#countAsserts(msg);
  438. this.#assertBase(first, second, true, msg);
  439. }
  440.  
  441. /**
  442. * Asserts that two arguments are NOT equal.
  443. * @param {*} first - First argument.
  444. * @param {*} second - Second argument.
  445. * @param {string} [msg=''] - Text to complement the failure message.
  446. */
  447. assertNotEqual(first, second, msg = '') {
  448. this.#countAsserts(msg);
  449. this.#assertBase(first, second, false, msg);
  450. }
  451.  
  452. /**
  453. * Asserts that the argument is a boolean true.
  454. * @param {*} arg - Argument to test.
  455. * @param {string} [msg=''] - Text to complement the failure message.
  456. */
  457. assertTrue(arg, msg = '') {
  458. this.#countAsserts(msg);
  459. if (!arg) {
  460. const failMsg = `${arg} is not true`;
  461. this.#failMsgs(failMsg, msg);
  462. }
  463. }
  464.  
  465. /**
  466. * Asserts that the argument is a boolean false.
  467. * @param {*} arg - Argument to test.
  468. * @param {string} [msg=''] - Text to complement the failure message.
  469. */
  470. assertFalse(arg, msg = '') {
  471. this.#countAsserts(msg);
  472. if (arg) {
  473. const s1 = this.repr(arg);
  474. const failMsg = `${s1} is not false`;
  475. this.#failMsgs(failMsg, msg);
  476. }
  477. }
  478.  
  479. /**
  480. * Asserts the expected exception is raised.
  481. * @param {function(): Error} exc - Expected Error class.
  482. * @param {function} func - Function to call.
  483. * @param {string} [msg=''] - Text to complement the failure message.
  484. */
  485. assertRaises(exc, func, msg = '') {
  486. this.assertRaisesRegExp(exc, /.*/u, func, msg);
  487. }
  488.  
  489. /**
  490. * Asserts that no exception is raised.
  491. *
  492. * Useful for supplying descriptive text when verifying an error does not
  493. * occur.
  494. * @param {function} func - Function to call.
  495. * @param {string} [msg=''] - Text to complement the failure message.
  496. */
  497. assertNoRaises(func, msg = '') {
  498. this.#countAsserts(msg);
  499. try {
  500. func();
  501. } catch (e) {
  502. const failMsg = `Unexpected exception: ${e.name}: ${e.message}`;
  503. this.#failMsgs(failMsg, msg);
  504. }
  505. }
  506.  
  507. /**
  508. * Asserts the expected exception is raised and the message matches the
  509. * regular expression.
  510. * @param {function(): Error} exc - Expected Error class.
  511. * @param {RegExp} regexp - Regular expression to match.
  512. * @param {function} func - Function to call.
  513. * @param {string} [msg=''] - Text to complement the failure message.
  514. */
  515. assertRaisesRegExp(exc, regexp, func, msg = '') { // eslint-disable-line max-params
  516. this.#countAsserts(msg);
  517. let failMsg = `Expected ${exc.name}, caught nothing`;
  518. try {
  519. func();
  520. } catch (e) {
  521. if (e instanceof exc) {
  522. if (regexp.test(e.message)) {
  523. return;
  524. }
  525. failMsg = `Exception message:\n"${e.message}"\ndid not match ` +
  526. `regular expression:\n"${regexp}"`;
  527. } else {
  528. failMsg = `Expected ${exc.name}, caught ${e.name}`;
  529. }
  530. }
  531. this.#failMsgs(failMsg, msg);
  532. }
  533.  
  534. /**
  535. * Asserts the target matches the regular expression.
  536. * @param {string} target - Target string to check.
  537. * @param {RegExp} regexp - Regular expression to match.
  538. * @param {string} [msg=''] - Text to complement the failure message.
  539. */
  540. assertRegExp(target, regexp, msg = '') {
  541. this.#countAsserts(msg);
  542. if (!regexp.test(target)) {
  543. const failMsg = `Target "${target}" did not match ` +
  544. `regular expression "${regexp}"`;
  545. this.#failMsgs(failMsg, msg);
  546. }
  547. }
  548.  
  549. // TODO: Add assertions as needed.
  550.  
  551. /**
  552. * Returns a string representation of the item using the registration
  553. * system.
  554. *
  555. * @param {*} item - Anything.
  556. * @returns {string} - String version of item.
  557. */
  558. repr(item) {
  559. const reprFunc = this.getReprFunc(item);
  560. return reprFunc(item);
  561. }
  562.  
  563. /**
  564. * @callback ReprFunc
  565. * @param {*} item - Anything.
  566. * @returns {string} - String version of item.
  567. */
  568.  
  569. /**
  570. * Find a ReprFunc for the given item.
  571. * @param {*} item - Item of interest.
  572. * @returns {ReprFunc} - Function for this item.
  573. */
  574. getReprFunc(item) {
  575. const type = getType(item);
  576. return this.#reprFuncs.get(type) ?? this.defaultRepr;
  577. }
  578.  
  579. /**
  580. * @param {string} type - Type of interest.
  581. * @param {ReprFunc} func - Function for this type.
  582. */
  583. addReprFunc(type, func) {
  584. this.#reprFuncs.set(type, func);
  585. }
  586.  
  587. /**
  588. * @implements {ReprFunc}
  589. * @param {string} item - String to wrap.
  590. * @returns {string} - Wrapped version of item.
  591. */
  592. reprString = (item) => {
  593. const str = `"${item}"`;
  594. return str;
  595. }
  596.  
  597. /**
  598. * @implements {ReprFunc}
  599. * @param {[*]} array - Array of anything.
  600. * @returns {string} - String version of item.
  601. */
  602. reprArray = (array) => {
  603. const items = array.map(this.repr.bind(this));
  604. return `[${items.join(', ')}]`;
  605. }
  606.  
  607. /**
  608. * @implements {ReprFunc}
  609. * @param {object} obj - Any object.
  610. * @returns {string} - String version of obj.
  611. */
  612. reprObject = (obj) => {
  613. const items = [];
  614. for (const [key, value] of Object.entries(obj)) {
  615. const strKey = this.repr(key);
  616. const strValue = this.repr(value);
  617. items.push(`${strKey}: ${strValue}`);
  618. }
  619. return `{${items.join(', ')}}`;
  620. }
  621.  
  622. /**
  623. * @implements {ReprFunc}
  624. * @param {Map<*,*>} map - Any Map.
  625. * @returns {string} - String version of map.
  626. */
  627. reprMap = (map) => {
  628. const items = [];
  629. for (const [key, value] of map.entries()) {
  630. const strKey = this.repr(key);
  631. const strValue = this.repr(value);
  632. items.push(`[${strKey}, ${strValue}]`);
  633. }
  634. return `Map([${items.join(', ')}])`;
  635. }
  636.  
  637. /**
  638. * @implements {ReprFunc}
  639. * @param {Set<*>} set - Any Set.
  640. * @returns {string} - String version of set.
  641. */
  642. reprSet = (set) => {
  643. const items = [];
  644. for (const value of set.values()) {
  645. const strValue = this.repr(value);
  646. items.push(`${strValue}`);
  647. }
  648. return `Set([${items.join(', ')}])`;
  649. }
  650.  
  651. /**
  652. * @typedef {object} EqualOutput
  653. * @property {boolean} equal - Result of equality test.
  654. * @property {string} detail - Details appropriate to the test (e.g.,
  655. * where items differed).
  656. */
  657.  
  658. /**
  659. * @callback EqualFunc
  660. * @param {*} first - First argument.
  661. * @param {*} second - Second argument.
  662. * @returns {EqualOutput} - Results of testing equality.
  663. */
  664.  
  665. /**
  666. * Find an equality function appropriate for the arguments.
  667. * @param {*} first - First argument.
  668. * @param {*} second - Second argument.
  669. * @returns {EqualFunc} - Function that should be used to test equality.
  670. */
  671. getEqualFunc(first, second) {
  672. let equal = this.defaultEqual;
  673. const t1 = getType(first);
  674. const t2 = getType(second);
  675. if (t1 === t2) {
  676. equal = this.#equalFuncs.get(t1) ?? equal;
  677. }
  678. return equal;
  679. }
  680.  
  681. /**
  682. * @param {string} type - As returned from {@link getType}.
  683. * @param {EqualFunc} func - Function to call to compare that type.
  684. */
  685. addEqualFunc(type, func) {
  686. this.#equalFuncs.set(type, func);
  687. }
  688.  
  689. /**
  690. * @implements {EqualFunc}
  691. * @param {*} first - First argument.
  692. * @param {*} second - Second argument.
  693. * @returns {EqualOutput} - Results of testing equality.
  694. */
  695. equalEqEqEq = (first, second) => {
  696. const equal = first === second;
  697. return {
  698. equal: equal,
  699. details: '',
  700. };
  701. }
  702.  
  703. /**
  704. * For those cases when '===' is too strict.
  705. * @implements {EqualFunc}
  706. * @param {*} first - First argument.
  707. * @param {*} second - Second argument.
  708. * @returns {EqualOutput} - Results of testing equality.
  709. */
  710. equalValueOf = (first, second) => {
  711. const val1 = first?.valueOf() ?? first;
  712. const val2 = second?.valueOf() ?? second;
  713. const equal = val1 === val2;
  714. return {
  715. equal: equal,
  716. details: 'Using valueOf()',
  717. };
  718. }
  719.  
  720. /**
  721. * @implements {EqualFunc}
  722. * @param {*} first - First argument.
  723. * @param {*} second - Second argument.
  724. * @returns {EqualOutput} - Results of testing equality.
  725. */
  726. equalString = (first, second) => {
  727. let details = '';
  728. const equal = first === second;
  729. if (!equal) {
  730. let indicator = '';
  731. const len = Math.min(first.length, second.length);
  732. for (let idx = 0; idx < len; idx += 1) {
  733. const c1 = first.at(idx);
  734. const c2 = second.at(idx);
  735. if (c1 === c2) {
  736. indicator += ' ';
  737. } else {
  738. break;
  739. }
  740. }
  741. indicator += '|';
  742. details = `\n 1: ${first}\ndiff: ${indicator}\n 2: ${second}\n`;
  743. }
  744. return {
  745. equal: equal,
  746. details: details,
  747. };
  748. }
  749.  
  750. /**
  751. * This currently only tests Object.entries().
  752. *
  753. * Order is ignored.
  754. *
  755. * Other tests, like frozen and sealed states may be implemented later.
  756. * @implements {EqualFunc}
  757. * @param {object} first - First argument.
  758. * @param {object} second - Second argument.
  759. * @returns {EqualOutput} - Results of testing equality.
  760. */
  761. equalObject = (first, second) => {
  762. const m1 = new Map(Object.entries(first));
  763. const m2 = new Map(Object.entries(second));
  764. return this.equalMap(m1, m2);
  765. }
  766.  
  767. /**
  768. * @implements {EqualFunc}
  769. * @param {[*]} first - First argument.
  770. * @param {[*]} second - Second argument.
  771. * @returns {EqualOutput} - Results of testing equality.
  772. */
  773. equalArray = (first, second) => {
  774. let equal = true;
  775. const details = [];
  776.  
  777. const len = Math.min(first.length, second.length);
  778. for (let idx = 0; idx < len; idx += 1) {
  779. const i1 = first.at(idx);
  780. const i2 = second.at(idx);
  781. const equalFunc = this.getEqualFunc(i1, i2);
  782. const result = equalFunc(i1, i2);
  783. if (!result.equal) {
  784. equal = false;
  785. details.push(
  786. '',
  787. `First difference at element ${idx}:`,
  788. this.repr(i1),
  789. this.repr(i2)
  790. );
  791. break;
  792. }
  793. }
  794.  
  795. if (first.length !== second.length) {
  796. equal = false;
  797. const diff = Math.abs(first.length - second.length);
  798. const longest = first.length > second.length ? 'First' : 'Second';
  799. details.push(
  800. '',
  801. `${longest} array contains ${diff} more elements.`,
  802. `First additional element is at position ${len}:`,
  803. this.repr(first.at(len) ?? second.at(len))
  804. );
  805. }
  806.  
  807. return {
  808. equal: equal,
  809. details: details.join('\n'),
  810. };
  811. }
  812.  
  813. /**
  814. * Order is ignored.
  815. *
  816. * @implements {EqualFunc}
  817. * @param {Map<*,*>} first - First argument.
  818. * @param {Map<*,*>} second - Second argument.
  819. * @returns {EqualOutput} - Results of testing equality.
  820. */
  821. equalMap = (first, second) => {
  822. const m1 = this.#normalizeContainer(first);
  823. const m2 = this.#normalizeContainer(second);
  824. let equal = true;
  825. const differences = [];
  826. const missingFirst = [];
  827. const missingSecond = [];
  828.  
  829. for (const [key, val1] of m1.entries()) {
  830. if (m2.has(key)) {
  831. const val2 = m2.get(key);
  832. if (val1 !== val2) {
  833. equal = false;
  834. differences.push(
  835. '',
  836. `Difference with key: ${key}`,
  837. `Value in first : ${val1}`,
  838. `Value in second: ${val2}`,
  839. );
  840. }
  841. } else {
  842. equal = false;
  843. missingSecond.push(
  844. '',
  845. `Key missing from second: ${key}`,
  846. `Value in first : ${val1}`,
  847. );
  848. }
  849. }
  850. for (const [key, val2] of m2.entries()) {
  851. if (!m1.has(key)) {
  852. equal = false;
  853. missingFirst.push(
  854. '',
  855. `Key missing from first : ${key}`,
  856. `Value in second: ${val2}`,
  857. );
  858. }
  859. }
  860. const details = [
  861. ...differences,
  862. ...missingFirst,
  863. ...missingSecond,
  864. ];
  865.  
  866. return {
  867. equal: equal,
  868. details: details.join('\n'),
  869. };
  870. }
  871.  
  872. /**
  873. * Order is ignored.
  874. *
  875. * @implements {EqualFunc}
  876. * @param {Set<*>} first - First argument.
  877. * @param {Set<*>} second - Second argument.
  878. * @returns {EqualOutput} - Results of testing equality.
  879. */
  880. equalSet = (first, second) => {
  881. const s1 = this.#normalizeContainer(first);
  882. const s2 = this.#normalizeContainer(second);
  883.  
  884. let equal = true;
  885. const missingFirst = [];
  886. const missingSecond = [];
  887.  
  888. for (const val of s1.values()) {
  889. if (!s2.has(val)) {
  890. equal = false;
  891. missingSecond.push(
  892. '',
  893. `Value missing from second: ${val}`,
  894. );
  895. }
  896. }
  897. for (const val of s2.values()) {
  898. if (!s1.has(val)) {
  899. equal = false;
  900. missingFirst.push(
  901. '',
  902. `Value missing from first : ${val}`,
  903. );
  904. }
  905. }
  906. const details = [
  907. ...missingFirst,
  908. ...missingSecond,
  909. ];
  910.  
  911. return {
  912. equal: equal,
  913. details: details.join('\n'),
  914. };
  915. }
  916.  
  917. #assertsCalled = 0;
  918. #assertsWithNoMsg = 0;
  919. #cleanups = [];
  920. #equalFuncs = new Map();
  921. #methodName
  922. #reprFuncs = new Map();
  923.  
  924. /**
  925. * Count how many asserts are called per test method.
  926. *
  927. * Each *assertX()* method should call this first this along with its
  928. * optional *msg* parameter.
  929. * @param {string} msg - The message the assert method was given.
  930. */
  931. #countAsserts = (msg) => {
  932. this.#assertsCalled += 1;
  933. if (!msg) {
  934. this.#assertsWithNoMsg += 1;
  935. }
  936. }
  937.  
  938. #checkAssertionCounts = () => {
  939. // How many asserts must exist in the test method before caring.
  940. const MIN_ASSERTS = 2;
  941. // How many asserts are allowed to be missing descriptions.
  942. const MAX_MISSING = 1;
  943. if (this.#assertsCalled >= MIN_ASSERTS &&
  944. this.#assertsWithNoMsg > MAX_MISSING) {
  945. // eslint-disable-next-line no-console
  946. console.debug('Too many asserts without descriptions!',
  947. this.id,
  948. this.#assertsWithNoMsg);
  949. if (testing.missingDescriptionsAreErrors) {
  950. this.fail(`Too many asserts without descriptions: ${this.id}`);
  951. }
  952. }
  953. }
  954.  
  955. /**
  956. * Asserts that two arguments have the expected equality
  957. *
  958. * @param {*} first - First argument.
  959. * @param {*} second - Second argument.
  960. * @param {boolean} expected - Expectation of equality.
  961. * @param {string} [msg=''] - Text to complement the failure message.
  962. */
  963. #assertBase = (first, second, expected, msg) => { // eslint-disable-line max-params
  964. const equal = this.getEqualFunc(first, second);
  965. const results = equal(first, second);
  966. const passed = results.equal === expected;
  967. if (!passed) {
  968. const badCmp = expected ? '!==' : '===';
  969. const s1 = this.repr(first);
  970. const s2 = this.repr(second);
  971. const failMsg = `${s1} ${badCmp} ${s2}`;
  972. if (!expected) {
  973. results.details = '';
  974. }
  975. this.#failMsgs(failMsg, results.details, msg);
  976. }
  977. }
  978.  
  979. /**
  980. * Turn all keys and values in a container into a string via *repr()*.
  981. *
  982. * Containers must meet the following criteria:
  983. * + Have an *entries()* method that returns [key, value] pairs.
  984. * + Have at least one of *set(key, value)* or *add(value)* method.
  985. * + Support a constructor taking own type that results in a copy.
  986. *
  987. * @param {Map<*,*>|Set<*>} container - Or any type with similar
  988. * signatures.
  989. * @returns {Map<*,*>|Set<*>} - Clone of container with all keys and
  990. * values transformed into a string.
  991. */
  992. #normalizeContainer = (container) => {
  993. const clone = new (Object.getPrototypeOf(container)
  994. .constructor)(container);
  995. if (!clone.set) {
  996. clone.set = (k, v) => {
  997. clone.add(v);
  998. };
  999. }
  1000. clone.clear();
  1001. for (const [k, v] of container.entries()) {
  1002. const newK = this.repr(k);
  1003. const newV = this.repr(v);
  1004. clone.set(newK, newV);
  1005. }
  1006. return clone;
  1007. }
  1008.  
  1009. /**
  1010. * Immediately fail while combining messages.
  1011. * @param {...string} messages - Messages to join.
  1012. */
  1013. #failMsgs = (...messages) => {
  1014. const filtered = messages
  1015. .filter(x => x)
  1016. .map(x => String(x))
  1017. .join(' : ');
  1018. this.fail(filtered);
  1019. }
  1020.  
  1021. }
  1022.  
  1023. /* eslint-disable no-array-constructor */
  1024. /* eslint-disable no-new-wrappers */
  1025. /* eslint-disable no-undef */
  1026. /* eslint-disable no-undefined */
  1027. /* eslint-disable require-jsdoc */
  1028. class GetTypeTestCase extends TestCase {
  1029.  
  1030. testPrimitives() {
  1031. this.assertEqual(getType(0), 'Number', 'zero');
  1032. this.assertEqual(getType(NaN), 'Number', 'Nan');
  1033. this.assertEqual(getType('0'), 'String', '"0"');
  1034. this.assertEqual(getType(true), 'Boolean', 'true');
  1035. this.assertEqual(getType(false), 'Boolean', 'false');
  1036. this.assertEqual(getType(BigInt('123')), 'BigInt', 'BigInt()');
  1037. this.assertEqual(getType(456n), 'BigInt', '456n');
  1038. this.assertEqual(getType(undefined), 'Undefined', 'undefined');
  1039. this.assertEqual(getType(null), 'Null', 'null');
  1040. }
  1041.  
  1042. testBuiltInFunctionLike() {
  1043. this.assertEqual(getType(String('xyzzy')), 'String', 'string-xyzzy');
  1044. this.assertEqual(getType(new String('abc')), 'String', 'string-abc');
  1045. this.assertEqual(getType(String), 'Function', 'bare String');
  1046. this.assertEqual(getType(Symbol('xyzzy')), 'Symbol', 'symbol');
  1047. this.assertEqual(getType(Symbol), 'Function', 'bare Symbol');
  1048. this.assertEqual(getType(/abc123/u), 'RegExp', '/regexp/');
  1049. this.assertEqual(getType(new Date()), 'Date', 'new Date');
  1050. this.assertEqual(getType(Date()), 'String', 'Date()');
  1051. this.assertEqual(getType(Date), 'class', 'bare Date');
  1052. this.assertEqual(getType(Math.min), 'Function', 'math.min');
  1053. this.assertEqual(getType(Math), 'Math', 'Math');
  1054. }
  1055.  
  1056. testBuiltinClasses() {
  1057. this.assertEqual(getType({}), 'Object', '{}');
  1058. this.assertEqual(getType([]), 'Array', '[]');
  1059. this.assertEqual(getType(new Array()), 'Array', 'new array');
  1060. this.assertEqual(getType(Array), 'class', 'bare Array');
  1061. this.assertEqual(getType(new Map()), 'Map', 'map');
  1062. this.assertEqual(getType(Map), 'class', 'bare Map');
  1063. this.assertEqual(getType(new Set()), 'Set', 'set');
  1064. this.assertEqual(getType(Set), 'class', 'bare Set');
  1065. this.assertEqual(getType(new Error()), 'Error', 'error');
  1066. this.assertEqual(getType(Error), 'class', 'bare Error');
  1067. }
  1068.  
  1069. testRegularClasses() {
  1070. this.assertEqual(getType(TestCase), 'class', 'bare TestCase');
  1071. this.assertEqual(getType(this), 'GetTypeTestCase', 'this');
  1072. this.assertEqual(getType(getType), 'Function', 'bare getType');
  1073. this.assertEqual(getType(TestCase.Skip), 'class', 'nested class');
  1074. }
  1075.  
  1076. }
  1077. /* eslint-enable */
  1078.  
  1079. testing.testCases.push(GetTypeTestCase);
  1080.  
  1081. /* eslint-disable class-methods-use-this */
  1082. /* eslint-disable no-magic-numbers */
  1083. /* eslint-disable require-jsdoc */
  1084. /**
  1085. * For testing TestCase basic features.
  1086. *
  1087. * Do not use directly, but rather inside `TestCaseTestCase`.
  1088. */
  1089. class BasicFeaturesTestCase extends TestCase {
  1090.  
  1091. static classCalls = [];
  1092.  
  1093. /** Register cleanup functions.. */
  1094. static setUpClassCleanups() {
  1095. this.classCalls = [];
  1096. this.addClassCleanup(this.one);
  1097. this.addClassCleanup(this.two, 3, 4);
  1098. }
  1099.  
  1100. /** Capture that it was called. */
  1101. static one() {
  1102. this.classCalls.push('one');
  1103. }
  1104.  
  1105. /**
  1106. * Capture that it was called with arguments.
  1107. * @param {*} a - Anything.
  1108. * @param {*} b - Anything.
  1109. */
  1110. static two(a, b) {
  1111. this.classCalls.push('two', a, b);
  1112. }
  1113.  
  1114. testInstanceCleanups() {
  1115. this.instanceCalls = [];
  1116. this.addCleanup(this.three);
  1117. this.addCleanup(this.four, 5, 6);
  1118. }
  1119.  
  1120. /** Capture that it was called. */
  1121. three() {
  1122. this.instanceCalls.push('three');
  1123. }
  1124.  
  1125. /**
  1126. * Capture that it was called with arguments.
  1127. * @param {*} a - Anything.
  1128. * @param {*} b - Anything.
  1129. */
  1130. four(a, b) {
  1131. this.instanceCalls.push('four', a, b);
  1132. }
  1133.  
  1134. testInstanceCleanupsWithError() {
  1135. this.addCleanup(this.willError);
  1136. }
  1137.  
  1138. testInstanceCleanupsWithSkip() {
  1139. this.addCleanup(this.willSkip);
  1140. }
  1141.  
  1142. testInstanceCleanupsWithFail() {
  1143. this.addCleanup(this.willFail);
  1144. }
  1145.  
  1146. willError() {
  1147. throw new Error('from willError');
  1148. }
  1149.  
  1150. willSkip() {
  1151. this.skip('from willSkip');
  1152. }
  1153.  
  1154. willFail() {
  1155. this.fail('from willFail');
  1156. }
  1157.  
  1158. }
  1159. /* eslint-enable */
  1160.  
  1161. /* eslint-disable max-lines-per-function */
  1162. /* eslint-disable max-statements */
  1163. /* eslint-disable no-array-constructor */
  1164. /* eslint-disable no-empty-function */
  1165. /* eslint-disable no-magic-numbers */
  1166. /* eslint-disable no-new */
  1167. /* eslint-disable no-new-wrappers */
  1168. /* eslint-disable no-undef */
  1169. /* eslint-disable no-undefined */
  1170. /* eslint-disable no-unused-vars */
  1171. /* eslint-disable require-jsdoc */
  1172. class TestCaseTestCase extends TestCase {
  1173.  
  1174. testCannotInstantiateDirectly() {
  1175. this.assertRaises(TypeError, () => {
  1176. new TestCase();
  1177. });
  1178. }
  1179.  
  1180. testStaticSetUpClassExists() {
  1181. this.assertNoRaises(() => {
  1182. TestCase.setUpClass();
  1183. });
  1184. }
  1185.  
  1186. testDoClassCleanups() {
  1187. // Assemble
  1188. BasicFeaturesTestCase.setUpClassCleanups();
  1189.  
  1190. // Act
  1191. BasicFeaturesTestCase.doClassCleanups();
  1192.  
  1193. // Assert
  1194. const actual = BasicFeaturesTestCase.classCalls;
  1195. const expected = ['two', 3, 4, 'one'];
  1196. this.assertEqual(actual, expected);
  1197. }
  1198.  
  1199. testId() {
  1200. // Assemble
  1201. const instance = new BasicFeaturesTestCase('testSomething');
  1202.  
  1203. // Assert
  1204. const actual = instance.id;
  1205. const expected = 'BasicFeaturesTestCase.testSomething';
  1206. this.assertEqual(actual, expected);
  1207. }
  1208.  
  1209. testDoInstanceCleanups() {
  1210. // Assemble
  1211. const method = 'testInstanceCleanups';
  1212. const instance = new BasicFeaturesTestCase(method);
  1213.  
  1214. // Act
  1215. const result = instance.run();
  1216.  
  1217. // Assert
  1218. this.assertTrue(result.wasSuccessful(), 'success');
  1219. // Next assert has timestamps in it.
  1220. this.assertEqual(result.tests.size, 1, 'tests collected');
  1221. const actual = instance.instanceCalls;
  1222. const expected = ['four', 5, 6, 'three'];
  1223. this.assertEqual(actual, expected, 'calls');
  1224. }
  1225.  
  1226. testDoInstanceCleanupsWithError() {
  1227. // Assemble
  1228. const method = 'testInstanceCleanupsWithError';
  1229. const instance = new BasicFeaturesTestCase(method);
  1230.  
  1231. // Act
  1232. const result = instance.run();
  1233.  
  1234. // Assert
  1235. this.assertFalse(result.wasSuccessful(), 'success');
  1236. // Next assert has timestamps in it.
  1237. this.assertEqual(result.tests.size, 1, 'tests collected');
  1238. this.assertEqual(
  1239. result.errors,
  1240. [
  1241. {
  1242. name: 'BasicFeaturesTestCase.doCleanups',
  1243. error: 'Error',
  1244. message: 'from willError',
  1245. },
  1246. ],
  1247. 'errors'
  1248. );
  1249. }
  1250.  
  1251. testDoInstanceCleanupsWithSkip() {
  1252. // Assemble
  1253. const method = 'testInstanceCleanupsWithSkip';
  1254. const instance = new BasicFeaturesTestCase(method);
  1255.  
  1256. // Act
  1257. const result = instance.run();
  1258.  
  1259. // Assert
  1260. this.assertFalse(result.wasSuccessful(), 'success');
  1261. // Next assert has timestamps in it.
  1262. this.assertEqual(result.tests.size, 1, 'tests collected');
  1263. this.assertEqual(
  1264. result.errors,
  1265. [
  1266. {
  1267. name: 'BasicFeaturesTestCase.doCleanups',
  1268. error: 'TestCase.Skip',
  1269. message: 'from willSkip',
  1270. },
  1271. ],
  1272. 'errors'
  1273. );
  1274. }
  1275.  
  1276. testDoInstanceCleanupsWithFail() {
  1277. // Assemble
  1278. const method = 'testInstanceCleanupsWithFail';
  1279. const instance = new BasicFeaturesTestCase(method);
  1280.  
  1281. // Act
  1282. const result = instance.run();
  1283.  
  1284. // Assert
  1285. this.assertFalse(result.wasSuccessful(), 'success');
  1286. // Next assert has timestamps in it.
  1287. this.assertEqual(result.tests.size, 1, 'tests collected');
  1288. this.assertEqual(
  1289. result.errors,
  1290. [
  1291. {
  1292. name: 'BasicFeaturesTestCase.doCleanups',
  1293. error: 'TestCase.Fail',
  1294. message: 'from willFail',
  1295. },
  1296. ],
  1297. 'errors'
  1298. );
  1299. }
  1300.  
  1301. testCollectTests() {
  1302. // Assemble
  1303. const result = new TestResult();
  1304. const methods = [
  1305. 'testInstanceCleanups',
  1306. 'testInstanceCleanupsWithError',
  1307. 'testInstanceCleanupsWithSkip',
  1308. 'testInstanceCleanupsWithFail',
  1309. ];
  1310.  
  1311. // Act
  1312. for (const method of methods) {
  1313. const instance = new BasicFeaturesTestCase(method);
  1314. instance.run(result);
  1315. }
  1316.  
  1317. // Assert
  1318. // Next assert has timestamps in it.
  1319. this.assertEqual(result.tests.size, 4);
  1320. }
  1321.  
  1322. testSkip() {
  1323. // Act/Assert
  1324. this.assertRaisesRegExp(
  1325. TestCase.Skip,
  1326. /^$/u,
  1327. () => {
  1328. this.skip();
  1329. },
  1330. 'basic skip'
  1331. );
  1332.  
  1333. // Act/Assert
  1334. this.assertRaisesRegExp(
  1335. TestCase.Skip,
  1336. /a message/u,
  1337. () => {
  1338. this.skip('a message');
  1339. },
  1340. 'with a message'
  1341. );
  1342. }
  1343.  
  1344. testFail() {
  1345. // Act/Assert
  1346. this.assertRaisesRegExp(
  1347. TestCase.Fail,
  1348. /^$/u,
  1349. () => {
  1350. this.fail();
  1351. },
  1352. 'no description'
  1353. );
  1354.  
  1355. // Act/Assert
  1356. this.assertRaisesRegExp(
  1357. TestCase.Fail,
  1358. /for the masses/u,
  1359. () => {
  1360. this.fail('for the masses');
  1361. },
  1362. 'with description'
  1363. );
  1364. }
  1365.  
  1366. testGetReprFunc() {
  1367. this.assertEqual(this.getReprFunc(null), String, 'null');
  1368. this.assertEqual(this.getReprFunc(undefined), String, 'undefined');
  1369. this.assertEqual(this.getReprFunc(1), String, 'number');
  1370. this.assertEqual(this.getReprFunc(''), this.reprString, 'string');
  1371. this.assertEqual(this.getReprFunc([]), this.reprArray, 'array');
  1372. this.assertEqual(this.getReprFunc({}), this.reprObject, 'object');
  1373. this.assertEqual(this.getReprFunc(new Map()), this.reprMap, 'map');
  1374. this.assertEqual(this.getReprFunc(new Set()), this.reprSet, 'set');
  1375. }
  1376.  
  1377. testChangingDefaultRepr() {
  1378. // Assemble
  1379. function x() {}
  1380.  
  1381. // Act
  1382. this.defaultRepr = x;
  1383.  
  1384. // Assert
  1385. this.assertEqual(this.getReprFunc(null), x, 'null');
  1386. this.assertEqual(this.getReprFunc(undefined), x, 'undefined');
  1387. this.assertEqual(this.getReprFunc(1), x, 'number');
  1388. this.assertEqual(this.getReprFunc(''), this.reprString, 'string');
  1389. this.assertEqual(this.getReprFunc([]), this.reprArray, 'array');
  1390. this.assertEqual(this.getReprFunc({}), this.reprObject, 'object');
  1391. this.assertEqual(this.getReprFunc(new Map()), this.reprMap, 'map');
  1392. this.assertEqual(this.getReprFunc(new Set()), this.reprSet, 'set');
  1393. }
  1394.  
  1395. testAddReprFunc() {
  1396. // Assemble
  1397. class C {}
  1398. const c = new C();
  1399. function reprC(item) {}
  1400.  
  1401. this.assertNotEqual(this.getReprFunc(c), reprC, 'no reprC');
  1402.  
  1403. // Act
  1404. this.addReprFunc('C', reprC);
  1405.  
  1406. // Assert
  1407. this.assertEqual(this.getReprFunc(c), reprC, 'found reprC');
  1408. this.assertEqual(this.getReprFunc(null), String, 'null');
  1409. this.assertEqual(this.getReprFunc(undefined), String, 'undefined');
  1410. this.assertEqual(this.getReprFunc(1), String, 'number');
  1411. this.assertEqual(this.getReprFunc(''), this.reprString, 'string');
  1412. this.assertEqual(
  1413. this.getReprFunc(new String('str')), this.reprString, 'new string'
  1414. );
  1415. this.assertEqual(this.getReprFunc([]), this.reprArray, 'array');
  1416. this.assertEqual(
  1417. this.getReprFunc(new Array(1, 2, 3)), this.reprArray, 'new array'
  1418. );
  1419. this.assertEqual(this.getReprFunc({}), this.reprObject, 'object');
  1420. this.assertEqual(this.getReprFunc(new Map()), this.reprMap, 'map');
  1421. this.assertEqual(this.getReprFunc(new Set()), this.reprSet, 'set');
  1422. }
  1423.  
  1424. testReprPrimitives() {
  1425. this.assertEqual(this.repr(1), '1', 'number');
  1426. this.assertEqual(this.repr(null), 'null', 'null');
  1427. this.assertEqual(this.repr(undefined), 'undefined', 'undefined');
  1428. this.assertEqual(
  1429. this.repr(Symbol('qwerty')),
  1430. 'Symbol(qwerty)',
  1431. 'symbol'
  1432. );
  1433. }
  1434.  
  1435. testReprString() {
  1436. this.assertEqual(this.repr('a. b'), '"a. b"', 'string');
  1437. this.assertEqual(this.repr(new String('xyz')), '"xyz"', 'new string');
  1438. }
  1439.  
  1440. testReprArray() {
  1441. this.assertEqual(this.repr(['b', 2]), '["b", 2]', 'mixed array');
  1442. this.assertEqual(
  1443. this.repr(['b', [1, '2']]),
  1444. '["b", [1, "2"]]',
  1445. 'nested array'
  1446. );
  1447. this.assertEqual(
  1448. this.repr(new Array(1, '2', 'three')),
  1449. '[1, "2", "three"]',
  1450. 'new array'
  1451. );
  1452. }
  1453.  
  1454. testReprObject() {
  1455. this.assertEqual(this.repr({a: '1'}), '{"a": "1"}', 'simple');
  1456. this.assertEqual(
  1457. this.repr({b: {c: 'd', e: 1}}),
  1458. '{"b": {"c": "d", "e": 1}}',
  1459. 'nested'
  1460. );
  1461. }
  1462.  
  1463. testReprMap() {
  1464. this.assertEqual(this.repr(new Map()), 'Map([])', 'empty');
  1465. this.assertEqual(
  1466. this.repr(new Map([])),
  1467. 'Map([])',
  1468. 'empty init'
  1469. );
  1470. this.assertEqual(
  1471. this.repr(new Map([[1, 'one'], ['two', 2]])),
  1472. 'Map([[1, "one"], ["two", 2]])',
  1473. 'with items'
  1474. );
  1475. this.assertEqual(
  1476. this.repr(new Map([[1, 'one'], ['map', new Map([['x', 3]])]])),
  1477. 'Map([[1, "one"], ["map", Map([["x", 3]])]])',
  1478. 'nested'
  1479. );
  1480. }
  1481.  
  1482. testReprSet() {
  1483. this.assertEqual(this.repr(new Set()), 'Set([])', 'empty');
  1484. this.assertEqual(
  1485. this.repr(new Set([])),
  1486. 'Set([])',
  1487. 'empty init'
  1488. );
  1489. this.assertEqual(
  1490. this.repr(new Set([1, 'b', 'b', 'xyz', 99])),
  1491. 'Set([1, "b", "xyz", 99])',
  1492. 'with items'
  1493. );
  1494. this.assertEqual(
  1495. this.repr(new Set([1, new Set(['x', 3]), 'qqq'])),
  1496. 'Set([1, Set(["x", 3]), "qqq"])',
  1497. 'nested'
  1498. );
  1499. }
  1500.  
  1501. testGetEqualFunc() {
  1502. this.assertEqual(
  1503. this.getEqualFunc({}, []),
  1504. this.equalEqEqEq,
  1505. 'obj vs array'
  1506. );
  1507. this.assertEqual(
  1508. this.getEqualFunc('a', 'b'),
  1509. this.equalString,
  1510. 'str vs str'
  1511. );
  1512. this.assertEqual(
  1513. this.getEqualFunc('a', new String('b')),
  1514. this.equalString,
  1515. 'str vs new str'
  1516. );
  1517. }
  1518.  
  1519. testChangingDefaultEqual() {
  1520. // Assemble
  1521. this.assertEqual(this.getEqualFunc({}, []), this.equalEqEqEq, '===');
  1522. this.defaultEqual = this.equalValueOf;
  1523.  
  1524. // Act/Assert
  1525. this.assertEqual(
  1526. this.getEqualFunc({}, []),
  1527. this.equalValueOf,
  1528. 'valueOf'
  1529. );
  1530. }
  1531.  
  1532. testAddEqualFunc() {
  1533. // Assemble
  1534. class C {}
  1535. const c = new C();
  1536. function equalC(first, second) {}
  1537.  
  1538. this.assertNotEqual(this.getEqualFunc(c, c), equalC, 'not equalC');
  1539.  
  1540. // Act
  1541. this.addEqualFunc(getType(c), equalC);
  1542.  
  1543. // Assert
  1544. this.assertEqual(this.getEqualFunc(c, c), equalC, 'found equalC');
  1545. }
  1546.  
  1547. testAssertEqualPrimitives() {
  1548. this.assertEqual(0, 0, '0 vs 0');
  1549. this.assertEqual(42, 42, 'number vs number');
  1550. this.assertEqual(true, true, 'true vs true');
  1551. this.assertEqual(false, false, 'false vs false');
  1552. this.assertEqual(
  1553. BigInt('123456789'),
  1554. BigInt('123456789'),
  1555. 'bigint vs bigint'
  1556. );
  1557. this.assertEqual(undefined, {}.undef, 'undefined vs undef');
  1558. this.assertEqual(null, null, 'null vs null');
  1559.  
  1560. const bar = Symbol('bar');
  1561. this.assertEqual(bar, bar, 'same symbol');
  1562.  
  1563. // Equivalent Symbols cannot be equal.
  1564. this.assertRaisesRegExp(
  1565. TestCase.Fail,
  1566. /^Symbol.foo. !== Symbol.foo.$/u,
  1567. () => {
  1568. this.assertEqual(Symbol('foo'), Symbol('foo'));
  1569. },
  1570. 'different, but equiv symbols'
  1571. );
  1572. }
  1573.  
  1574. testAssertEqualValueOf() {
  1575. // Assemble
  1576. class Silly extends Number {}
  1577.  
  1578. const n = new Number(3);
  1579. const s = new Silly(3);
  1580.  
  1581. this.assertNotEqual(n, s, 'before');
  1582.  
  1583. // Act
  1584. this.defaultEqual = this.equalValueOf;
  1585.  
  1586. // Assert
  1587. this.assertEqual(n, s, 'after');
  1588. }
  1589.  
  1590. testAssertEqualPrimitivesWithValueOf() {
  1591. this.defaultEqual = this.equalValueOf;
  1592.  
  1593. this.assertEqual(0, 0, '0 vs 0');
  1594. this.assertEqual(42, 42, 'number vs number');
  1595. this.assertEqual(true, true, 'true vs true');
  1596. this.assertEqual(false, false, 'false vs false');
  1597. this.assertEqual(
  1598. BigInt('123456789'),
  1599. BigInt('123456789'),
  1600. 'bigint vs bigint'
  1601. );
  1602. this.assertEqual(undefined, {}.undef, 'undefined vs undef');
  1603. this.assertEqual(null, null, 'null vs null');
  1604.  
  1605. const bar = Symbol('bar');
  1606. this.assertEqual(bar, bar, 'same symbol');
  1607.  
  1608. // Equivalent Symbols cannot be equal, even with valueOf().
  1609. this.assertRaisesRegExp(
  1610. TestCase.Fail,
  1611. /^Symbol.foo. !== Symbol.foo. : Using valueOf/u,
  1612. () => {
  1613. this.assertEqual(Symbol('foo'), Symbol('foo'));
  1614. },
  1615. 'different, but equiv symbols'
  1616. );
  1617. }
  1618.  
  1619. testAssertEqualFailureMessages() {
  1620. this.assertRaisesRegExp(
  1621. TestCase.Fail,
  1622. /^\{\} !== \[\] :/u,
  1623. () => {
  1624. this.assertEqual({}, [], 'assert under test');
  1625. },
  1626. 'obj vs array'
  1627. );
  1628.  
  1629. this.assertRaisesRegExp(
  1630. TestCase.Fail,
  1631. /^undefined !== null :/u,
  1632. () => {
  1633. this.assertEqual(undefined, null, 'assert under test');
  1634. },
  1635. 'undefined vs null'
  1636. );
  1637.  
  1638. this.assertRaisesRegExp(
  1639. TestCase.Fail,
  1640. /^0 !== "0" :/u,
  1641. () => {
  1642. this.assertEqual(0, '0', 'assert under test');
  1643. },
  1644. 'number vs string of same'
  1645. );
  1646. }
  1647.  
  1648. // Old version of eslint does not know BigInt.
  1649. /* eslint-disable no-undef */
  1650. testAssertNotEqualPrimitives() {
  1651. this.assertNotEqual(NaN, NaN, 'NaN');
  1652. this.assertNotEqual(true, false, 'true/false');
  1653. this.assertNotEqual(false, true, 'false/true');
  1654. this.assertNotEqual(BigInt('12345678'), BigInt('123456789'), 'BigInt');
  1655. this.assertNotEqual(undefined, null, 'undef/null');
  1656. this.assertNotEqual(Symbol('foo'), Symbol('foo'), 'symbols');
  1657. }
  1658.  
  1659. testAssertNotEqualFailureMessages() {
  1660. this.assertRaisesRegExp(TestCase.Fail,
  1661. /^0 === 0 : assert under test$/u,
  1662. () => {
  1663. this.assertNotEqual(0, 0, 'assert under test');
  1664. }, '0 vs 0');
  1665.  
  1666. this.assertRaisesRegExp(TestCase.Fail,
  1667. /^undefined === undefined :/u,
  1668. () => {
  1669. this.assertNotEqual(undefined, undefined, 'assert under test');
  1670. }, 'undefined vs undefined');
  1671.  
  1672. this.assertRaisesRegExp(TestCase.Fail,
  1673. /^null === null :/u,
  1674. () => {
  1675. this.assertNotEqual(null, null, 'assert under test');
  1676. }, 'null vs null');
  1677.  
  1678. this.assertRaisesRegExp(TestCase.Fail,
  1679. /^Symbol\(sym\) === Symbol\(sym\) :/u,
  1680. () => {
  1681. const sym = Symbol('sym');
  1682. this.assertNotEqual(sym, sym, 'assert under test');
  1683. }, 'symbol vs self');
  1684.  
  1685. this.assertRaisesRegExp(
  1686. TestCase.Fail,
  1687. /"a" === "a" :/u,
  1688. () => {
  1689. this.assertNotEqual('a', 'a', 'assert under test');
  1690. },
  1691. 'str vs str'
  1692. );
  1693. }
  1694.  
  1695. testEqualString() {
  1696. let expected = '';
  1697.  
  1698. this.assertEqual(this.getEqualFunc('a', 'b'),
  1699. this.equalString,
  1700. 'equalFunc');
  1701. this.assertEqual('string', 'string', 'str === str');
  1702. this.assertNotEqual('string 1', 'string 2', 'str !== str');
  1703.  
  1704. expected = `"abc1234" !== "abc123" :[ ]
  1705. 1: abc1234
  1706. diff: |
  1707. 2: abc123`;
  1708. this.assertRaisesRegExp(
  1709. TestCase.Fail,
  1710. RegExp(expected, 'u'),
  1711. () => {
  1712. this.assertEqual('abc1234', 'abc123', 'assert under test');
  1713. },
  1714. 'first longer',
  1715. );
  1716.  
  1717. expected = `"abcd" !== "abxd" :[ ]
  1718. 1: abcd
  1719. diff: |
  1720. 2: abxd`;
  1721. this.assertRaisesRegExp(
  1722. TestCase.Fail,
  1723. RegExp(expected, 'u'),
  1724. () => {
  1725. this.assertEqual('abcd', 'abxd', 'assert under test');
  1726. },
  1727. 'diff in middle'
  1728. );
  1729.  
  1730. expected += '\n : extra';
  1731. this.assertRaisesRegExp(
  1732. TestCase.Fail,
  1733. RegExp(expected, 'u'),
  1734. () => {
  1735. this.assertEqual('abcd', 'abxd', 'extra');
  1736. },
  1737. 'diff in middle with description'
  1738. );
  1739. }
  1740.  
  1741. testEqualObject() {
  1742. let o1 = {};
  1743. let o2 = {};
  1744. let expected = '';
  1745. const sym = Symbol('xyzzy');
  1746.  
  1747. this.assertEqual(this.getEqualFunc({}, {a: 1}),
  1748. this.equalObject,
  1749. 'equalFunc');
  1750.  
  1751. o1 = {};
  1752. o2 = {};
  1753. this.assertEqual(o1, o2, 'empty');
  1754.  
  1755. o1 = {1: 'a', b: 2};
  1756. o2 = {b: 2, 1: 'a'};
  1757. this.assertEqual(o1, o2, 'different order');
  1758.  
  1759. o1 = {1: 1, 2: {a: 42}};
  1760. o2 = {1: 1, 2: {a: 42}};
  1761. this.assertEqual(o1, o2, 'nested');
  1762.  
  1763. o1 = {sym: 'foo'};
  1764. o2 = {sym: 'foo'};
  1765. this.assertEqual(o1, o2, 'symbol');
  1766.  
  1767. o1 = {1: 'a', b: 2};
  1768. o2 = {b: 2};
  1769. this.assertNotEqual(o1, o2, 'first has more');
  1770. this.assertNotEqual(o2, o1, 'second has more');
  1771.  
  1772. o1 = {1: 'a'};
  1773. o2 = {1: 'b'};
  1774. this.assertNotEqual(o1, o2, 'different values');
  1775.  
  1776. o1 = {21: 'b'};
  1777. o2 = {21: 'a'};
  1778. expected = `\\{"21": "b"\\} !== \\{"21": "a"\\} :[ ]
  1779. Difference with key: "21"
  1780. Value in first : "b"
  1781. Value in second: "a"`;
  1782. this.assertRaisesRegExp(
  1783. TestCase.Fail,
  1784. RegExp(expected, 'u'),
  1785. () => {
  1786. this.assertEqual(o1, o2, 'assert under test');
  1787. },
  1788. 'same key, different values'
  1789. );
  1790.  
  1791. o1 = {1: 'a', 3: 'c', q: 86, null: 'abc', sym: 'x'};
  1792. o2 = {1: 'a', 2: 'b', q: 99, null: 54321, sym: 'y'};
  1793. expected = ` :[ ]
  1794. Difference with key: "q"
  1795. Value in first : 86
  1796. Value in second: 99
  1797.  
  1798. Difference with key: "null"
  1799. Value in first : "abc"
  1800. Value in second: 54321
  1801.  
  1802. Difference with key: "sym"
  1803. Value in first : "x"
  1804. Value in second: "y"
  1805.  
  1806. Key missing from first : "2"
  1807. Value in second: "b"
  1808.  
  1809. Key missing from second: "3"
  1810. Value in first : "c"`;
  1811. this.assertRaisesRegExp(
  1812. TestCase.Fail,
  1813. RegExp(expected, 'u'),
  1814. () => {
  1815. this.assertEqual(o1, o2, 'assert under test');
  1816. },
  1817. 'bit of everything',
  1818. );
  1819.  
  1820. o1 = {1: 1, 2: {a: 42}};
  1821. o2 = {1: 1, 2: {a: 43}};
  1822. expected = ` :[ ]
  1823. Difference with key: "2"
  1824. Value in first : \\{"a": 42\\}
  1825. Value in second: \\{"a": 43\\}`;
  1826. this.assertRaisesRegExp(
  1827. TestCase.Fail,
  1828. RegExp(expected, 'u'),
  1829. () => {
  1830. this.assertEqual(o1, o2, 'assert under test');
  1831. },
  1832. 'nested, different',
  1833. );
  1834. }
  1835.  
  1836. testEqualArray() {
  1837. let expected = '';
  1838.  
  1839. this.assertEqual(this.getEqualFunc([1], [2, 3]),
  1840. this.equalArray,
  1841. 'equalFunc');
  1842. this.assertEqual([], [], 'empty');
  1843. this.assertEqual([1, 'a'], [1, 'a'], 'mixed');
  1844. this.assertEqual([1, [2, 3]], [1, [2, 3]], 'nested');
  1845. this.assertNotEqual([0], [1], 'simple notequal');
  1846. this.assertNotEqual([], [1], 'different lengths');
  1847.  
  1848. expected = ` !== .* :[ ]
  1849. First difference at element 1:
  1850. 1
  1851. 2`;
  1852. this.assertRaisesRegExp(
  1853. TestCase.Fail,
  1854. RegExp(expected, 'u'),
  1855. () => {
  1856. this.assertEqual([0, 1], [0, 2], 'assert under test');
  1857. },
  1858. 'simple unequal'
  1859. );
  1860.  
  1861. expected = ` :[ ]
  1862. First array contains 1 more elements.
  1863. First additional element is at position 1:
  1864. "xyzzy"`;
  1865. this.assertRaisesRegExp(
  1866. TestCase.Fail,
  1867. RegExp(expected, 'u'),
  1868. () => {
  1869. this.assertEqual([3, 'xyzzy'], [3], 'assert under test');
  1870. },
  1871. 'first longer'
  1872. );
  1873.  
  1874. expected = ` :[ ]
  1875. Second array contains 1 more elements.
  1876. First additional element is at position 2:
  1877. "asdf"`;
  1878. this.assertRaisesRegExp(
  1879. TestCase.Fail,
  1880. RegExp(expected, 'u'),
  1881. () => {
  1882. this.assertEqual([1, 2], [1, 2, 'asdf'], 'assert under test');
  1883. },
  1884. 'second longer'
  1885. );
  1886.  
  1887. expected = ` :[ ]
  1888. First difference at element 1:
  1889. 3
  1890. 2
  1891.  
  1892. Second array contains 1 more elements.
  1893. First additional element is at position 2:
  1894. "asdf"`;
  1895. this.assertRaisesRegExp(
  1896. TestCase.Fail,
  1897. RegExp(expected, 'u'),
  1898. () => {
  1899. this.assertEqual([1, 3], [1, 2, 'asdf'], 'assert under test');
  1900. },
  1901. 'different element and sizes'
  1902. );
  1903.  
  1904. expected = ` :[ ]
  1905. First difference at element 2:
  1906. \\[1, 2\\]
  1907. \\[1, 3\\]`;
  1908. this.assertRaisesRegExp(
  1909. TestCase.Fail,
  1910. RegExp(expected, 'u'),
  1911. () => {
  1912. this.assertEqual(
  1913. [-1, 0, [1, 2]], [-1, 0, [1, 3]], 'assert under test'
  1914. );
  1915. },
  1916. 'nested unequal'
  1917. );
  1918. }
  1919.  
  1920. testEqualMap() {
  1921. let m1 = new Map();
  1922. let m2 = new Map();
  1923. let expected = '';
  1924.  
  1925. this.assertEqual(this.getEqualFunc(m1, m2), this.equalMap, 'equalFunc');
  1926. this.assertEqual(m1, m2, 'empty');
  1927.  
  1928. m1 = new Map([[1, 'a'], ['b', 2]]);
  1929. m2 = new Map([['b', 2], [1, 'a']]);
  1930. this.assertEqual(m1, m2, 'different order');
  1931.  
  1932. m1 = new Map([[1, 'a'], [2, new Map([['a', 42]])]]);
  1933. m2 = new Map([[1, 'a'], [2, new Map([['a', 42]])]]);
  1934. this.assertEqual(m1, m2, 'nested');
  1935.  
  1936. m1 = new Map([[1, 'a'], ['b', 2]]);
  1937. m2 = new Map([[1, 'a']]);
  1938. this.assertNotEqual(m1, m2, 'first has more');
  1939. this.assertNotEqual(m2, m1, 'second has more');
  1940.  
  1941. m1 = new Map([[1, 'b']]);
  1942. m2 = new Map([[1, 'a']]);
  1943. this.assertNotEqual(m1, m2, 'different values');
  1944.  
  1945. m1 = new Map([[19, 'a']]);
  1946. m2 = new Map([[19, 'b']]);
  1947. expected = ` !== .* :[ ]
  1948. Difference with key: 19
  1949. Value in first : "a"
  1950. Value in second: "b"`;
  1951. this.assertRaisesRegExp(
  1952. TestCase.Fail,
  1953. RegExp(expected, 'u'),
  1954. () => {
  1955. this.assertEqual(m1, m2, 'assert under test');
  1956. },
  1957. 'same key, different values'
  1958. );
  1959.  
  1960. m1 = new Map([[1, 'a']]);
  1961. m2 = new Map([[1, 'a'], ['b', 42]]);
  1962. expected = ` :[ ]
  1963. Key missing from first : "b"
  1964. Value in second: 42`;
  1965. this.assertRaisesRegExp(
  1966. TestCase.Fail,
  1967. RegExp(expected, 'u'),
  1968. () => {
  1969. this.assertEqual(m1, m2, 'assert under test');
  1970. },
  1971. 'second has extra key'
  1972. );
  1973.  
  1974. m1 = new Map([[1, 'a'], ['b', 42]]);
  1975. m2 = new Map([[1, 'a']]);
  1976. expected = ` :[ ]
  1977. Key missing from second: "b"
  1978. Value in first : 42`;
  1979. this.assertRaisesRegExp(
  1980. TestCase.Fail,
  1981. RegExp(expected, 'u'),
  1982. () => {
  1983. this.assertEqual(m1, m2, 'assert under test');
  1984. },
  1985. 'first has extra key'
  1986. );
  1987.  
  1988. m1 = new Map([[1, 'a'], [3, 'c'], ['q', 86], [null, 'abc'], [{}, 'x']]);
  1989. m2 = new Map([[1, 'a'], [2, 'b'], ['q', 99], [null, 54321], [{}, 'y']]);
  1990. expected = ` !== .* :[ ]
  1991. Difference with key: "q"
  1992. Value in first : 86
  1993. Value in second: 99
  1994.  
  1995. Difference with key: null
  1996. Value in first : "abc"
  1997. Value in second: 54321
  1998.  
  1999. Difference with key: \\{\\}
  2000. Value in first : "x"
  2001. Value in second: "y"
  2002.  
  2003. Key missing from first : 2
  2004. Value in second: "b"
  2005.  
  2006. Key missing from second: 3
  2007. Value in first : "c"`;
  2008. this.assertRaisesRegExp(
  2009. TestCase.Fail,
  2010. RegExp(expected, 'u'),
  2011. () => {
  2012. this.assertEqual(m1, m2, 'assert under test');
  2013. },
  2014. 'bit of everything'
  2015. );
  2016.  
  2017. expected = ` :[ ]
  2018. Difference with key: 2
  2019. Value in first : Map\\(\\[\\["a", 42\\]\\]\\)
  2020. Value in second: Map\\(\\[\\["a", 43\\]\\]\\)`;
  2021. m1 = new Map([[1, 'a'], [2, new Map([['a', 42]])]]);
  2022. m2 = new Map([[1, 'a'], [2, new Map([['a', 43]])]]);
  2023. this.assertRaisesRegExp(
  2024. TestCase.Fail,
  2025. RegExp(expected, 'u'),
  2026. () => {
  2027. this.assertEqual(m1, m2, 'assert under test');
  2028. },
  2029. 'nested different'
  2030. );
  2031. }
  2032.  
  2033. testEqualSet() {
  2034. let s1 = new Set();
  2035. let s2 = new Set();
  2036. let expected = '';
  2037.  
  2038. this.assertEqual(this.getEqualFunc(s1, s2), this.equalSet, 'equalFunc');
  2039. this.assertEqual(s1, s2, 'empty');
  2040.  
  2041. s1 = new Set([1, 'a']);
  2042. s2 = new Set(['a', 1]);
  2043. this.assertEqual(s1, s2, 'different order');
  2044.  
  2045. s1 = new Set([1, new Set(['a', 42])]);
  2046. s2 = new Set([1, new Set(['a', 42])]);
  2047. this.assertEqual(s1, s2, 'nested');
  2048.  
  2049. s1 = new Set([1, 'a', 'xyz']);
  2050. s2 = new Set([1, 'a']);
  2051. this.assertNotEqual(s1, s2, 'first has more');
  2052. this.assertNotEqual(s2, s1, 'second has more');
  2053.  
  2054. s1 = new Set([1]);
  2055. s2 = new Set([2]);
  2056. this.assertNotEqual(s1, s2, 'different values');
  2057.  
  2058. s1 = new Set([1, 2]);
  2059. s2 = new Set([2, 'three']);
  2060. expected = `Set\\(\\[1, 2\\]\\) !== Set\\(\\[2, "three"\\]\\) :[ ]
  2061. Value missing from first : "three"
  2062.  
  2063. Value missing from second: 1`;
  2064. this.assertRaisesRegExp(
  2065. TestCase.Fail,
  2066. RegExp(expected, 'u'),
  2067. () => {
  2068. this.assertEqual(s1, s2);
  2069. },
  2070. 'bit of everything'
  2071. );
  2072. }
  2073.  
  2074. testAssertTrue() {
  2075. this.assertTrue(true, 'boolean');
  2076. this.assertTrue(1, 'one');
  2077. this.assertTrue(' ', 'single space');
  2078. this.assertTrue({}, 'empty object');
  2079. this.assertTrue([], 'empty array');
  2080. this.assertTrue(Symbol('true'), 'symbol');
  2081.  
  2082. this.assertRaisesRegExp(
  2083. TestCase.Fail,
  2084. /false is not true/u,
  2085. () => {
  2086. this.assertTrue(false, 'assert under test');
  2087. }, 'testing false'
  2088. );
  2089.  
  2090. this.assertRaisesRegExp(
  2091. TestCase.Fail,
  2092. /0 is not true/u,
  2093. () => {
  2094. this.assertTrue(0, 'assert under test');
  2095. }, 'testing zero'
  2096. );
  2097.  
  2098. this.assertRaisesRegExp(
  2099. TestCase.Fail,
  2100. /^0 is not true : xyzzy$/u,
  2101. () => {
  2102. this.assertTrue(0, 'xyzzy');
  2103. },
  2104. 'testing with description as string'
  2105. );
  2106.  
  2107. this.assertRaisesRegExp(
  2108. TestCase.Fail,
  2109. /^undefined is not true : Symbol\(xyzzy\)$/u,
  2110. () => {
  2111. this.assertTrue(undefined, Symbol('xyzzy'));
  2112. },
  2113. 'testing with description as symbol'
  2114. );
  2115.  
  2116. this.assertRaisesRegExp(
  2117. TestCase.Fail,
  2118. /^null is not true$/u,
  2119. () => {
  2120. this.assertTrue(null, false);
  2121. },
  2122. 'testing with description as boolean'
  2123. );
  2124. }
  2125.  
  2126. testAssertFalse() {
  2127. this.assertFalse(false, 'boolean');
  2128. this.assertFalse(0, 'zero');
  2129. this.assertFalse('', 'empty string');
  2130.  
  2131. this.assertRaisesRegExp(
  2132. TestCase.Fail,
  2133. /true is not false/u,
  2134. () => {
  2135. this.assertFalse(true, 'assert under test');
  2136. },
  2137. 'testing true'
  2138. );
  2139.  
  2140. this.assertRaisesRegExp(
  2141. TestCase.Fail,
  2142. /-1 is not false/u,
  2143. () => {
  2144. this.assertFalse(-1, 'assert under test');
  2145. },
  2146. 'testing -1'
  2147. );
  2148.  
  2149. this.assertRaisesRegExp(
  2150. TestCase.Fail,
  2151. /\{\} is not false/u,
  2152. () => {
  2153. this.assertFalse({}, 'assert under test');
  2154. },
  2155. 'testing Boolean({})'
  2156. );
  2157.  
  2158. this.assertRaisesRegExp(
  2159. TestCase.Fail,
  2160. /^\[\] is not false : abc123$/u,
  2161. () => {
  2162. this.assertFalse([], 'abc123');
  2163. },
  2164. 'testing array'
  2165. );
  2166.  
  2167. this.assertRaisesRegExp(
  2168. TestCase.Fail,
  2169. /Symbol\(bar\) is not false/u,
  2170. () => {
  2171. this.assertFalse(Symbol('bar'), 'assert under test');
  2172. },
  2173. 'testing symbol'
  2174. );
  2175. }
  2176.  
  2177. testAssertRaises() {
  2178. this.assertRaises(
  2179. Error,
  2180. () => {
  2181. throw new Error();
  2182. },
  2183. 'empty Error'
  2184. );
  2185.  
  2186. this.assertRaises(
  2187. Error,
  2188. () => {
  2189. throw new Error('with a message');
  2190. },
  2191. 'Error with message'
  2192. );
  2193.  
  2194. this.assertRaisesRegExp(
  2195. TestCase.Fail,
  2196. /caught nothing/u,
  2197. () => {
  2198. this.assertRaises(Error, () => {}, 'assert under test');
  2199. },
  2200. 'caught nothing'
  2201. );
  2202.  
  2203. this.assertRaisesRegExp(
  2204. TestCase.Fail,
  2205. /TypeError.* Error/u,
  2206. () => {
  2207. this.assertRaises(
  2208. TypeError,
  2209. () => {
  2210. throw new Error();
  2211. },
  2212. 'assert under test'
  2213. );
  2214. },
  2215. 'regexp, empty Error, no description'
  2216. );
  2217.  
  2218. this.assertRaisesRegExp(
  2219. TestCase.Fail,
  2220. / : hovercraft/u,
  2221. () => {
  2222. this.assertRaises(TypeError,
  2223. () => {
  2224. throw new Error();
  2225. },
  2226. 'hovercraft full of eels');
  2227. },
  2228. 'regexp, empty error, with eels'
  2229. );
  2230. }
  2231.  
  2232. testAssertNoRaises() {
  2233. this.assertNoRaises(() => {
  2234. });
  2235.  
  2236. this.assertRaisesRegExp(
  2237. TestCase.Fail,
  2238. /^Unexpected exception:.*threw an error/u,
  2239. () => {
  2240. this.assertNoRaises(() => {
  2241. throw new Error('This function threw an error');
  2242. }, 'assert under test');
  2243. },
  2244. 'basic error'
  2245. );
  2246.  
  2247. this.assertRaisesRegExp(
  2248. TestCase.Fail,
  2249. /^Unexpected exception:.*: custom text/u,
  2250. () => {
  2251. this.assertNoRaises(() => {
  2252. throw new Error('Bad function. No cookie.');
  2253. }, 'custom text');
  2254. },
  2255. 'with descriptive text'
  2256. );
  2257. }
  2258.  
  2259. testAssertRaisesRegExp() {
  2260. this.assertRaisesRegExp(
  2261. Error,
  2262. /xyzzy/u,
  2263. () => {
  2264. throw new Error('xyzzy');
  2265. },
  2266. 'match text in exception'
  2267. );
  2268.  
  2269. this.assertRaisesRegExp(
  2270. TestCase.Fail,
  2271. /caught nothing/u,
  2272. () => {
  2273. this.assertRaisesRegExp(
  2274. Error,
  2275. /.*/u,
  2276. () => {},
  2277. 'assert under test'
  2278. );
  2279. },
  2280. 'no error raise, caught nothing'
  2281. );
  2282.  
  2283. this.assertRaisesRegExp(
  2284. TestCase.Fail,
  2285. / : my message/u,
  2286. () => {
  2287. this.assertRaisesRegExp(
  2288. Error,
  2289. /.*/u,
  2290. () => {},
  2291. 'my message'
  2292. );
  2293. },
  2294. 'matched description'
  2295. );
  2296.  
  2297. this.assertRaisesRegExp(
  2298. TestCase.Fail,
  2299. /Expected TypeError/u,
  2300. () => {
  2301. this.assertRaisesRegExp(
  2302. TypeError,
  2303. /message/u,
  2304. () => {
  2305. throw new Error('message');
  2306. },
  2307. 'assert under test'
  2308. );
  2309. },
  2310. 'wrong exception thrown'
  2311. );
  2312.  
  2313. this.assertRaisesRegExp(
  2314. TestCase.Fail,
  2315. /did not match regular expression/u,
  2316. () => {
  2317. this.assertRaisesRegExp(
  2318. Error,
  2319. /message/u,
  2320. () => {
  2321. throw new Error('xyzzy');
  2322. },
  2323. 'assert under test'
  2324. );
  2325. },
  2326. 'wrong regexp'
  2327. );
  2328. }
  2329.  
  2330. testAssertRegExp() {
  2331. this.assertRegExp('abc', /ab./u, 'basic match');
  2332.  
  2333. this.assertRaisesRegExp(
  2334. TestCase.Fail,
  2335. /Target.*did not match regular expression/u,
  2336. () => {
  2337. this.assertRegExp('abc', /ab.d/u, 'assert under test');
  2338. },
  2339. 'should not match'
  2340. );
  2341.  
  2342. this.assertRaisesRegExp(
  2343. TestCase.Fail,
  2344. / : what do you expect/u,
  2345. () => {
  2346. this.assertRegExp('abc', /xyz/u, 'what do you expect');
  2347. },
  2348. 'testing descriptive message'
  2349. );
  2350. }
  2351.  
  2352. }
  2353. /* eslint-enable */
  2354.  
  2355. testing.testCases.push(TestCaseTestCase);
  2356.  
  2357. /* eslint-disable max-lines-per-function */
  2358. /* eslint-disable no-magic-numbers */
  2359. /* eslint-disable require-jsdoc */
  2360. class TestResultTestCase extends TestCase {
  2361.  
  2362. setUp() {
  2363. this.result = new TestResult();
  2364. }
  2365.  
  2366. testAddSuccess() {
  2367. this.assertEqual(this.result.successes, [], 'paranoia check');
  2368.  
  2369. // Act
  2370. this.result.addSuccess('TestClass.testMethod');
  2371. this.result.addSuccess('TestClass.testMethod');
  2372.  
  2373. // Assert
  2374. this.assertEqual(
  2375. this.result.successes,
  2376. ['TestClass.testMethod', 'TestClass.testMethod'],
  2377. 'real check'
  2378. );
  2379. }
  2380.  
  2381. testAddError() {
  2382. this.assertEqual(this.result.errors, [], 'paranoia check');
  2383.  
  2384. // Act
  2385. this.result.addError('name1', new Error('first message'));
  2386. this.result.addError('name2', new TypeError('second message'));
  2387. this.result.addError('name3', new Error('third message'));
  2388.  
  2389. // Assert
  2390. const actual = this.result.errors;
  2391. const expected = [
  2392. {name: 'name1', error: 'Error', message: 'first message'},
  2393. {name: 'name2', error: 'TypeError', message: 'second message'},
  2394. {name: 'name3', error: 'Error', message: 'third message'},
  2395. ];
  2396. this.assertEqual(actual, expected, 'real check');
  2397. }
  2398.  
  2399. testAddFailure() {
  2400. this.assertEqual(this.result.failures, [], 'paranoia check');
  2401.  
  2402. // Act
  2403. this.result.addFailure('method1', 'a message');
  2404. this.result.addFailure('method2', 'another message');
  2405.  
  2406. // Assert
  2407. const actual = this.result.failures;
  2408. const expected = [
  2409. {name: 'method1', message: 'a message'},
  2410. {name: 'method2', message: 'another message'},
  2411. ];
  2412. this.assertEqual(actual, expected, 'real check');
  2413. }
  2414.  
  2415. testAddSkip() {
  2416. this.assertEqual(this.result.skipped, [], 'paranoia check');
  2417.  
  2418. // Act
  2419. this.result.addSkip('Skip.Skip', 'skip to my lou');
  2420. this.result.addSkip('Skip.Skip', 'skip to my lou');
  2421. this.result.addSkip('Skip.ToMyLou', 'my darling');
  2422.  
  2423. // Assert
  2424. const actual = this.result.skipped;
  2425. const expected = [
  2426. {name: 'Skip.Skip', message: 'skip to my lou'},
  2427. {name: 'Skip.Skip', message: 'skip to my lou'},
  2428. {name: 'Skip.ToMyLou', message: 'my darling'},
  2429. ];
  2430. this.assertEqual(actual, expected, 'real check');
  2431. }
  2432.  
  2433. testStartStop() {
  2434. // Act
  2435. this.result.startTest('Foo.testSomething');
  2436. this.result.startTest('Foo.testOrTheOther');
  2437. this.result.stopTest('Foo.testSomething');
  2438.  
  2439. // Assert
  2440. this.assertEqual(this.result.tests.size, 2, 'tests ran');
  2441. this.assertTrue(this.result.tests.get('Foo.testSomething').start,
  2442. 'first start');
  2443. this.assertTrue(this.result.tests.get('Foo.testSomething').stop,
  2444. 'first stop');
  2445. this.assertTrue(this.result.tests.get('Foo.testOrTheOther').start,
  2446. 'second start');
  2447. this.assertFalse(this.result.tests.get('Foo.testOrTheOther').stop,
  2448. 'second stop');
  2449. }
  2450.  
  2451. testWasSuccessful() {
  2452. this.assertTrue(this.result.wasSuccessful(), 'no results is a pass');
  2453.  
  2454. this.result.addSuccess('Class.method');
  2455. this.assertTrue(this.result.wasSuccessful(), 'new success is a pass');
  2456.  
  2457. this.result.addSkip('Class.differentMethod', 'rocks');
  2458. this.assertTrue(this.result.wasSuccessful(), 'a skip is a pass');
  2459.  
  2460. this.result.addError('NewClass.method', new Error());
  2461. this.assertFalse(this.result.wasSuccessful(), 'an error is not a pass');
  2462.  
  2463. const result = new TestResult();
  2464.  
  2465. this.assertTrue(result.wasSuccessful(), 'paranoia check');
  2466.  
  2467. result.addFailure('NewClass.failedMethod', 'oops');
  2468. this.assertFalse(result.wasSuccessful(), 'a failure is not a pass');
  2469. }
  2470.  
  2471. testSummary() {
  2472. const result = new TestResult();
  2473.  
  2474. this.assertEqual(
  2475. result.summary(),
  2476. [
  2477. 'total : 0',
  2478. 'successes : 0',
  2479. 'skipped : 0',
  2480. 'errors : 0',
  2481. 'failures : 0',
  2482. ],
  2483. 'empty, no formatting'
  2484. );
  2485.  
  2486. this.assertEqual(
  2487. result.summary(true),
  2488. [
  2489. 'total : 0',
  2490. 'successes : 0',
  2491. 'skipped : 0',
  2492. 'errors : 0',
  2493. 'failures : 0',
  2494. ],
  2495. 'empty, with formatting'
  2496. );
  2497.  
  2498. for (let i = 0; i < 100; i += 1) {
  2499. const currentMethod = `test-${i}`;
  2500. result.startTest(currentMethod);
  2501. if (i % 17 === 0) {
  2502. result.addError(currentMethod, new Error(`oops-${i}`));
  2503. } else if (i % 19 === 0) {
  2504. result.addFailure(currentMethod, `failed ${i}`);
  2505. } else if (i % 37 === 0) {
  2506. result.addSkip(currentMethod, `skip ${i}`);
  2507. } else {
  2508. result.addSuccess(currentMethod);
  2509. }
  2510. result.stopTest(currentMethod);
  2511. }
  2512.  
  2513. this.assertEqual(
  2514. result.summary(),
  2515. [
  2516. 'total : 100',
  2517. 'successes : 87',
  2518. 'skipped : 2',
  2519. 'errors : 6',
  2520. 'failures : 5',
  2521. ],
  2522. 'full, no formatting'
  2523. );
  2524.  
  2525. this.assertEqual(
  2526. result.summary(true),
  2527. [
  2528. 'total : 100',
  2529. 'successes : 87',
  2530. 'skipped : 2',
  2531. 'errors : 6',
  2532. 'failures : 5',
  2533. ],
  2534. 'full, with formatting'
  2535. );
  2536.  
  2537. for (let i = 0; i < 1000; i += 1) {
  2538. const currentMethod = `test-${i}`;
  2539. result.addFailure(currentMethod, `failed group 2 ${i}`);
  2540. }
  2541.  
  2542. this.assertEqual(
  2543. result.summary(),
  2544. [
  2545. 'total : 100',
  2546. 'successes : 87',
  2547. 'skipped : 2',
  2548. 'errors : 6',
  2549. 'failures : 1005',
  2550. ],
  2551. 'extra failures, no formatting'
  2552. );
  2553.  
  2554. this.assertEqual(
  2555. result.summary(true),
  2556. [
  2557. 'total : 100',
  2558. 'successes : 87',
  2559. 'skipped : 2',
  2560. 'errors : 6',
  2561. 'failures : 1005',
  2562. ],
  2563. 'extra, with formatting'
  2564. );
  2565. }
  2566.  
  2567. }
  2568. /* eslint-enable */
  2569.  
  2570. testing.testCases.push(TestResultTestCase);
  2571.  
  2572. /** Assembles and drives execution of {@link TestCase}s. */
  2573. class TestRunner {
  2574.  
  2575. /** @param {function(): TestCase} tests - TestCases to execute. */
  2576. constructor(tests) {
  2577. const badKlasses = [];
  2578. const testMethods = [];
  2579. for (const klass of tests) {
  2580. if (klass.prototype instanceof TestCase) {
  2581. testMethods.push(...this.#extractTestMethods(klass));
  2582. } else {
  2583. badKlasses.push(klass);
  2584. }
  2585. }
  2586. if (badKlasses.length) {
  2587. const msg = `Bad class count: ${badKlasses.length}`;
  2588. for (const klass of badKlasses) {
  2589. // eslint-disable-next-line no-console
  2590. console.error('Not a TestCase:', klass);
  2591. }
  2592. throw new TypeError(`Bad classes: ${msg}`);
  2593. }
  2594.  
  2595. this.#tests = testMethods;
  2596. }
  2597.  
  2598. /**
  2599. * Run each test method in turn.
  2600. * @returns {TestResult} - Collected results.
  2601. */
  2602. runTests() {
  2603. const result = new TestResult();
  2604.  
  2605. let lastKlass = null;
  2606. let doRunTests = true;
  2607. for (const {klass, method} of this.#tests) {
  2608. if (klass !== lastKlass) {
  2609. this.#doClassCleanups(lastKlass, result);
  2610. doRunTests = this.#doSetUpClass(klass, result);
  2611. }
  2612. lastKlass = klass;
  2613.  
  2614. if (doRunTests) {
  2615. this.#doRunTestMethod(klass, method, result);
  2616. }
  2617. }
  2618.  
  2619. this.#doClassCleanups(lastKlass, result);
  2620.  
  2621. return result;
  2622. }
  2623.  
  2624. #tests
  2625.  
  2626. /** @param {function(): TestCase} klass - TestCase to process. */
  2627. #extractTestMethods = function *extractTestMethods(klass) {
  2628. let obj = klass;
  2629. while (obj) {
  2630. if (obj.prototype instanceof TestCase) {
  2631. for (const prop of Object.getOwnPropertyNames(obj.prototype)) {
  2632. if (prop.startsWith('test')) {
  2633. yield {klass: klass, method: prop};
  2634. }
  2635. }
  2636. }
  2637. obj = Object.getPrototypeOf(obj);
  2638. }
  2639. }
  2640.  
  2641. /**
  2642. * @param {function(): TestCase} klass - TestCase to process.
  2643. * @param {TestResult} result - Result to use if any errors.
  2644. */
  2645. #doClassCleanups = (klass, result) => {
  2646. if (klass) {
  2647. const currentMethod = `${klass.name}.doClassCleanups`;
  2648. try {
  2649. klass.doClassCleanups();
  2650. } catch (e) {
  2651. result.addError(currentMethod, e);
  2652. }
  2653. }
  2654. }
  2655.  
  2656. /**
  2657. * @param {function(): TestCase} klass - TestCase to process.
  2658. * @param {TestResult} result - Result to use if any errors.
  2659. * @returns {boolean} - Indicates success of calling setUpClass().
  2660. */
  2661. #doSetUpClass = (klass, result) => {
  2662. const currentMethod = `${klass.name}.setUpClass`;
  2663. try {
  2664. klass.setUpClass();
  2665. } catch (e) {
  2666. if (e instanceof TestCase.Skip) {
  2667. result.addSkip(currentMethod, e.message);
  2668. } else {
  2669. result.addError(currentMethod, e);
  2670. }
  2671. return false;
  2672. }
  2673. return true;
  2674. }
  2675.  
  2676. /**
  2677. * @param {function(): TestCase} Klass - TestCase to process.
  2678. * @param {string} methodName - Name of the test method to execute.
  2679. * @param {TestResult} result - Result of the execution.
  2680. */
  2681. #doRunTestMethod = (Klass, methodName, result) => {
  2682. const instance = new Klass(methodName);
  2683. instance.run(result);
  2684. }
  2685.  
  2686. }
  2687.  
  2688. /* eslint-disable class-methods-use-this */
  2689. /* eslint-disable no-empty-function */
  2690. /* eslint-disable require-jsdoc */
  2691. /**
  2692. * TestCases require at least one test method to get instantiated by {@link
  2693. * TestRunner}
  2694. */
  2695. class DummyMethodTestCase extends TestCase {
  2696.  
  2697. testDummy() {}
  2698.  
  2699. }
  2700. /* eslint-enable */
  2701.  
  2702. /* eslint-disable class-methods-use-this */
  2703. /* eslint-disable max-lines-per-function */
  2704. /* eslint-disable no-empty-function */
  2705. /* eslint-disable no-magic-numbers */
  2706. /* eslint-disable no-new */
  2707. /* eslint-disable require-jsdoc */
  2708. class TestRunnerTestCase extends TestCase {
  2709.  
  2710. testNoClasses() {
  2711. // Assemble
  2712. const runner = new TestRunner([]);
  2713.  
  2714. // Act
  2715. const result = runner.runTests();
  2716.  
  2717. // Assert
  2718. this.assertTrue(result.wasSuccessful());
  2719. }
  2720.  
  2721. testBadClasses() {
  2722. this.assertRaisesRegExp(TypeError, /Bad class count: 2$/u, () => {
  2723. new TestRunner([Error, TestRunnerTestCase, TypeError]);
  2724. });
  2725. }
  2726.  
  2727. testStrangeClassSetup() {
  2728. // Assemble
  2729. class ClassSetupErrorTestCase extends DummyMethodTestCase {
  2730.  
  2731. static setUpClass() {
  2732. throw new Error('erroring');
  2733. }
  2734.  
  2735. }
  2736.  
  2737. class ClassSetupFailTestCase extends DummyMethodTestCase {
  2738.  
  2739. static setUpClass() {
  2740. throw new this.Fail('failing');
  2741. }
  2742.  
  2743. }
  2744.  
  2745. class ClassSetupSkipTestCase extends DummyMethodTestCase {
  2746.  
  2747. static setUpClass() {
  2748. throw new this.Skip('skipping');
  2749. }
  2750.  
  2751. }
  2752.  
  2753. const classes = [
  2754. DummyMethodTestCase,
  2755. ClassSetupErrorTestCase,
  2756. ClassSetupFailTestCase,
  2757. ClassSetupSkipTestCase,
  2758. ];
  2759. const runner = new TestRunner(classes);
  2760.  
  2761. // Act
  2762. const result = runner.runTests();
  2763.  
  2764. // Assert
  2765. this.assertFalse(result.wasSuccessful());
  2766.  
  2767. // In setUpClass, TestCase.Fail should count as an error
  2768. this.assertEqual(
  2769. result.successes,
  2770. ['DummyMethodTestCase.testDummy'],
  2771. 'successes'
  2772. );
  2773. this.assertEqual(
  2774. result.errors,
  2775. [
  2776. {
  2777. name: 'ClassSetupErrorTestCase.setUpClass',
  2778. error: 'Error',
  2779. message: 'erroring',
  2780. },
  2781. {
  2782. name: 'ClassSetupFailTestCase.setUpClass',
  2783. error: 'TestCase.Fail',
  2784. message: 'failing',
  2785. },
  2786. ],
  2787. 'errors'
  2788. );
  2789. this.assertEqual(result.failures, [], 'failures');
  2790. this.assertEqual(
  2791. result.skipped,
  2792. [{name: 'ClassSetupSkipTestCase.setUpClass', message: 'skipping'}],
  2793. 'skipped'
  2794. );
  2795. }
  2796.  
  2797. testStrangeClassCleanups() {
  2798. // Assemble
  2799. class BaseClassCleanupTestCase extends DummyMethodTestCase {
  2800.  
  2801. static setUpClass() {
  2802. this.addClassCleanup(this.cleanupFunc);
  2803. }
  2804.  
  2805. static cleanupFunc() {}
  2806.  
  2807. }
  2808. class CleanupErrorTestCase extends BaseClassCleanupTestCase {
  2809.  
  2810. static cleanupFunc() {
  2811. throw new Error('cleanup error');
  2812. }
  2813.  
  2814. }
  2815.  
  2816. class CleanupFailTestCase extends BaseClassCleanupTestCase {
  2817.  
  2818. static cleanupFunc() {
  2819. throw new this.Fail('cleanup fail');
  2820. }
  2821.  
  2822. }
  2823. class CleanupSkipTestCase extends BaseClassCleanupTestCase {
  2824.  
  2825. static cleanupFunc() {
  2826. throw new this.Skip('cleanup skip');
  2827. }
  2828.  
  2829. }
  2830.  
  2831. const classes = [
  2832. BaseClassCleanupTestCase,
  2833. CleanupErrorTestCase,
  2834. CleanupFailTestCase,
  2835. CleanupSkipTestCase,
  2836. ];
  2837. const runner = new TestRunner(classes);
  2838.  
  2839. // Act
  2840. const result = runner.runTests();
  2841.  
  2842. // Assert
  2843. this.assertFalse(result.wasSuccessful());
  2844.  
  2845. // In doClassCleanups, TestCase.{Fail,Skip} should count as errors,
  2846. // however, the test *also* passed already, so we get extra counts. Not
  2847. // sure if this is a bug or a feature.
  2848. this.assertEqual(
  2849. result.successes,
  2850. [
  2851. 'BaseClassCleanupTestCase.testDummy',
  2852. 'CleanupErrorTestCase.testDummy',
  2853. 'CleanupFailTestCase.testDummy',
  2854. 'CleanupSkipTestCase.testDummy',
  2855. ],
  2856. 'successes'
  2857. );
  2858. this.assertEqual(
  2859. result.errors,
  2860. [
  2861. {
  2862. name: 'CleanupErrorTestCase.doClassCleanups',
  2863. error: 'Error',
  2864. message: 'cleanup error',
  2865. },
  2866. {
  2867. name: 'CleanupFailTestCase.doClassCleanups',
  2868. error: 'TestCase.Fail',
  2869. message: 'cleanup fail',
  2870. },
  2871. {
  2872. name: 'CleanupSkipTestCase.doClassCleanups',
  2873. error: 'TestCase.Skip',
  2874. message: 'cleanup skip',
  2875. },
  2876. ],
  2877. 'errors'
  2878. );
  2879. this.assertEqual(result.failures, [], 'failures');
  2880. this.assertEqual(result.skipped, [], 'skipped');
  2881. }
  2882.  
  2883. testFindsTestMethods() {
  2884. // Assemble
  2885. class One extends TestCase {
  2886.  
  2887. test() {}
  2888.  
  2889. test_() {}
  2890.  
  2891. _test() {
  2892. this.fail('_test');
  2893. }
  2894.  
  2895. testOne() {
  2896. this.skip('One');
  2897. }
  2898.  
  2899. notATest() {
  2900. this.fail('notATest');
  2901. }
  2902.  
  2903. }
  2904. class Two extends TestCase {
  2905.  
  2906. alsoNotATest() {
  2907. this.fail('alsoNotATest');
  2908. }
  2909.  
  2910. testTwo() {
  2911. this.skip('Two');
  2912. }
  2913.  
  2914. }
  2915. const runner = new TestRunner([One, Two]);
  2916.  
  2917. // Act
  2918. const result = runner.runTests();
  2919.  
  2920. // Assert
  2921. this.assertTrue(result.wasSuccessful());
  2922. this.assertEqual(
  2923. result.successes, ['One.test', 'One.test_'], 'successes'
  2924. );
  2925. this.assertEqual(result.errors, [], 'errors');
  2926. this.assertEqual(result.failures, [], 'failures');
  2927. this.assertEqual(
  2928. result.skipped,
  2929. [
  2930. {name: 'One.testOne', message: 'One'},
  2931. {name: 'Two.testTwo', message: 'Two'},
  2932. ],
  2933. 'skipped'
  2934. );
  2935. }
  2936.  
  2937. testAccumulatesResults() {
  2938. class FooTestCase extends TestCase {
  2939.  
  2940. testFail() {
  2941. this.fail('Fail failed');
  2942. }
  2943.  
  2944. testNotEqual() {
  2945. this.assertEqual(1, 2);
  2946. }
  2947.  
  2948. testPass() {}
  2949.  
  2950. testError() {
  2951. throw new Error('Oh, dear!');
  2952. }
  2953.  
  2954. testSkip() {
  2955. this.skip('Skip skipped');
  2956. }
  2957.  
  2958. }
  2959. const runner = new TestRunner([FooTestCase]);
  2960.  
  2961. // Act
  2962. const result = runner.runTests();
  2963.  
  2964. // Assert
  2965. this.assertFalse(result.wasSuccessful(), 'had failures');
  2966.  
  2967. this.assertEqual(
  2968. result.errors,
  2969. [
  2970. {
  2971. name: 'FooTestCase.testError',
  2972. error: 'Error',
  2973. message: 'Oh, dear!',
  2974. },
  2975. ],
  2976. 'errors'
  2977. );
  2978. this.assertEqual(
  2979. result.failures,
  2980. [
  2981. {name: 'FooTestCase.testFail', message: 'Fail failed'},
  2982. {name: 'FooTestCase.testNotEqual', message: '1 !== 2'},
  2983. ],
  2984. 'failures'
  2985. );
  2986. this.assertEqual(
  2987. result.skipped,
  2988. [{name: 'FooTestCase.testSkip', message: 'Skip skipped'}],
  2989. 'skipped'
  2990. );
  2991. this.assertEqual(
  2992. result.successes,
  2993. ['FooTestCase.testPass'],
  2994. 'successes'
  2995. );
  2996. }
  2997.  
  2998. }
  2999. /* eslint-enable */
  3000.  
  3001. testing.testCases.push(TestRunnerTestCase);
  3002.  
  3003. /**
  3004. * Run registered TestCases.
  3005. * @returns {TestResult} - Accumulated results of these tests.
  3006. */
  3007. function runTests() {
  3008. const runner = new TestRunner(testing.testCases);
  3009. return runner.runTests();
  3010. }
  3011.  
  3012. return {
  3013. version: version,
  3014. testing: testing,
  3015. TestCase: TestCase,
  3016. runTests: runTests,
  3017. };
  3018.  
  3019. }());