test-utils

primitive assert-methods for unit-testing

当前为 2016-08-30 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/22762/144812/test-utils.js

  1. // ==UserScript==
  2. // @name test-utils
  3. // @name:de test-utils
  4. // @namespace dannysaurus.camamba
  5. // @version 0.1
  6. // @license MIT License
  7. // @description primitive assert-methods for unit-testing
  8. // @description:de primitive assert-methods for unit-testing
  9. // ==/UserScript==
  10. var LIB = LIB || {};
  11. /**
  12. * @type {{assertTrue}}
  13. */
  14. LIB.testUtils = (function() {
  15. 'use strict';
  16. /**
  17. * Throws an error if assertion fails
  18. * @param {boolean} condition - condition to be checked</br><code>true</code> has the assertion succeed </br>false has the assertion fail (and throws an Error)
  19. * @param {string} [message] - debug-message to display if the assertion fails
  20. */
  21. var assertTrue = function(condition, message) {
  22. message = message || "Assertion failed";
  23. if (!condition) {
  24. throw new Error(message);
  25. }
  26. };
  27. /**
  28. * Asserts that two values or objects are equal. Throws an Error if assertion fails.
  29. * Strict comparison (<code>===</code>) is used to check for equality.
  30. * @param {*} expected - the expected value or object
  31. * @param {*} actual - the value or object to check against <code>expected</code>
  32. * @param {string} [message] - the identifying Error message for an assertion fail
  33. */
  34. var assertEquals = function(expected, actual, message) {
  35. assertTrue(expected === actual, message);
  36. };
  37. /**
  38. * Asserts that two values or objects are equal. Throws an Error if assertion fails.
  39. * Nonstrict comparison with type convertation (<code>==</code>) is used to check for equality.
  40. * @param {*} expected - the expected value or object
  41. * @param {*} actual - the value or object to check against <code>expected</code>
  42. * @param {string} [message] - the identifying Error message for an assertion fail
  43. */
  44. var assertEqualsNonStrict = function(expected, actual, message) {
  45. assertTrue(expected == actual, message);
  46. };
  47. return {
  48. assertTrue: assertTrue,
  49. assertEquals: assertEquals,
  50. assertEqualsNonStrict: assertEqualsNonStrict
  51. };
  52. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址