Skip to content

Commit ae2b7c4

Browse files
committed
test: add formatter tests
1 parent 5956ecb commit ae2b7c4

File tree

3 files changed

+209
-2
lines changed

3 files changed

+209
-2
lines changed

v-next/hardhat/src/internal/builtin-plugins/solidity-test/formatters.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function formatLogs(logs: string[], indent: number): string {
2828
);
2929
}
3030

31-
export function formatInputs(
31+
function formatInputs(
3232
inputs: DecodedTraceParameters | Uint8Array,
3333
color?: (text: string) => string,
3434
): string | undefined {
@@ -186,5 +186,7 @@ function formatNestedArray(
186186

187187
export function formatTraces(traces: CallTrace[], indent: number): string {
188188
const lines = traces.map(formatTrace);
189-
return formatNestedArray(lines, " ".repeat(indent));
189+
const formattedTraces = formatNestedArray(lines, " ".repeat(indent));
190+
// Remove the trailing newline
191+
return formattedTraces.slice(0, -1);
190192
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import type { CallTrace } from "@ignored/edr-optimism";
2+
3+
import assert from "node:assert/strict";
4+
import { describe, it } from "node:test";
5+
6+
import chalk from "chalk";
7+
8+
import {
9+
formatLogs,
10+
formatTraces,
11+
} from "../../../../src/internal/builtin-plugins/solidity-test/formatters.js";
12+
13+
describe("formatLogs", () => {
14+
it("should format logs correctly", async () => {
15+
const lines = ["a", "b", "c"];
16+
17+
const actual = ` a
18+
b
19+
c`;
20+
21+
const expected = formatLogs(lines, 2);
22+
23+
assert.equal(expected, chalk.grey(actual));
24+
});
25+
26+
it("should return an empty string for empty logs", async () => {
27+
const lines: string[] = [];
28+
29+
const expected = "";
30+
31+
const actual = formatLogs(lines, 2);
32+
33+
assert.equal(expected, actual);
34+
});
35+
});
36+
37+
describe("formatTraces", () => {
38+
it("should format traces correctly", async () => {
39+
const traces = [
40+
{
41+
kind: 0,
42+
success: true,
43+
isCheatcode: false,
44+
gasUsed: 127552n,
45+
value: 0n,
46+
contract: "FailingCounterTest",
47+
inputs: { name: "setUp", arguments: [] },
48+
outputs: new Uint8Array(),
49+
children: [
50+
{
51+
kind: 3,
52+
success: true,
53+
isCheatcode: false,
54+
gasUsed: 0n,
55+
value: 0n,
56+
contract: "console",
57+
inputs: { name: "log", arguments: ['"Setting up"'] },
58+
outputs: new Uint8Array(),
59+
children: [],
60+
},
61+
{
62+
kind: 4,
63+
success: true,
64+
isCheatcode: false,
65+
gasUsed: 68915n,
66+
value: 0n,
67+
contract: "Counter",
68+
inputs: new Uint8Array([1, 2, 3]),
69+
outputs: "344 bytes of code",
70+
children: [],
71+
},
72+
{
73+
kind: 3,
74+
success: true,
75+
isCheatcode: false,
76+
gasUsed: 0n,
77+
value: 0n,
78+
contract: "console",
79+
inputs: { name: "log", arguments: ['"Counter set up"'] },
80+
outputs: new Uint8Array(),
81+
children: [],
82+
},
83+
],
84+
},
85+
{
86+
kind: 0,
87+
success: true,
88+
isCheatcode: false,
89+
gasUsed: 32272n,
90+
value: 0n,
91+
contract: "FailingCounterTest",
92+
inputs: { name: "testFailFuzzInc", arguments: ["1"] },
93+
outputs: new Uint8Array(),
94+
children: [
95+
{
96+
kind: 3,
97+
success: true,
98+
isCheatcode: false,
99+
gasUsed: 0n,
100+
value: 0n,
101+
contract: "console",
102+
inputs: { name: "log", arguments: ['"Fuzz testing inc fail"'] },
103+
outputs: new Uint8Array(),
104+
children: [],
105+
},
106+
{
107+
kind: 0,
108+
success: true,
109+
isCheatcode: false,
110+
gasUsed: 22397n,
111+
value: 0n,
112+
contract: "Counter",
113+
inputs: { name: "inc", arguments: [] },
114+
outputs: new Uint8Array(),
115+
children: [],
116+
},
117+
{
118+
kind: 3,
119+
success: true,
120+
isCheatcode: false,
121+
gasUsed: 402n,
122+
value: 0n,
123+
contract: "Counter",
124+
inputs: { name: "x", arguments: [] },
125+
outputs: "1",
126+
children: [],
127+
},
128+
],
129+
},
130+
];
131+
132+
const expected = ` [127552] ${chalk.green("FailingCounterTest")}::${chalk.green("setUp")}()
133+
├─ [0] ${chalk.green("console")}::${chalk.green("log")}("Setting up") ${chalk.yellow("[staticcall]")}
134+
├─ [68915] ${chalk.yellow("→ new")} Counter@0x010203
135+
| └─ ${chalk.green("←")} 344 bytes of code
136+
└─ [0] ${chalk.green("console")}::${chalk.green("log")}("Counter set up") ${chalk.yellow("[staticcall]")}
137+
[32272] ${chalk.green("FailingCounterTest")}::${chalk.green("testFailFuzzInc")}(1)
138+
├─ [0] ${chalk.green("console")}::${chalk.green("log")}("Fuzz testing inc fail") ${chalk.yellow("[staticcall]")}
139+
├─ [22397] ${chalk.green("Counter")}::${chalk.green("inc")}()
140+
└─ [402] ${chalk.green("Counter")}::${chalk.green("x")}() ${chalk.yellow("[staticcall]")}
141+
└─ ${chalk.green("←")} 1`;
142+
143+
const actual = formatTraces(traces, 2);
144+
145+
assert.equal(expected, actual);
146+
});
147+
148+
it("should return an empty string for empty traces", async () => {
149+
const traces: CallTrace[] = [];
150+
151+
const expected = "";
152+
153+
const actual = formatTraces(traces, 2);
154+
155+
assert.equal(expected, actual);
156+
});
157+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { IncludeTraces } from "@ignored/edr-optimism";
5+
6+
import { solidityTestConfigToSolidityTestRunnerConfigArgs } from "../../../../src/internal/builtin-plugins/solidity-test/helpers.js";
7+
import { GENERIC_CHAIN_TYPE } from "../../../../src/internal/constants.js";
8+
9+
describe("solidityTestConfigToSolidityTestRunnerConfigArgs", () => {
10+
it("should not include traces for verbosity level 0 through 2", async () => {
11+
for (const verbosity of [0, 1, 2]) {
12+
const args = solidityTestConfigToSolidityTestRunnerConfigArgs(
13+
GENERIC_CHAIN_TYPE,
14+
process.cwd(),
15+
{},
16+
verbosity,
17+
);
18+
19+
assert.equal(args.includeTraces, IncludeTraces.None);
20+
}
21+
});
22+
23+
it("should include failing traces for verbosity level 3 and 4", async () => {
24+
for (const verbosity of [3, 4]) {
25+
const args = solidityTestConfigToSolidityTestRunnerConfigArgs(
26+
GENERIC_CHAIN_TYPE,
27+
process.cwd(),
28+
{},
29+
verbosity,
30+
);
31+
32+
assert.equal(args.includeTraces, IncludeTraces.Failing);
33+
}
34+
});
35+
36+
it("should include all traces for verbosity level 5 and above", async () => {
37+
for (const verbosity of [5, 6, 7]) {
38+
const args = solidityTestConfigToSolidityTestRunnerConfigArgs(
39+
GENERIC_CHAIN_TYPE,
40+
process.cwd(),
41+
{},
42+
verbosity,
43+
);
44+
45+
assert.equal(args.includeTraces, IncludeTraces.All);
46+
}
47+
});
48+
});

0 commit comments

Comments
 (0)