Merge pull request #3748 from ethereum/extractScopingTests

Extract scoping tests
This commit is contained in:
chriseth 2018-03-27 15:23:36 +02:00 committed by GitHub
commit af26228159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 179 additions and 223 deletions

49
scripts/extract_test_cases.py Executable file
View 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)

View File

@ -43,229 +43,6 @@ namespace test
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)
{
char const* text = R"(

View File

@ -0,0 +1,6 @@
contract test {
function fun() public { }
function fun() public { }
}
// ----
// DeclarationError: Function with same name and arguments defined twice.

View File

@ -0,0 +1,8 @@
contract test {
function f() pure public {
{ uint x; }
{ uint x; }
}
}
// ----
// DeclarationError: Identifier already declared.

View File

@ -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.

View File

@ -0,0 +1,8 @@
contract test {
function f() pure public {
{ uint x; }
uint x;
}
}
// ----
// DeclarationError: Identifier already declared.

View File

@ -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.

View File

@ -0,0 +1,7 @@
contract test {
uint256 variable;
function f() pure public { uint32 variable; variable = 2; }
}
// ----
// Warning: This declaration shadows an existing declaration.

View File

@ -0,0 +1,11 @@
pragma experimental "v0.5.0";
contract test {
function f() public {
{
uint256 x;
}
x = 2;
}
}
// ----
// DeclarationError: Undeclared identifier.

View File

@ -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"?

View File

@ -0,0 +1,6 @@
contract test {
function f() pure public {
x = 3;
uint x;
}
}

View 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;
}
}
}

View 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;
}
}

View 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.

View File

@ -0,0 +1,10 @@
pragma experimental "v0.5.0";
contract test {
function f() pure public {
for (;; y++){
uint y = 3;
}
}
}
// ----
// DeclarationError: Undeclared identifier.

View File

@ -0,0 +1,6 @@
contract test {
function f() pure public {
x = 4;
uint256 x = 2;
}
}

View File

@ -0,0 +1,5 @@
contract test {
function f() pure public {
uint a = a;
}
}

View File

@ -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"?