-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathAbiDecode.t.sol
More file actions
48 lines (40 loc) · 1.55 KB
/
AbiDecode.t.sol
File metadata and controls
48 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Test} from "forge-std/Test.sol";
import {AbiDecode} from "@openzeppelin/contracts/utils/AbiDecode.sol";
import {Memory} from "@openzeppelin/contracts/utils/Memory.sol";
contract AbiDecodeTest is Test {
using AbiDecode for *;
using Memory for *;
function testDecode(bytes memory buffer) public pure {
(bool success, Memory.Slice output) = abi.encode(buffer).tryDecodeBytes();
assertTrue(success);
assertEq(output.toBytes(), buffer);
}
function testDecodeNoRevert(bytes memory buffer) public pure {
(bool success, Memory.Slice output) = buffer.tryDecodeBytes();
if (success) {
assertEq(output.toBytes(), abi.decode(buffer, (bytes)));
} else {
assertEq(output.toBytes(), new bytes(0));
}
}
function testDecodeCalldata(bytes memory buffer) public view {
(bool success, bytes memory output) = this.__tryDecodeBytesCalldata(abi.encode(buffer));
assertTrue(success);
assertEq(output, buffer);
}
function testDecodeCalldataNoRevert(bytes calldata buffer) public pure {
(bool success, bytes calldata output) = buffer.tryDecodeBytesCalldata();
if (success) {
assertEq(output, new bytes(0));
} else {
assertEq(output, new bytes(0));
}
}
function __tryDecodeBytesCalldata(
bytes calldata buffer
) external pure returns (bool success, bytes calldata output) {
return buffer.tryDecodeBytesCalldata();
}
}