github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/method_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:31</date>
    10  //</624450062130352128>
    11  
    12  
    13  package abi
    14  
    15  import (
    16  	"strings"
    17  	"testing"
    18  )
    19  
    20  const methoddata = `
    21  [
    22  	{ "type" : "function", "name" : "balance", "constant" : true },
    23  	{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
    24  	{ "type" : "function", "name" : "transfer", "constant" : false, "inputs" : [ { "name" : "from", "type" : "address" }, { "name" : "to", "type" : "address" }, { "name" : "value", "type" : "uint256" } ], "outputs" : [ { "name" : "success", "type" : "bool" } ]  }
    25  ]`
    26  
    27  func TestMethodString(t *testing.T) {
    28  	var table = []struct {
    29  		method      string
    30  		expectation string
    31  	}{
    32  		{
    33  			method:      "balance",
    34  			expectation: "function balance() constant returns()",
    35  		},
    36  		{
    37  			method:      "send",
    38  			expectation: "function send(uint256 amount) returns()",
    39  		},
    40  		{
    41  			method:      "transfer",
    42  			expectation: "function transfer(address from, address to, uint256 value) returns(bool success)",
    43  		},
    44  	}
    45  
    46  	abi, err := JSON(strings.NewReader(methoddata))
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	for _, test := range table {
    52  		got := abi.Methods[test.method].String()
    53  		if got != test.expectation {
    54  			t.Errorf("expected string to be %s, got %s", test.expectation, got)
    55  		}
    56  	}
    57  }
    58