|
1 | 1 | import type EthersT from "ethers";
|
2 | 2 | import ordinal from "ordinal";
|
| 3 | +import { AssertionError } from "chai"; |
3 | 4 | import { AssertWithSsfi, Ssfi } from "../utils";
|
4 | 5 | import { PREVIOUS_MATCHER_NAME } from "./constants";
|
5 | 6 | import {
|
@@ -49,78 +50,94 @@ export function assertArgsArraysEqual(
|
49 | 50 | expectedArgs: any[],
|
50 | 51 | actualArgs: any[],
|
51 | 52 | tag: string,
|
52 |
| - name: string, |
| 53 | + assert: AssertWithSsfi, |
| 54 | + ssfi: Ssfi |
| 55 | +) { |
| 56 | + try { |
| 57 | + assertArgsArraysEqualNested( |
| 58 | + Assertion, |
| 59 | + expectedArgs, |
| 60 | + actualArgs, |
| 61 | + assert, |
| 62 | + ssfi |
| 63 | + ); |
| 64 | + } catch (err: any) { |
| 65 | + // throw new AssertionError('Fix this'); |
| 66 | + err.message = `Error in ${tag}: ${err.message}`; |
| 67 | + throw err; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export function assertArgsArraysEqualNested( |
| 72 | + Assertion: Chai.AssertionStatic, |
| 73 | + expectedArgs: any[], |
| 74 | + actualArgs: any[], |
53 | 75 | assert: AssertWithSsfi,
|
54 | 76 | ssfi: Ssfi
|
55 | 77 | ) {
|
56 | 78 | const ethers = require("ethers") as typeof EthersT;
|
57 | 79 | assert(
|
58 | 80 | actualArgs.length === expectedArgs.length,
|
59 |
| - `Expected "${name}" ${tag} to have ${expectedArgs.length} argument(s), but it has ${actualArgs.length}` |
| 81 | + `Expected arguments array to have length ${expectedArgs.length}, but it has ${actualArgs.length}` |
60 | 82 | );
|
61 | 83 | for (const [index, expectedArg] of expectedArgs.entries()) {
|
62 | 84 | const actualArg = actualArgs[index];
|
63 |
| - if (typeof expectedArg === "function") { |
64 |
| - const errorPrefix = `The predicate for the ${ordinal( |
65 |
| - index + 1 |
66 |
| - )} ${tag} argument`; |
67 |
| - try { |
68 |
| - if (expectedArg(actualArg) === true) continue; |
69 |
| - } catch (e: any) { |
| 85 | + try { |
| 86 | + if (typeof expectedArg === "function") { |
| 87 | + try { |
| 88 | + if (expectedArg(actualArg) === true) continue; |
| 89 | + } catch (e: any) { |
| 90 | + assert( |
| 91 | + false, |
| 92 | + `The predicate threw when called: ${e.message}` |
| 93 | + // no need for a negated message, since we disallow mixing .not. with |
| 94 | + // .withArgs |
| 95 | + ); |
| 96 | + } |
70 | 97 | assert(
|
71 | 98 | false,
|
72 |
| - `${errorPrefix} threw when called: ${e.message}` |
| 99 | + `The predicate did not return true` |
73 | 100 | // no need for a negated message, since we disallow mixing .not. with
|
74 | 101 | // .withArgs
|
75 | 102 | );
|
76 |
| - } |
77 |
| - assert( |
78 |
| - false, |
79 |
| - `${errorPrefix} did not return true ` |
80 |
| - // no need for a negated message, since we disallow mixing .not. with |
81 |
| - // .withArgs |
82 |
| - ); |
83 |
| - } else if (expectedArg instanceof Uint8Array) { |
84 |
| - new Assertion(actualArg, undefined, ssfi, true).equal( |
85 |
| - ethers.hexlify(expectedArg) |
86 |
| - ); |
87 |
| - } else if ( |
88 |
| - expectedArg?.length !== undefined && |
89 |
| - typeof expectedArg !== "string" |
90 |
| - ) { |
91 |
| - const expectedLength = expectedArg.length; |
92 |
| - const actualLength = actualArg.length; |
93 |
| - assert( |
94 |
| - expectedLength === actualLength, |
95 |
| - `Expected the ${ordinal( |
96 |
| - index + 1 |
97 |
| - )} argument of the "${name}" ${tag} to have ${expectedLength} ${ |
98 |
| - expectedLength === 1 ? "element" : "elements" |
99 |
| - }, but it has ${actualLength}` |
100 |
| - ); |
101 |
| - |
102 |
| - for (let j = 0; j < expectedArg.length; j++) { |
103 |
| - new Assertion(actualArg[j], undefined, ssfi, true).equal( |
104 |
| - expectedArg[j] |
| 103 | + } else if (expectedArg instanceof Uint8Array) { |
| 104 | + new Assertion(actualArg, undefined, ssfi, true).equal( |
| 105 | + ethers.hexlify(expectedArg) |
105 | 106 | );
|
106 |
| - } |
107 |
| - } else { |
108 |
| - if (actualArg.hash !== undefined && actualArg._isIndexed === true) { |
109 |
| - new Assertion(actualArg.hash, undefined, ssfi, true).to.not.equal( |
| 107 | + } else if ( |
| 108 | + expectedArg?.length !== undefined && |
| 109 | + typeof expectedArg !== "string" |
| 110 | + ) { |
| 111 | + assertArgsArraysEqualNested( |
| 112 | + Assertion, |
110 | 113 | expectedArg,
|
111 |
| - "The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion should be the actual event argument (the pre-image of the hash). You provided the hash itself. Please supply the actual event argument (the pre-image of the hash) instead." |
112 |
| - ); |
113 |
| - const expectedArgBytes = ethers.isHexString(expectedArg) |
114 |
| - ? ethers.getBytes(expectedArg) |
115 |
| - : ethers.toUtf8Bytes(expectedArg); |
116 |
| - const expectedHash = ethers.keccak256(expectedArgBytes); |
117 |
| - new Assertion(actualArg.hash, undefined, ssfi, true).to.equal( |
118 |
| - expectedHash, |
119 |
| - `The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion was hashed to produce ${expectedHash}. The actual hash and the expected hash did not match` |
| 114 | + actualArg, |
| 115 | + assert, |
| 116 | + ssfi |
120 | 117 | );
|
121 | 118 | } else {
|
122 |
| - new Assertion(actualArg, undefined, ssfi, true).equal(expectedArg); |
| 119 | + if (actualArg.hash !== undefined && actualArg._isIndexed === true) { |
| 120 | + new Assertion(actualArg.hash, undefined, ssfi, true).to.not.equal( |
| 121 | + expectedArg, |
| 122 | + "The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion should be the actual event argument (the pre-image of the hash). You provided the hash itself. Please supply the actual event argument (the pre-image of the hash) instead." |
| 123 | + ); |
| 124 | + const expectedArgBytes = ethers.isHexString(expectedArg) |
| 125 | + ? ethers.getBytes(expectedArg) |
| 126 | + : ethers.toUtf8Bytes(expectedArg); |
| 127 | + const expectedHash = ethers.keccak256(expectedArgBytes); |
| 128 | + new Assertion(actualArg.hash, undefined, ssfi, true).to.equal( |
| 129 | + expectedHash, |
| 130 | + `The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion was hashed to produce ${expectedHash}. The actual hash and the expected hash ${actualArg.hash} did not match` |
| 131 | + ); |
| 132 | + } else { |
| 133 | + new Assertion(actualArg, undefined, ssfi, true).equal(expectedArg); |
| 134 | + } |
123 | 135 | }
|
| 136 | + } catch (err: any) { |
| 137 | + err.message = `Error in the ${ordinal(index + 1)} argument assertion: ${ |
| 138 | + err.message |
| 139 | + }`; |
| 140 | + throw err; |
124 | 141 | }
|
125 | 142 | }
|
126 | 143 | }
|
0 commit comments