mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Provide access to the name of contracts.
This commit is contained in:
@@ -15113,6 +15113,55 @@ BOOST_AUTO_TEST_CASE(code_access_content)
|
||||
ABI_CHECK(callContractFunction("testCreation()"), encodeArgs(true));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(contract_name)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
string public nameAccessor = type(C).name;
|
||||
string public constant constantNameAccessor = type(C).name;
|
||||
|
||||
function name() public pure returns (string memory) {
|
||||
return type(C).name;
|
||||
}
|
||||
}
|
||||
contract D is C {
|
||||
function name() public pure returns (string memory) {
|
||||
return type(D).name;
|
||||
}
|
||||
function name2() public pure returns (string memory) {
|
||||
return type(C).name;
|
||||
}
|
||||
}
|
||||
contract ThisIsAVeryLongContractNameExceeding256bits {
|
||||
string public nameAccessor = type(ThisIsAVeryLongContractNameExceeding256bits).name;
|
||||
string public constant constantNameAccessor = type(ThisIsAVeryLongContractNameExceeding256bits).name;
|
||||
|
||||
function name() public pure returns (string memory) {
|
||||
return type(ThisIsAVeryLongContractNameExceeding256bits).name;
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
bytes argsC = encodeArgs(u256(0x20), u256(1), "C");
|
||||
ABI_CHECK(callContractFunction("name()"), argsC);
|
||||
ABI_CHECK(callContractFunction("nameAccessor()"), argsC);
|
||||
ABI_CHECK(callContractFunction("constantNameAccessor()"), argsC);
|
||||
|
||||
compileAndRun(sourceCode, 0, "D");
|
||||
bytes argsD = encodeArgs(u256(0x20), u256(1), "D");
|
||||
ABI_CHECK(callContractFunction("name()"), argsD);
|
||||
ABI_CHECK(callContractFunction("name2()"), argsC);
|
||||
|
||||
string longName = "ThisIsAVeryLongContractNameExceeding256bits";
|
||||
compileAndRun(sourceCode, 0, longName);
|
||||
bytes argsLong = encodeArgs(u256(0x20), u256(longName.length()), longName);
|
||||
ABI_CHECK(callContractFunction("name()"), argsLong);
|
||||
ABI_CHECK(callContractFunction("nameAccessor()"), argsLong);
|
||||
ABI_CHECK(callContractFunction("constantNameAccessor()"), argsLong);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
contract Test {
|
||||
function f() public pure returns (string memory) {
|
||||
return type(Test).name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
contract C {
|
||||
string public constant name = type(C).name;
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,10 @@
|
||||
contract Test {
|
||||
function f() public pure returns (string memory) {
|
||||
return type(C).name;
|
||||
}
|
||||
}
|
||||
|
||||
contract C {
|
||||
function f() pure public {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user