github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/reflect_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  //</624450062818217984>
    11  
    12  
    13  package abi
    14  
    15  import (
    16  	"reflect"
    17  	"testing"
    18  )
    19  
    20  type reflectTest struct {
    21  	name  string
    22  	args  []string
    23  	struc interface{}
    24  	want  map[string]string
    25  	err   string
    26  }
    27  
    28  var reflectTests = []reflectTest{
    29  	{
    30  		name: "OneToOneCorrespondance",
    31  		args: []string{"fieldA"},
    32  		struc: struct {
    33  			FieldA int `abi:"fieldA"`
    34  		}{},
    35  		want: map[string]string{
    36  			"fieldA": "FieldA",
    37  		},
    38  	},
    39  	{
    40  		name: "MissingFieldsInStruct",
    41  		args: []string{"fieldA", "fieldB"},
    42  		struc: struct {
    43  			FieldA int `abi:"fieldA"`
    44  		}{},
    45  		want: map[string]string{
    46  			"fieldA": "FieldA",
    47  		},
    48  	},
    49  	{
    50  		name: "MoreFieldsInStructThanArgs",
    51  		args: []string{"fieldA"},
    52  		struc: struct {
    53  			FieldA int `abi:"fieldA"`
    54  			FieldB int
    55  		}{},
    56  		want: map[string]string{
    57  			"fieldA": "FieldA",
    58  		},
    59  	},
    60  	{
    61  		name: "MissingFieldInArgs",
    62  		args: []string{"fieldA"},
    63  		struc: struct {
    64  			FieldA int `abi:"fieldA"`
    65  			FieldB int `abi:"fieldB"`
    66  		}{},
    67  		err: "struct: abi tag 'fieldB' defined but not found in abi",
    68  	},
    69  	{
    70  		name: "NoAbiDescriptor",
    71  		args: []string{"fieldA"},
    72  		struc: struct {
    73  			FieldA int
    74  		}{},
    75  		want: map[string]string{
    76  			"fieldA": "FieldA",
    77  		},
    78  	},
    79  	{
    80  		name: "NoArgs",
    81  		args: []string{},
    82  		struc: struct {
    83  			FieldA int `abi:"fieldA"`
    84  		}{},
    85  		err: "struct: abi tag 'fieldA' defined but not found in abi",
    86  	},
    87  	{
    88  		name: "DifferentName",
    89  		args: []string{"fieldB"},
    90  		struc: struct {
    91  			FieldA int `abi:"fieldB"`
    92  		}{},
    93  		want: map[string]string{
    94  			"fieldB": "FieldA",
    95  		},
    96  	},
    97  	{
    98  		name: "DifferentName",
    99  		args: []string{"fieldB"},
   100  		struc: struct {
   101  			FieldA int `abi:"fieldB"`
   102  		}{},
   103  		want: map[string]string{
   104  			"fieldB": "FieldA",
   105  		},
   106  	},
   107  	{
   108  		name: "MultipleFields",
   109  		args: []string{"fieldA", "fieldB"},
   110  		struc: struct {
   111  			FieldA int `abi:"fieldA"`
   112  			FieldB int `abi:"fieldB"`
   113  		}{},
   114  		want: map[string]string{
   115  			"fieldA": "FieldA",
   116  			"fieldB": "FieldB",
   117  		},
   118  	},
   119  	{
   120  		name: "MultipleFieldsABIMissing",
   121  		args: []string{"fieldA", "fieldB"},
   122  		struc: struct {
   123  			FieldA int `abi:"fieldA"`
   124  			FieldB int
   125  		}{},
   126  		want: map[string]string{
   127  			"fieldA": "FieldA",
   128  			"fieldB": "FieldB",
   129  		},
   130  	},
   131  	{
   132  		name: "NameConflict",
   133  		args: []string{"fieldB"},
   134  		struc: struct {
   135  			FieldA int `abi:"fieldB"`
   136  			FieldB int
   137  		}{},
   138  		err: "abi: multiple variables maps to the same abi field 'fieldB'",
   139  	},
   140  	{
   141  		name: "Underscored",
   142  		args: []string{"_"},
   143  		struc: struct {
   144  			FieldA int
   145  		}{},
   146  		err: "abi: purely underscored output cannot unpack to struct",
   147  	},
   148  	{
   149  		name: "DoubleMapping",
   150  		args: []string{"fieldB", "fieldC", "fieldA"},
   151  		struc: struct {
   152  			FieldA int `abi:"fieldC"`
   153  			FieldB int
   154  		}{},
   155  		err: "abi: multiple outputs mapping to the same struct field 'FieldA'",
   156  	},
   157  	{
   158  		name: "AlreadyMapped",
   159  		args: []string{"fieldB", "fieldB"},
   160  		struc: struct {
   161  			FieldB int `abi:"fieldB"`
   162  		}{},
   163  		err: "struct: abi tag in 'FieldB' already mapped",
   164  	},
   165  }
   166  
   167  func TestReflectNameToStruct(t *testing.T) {
   168  	for _, test := range reflectTests {
   169  		t.Run(test.name, func(t *testing.T) {
   170  			m, err := mapArgNamesToStructFields(test.args, reflect.ValueOf(test.struc))
   171  			if len(test.err) > 0 {
   172  				if err == nil || err.Error() != test.err {
   173  					t.Fatalf("Invalid error: expected %v, got %v", test.err, err)
   174  				}
   175  			} else {
   176  				if err != nil {
   177  					t.Fatalf("Unexpected error: %v", err)
   178  				}
   179  				for fname := range test.want {
   180  					if m[fname] != test.want[fname] {
   181  						t.Fatalf("Incorrect value for field %s: expected %v, got %v", fname, test.want[fname], m[fname])
   182  					}
   183  				}
   184  			}
   185  		})
   186  	}
   187  }
   188