mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #3748 from ethereum/extractScopingTests
Extract scoping tests
This commit is contained in:
commit
af26228159
49
scripts/extract_test_cases.py
Executable file
49
scripts/extract_test_cases.py
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
#
|
||||||
|
# This script reads C++ or RST source files and writes all
|
||||||
|
# multi-line strings into individual files.
|
||||||
|
# This can be used to extract the Solidity test cases
|
||||||
|
# into files for e.g. fuzz testing as
|
||||||
|
# scripts/isolate_tests.py test/libsolidity/*
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
import hashlib
|
||||||
|
from os.path import join
|
||||||
|
|
||||||
|
def extract_test_cases(path):
|
||||||
|
lines = open(path, 'rb').read().splitlines()
|
||||||
|
|
||||||
|
inside = False
|
||||||
|
delimiter = ''
|
||||||
|
test = ''
|
||||||
|
|
||||||
|
ctr = 1
|
||||||
|
test_name = ''
|
||||||
|
|
||||||
|
for l in lines:
|
||||||
|
if inside:
|
||||||
|
if l.strip().endswith(')' + delimiter + '";'):
|
||||||
|
open('%03d_%s.sol' % (ctr, test_name), 'wb').write(test)
|
||||||
|
ctr += 1
|
||||||
|
inside = False
|
||||||
|
test = ''
|
||||||
|
else:
|
||||||
|
l = re.sub('^\t\t', '', l)
|
||||||
|
l = l.replace('\t', ' ')
|
||||||
|
test += l + '\n'
|
||||||
|
else:
|
||||||
|
m = re.search(r'BOOST_AUTO_TEST_CASE\(([^(]*)\)', l.strip())
|
||||||
|
if m:
|
||||||
|
test_name = m.group(1)
|
||||||
|
m = re.search(r'R"([^(]*)\($', l.strip())
|
||||||
|
if m:
|
||||||
|
inside = True
|
||||||
|
delimiter = m.group(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
path = sys.argv[1]
|
||||||
|
extract_test_cases(path)
|
||||||
|
|
@ -43,229 +43,6 @@ namespace test
|
|||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(SolidityNameAndTypeResolution, AnalysisFramework)
|
BOOST_FIXTURE_TEST_SUITE(SolidityNameAndTypeResolution, AnalysisFramework)
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(double_function_declaration)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
contract test {
|
|
||||||
function fun() public { }
|
|
||||||
function fun() public { }
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Function with same name and arguments defined twice.");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope)
|
|
||||||
{
|
|
||||||
string text = R"(
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
{ uint x; }
|
|
||||||
{ uint x; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Identifier already declared");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope_050)
|
|
||||||
{
|
|
||||||
string text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
{ uint x; }
|
|
||||||
{ uint x; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_WARNING_ALLOW_MULTI(text, (vector<string>{
|
|
||||||
"Unused local variable",
|
|
||||||
"Unused local variable"
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope_activation)
|
|
||||||
{
|
|
||||||
string text = R"(
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
{ uint x; }
|
|
||||||
uint x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Identifier already declared");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope_activation_050)
|
|
||||||
{
|
|
||||||
string text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
{ uint x; }
|
|
||||||
uint x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_WARNING_ALLOW_MULTI(text, (vector<string>{
|
|
||||||
"Unused local variable",
|
|
||||||
"Unused local variable"
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_old)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
x = 4;
|
|
||||||
uint256 x = 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() public {
|
|
||||||
{
|
|
||||||
uint256 x;
|
|
||||||
}
|
|
||||||
x = 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Undeclared identifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_activation_old)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
x = 3;
|
|
||||||
uint x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_activation)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
x = 3;
|
|
||||||
uint x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Undeclared identifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_self_use)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
uint a = a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_self_use_050)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
uint a = a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Undeclared identifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_for)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
for (uint x = 0; x < 10; x ++){
|
|
||||||
x = 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_for2)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
for (uint x = 0; x < 10; x ++)
|
|
||||||
x = 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_for3)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
for (uint x = 0; x < 10; x ++){
|
|
||||||
x = 2;
|
|
||||||
}
|
|
||||||
x = 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Undeclared identifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(scoping_for_decl_in_body)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
pragma experimental "v0.5.0";
|
|
||||||
contract test {
|
|
||||||
function f() pure public {
|
|
||||||
for (;; y++){
|
|
||||||
uint y = 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_ERROR(text, DeclarationError, "Undeclared identifier");
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(name_shadowing)
|
|
||||||
{
|
|
||||||
char const* text = R"(
|
|
||||||
contract test {
|
|
||||||
uint256 variable;
|
|
||||||
function f() public { uint32 variable; variable = 2; }
|
|
||||||
}
|
|
||||||
)";
|
|
||||||
CHECK_SUCCESS(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(name_references)
|
BOOST_AUTO_TEST_CASE(name_references)
|
||||||
{
|
{
|
||||||
char const* text = R"(
|
char const* text = R"(
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
contract test {
|
||||||
|
function fun() public { }
|
||||||
|
function fun() public { }
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Function with same name and arguments defined twice.
|
@ -0,0 +1,8 @@
|
|||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
{ uint x; }
|
||||||
|
{ uint x; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Identifier already declared.
|
@ -0,0 +1,10 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
{ uint x; }
|
||||||
|
{ uint x; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// Warning: Unused local variable.
|
||||||
|
// Warning: Unused local variable.
|
@ -0,0 +1,8 @@
|
|||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
{ uint x; }
|
||||||
|
uint x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Identifier already declared.
|
@ -0,0 +1,10 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
{ uint x; }
|
||||||
|
uint x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// Warning: Unused local variable.
|
||||||
|
// Warning: Unused local variable.
|
7
test/libsolidity/syntaxTests/scoping/name_shadowing.sol
Normal file
7
test/libsolidity/syntaxTests/scoping/name_shadowing.sol
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
contract test {
|
||||||
|
uint256 variable;
|
||||||
|
function f() pure public { uint32 variable; variable = 2; }
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// Warning: This declaration shadows an existing declaration.
|
||||||
|
|
11
test/libsolidity/syntaxTests/scoping/scoping.sol
Normal file
11
test/libsolidity/syntaxTests/scoping/scoping.sol
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() public {
|
||||||
|
{
|
||||||
|
uint256 x;
|
||||||
|
}
|
||||||
|
x = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Undeclared identifier.
|
@ -0,0 +1,9 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
x = 3;
|
||||||
|
uint x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Undeclared identifier. Did you mean "x"?
|
@ -0,0 +1,6 @@
|
|||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
x = 3;
|
||||||
|
uint x;
|
||||||
|
}
|
||||||
|
}
|
8
test/libsolidity/syntaxTests/scoping/scoping_for.sol
Normal file
8
test/libsolidity/syntaxTests/scoping/scoping_for.sol
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
for (uint x = 0; x < 10; x ++){
|
||||||
|
x = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
test/libsolidity/syntaxTests/scoping/scoping_for2.sol
Normal file
7
test/libsolidity/syntaxTests/scoping/scoping_for2.sol
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
for (uint x = 0; x < 10; x ++)
|
||||||
|
x = 2;
|
||||||
|
}
|
||||||
|
}
|
11
test/libsolidity/syntaxTests/scoping/scoping_for3.sol
Normal file
11
test/libsolidity/syntaxTests/scoping/scoping_for3.sol
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
for (uint x = 0; x < 10; x ++){
|
||||||
|
x = 2;
|
||||||
|
}
|
||||||
|
x = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Undeclared identifier.
|
@ -0,0 +1,10 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
for (;; y++){
|
||||||
|
uint y = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Undeclared identifier.
|
6
test/libsolidity/syntaxTests/scoping/scoping_old.sol
Normal file
6
test/libsolidity/syntaxTests/scoping/scoping_old.sol
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
x = 4;
|
||||||
|
uint256 x = 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
uint a = a;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
contract test {
|
||||||
|
function f() pure public {
|
||||||
|
uint a = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// DeclarationError: Undeclared identifier. Did you mean "a"?
|
Loading…
Reference in New Issue
Block a user