aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/SyntaxTest.cpp
blob: 45a851b6008604cabbf530d61825f7a114bfd609 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <test/libsolidity/SyntaxTest.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/throw_exception.hpp>
#include <cctype>
#include <fstream>
#include <memory>
#include <stdexcept>

using namespace dev;
using namespace solidity;
using namespace dev::solidity::test;
using namespace std;
namespace fs = boost::filesystem;
using namespace boost::unit_test;

template<typename IteratorType>
void skipWhitespace(IteratorType& it, IteratorType end)
{
    while (it != end && isspace(*it))
        ++it;
}

template<typename IteratorType>
void skipSlashes(IteratorType& it, IteratorType end)
{
    while (it != end && *it == '/')
        ++it;
}

SyntaxTest::SyntaxTest(string const& _filename)
{
    ifstream file(_filename);
    if (!file)
        BOOST_THROW_EXCEPTION(runtime_error("Cannot open test contract: \"" + _filename + "\"."));
    file.exceptions(ios::badbit);

    m_source = parseSource(file);
    m_expectations = parseExpectations(file);
}

bool SyntaxTest::run(ostream& _stream, string const& _indent)
{
    m_errorList = parseAnalyseAndReturnError(m_source, true, true, true).second;
    if (!matchesExpectations(m_errorList))
    {
        std::string nextIndentLevel = _indent + "\t";
        _stream << _indent << "Expected result:" << endl;
        printExpected(_stream, nextIndentLevel);
        _stream << _indent << "Obtained result:\n";
        printErrorList(_stream, m_errorList, nextIndentLevel);
        return false;
    }
    return true;
}

void SyntaxTest::printExpected(ostream& _stream, string const& _indent) const
{
    if (m_expectations.empty())
        _stream << _indent << "Success" << endl;
    else
        for (auto const& expectation: m_expectations)
            _stream << _indent << expectation.type << ": " << expectation.message << endl;
}

void SyntaxTest::printErrorList(
    ostream& _stream,
    ErrorList const& _errorList,
    string const& _indent
) const
{
    if (_errorList.empty())
        _stream << _indent << "Success" << endl;
    else
        for (auto const& error: _errorList)
            _stream << _indent << error->typeName() << ": " << errorMessage(*error) << endl;
}

bool SyntaxTest::matchesExpectations(ErrorList const& _errorList) const
{
    if (_errorList.size() != m_expectations.size())
        return false;
    else
        for (size_t i = 0; i < _errorList.size(); i++)
            if (
                (_errorList[i]->typeName() != m_expectations[i].type) ||
                (errorMessage(*_errorList[i]) != m_expectations[i].message)
            )
                return false;
    return true;
}

string SyntaxTest::errorMessage(Error const& _e)
{
    if (_e.comment())
        return boost::replace_all_copy(*_e.comment(), "\n", "\\n");
    else
        return "NONE";
}

string SyntaxTest::parseSource(istream& _stream)
{
    string source;
    string line;
    string const delimiter("// ----");
    while (getline(_stream, line))
        if (boost::algorithm::starts_with(line, delimiter))
            break;
        else
            source += line + "\n";
    return source;
}

vector<SyntaxTestExpectation> SyntaxTest::parseExpectations(istream& _stream)
{
    vector<SyntaxTestExpectation> expectations;
    string line;
    while (getline(_stream, line))
    {
        auto it = line.begin();

        skipSlashes(it, line.end());
        skipWhitespace(it, line.end());

        if (it == line.end()) continue;

        auto typeBegin = it;
        while (it != line.end() && *it != ':')
            ++it;
        string errorType(typeBegin, it);

        // skip colon
        if (it != line.end()) it++;

        skipWhitespace(it, line.end());

        string errorMessage(it, line.end());
        expectations.emplace_back(SyntaxTestExpectation{move(errorType), move(errorMessage)});
    }
    return expectations;
}

#if BOOST_VERSION < 105900
test_case *make_test_case(
    function<void()> const& _fn,
    string const& _name,
    string const& /* _filename */,
    size_t /* _line */
)
{
    return make_test_case(_fn, _name);
}
#endif

int SyntaxTest::registerTests(
    boost::unit_test::test_suite& _suite,
    boost::filesystem::path const& _basepath,
    boost::filesystem::path const& _path
)
{
    int numTestsAdded = 0;
    fs::path fullpath = _basepath / _path;
    if (fs::is_directory(fullpath))
    {
        test_suite* sub_suite = BOOST_TEST_SUITE(_path.filename().string());
        for (auto const& entry: boost::iterator_range<fs::directory_iterator>(
            fs::directory_iterator(fullpath),
            fs::directory_iterator()
        ))
            numTestsAdded += registerTests(*sub_suite, _basepath, _path / entry.path().filename());
        _suite.add(sub_suite);
    }
    else
    {
        static vector<unique_ptr<string>> filenames;

        filenames.emplace_back(new string(_path.string()));
        _suite.add(make_test_case(
            [fullpath]
            {
                std::stringstream errorStream;
                if (!SyntaxTest(fullpath.string()).run(errorStream, ""))
                    BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str());
            },
            _path.stem().string(),
            *filenames.back(),
            0
        ));
        numTestsAdded = 1;
    }
    return numTestsAdded;
}