github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/accounts/abi/reflect_test.go (about)

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