2015-03-06 02:43:34 +00:00
|
|
|
package natspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNotice(t *testing.T) {
|
|
|
|
|
2015-03-08 13:09:13 +00:00
|
|
|
tx := `
|
2015-03-06 02:43:34 +00:00
|
|
|
{
|
2015-03-08 13:09:13 +00:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "eth_call",
|
|
|
|
"params": [{
|
|
|
|
"to": "0x8521742d3f456bd237e312d6e30724960f72517a",
|
|
|
|
"data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
|
|
|
|
}],
|
|
|
|
"id": 6
|
|
|
|
}
|
|
|
|
`
|
2015-03-06 02:43:34 +00:00
|
|
|
|
2015-03-08 13:09:13 +00:00
|
|
|
abi := `
|
|
|
|
[{
|
|
|
|
"name": "multiply",
|
|
|
|
"constant": false,
|
|
|
|
"type": "function",
|
|
|
|
"inputs": [{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint256"
|
|
|
|
}],
|
|
|
|
"outputs": [{
|
|
|
|
"name": "d",
|
|
|
|
"type": "uint256"
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
`
|
|
|
|
|
|
|
|
desc := "Will multiply `a` by 7 and return `a * 7`."
|
|
|
|
|
|
|
|
method := "multiply"
|
|
|
|
|
2015-03-08 13:17:20 +00:00
|
|
|
ns, err := New()
|
2015-03-06 02:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("NewNATSpec error %v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-08 13:09:13 +00:00
|
|
|
notice, err := ns.Notice(tx, abi, method, desc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error got %v", err)
|
|
|
|
}
|
2015-03-06 02:43:34 +00:00
|
|
|
|
|
|
|
expected := "Will multiply 122 by 7 and return 854."
|
|
|
|
if notice != expected {
|
|
|
|
t.Errorf("incorrect notice. expected %v, got %v", expected, notice)
|
|
|
|
} else {
|
|
|
|
t.Logf("returned notice \"%v\"", notice)
|
|
|
|
}
|
2015-03-08 13:09:13 +00:00
|
|
|
|
|
|
|
notice, err = ns.Notice(tx, abi, method, "Will multiply 122 by \"7\" and return 854.")
|
|
|
|
|
|
|
|
expected = "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number"
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error, got nothing (notice: '%v')", err, notice)
|
|
|
|
} else {
|
|
|
|
if err.Error() != expected {
|
|
|
|
t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 13:17:20 +00:00
|
|
|
// https://github.com/ethereum/natspec.js/issues/1
|
2015-03-12 12:08:30 +00:00
|
|
|
badDesc := "Will multiply `e` by 7 and return `a * 7`."
|
|
|
|
notice, err = ns.Notice(tx, abi, method, badDesc)
|
2015-03-08 13:09:13 +00:00
|
|
|
|
2015-03-23 17:36:04 +00:00
|
|
|
expected = "natspec.js error evaluating expression: Natspec evaluation failed, wrong input params"
|
2015-03-08 13:09:13 +00:00
|
|
|
|
2015-03-12 12:08:30 +00: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-08 13:09:13 +00:00
|
|
|
|
|
|
|
notice, err = ns.Notice(tx, abi, "missing_method", desc)
|
|
|
|
|
2015-03-23 17:36:04 +00:00
|
|
|
expected = "natspec.js error evaluating expression: Natspec evaluation failed, method does not exist"
|
2015-03-08 13:09:13 +00: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 02:43:34 +00:00
|
|
|
}
|