spec-helper.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const EPSILON = 0.00001;
  2. const assert = require('assert');
  3. global.expect = function(e) {
  4. function expected(e, message, a) {
  5. assert.fail(e, a, `expected ${JSON.stringify(e)} ${message} ${JSON.stringify(a)}`);
  6. }
  7. return {
  8. toBe: function(a) {
  9. assert.strictEqual(e, a);
  10. },
  11. toEqual: function(a) {
  12. assert.strictEqual(e,a);
  13. },
  14. toBeDefined: function() {
  15. assert.notStrictEqual(e, undefined);
  16. },
  17. toBeTruthy: function() {
  18. assert(e);
  19. },
  20. toBeFalsy: function() {
  21. assert(!e);
  22. },
  23. toBeNull: function() {
  24. assert.strictEqual(e, null);
  25. },
  26. not: {
  27. toBe: function(a) {
  28. assert.notStrictEqual(e, a);
  29. },
  30. toBeEqualish: function(a) {
  31. if (typeof(e) == 'number')
  32. assert(Math.abs(e - a) >= EPSILON);
  33. if (e.length != a.length)
  34. return;
  35. for (let i = 0; i < e.length; i++) {
  36. if (isNaN(e[i]) !== isNaN(a[i]))
  37. return;
  38. if (Math.abs(e[i] - a[i]) >= EPSILON)
  39. return;
  40. }
  41. assert.fail(e, a);
  42. }
  43. },
  44. toBeGreaterThan: function(a) {
  45. assert(e > a);
  46. },
  47. toBeLessThan: function(a) {
  48. assert(e < a);
  49. },
  50. /*
  51. Returns true if `actual` has the same length as `expected`, and
  52. if each element of both arrays is within 0.000001 of each other.
  53. This is a way to check for "equal enough" conditions, as a way
  54. of working around floating point imprecision.
  55. */
  56. toBeEqualish: function(a) {
  57. if (typeof(e) == 'number') {
  58. if(isNaN(e) !== isNaN(a))
  59. expected(e, "to be equalish to", a);
  60. if(Math.abs(e - a) >= EPSILON)
  61. expected(e, "to be equalish to", a);
  62. }
  63. if (e.length != a.length)
  64. assert.fail(e.length, a.length, "length mismatch");
  65. for (let i = 0; i < e.length; i++) {
  66. if (isNaN(e[i]) !== isNaN(a[i]))
  67. assert.fail(isNaN(e[i]), isNaN(a[i]));
  68. if (Math.abs(e[i] - a[i]) >= EPSILON)
  69. assert.fail(Math.abs(e[i] - a[i]));
  70. }
  71. },
  72. //Dual quaternions are very special & unique snowflakes
  73. toBeEqualishQuat2: function(a, epsilon) {
  74. if(epsilon == undefined) epsilon = EPSILON;
  75. let allSignsFlipped = false;
  76. if (e.length != a.length)
  77. expected(e, "to have the same length as", a);
  78. for (let i = 0; i < e.length; i++) {
  79. if (isNaN(e[i]) !== isNaN(a[i]))
  80. expected(e, "to be equalish to", a);
  81. if (allSignsFlipped) {
  82. if (Math.abs(e[i] - (-a[i])) >= epsilon)
  83. expected(e, "to be equalish to", a);
  84. } else {
  85. if (Math.abs(e[i] - a[i]) >= epsilon) {
  86. allSignsFlipped = true;
  87. i = 0;
  88. }
  89. }
  90. }
  91. }
  92. };
  93. };