aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/ASTJSON.cpp
blob: 03e74097aea3c0f52a846baf115f00b63c307a84 (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
/*
    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/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2016
 * Tests for the json ast output.
 */

#include <test/Options.h>

#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/ast/ASTJsonConverter.h>

#include <boost/test/unit_test.hpp>

#include <string>

using namespace std;

namespace dev
{
namespace solidity
{
namespace test
{

BOOST_AUTO_TEST_SUITE(SolidityASTJSON)

BOOST_AUTO_TEST_CASE(short_type_name)
{
    CompilerStack c;
    c.addSource("a", "contract c { function f() public { uint[] memory x; } }");
    c.setEVMVersion(dev::test::Options::get().evmVersion());
    c.parseAndAnalyze();
    map<string, unsigned> sourceIndices;
    sourceIndices["a"] = 1;
    Json::Value astJson = ASTJsonConverter(false, sourceIndices).toJson(c.ast("a"));
    Json::Value varDecl = astJson["nodes"][0]["nodes"][0]["body"]["statements"][0]["declarations"][0];
    BOOST_CHECK_EQUAL(varDecl["storageLocation"], "memory");
    BOOST_CHECK_EQUAL(varDecl["typeDescriptions"]["typeIdentifier"], "t_array$_t_uint256_$dyn_memory_ptr");
    BOOST_CHECK_EQUAL(varDecl["typeDescriptions"]["typeString"], "uint256[]");
}

BOOST_AUTO_TEST_CASE(short_type_name_ref)
{
    CompilerStack c;
    c.addSource("a", "contract c { function f() public { uint[][] memory rows; } }");
    c.setEVMVersion(dev::test::Options::get().evmVersion());
    c.parseAndAnalyze();
    map<string, unsigned> sourceIndices;
    sourceIndices["a"] = 1;
    Json::Value astJson = ASTJsonConverter(false, sourceIndices).toJson(c.ast("a"));
    Json::Value varDecl = astJson["nodes"][0]["nodes"][0]["body"]["statements"][0]["declarations"][0];
    BOOST_CHECK_EQUAL(varDecl["storageLocation"], "memory");
    BOOST_CHECK_EQUAL(varDecl["typeName"]["typeDescriptions"]["typeIdentifier"], "t_array$_t_array$_t_uint256_$dyn_storage_$dyn_storage_ptr");
    BOOST_CHECK_EQUAL(varDecl["typeName"]["typeDescriptions"]["typeString"], "uint256[][]");
}

BOOST_AUTO_TEST_CASE(long_type_name_binary_operation)
{
    CompilerStack c;
    c.addSource("a", "contract c { function f() public { uint a = 2 + 3; } }");
    c.setEVMVersion(dev::test::Options::get().evmVersion());
    c.parseAndAnalyze();
    map<string, unsigned> sourceIndices;
    sourceIndices["a"] = 1;
    Json::Value astJson = ASTJsonConverter(false, sourceIndices).toJson(c.ast("a"));
    Json::Value varDecl = astJson["nodes"][0]["nodes"][0]["body"]["statements"][0]["initialValue"]["commonType"];
    BOOST_CHECK_EQUAL(varDecl["typeIdentifier"], "t_rational_5_by_1");
    BOOST_CHECK_EQUAL(varDecl["typeString"], "int_const 5");
}

BOOST_AUTO_TEST_CASE(long_type_name_identifier)
{
    CompilerStack c;
    c.addSource("a", "contract c { uint[] a; function f() public { uint[] b = a; } }");
    c.setEVMVersion(dev::test::Options::get().evmVersion());
    c.parseAndAnalyze();
    map<string, unsigned> sourceIndices;
    sourceIndices["a"] = 1;
    Json::Value astJson = ASTJsonConverter(false, sourceIndices).toJson(c.ast("a"));
    Json::Value varDecl = astJson["nodes"][0]["nodes"][1]["body"]["statements"][0]["initialValue"];
    BOOST_CHECK_EQUAL(varDecl["typeDescriptions"]["typeIdentifier"], "t_array$_t_uint256_$dyn_storage");
    BOOST_CHECK_EQUAL(varDecl["typeDescriptions"]["typeString"], "uint256[] storage ref");
}

BOOST_AUTO_TEST_CASE(documentation)
{
    CompilerStack c;
    c.addSource("a", "/**This contract is empty*/ contract C {}");
    c.addSource("b",
        "/**This contract is empty"
        " and has a line-breaking comment.*/"
        "contract C {}"
    );
    c.addSource("c",
        "contract C {"
        "  /** Some comment on Evt.*/ event Evt();"
        "  /** Some comment on mod.*/ modifier mod() { _; }"
        "  /** Some comment on fn.*/ function fn() public {}"
        "}"
    );
    c.setEVMVersion(dev::test::Options::get().evmVersion());
    c.parseAndAnalyze();
    map<string, unsigned> sourceIndices;
    sourceIndices["a"] = 0;
    sourceIndices["b"] = 1;
    sourceIndices["c"] = 2;
    //same tests for non-legacy mode
    Json::Value astJsonA = ASTJsonConverter(false, sourceIndices).toJson(c.ast("a"));
    Json::Value documentationA = astJsonA["nodes"][0]["documentation"];
    BOOST_CHECK_EQUAL(documentationA, "This contract is empty");
    Json::Value astJsonB = ASTJsonConverter(false, sourceIndices).toJson(c.ast("b"));
    Json::Value documentationB = astJsonB["nodes"][0]["documentation"];
    BOOST_CHECK_EQUAL(documentationB, "This contract is empty and has a line-breaking comment.");
    Json::Value astJsonC = ASTJsonConverter(false, sourceIndices).toJson(c.ast("c"));
    Json::Value documentationC0 = astJsonC["nodes"][0]["nodes"][0]["documentation"];
    Json::Value documentationC1 = astJsonC["nodes"][0]["nodes"][1]["documentation"];
    Json::Value documentationC2 = astJsonC["nodes"][0]["nodes"][2]["documentation"];
    BOOST_CHECK_EQUAL(documentationC0, "Some comment on Evt.");
    BOOST_CHECK_EQUAL(documentationC1, "Some comment on mod.");
    BOOST_CHECK_EQUAL(documentationC2, "Some comment on fn.");
}


BOOST_AUTO_TEST_SUITE_END()

}
}
} // end namespaces