2015-03-06 04:43:34 +02:00
|
|
|
package natspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
func makeUserdoc(desc string) []byte {
|
|
|
|
return []byte(`
|
|
|
|
{
|
|
|
|
"source": "...",
|
|
|
|
"language": "Solidity",
|
|
|
|
"languageVersion": 1,
|
|
|
|
"methods": {
|
|
|
|
"multiply(uint256)": {
|
2015-03-31 09:28:12 +03:00
|
|
|
"notice": "` + desc + `"
|
2015-03-31 02:49:38 +03:00
|
|
|
},
|
|
|
|
"balance(address)": {
|
2015-03-31 09:28:12 +03:00
|
|
|
"notice": "` + "`(balanceInmGAV / 1000).fixed(0,3)`" + ` GAV is the total funds available to ` + "`who.address()`." + `"
|
2015-03-31 02:49:38 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"invariants": [
|
|
|
|
{ "notice": "The sum total amount of GAV in the system is 1 million." }
|
|
|
|
],
|
|
|
|
"construction": [
|
|
|
|
{ "notice": "Endows ` + "`message.caller.address()`" + ` with 1m GAV." }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
}
|
2015-03-06 04:43:34 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
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"
|
2015-03-08 15:09:13 +02:00
|
|
|
}]
|
2015-03-31 02:49:38 +03:00
|
|
|
}]
|
|
|
|
`)
|
|
|
|
|
|
|
|
func TestNotice(t *testing.T) {
|
2015-03-08 15:09:13 +02:00
|
|
|
|
|
|
|
desc := "Will multiply `a` by 7 and return `a * 7`."
|
2015-03-31 02:49:38 +03:00
|
|
|
expected := "Will multiply 122 by 7 and return 854."
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
userdoc := makeUserdoc(desc)
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 09:28:12 +03:00
|
|
|
ns, err := NewWithDocs(abi, userdoc, tx)
|
2015-03-06 04:43:34 +02:00
|
|
|
if err != nil {
|
2015-03-31 02:49:38 +03:00
|
|
|
t.Errorf("New: error: %v", err)
|
2015-03-06 04:43:34 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 09:28:12 +03:00
|
|
|
notice, err := ns.Notice()
|
2015-03-08 15:09:13 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2015-03-31 02:49:38 +03:00
|
|
|
t.Errorf("expected no error, got %v", err)
|
2015-03-08 15:09:13 +02:00
|
|
|
}
|
2015-03-06 04:43:34 +02:00
|
|
|
|
|
|
|
if notice != expected {
|
|
|
|
t.Errorf("incorrect notice. expected %v, got %v", expected, notice)
|
|
|
|
} else {
|
|
|
|
t.Logf("returned notice \"%v\"", notice)
|
|
|
|
}
|
2015-03-31 02:49:38 +03:00
|
|
|
}
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
// test missing method
|
|
|
|
func TestMissingMethod(t *testing.T) {
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
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"
|
|
|
|
|
2015-03-31 09:28:12 +03:00
|
|
|
ns, err := NewWithDocs(abi, userdoc, tx)
|
2015-03-31 02:49:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("New: error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
notice, err := ns.noticeForMethod(tx, "missing_method", "")
|
2015-03-08 15:09:13 +02:00
|
|
|
|
|
|
|
if err == nil {
|
2015-03-31 02:49:38 +03:00
|
|
|
t.Errorf("expected error, got nothing (notice: '%v')", notice)
|
2015-03-08 15:09:13 +02:00
|
|
|
} else {
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 02:49:38 +03:00
|
|
|
}
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
// test invalid desc
|
2015-03-31 09:28:12 +03:00
|
|
|
/*
|
2015-03-31 02:49:38 +03:00
|
|
|
func TestInvalidDesc(t *testing.T) {
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
desc := "Will multiply 122 by \"7\" and return 854."
|
|
|
|
expected := "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number"
|
|
|
|
|
|
|
|
userdoc := makeUserdoc(desc)
|
|
|
|
|
2015-03-31 09:28:12 +03:00
|
|
|
ns, err := NewWithDocs(abi, userdoc, tx)
|
2015-03-31 02:49:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("New: error: %v", err)
|
|
|
|
}
|
2015-03-31 09:28:12 +03:00
|
|
|
notice, err := ns.Notice()
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-12 14:08:30 +02:00
|
|
|
if err == nil {
|
2015-03-31 02:49:38 +03:00
|
|
|
t.Errorf("expected error, got nothing (notice: '%v')", err, notice)
|
2015-03-12 14:08:30 +02:00
|
|
|
} else {
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 02:49:38 +03:00
|
|
|
}
|
2015-03-31 09:28:12 +03:00
|
|
|
*/
|
2015-03-31 02:49:38 +03:00
|
|
|
|
|
|
|
// 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"
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 02:49:38 +03:00
|
|
|
userdoc := makeUserdoc(desc)
|
|
|
|
|
2015-03-31 09:28:12 +03:00
|
|
|
ns, err := NewWithDocs(abi, userdoc, tx)
|
2015-03-31 02:49:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("New: error: %v", err)
|
|
|
|
}
|
2015-03-08 15:09:13 +02:00
|
|
|
|
2015-03-31 09:28:12 +03:00
|
|
|
notice, err := ns.Notice()
|
2015-03-08 15:09:13 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:43:34 +02:00
|
|
|
}
|