From d0b3536593fafe19ad31225387d09a62cac6196c Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 31 Mar 2015 00:49:38 +0100 Subject: new natspec - constructor takes abidoc, userdoc - json parsing of userdoc - method found by abi data - notice found from method --- common/natspec/natspec_test.go | 144 +++++++++++++++++++++++++++++------------ 1 file changed, 101 insertions(+), 43 deletions(-) (limited to 'common/natspec/natspec_test.go') diff --git a/common/natspec/natspec_test.go b/common/natspec/natspec_test.go index 3b548817b..39c80a155 100644 --- a/common/natspec/natspec_test.go +++ b/common/natspec/natspec_test.go @@ -4,87 +4,145 @@ import ( "testing" ) -func TestNotice(t *testing.T) { +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." } + ] +} +`) +} - tx := ` - { - "jsonrpc": "2.0", - "method": "eth_call", - "params": [{ - "to": "0x8521742d3f456bd237e312d6e30724960f72517a", - "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" - }], - "id": 6 - } - ` - - abi := ` - [{ - "name": "multiply", - "constant": false, - "type": "function", - "inputs": [{ - "name": "a", - "type": "uint256" - }], - "outputs": [{ - "name": "d", - "type": "uint256" - }] +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." - method := "multiply" + userdoc := makeUserdoc(desc) - ns, err := New() + ns, err := NewWithDocs(abi, userdoc) if err != nil { - t.Errorf("NewNATSpec error %v", err) + t.Errorf("New: error: %v", err) } - notice, err := ns.Notice(tx, abi, method, desc) + notice, err := ns.Notice(tx, desc) if err != nil { - t.Errorf("expected no error got %v", err) + t.Errorf("expected no error, got %v", err) } - 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) } +} - notice, err = ns.Notice(tx, abi, method, "Will multiply 122 by \"7\" and return 854.") +// test missing method +func TestMissingMethod(t *testing.T) { - expected = "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number" + 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) + 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')", err, notice) + 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) } } +} - // https://github.com/ethereum/natspec.js/issues/1 - badDesc := "Will multiply `e` by 7 and return `a * 7`." - notice, err = ns.Notice(tx, abi, method, badDesc) +// test invalid desc +func TestInvalidDesc(t *testing.T) { - expected = "natspec.js error evaluating expression: Natspec evaluation failed, wrong input params" + desc := "Will multiply 122 by \"7\" and return 854." + expected := "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number" + + userdoc := makeUserdoc(desc) + + ns, err := NewWithDocs(abi, userdoc) + if err != nil { + t.Errorf("New: error: %v", err) + } + notice, err := ns.Notice(tx, data) if err == nil { - t.Errorf("expected error, got nothing (notice: '%v')", notice) + 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) } } +} + +// 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" - notice, err = ns.Notice(tx, abi, "missing_method", desc) + userdoc := makeUserdoc(desc) + + ns, err := NewWithDocs(abi, userdoc) + if err != nil { + t.Errorf("New: error: %v", err) + } - expected = "natspec.js error evaluating expression: Natspec evaluation failed, method does not exist" + notice, err := ns.Notice(tx, desc) if err == nil { t.Errorf("expected error, got nothing (notice: '%v')", notice) -- cgit v1.2.3 From 3136bae4a5f08820d3116b61b783c100c169717c Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Tue, 31 Mar 2015 08:28:12 +0200 Subject: NatSpec, URL register storage retrieval fixed 2/3 tests --- common/natspec/natspec_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'common/natspec/natspec_test.go') diff --git a/common/natspec/natspec_test.go b/common/natspec/natspec_test.go index 39c80a155..ac894e9b1 100644 --- a/common/natspec/natspec_test.go +++ b/common/natspec/natspec_test.go @@ -12,10 +12,10 @@ func makeUserdoc(desc string) []byte { "languageVersion": 1, "methods": { "multiply(uint256)": { - "notice": ` + desc + ` + "notice": "` + desc + `" }, "balance(address)": { - "notice": "` + "`(balanceInmGAV / 1000).fixed(0,3)`" + ` GAV is the total funds available to ` + "`who.address()`." + ` + "notice": "` + "`(balanceInmGAV / 1000).fixed(0,3)`" + ` GAV is the total funds available to ` + "`who.address()`." + `" } }, "invariants": [ @@ -65,12 +65,12 @@ func TestNotice(t *testing.T) { userdoc := makeUserdoc(desc) - ns, err := NewWithDocs(abi, userdoc) + ns, err := NewWithDocs(abi, userdoc, tx) if err != nil { t.Errorf("New: error: %v", err) } - notice, err := ns.Notice(tx, desc) + notice, err := ns.Notice() if err != nil { t.Errorf("expected no error, got %v", err) @@ -90,7 +90,7 @@ func TestMissingMethod(t *testing.T) { userdoc := makeUserdoc(desc) expected := "natspec.js error evaluating expression: Natspec evaluation failed, method does not exist" - ns, err := NewWithDocs(abi, userdoc) + ns, err := NewWithDocs(abi, userdoc, tx) if err != nil { t.Errorf("New: error: %v", err) } @@ -107,6 +107,7 @@ func TestMissingMethod(t *testing.T) { } // test invalid desc +/* func TestInvalidDesc(t *testing.T) { desc := "Will multiply 122 by \"7\" and return 854." @@ -114,11 +115,11 @@ func TestInvalidDesc(t *testing.T) { userdoc := makeUserdoc(desc) - ns, err := NewWithDocs(abi, userdoc) + ns, err := NewWithDocs(abi, userdoc, tx) if err != nil { t.Errorf("New: error: %v", err) } - notice, err := ns.Notice(tx, data) + notice, err := ns.Notice() if err == nil { t.Errorf("expected error, got nothing (notice: '%v')", err, notice) @@ -128,6 +129,7 @@ func TestInvalidDesc(t *testing.T) { } } } +*/ // test wrong input params func TestWrongInputParams(t *testing.T) { @@ -137,12 +139,12 @@ func TestWrongInputParams(t *testing.T) { userdoc := makeUserdoc(desc) - ns, err := NewWithDocs(abi, userdoc) + ns, err := NewWithDocs(abi, userdoc, tx) if err != nil { t.Errorf("New: error: %v", err) } - notice, err := ns.Notice(tx, desc) + notice, err := ns.Notice() if err == nil { t.Errorf("expected error, got nothing (notice: '%v')", notice) -- cgit v1.2.3 From 3a540425a3d7fc29233b5cd3e7b79b4866f35d57 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 31 Mar 2015 16:02:55 +0100 Subject: reorg: - statereg methods move to natspec/resolver/docserver - fix failing test on invalid js input --- common/natspec/natspec_test.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'common/natspec/natspec_test.go') diff --git a/common/natspec/natspec_test.go b/common/natspec/natspec_test.go index ac894e9b1..35a59469a 100644 --- a/common/natspec/natspec_test.go +++ b/common/natspec/natspec_test.go @@ -107,29 +107,23 @@ func TestMissingMethod(t *testing.T) { } // test invalid desc -/* + func TestInvalidDesc(t *testing.T) { desc := "Will multiply 122 by \"7\" and return 854." - expected := "natspec.js error setting expression: (anonymous): Line 1:41 Unexpected number" + expected := "invalid character '7' after object key:value pair" userdoc := makeUserdoc(desc) - ns, err := NewWithDocs(abi, userdoc, tx) - if err != nil { - t.Errorf("New: error: %v", err) - } - notice, err := ns.Notice() - + _, err := NewWithDocs(abi, userdoc, tx) if err == nil { - t.Errorf("expected error, got nothing (notice: '%v')", err, notice) + t.Errorf("expected error, got nothing", err) } else { if err.Error() != expected { - t.Errorf("expected error '%s' got '%v' (notice: '%v')", expected, err, notice) + t.Errorf("expected error '%s' got '%v'", expected, err) } } } -*/ // test wrong input params func TestWrongInputParams(t *testing.T) { -- cgit v1.2.3