aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/ASTJSON.cpp
blob: d22db18dc2e4fea1d81e116ed4730c0e76ad1876 (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
/*
    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() { 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() { 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(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