aboutsummaryrefslogtreecommitdiffstats
path: root/common/natspec/natspec_test.go
blob: 35a59469a79178042f81d48bb0c81ef88487c0a2 (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
package natspec

import (
    "testing"
)

func makeUserdoc(desc string) []byte {
    return []byte(`
{
  "source": "...",
  "language": "Solidity",
  "languageVersion": 1,
  "methods": {
    "multiply(uint256)": {
      "notice":  "` + desc + `"
    },
    "balance(address)": {
      "notice": "` + "`(balanceInmGAV / 1000).fixed(0,3)`" + ` GAV is the total funds available to ` + "`who.address()`." + `"
    }
  },
  "invariants": [
    { "notice": "The sum total amount of GAV in the system is 1 million." }
  ],
  "construction": [
    { "notice": "Endows ` + "`message.caller.address()`" + ` with 1m GAV." }
  ]
}
`)
}

var data = "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"

var tx = `
{
  "jsonrpc": "2.0",
  "method": "eth_call",
  "params": [{
      "to": "0x8521742d3f456bd237e312d6e30724960f72517a",
      "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
  }],
  "id": 6
}
`

var abi = []byte(`
[{
  "name": "multiply",
  "constant": false,
  "type": "function",
  "inputs": [{
    "name": "a",
    "type": "uint256"
  }],
  "outputs": [{
    "name": "d",
    "type": "uint256"
  }]
}]
`)

func TestNotice(t *testing.T) {

    desc := "Will multiply `a` by 7 and return `a * 7`."
    expected := "Will multiply 122 by 7 and return 854."

    userdoc := makeUserdoc(desc)

    ns, err := NewWithDocs(abi, userdoc, tx)
    if err != nil {
        t.Errorf("New: error: %v", err)
    }

    notice, err := ns.Notice()

    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }

    if notice != expected {
        t.Errorf("incorrect notice. expected %v, got %v", expected, notice)
    } else {
        t.Logf("returned notice \"%v\"", notice)
    }
}

// test missing method
func TestMissingMethod(t *testing.T) {

    desc := "Will multiply `a` by 7 and return `a * 7`."
    userdoc := makeUserdoc(desc)
    expected := "natspec.js error evaluating expression: Natspec evaluation failed, method does not exist"

    ns, err := NewWithDocs(abi, userdoc, tx)
    if err != nil {
        t.Errorf("New: error: %v", err)
    }

    notice, err := ns.noticeForMethod(tx, "missing_method", "")

    if err == nil {
        t.Errorf("expected error, got nothing (notice: '%v')", notice)
    } else {
        if err.Error() != expected {
            t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
        }
    }
}

// test invalid desc

func TestInvalidDesc(t *testing.T) {

    desc := "Will multiply 122 by \"7\" and return 854."
    expected := "invalid character '7' after object key:value pair"

    userdoc := makeUserdoc(desc)

    _, err := NewWithDocs(abi, userdoc, tx)
    if err == nil {
        t.Errorf("expected error, got nothing", err)
    } else {
        if err.Error() != expected {
            t.Errorf("expected error '%s' got '%v'", expected, err)
        }
    }
}

// test wrong input params
func TestWrongInputParams(t *testing.T) {

    desc := "Will multiply `e` by 7 and return `a * 7`."
    expected := "natspec.js error evaluating expression: Natspec evaluation failed, wrong input params"

    userdoc := makeUserdoc(desc)

    ns, err := NewWithDocs(abi, userdoc, tx)
    if err != nil {
        t.Errorf("New: error: %v", err)
    }

    notice, err := ns.Notice()

    if err == nil {
        t.Errorf("expected error, got nothing (notice: '%v')", notice)
    } else {
        if err.Error() != expected {
            t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
        }
    }

}