aboutsummaryrefslogtreecommitdiffstats
path: root/test.js
blob: 59f46cdab3cfa511d2ceba16285190f4f4f7d6c5 (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
var chai = require('chai');
var natspec = require('../natspec.js');
var assert = chai.assert;

describe('natspec', function () {
    it('should evaluate simple expression', function () {
        // given
        var expression = "`x = 1` + `y = 2` will be equal `x + y`";

        // when
        var result = natspec.evaluateExpression(expression);
        var result2 = natspec.evaluateExpressionSafe(expression);

        // then
        assert.equal(result, "1 + 2 will be equal 3");
        assert.equal(result2, "1 + 2 will be equal 3");
    });

    it('should evalute expression using input params', function () {
        //given
        var expression = "Will multiply `a` by 7 and return `a * 7`.";
        var method = 'multiply';
        var abi = [{
            "name": "multiply",
            "constant": false,
            "type": "function",
            "inputs": [{
                "name": "a",
                "type": "uint256"
            }],
            "outputs": [{
                "name": "d",
                "type": "uint256"
            }]
        }];

        var transaction = {
            "jsonrpc": "2.0",
            "method": "eth_call",
            "params": [{
                "to": "0x8521742d3f456bd237e312d6e30724960f72517a",
                "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
            }],
            "id": 6
        };
        
        var call = {
            method: method,
            abi: abi,
            transaction: transaction
        };

        // when
        var result = natspec.evaluateExpression(expression, call);
        var result2 = natspec.evaluateExpressionSafe(expression, call);
        
        // then
        assert.equal(result, "Will multiply 122 by 7 and return 854.");
        assert.equal(result2, "Will multiply 122 by 7 and return 854.");
    });
    
    it('should evalute expression using input params', function () {
        //given
        var expression = "Will multiply `a` by 7 and return `a * 7`.";
        var method = 'multiply';
        var abi = [{
            "name": "multiply",
            "constant": false,
            "type": "function",
            "inputs": [{
                "name": "b",
                "type": "uint256"
            }],
            "outputs": [{
                "name": "d",
                "type": "uint256"
            }]
        }];

        var transaction = {
            "jsonrpc": "2.0",
            "method": "eth_call",
            "params": [{
                "to": "0x8521742d3f456bd237e312d6e30724960f72517a",
                "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
            }],
            "id": 6
        };
        
        var call = {
            method: method,
            abi: abi,
            transaction: transaction
        };

        // when
        var exceptionThrow = function () { natspec.evaluateExpression(expression, call);}
        var result = natspec.evaluateExpressionSafe(expression, call); 
        
        // then
        assert.equal(result, "Natspec evaluation failed, wrong input params");
        assert.throws(exceptionThrow, "Natspec evaluation failed, wrong input params");
    });

    it('should evalute expression using input params', function () {
        //given
        var expression = "Will multiply `a` by 7 and return `a * 7`.";
        var method = 'multiply2';
        var abi = [{
            "name": "multiply",
            "constant": false,
            "type": "function",
            "inputs": [{
                "name": "a",
                "type": "uint256"
            }],
            "outputs": [{
                "name": "d",
                "type": "uint256"
            }]
        }];

        var transaction = {
            "jsonrpc": "2.0",
            "method": "eth_call",
            "params": [{
                "to": "0x8521742d3f456bd237e312d6e30724960f72517a",
                "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
            }],
            "id": 6
        };
        
        var call = {
            method: method,
            abi: abi,
            transaction: transaction
        };

        // when
        var exceptionThrow = function () { natspec.evaluateExpression(expression, call);}
        var result = natspec.evaluateExpressionSafe(expression, call); 
        
        // then
        assert.equal(result, "Natspec evaluation failed, method does not exist");
        assert.throws(exceptionThrow, "Natspec evaluation failed, method does not exist");
    });
});