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
|
/*
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/>.
*/
/**
* @date 2017
* Unit tests for parsing Julia.
*/
#include "../TestHelper.h"
#include <libsolidity/inlineasm/AsmParser.h>
#include <libsolidity/inlineasm/AsmAnalysis.h>
#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
#include <libsolidity/parsing/Scanner.h>
#include <test/libsolidity/ErrorCheck.h>
#include <boost/optional.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <string>
#include <memory>
using namespace std;
namespace dev
{
namespace solidity
{
namespace test
{
namespace
{
bool parse(string const& _source, ErrorList& errors)
{
try
{
auto scanner = make_shared<Scanner>(CharStream(_source));
auto parserResult = assembly::Parser(errors, true).parse(scanner);
if (parserResult)
return true;
}
catch (FatalError const&)
{
BOOST_FAIL("Fatal error leaked.");
}
return false;
}
boost::optional<Error> parseAndReturnFirstError(string const& _source, bool _allowWarnings = true)
{
ErrorList errors;
if (!parse(_source, errors))
{
BOOST_REQUIRE_EQUAL(errors.size(), 1);
return *errors.front();
}
else
{
// If success is true, there might still be an error in the assembly stage.
if (_allowWarnings && Error::containsOnlyWarnings(errors))
return {};
else if (!errors.empty())
{
if (!_allowWarnings)
BOOST_CHECK_EQUAL(errors.size(), 1);
return *errors.front();
}
}
return {};
}
bool successParse(std::string const& _source, bool _allowWarnings = true)
{
return !parseAndReturnFirstError(_source, _allowWarnings);
}
bool successAssemble(string const& _source, bool _allowWarnings = true)
{
return successParse(_source, _allowWarnings);
}
Error expectError(std::string const& _source, bool _allowWarnings = false)
{
auto error = parseAndReturnFirstError(_source, _allowWarnings);
BOOST_REQUIRE(error);
return *error;
}
}
#define CHECK_ERROR(text, typ, substring) \
do \
{ \
Error err = expectError((text), false); \
BOOST_CHECK(err.type() == (Error::Type::typ)); \
BOOST_CHECK(searchErrorMessage(err, (substring))); \
} while(0)
BOOST_AUTO_TEST_SUITE(JuliaParser)
BOOST_AUTO_TEST_CASE(smoke_test)
{
BOOST_CHECK(successParse("{ }"));
}
BOOST_AUTO_TEST_CASE(vardecl)
{
BOOST_CHECK(successParse("{ let x:u256 := 7:u256 }"));
}
BOOST_AUTO_TEST_CASE(assignment)
{
BOOST_CHECK(successParse("{ let x:u256 := 2:u256 let y:u256 := x }"));
}
BOOST_AUTO_TEST_CASE(function_call)
{
BOOST_CHECK(successParse("{ fun() fun(fun()) }"));
}
BOOST_AUTO_TEST_CASE(vardecl_complex)
{
BOOST_CHECK(successParse("{ let y:u256 := 2:u256 let x:u256 := add(7:u256, mul(6:u256, y)) }"));
}
BOOST_AUTO_TEST_CASE(blocks)
{
BOOST_CHECK(successParse("{ let x:u256 := 7:u256 { let y:u256 := 3:u256 } { let z:u256 := 2:u256 } }"));
}
BOOST_AUTO_TEST_CASE(function_definitions)
{
BOOST_CHECK(successParse("{ function f() { } function g(a:u256) -> x:u256 { } }"));
}
BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
{
BOOST_CHECK(successParse("{ function f(a:u256, d:u256) { } function g(a:u256, d:u256) -> x:u256, y:u256 { } }"));
}
BOOST_AUTO_TEST_CASE(function_calls)
{
BOOST_CHECK(successParse("{ function f(a:u256) -> b:u256 {} function g(a:u256, b:u256, c:u256) {} function x() { g(1:u256, 2:u256, f(mul(2:u256, 3:u256))) x() } }"));
}
BOOST_AUTO_TEST_CASE(label)
{
CHECK_ERROR("{ label: }", ParserError, "Labels are not supported.");
}
BOOST_AUTO_TEST_CASE(instructions)
{
CHECK_ERROR("{ pop }", ParserError, "Call or assignment expected.");
}
BOOST_AUTO_TEST_CASE(push)
{
CHECK_ERROR("{ 0x42:u256 }", ParserError, "Call or assignment expected.");
}
BOOST_AUTO_TEST_CASE(assign_from_stack)
{
CHECK_ERROR("{ =: x:u256 }", ParserError, "Literal or identifier expected.");
}
BOOST_AUTO_TEST_CASE(empty_call)
{
CHECK_ERROR("{ () }", ParserError, "Literal or identifier expected.");
}
BOOST_AUTO_TEST_CASE(lacking_types)
{
CHECK_ERROR("{ let x := 1:u256 }", ParserError, "Expected token Identifier got 'Assign'");
CHECK_ERROR("{ let x:u256 := 1 }", ParserError, "Expected token Colon got 'RBrace'");
CHECK_ERROR("{ function f(a) {} }", ParserError, "Expected token Colon got 'RParen'");
CHECK_ERROR("{ function f(a:u256) -> b {} }", ParserError, "Expected token Colon got 'LBrace'");
}
BOOST_AUTO_TEST_SUITE_END()
}
}
} // end namespaces
|