diff --git a/tests/code_generation/CodeGeneration.sol b/tests/code_generation/CodeGeneration.sol new file mode 100644 index 000000000..c15017abd --- /dev/null +++ b/tests/code_generation/CodeGeneration.sol @@ -0,0 +1,56 @@ +pragma solidity ^0.8.4; +interface I { + enum SomeEnum { ONE, TWO, THREE } + error ErrorWithEnum(SomeEnum e); +} + +contract TestContract is I { + uint public stateA; + uint private stateB; + address public immutable owner = msg.sender; + mapping(address => mapping(uint => St)) public structs; + + event NoParams(); + event Anonymous() anonymous; + event OneParam(address addr); + event OneParamIndexed(address indexed addr); + + error ErrorSimple(); + error ErrorWithArgs(uint, uint); + error ErrorWithStruct(St s); + + struct St{ + uint v; + } + + function err0() public { + revert ErrorSimple(); + } + function err1() public { + St memory s; + revert ErrorWithStruct(s); + } + function err2(uint a, uint b) public { + revert ErrorWithArgs(a, b); + revert ErrorWithArgs(uint(SomeEnum.ONE), uint(SomeEnum.ONE)); + } + function err3() internal { + revert('test'); + } + function err4() private { + revert ErrorWithEnum(SomeEnum.ONE); + } + + function newSt(uint x) public returns (St memory) { + St memory st; + st.v = x; + structs[msg.sender][x] = st; + return st; + } + function getSt(uint x) public view returns (St memory) { + return structs[msg.sender][x]; + } + function removeSt(St memory st) public { + delete structs[msg.sender][st.v]; + } +} \ No newline at end of file diff --git a/tests/test_code_generation.py b/tests/test_code_generation.py new file mode 100644 index 000000000..9733f7c90 --- /dev/null +++ b/tests/test_code_generation.py @@ -0,0 +1,25 @@ +import os + +from solc_select import solc_select + +from slither import Slither +from slither.core.expressions import Literal +from slither.utils.code_generation import ( + generate_interface, + generate_interface_function_signature, + generate_struct_interface_str, +) + +SLITHER_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +CODE_TEST_ROOT = os.path.join(SLITHER_ROOT, "tests", "code_generation") + + +def test_interface_generation() -> None: + solc_select.switch_global_version("0.8.4", always_install=True) + + sl = Slither(os.path.join(CODE_TEST_ROOT, "CodeGeneration.sol")) + + with open("actual_generated_code.sol", "w", encoding="utf-8") as file: + file.write(generate_interface(sl.get_contract_from_name("TestContract")[0])) + +