natspec_test.go 2.28 KB
Newer Older
Fefe's avatar
Fefe committed
1 2 3 4 5 6 7 8
package natspec

import (
	"testing"
)

func TestNotice(t *testing.T) {

zelig's avatar
zelig committed
9
	tx := `
Fefe's avatar
Fefe committed
10
	{
zelig's avatar
zelig committed
11 12 13 14 15 16 17 18 19
    "jsonrpc": "2.0",
    "method": "eth_call",
    "params": [{
        "to": "0x8521742d3f456bd237e312d6e30724960f72517a",
        "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
    }],
    "id": 6
  }
	`
Fefe's avatar
Fefe committed
20

zelig's avatar
zelig committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	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"

zelig's avatar
zelig committed
41
	ns, err := New()
Fefe's avatar
Fefe committed
42 43 44 45
	if err != nil {
		t.Errorf("NewNATSpec error %v", err)
	}

zelig's avatar
zelig committed
46 47 48 49 50
	notice, err := ns.Notice(tx, abi, method, desc)

	if err != nil {
		t.Errorf("expected no error got %v", err)
	}
Fefe's avatar
Fefe committed
51 52 53 54 55 56 57

	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)
	}
zelig's avatar
zelig committed
58 59 60 61 62 63 64 65 66 67 68 69 70

	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)
		}
	}

zelig's avatar
zelig committed
71
	// https://github.com/ethereum/natspec.js/issues/1
zelig's avatar
zelig committed
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
	// badDesc := "Will multiply `e` by 7 and return `a * 7`."
	// notice, err = ns.Notice(tx, abi, method, badDesc)

	// expected = "natspec.js error evaluating expression: wrong input param in expression ''"

	// 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)
	// 	}
	// }

	notice, err = ns.Notice(tx, abi, "missing_method", desc)

	expected = "natspec.js error evaluating expression: wrong input params in expression 'Will multiply `a` by 7 and return `a * 7`.'"

	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)
		}
	}

Fefe's avatar
Fefe committed
97
}