Skip to content

Commit 077b788

Browse files
committed
Adds documentation and fixes to semantic test framework.
1 parent ba7b66a commit 077b788

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

test/ExecutionFramework.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ExecutionFramework
5353

5454
public:
5555
ExecutionFramework();
56-
ExecutionFramework(std::string const& _ipcPath);
56+
explicit ExecutionFramework(std::string const& _ipcPath);
5757
virtual ~ExecutionFramework() = default;
5858

5959
virtual bytes const& compileAndRunWithoutCheck(

test/libsolidity/SemanticTest.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ bool SemanticTest::run(ostream& _stream, string const& _linePrefix, bool const _
8484
FormattedScope(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
8585
for (auto const& test: m_tests)
8686
{
87-
printFunctionCall(_stream, test.call, _linePrefix);
88-
printFunctionCallTest(_stream, test, true, _linePrefix, _formatted);
87+
printFunctionCallHighlighted(_stream, test.call, _linePrefix);
88+
printFunctionCallTestHighlighted(_stream, test, true, _linePrefix, _formatted);
8989
}
9090

9191
FormattedScope(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
9292
for (auto const& test: m_tests)
9393
{
94-
printFunctionCall(_stream, test.call, _linePrefix);
95-
printFunctionCallTest(_stream, test, false, _linePrefix, _formatted);
94+
printFunctionCallHighlighted(_stream, test.call, _linePrefix);
95+
printFunctionCallTestHighlighted(_stream, test, false, _linePrefix, _formatted);
9696
}
9797
return false;
9898
}
@@ -111,8 +111,8 @@ void SemanticTest::printUpdatedExpectations(ostream& _stream, string const& _lin
111111
{
112112
for (auto const& test: m_tests)
113113
{
114-
printFunctionCall(_stream, test.call, _linePrefix);
115-
printFunctionCallTest(_stream, test, false, _linePrefix);
114+
printFunctionCallHighlighted(_stream, test.call, _linePrefix);
115+
printFunctionCallTestHighlighted(_stream, test, false, _linePrefix);
116116
}
117117
}
118118

@@ -121,16 +121,16 @@ void SemanticTest::parseExpectations(istream& _stream)
121121
{
122122
TestFileParser parser{_stream};
123123
for (auto const& call: parser.parseFunctionCalls())
124-
m_tests.emplace_back(FunctionCallTest{std::move(call), false, bytes{}, string{}});
124+
m_tests.emplace_back(FunctionCallTest{call, bytes{}, string{}});
125125
}
126126

127127
bool SemanticTest::deploy(string const& _contractName, u256 const& _value, bytes const& _arguments)
128128
{
129-
auto output = compileAndRunWithoutCheck(m_source, _value, _contractName, _arguments, m_libraryAddresses);
129+
auto output = compileAndRunWithoutCheck(m_source, _value, _contractName, _arguments);
130130
return !output.empty() && m_transactionSuccessful;
131131
}
132132

133-
void SemanticTest::printFunctionCall(ostream& _stream, FunctionCall const& _call, string const& _linePrefix) const
133+
void SemanticTest::printFunctionCallHighlighted(ostream& _stream, FunctionCall const& _call, string const& _linePrefix) const
134134
{
135135
_stream << _linePrefix << _call.signature;
136136
if (_call.value > u256(0))
@@ -142,18 +142,18 @@ void SemanticTest::printFunctionCall(ostream& _stream, FunctionCall const& _call
142142
_stream << endl;
143143
}
144144

145-
void SemanticTest::printFunctionCallTest(
145+
void SemanticTest::printFunctionCallTestHighlighted(
146146
ostream& _stream,
147147
FunctionCallTest const& _test,
148-
bool _expected,
148+
bool _printExcepted,
149149
string const& _linePrefix,
150150
bool const _formatted
151151
) const
152152
{
153153
_stream << _linePrefix;
154154
if (_formatted && !_test.matchesExpectation())
155155
_stream << formatting::RED_BACKGROUND;
156-
string output = _expected ? _test.call.expectations.output : _test.output;
156+
string output = _printExcepted ? _test.call.expectations.output : _test.output;
157157
_stream << boost::algorithm::trim_copy(output);
158158
if (_formatted && !_test.matchesExpectation())
159159
_stream << formatting::RESET;

test/libsolidity/SemanticTest.h

+37-6
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ namespace solidity
3333
namespace test
3434
{
3535

36+
/**
37+
* Class that represents a semantic test (or end-to-end test) and allows running it as part of the
38+
* boost unit test environment or isoltest. It reads the Solidity source and an additional comment
39+
* section from the given file. This comment section should define a set of functions to be called
40+
* and an expected result they return after being executed.
41+
*/
3642
class SemanticTest: public SolidityExecutionFramework, public TestCase
3743
{
3844
public:
3945
static std::unique_ptr<TestCase> create(Config const& _options)
40-
{ return std::unique_ptr<TestCase>(new SemanticTest(_options.filename, _options.ipcPath)); }
46+
{ return std::make_unique<SemanticTest>(_options.filename, _options.ipcPath); }
4147

4248
explicit SemanticTest(std::string const& _filename, std::string const& _ipcPath);
4349

@@ -46,17 +52,28 @@ class SemanticTest: public SolidityExecutionFramework, public TestCase
4652
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix = "") const override;
4753

4854
private:
55+
/**
56+
* Represents a function call and the result it returned. It stores the call
57+
* representation itself, the actual byte result (if any) and a string representation
58+
* used for the interactive update routine provided by isoltest. It also provides
59+
* functionality to compare the actual result with the expectations attached to the
60+
* call object, as well as a way to reset the result if executed multiple times.
61+
*/
4962
struct FunctionCallTest
5063
{
5164
FunctionCall call;
52-
bool status;
5365
bytes rawBytes;
5466
std::string output;
67+
bool status = false;
68+
/// Compares raw expectations (which are converted to a byte representation before),
69+
/// and also the expected transaction status of the function call to the actual test results.
5570
bool matchesExpectation() const
5671
{
5772
auto expectedByteFormat = TestFileParser::formattedStringToBytes(call.expectations.raw);
5873
return status == call.expectations.status && rawBytes == expectedByteFormat.first;
5974
}
75+
/// Resets current results in case the function was called and the result
76+
/// stored already (e.g. if test case was updated via isoltest).
6077
void reset()
6178
{
6279
status = false;
@@ -65,20 +82,34 @@ class SemanticTest: public SolidityExecutionFramework, public TestCase
6582
}
6683
};
6784

85+
/// Instantiates a test file parser that parses the additional comment section at the end of
86+
/// the input stream \param _stream. Each function call is represented using a `FunctionCallTest`
87+
/// and added to the list of call to be executed when `run()` is called.
88+
/// Throws if parsing expectations failed.
6889
void parseExpectations(std::istream& _stream);
90+
91+
/// Compiles and deploys currently held source.
92+
/// Returns true if deployment was successful, false otherwise.
6993
bool deploy(std::string const& _contractName, u256 const& _value, bytes const& _arguments);
7094

71-
void printFunctionCall(std::ostream& _stream, FunctionCall const& _call, std::string const& _linePrefix = "") const;
72-
void printFunctionCallTest(
95+
/// Prints a formatted and highlighted function call used for visual feedback in isoltest.
96+
void printFunctionCallHighlighted(
97+
std::ostream& _stream,
98+
FunctionCall const& _call,
99+
std::string const& _linePrefix = ""
100+
) const;
101+
102+
/// Prints a formatted and highlighted function call test used for visual feedback in isoltest.
103+
/// If \param _printExpected is true, it prints the expected result instead of the actual result.
104+
void printFunctionCallTestHighlighted(
73105
std::ostream& _stream,
74106
FunctionCallTest const& test,
75-
bool _expected,
107+
bool _printExpected,
76108
std::string const& _linePrefix = "",
77109
bool const _formatted = false
78110
) const;
79111

80112
std::string m_source;
81-
std::map<std::string, dev::test::Address> m_libraryAddresses;
82113
std::vector<FunctionCallTest> m_tests;
83114
};
84115

0 commit comments

Comments
 (0)