|
| 1 | +// Assertions aliased to names from the custom test framework. |
| 2 | +import chai from "chai"; |
| 3 | +const assert = chai.assert; |
| 4 | + |
| 5 | +export const pass = () => {}; |
| 6 | +export const fail = assert.fail; |
| 7 | +export const expect = assert; |
| 8 | +export const assertEquals = assert.strictEqual; |
| 9 | +export const assertNotEquals = assert.notStrictEqual; |
| 10 | +export const assertContains = assert.include; |
| 11 | +export const assertNotContains = assert.notInclude; |
| 12 | + |
| 13 | +export const assertSimilar = (actual: any, expected: any, msg?: any) => { |
| 14 | + console.error(`assertSimilar is deprecated, use assert.deepEqual`); |
| 15 | + assert.deepEqual(actual, expected, msg); |
| 16 | +}; |
| 17 | + |
| 18 | +export const assertNotSimilar = (actual: any, expected: any, msg?: any) => { |
| 19 | + console.error(`assertNotSimilar is deprecated, use assert.notDeepEqual`); |
| 20 | + assert.notDeepEqual(actual, expected, msg); |
| 21 | +}; |
| 22 | + |
| 23 | +// not using `assert.throws` because it can't test for fn to throw "anything" |
| 24 | +export const expectError = (msg: string | (() => void), fn?: () => void) => { |
| 25 | + let message: string; |
| 26 | + let fun: () => void; |
| 27 | + if (typeof msg === "function") { |
| 28 | + fun = msg; |
| 29 | + message = "Expected an error to be thrown"; |
| 30 | + } else { |
| 31 | + fun = fn!; |
| 32 | + message = msg; |
| 33 | + } |
| 34 | + |
| 35 | + let passed = false; |
| 36 | + try { |
| 37 | + fun(); |
| 38 | + } catch { |
| 39 | + passed = true; |
| 40 | + } |
| 41 | + assert(passed, message); |
| 42 | +}; |
| 43 | + |
| 44 | +export const expectNoError = (msg: string | (() => void), fn?: () => void) => { |
| 45 | + let message: string; |
| 46 | + let fun: () => void; |
| 47 | + if (typeof msg === "function") { |
| 48 | + fun = msg; |
| 49 | + message = "Unexpected error was thrown"; |
| 50 | + } else { |
| 51 | + fun = fn!; |
| 52 | + message = msg; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + fun(); |
| 57 | + } catch (ex) { |
| 58 | + assert.fail(appendToMessage(message, ex.message)); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +// Compares two floating point values and checks whether they are approximately equal to each other |
| 63 | +export const assertApproxEquals = ( |
| 64 | + actual: number, |
| 65 | + expected: number, |
| 66 | + msg?: string |
| 67 | +) => { |
| 68 | + // uses absolute error when |expected| <= 1, compatible with old version |
| 69 | + if (Math.abs(expected) <= 1) { |
| 70 | + assert.closeTo(actual, expected, 1e-9); |
| 71 | + } else { |
| 72 | + msg = appendToMessage( |
| 73 | + msg, |
| 74 | + "Expected actual value " + |
| 75 | + actual + |
| 76 | + " to approximately equal expected value " + |
| 77 | + expected + |
| 78 | + " (accepted relative error: 1e-9)" |
| 79 | + ); |
| 80 | + assert(Math.abs((expected - actual) / expected) <= 1e-9, msg); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +// Compares two floating point values and checks whether they are sufficiently different from each other |
| 85 | +export const assertNotApproxEquals = ( |
| 86 | + actual: number, |
| 87 | + unexpected: number, |
| 88 | + msg?: string |
| 89 | +) => { |
| 90 | + msg = appendToMessage( |
| 91 | + msg, |
| 92 | + "Actual value " + |
| 93 | + actual + |
| 94 | + " should not approximately equal unexpected value " + |
| 95 | + unexpected + |
| 96 | + " (rejected relative error: 1e-9)" |
| 97 | + ); |
| 98 | + if (Math.abs(unexpected) <= 1) { |
| 99 | + assert(Math.abs(unexpected - actual) > 1e-9, msg); |
| 100 | + } else { |
| 101 | + assert(Math.abs((unexpected - actual) / unexpected) > 1e-9, msg); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +const appendToMessage = (msg: string | undefined, s: string) => |
| 106 | + msg ? msg + " - " + s : s; |
0 commit comments