github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/smartcontract/binding/generate_test.go (about)

     1  package binding
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/smartcontract"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestExtendedType_Equals(t *testing.T) {
    11  	crazyT := ExtendedType{
    12  		Base:      smartcontract.StringType,
    13  		Name:      "qwertyu",
    14  		Interface: "qwerty",
    15  		Key:       smartcontract.BoolType,
    16  		Value: &ExtendedType{
    17  			Base: smartcontract.IntegerType,
    18  		},
    19  		Fields: []FieldExtendedType{
    20  			{
    21  				Field: "qwe",
    22  				ExtendedType: ExtendedType{
    23  					Base:      smartcontract.IntegerType,
    24  					Name:      "qwer",
    25  					Interface: "qw",
    26  					Key:       smartcontract.ArrayType,
    27  					Fields: []FieldExtendedType{
    28  						{
    29  							Field: "as",
    30  						},
    31  					},
    32  				},
    33  			},
    34  			{
    35  				Field: "asf",
    36  				ExtendedType: ExtendedType{
    37  					Base: smartcontract.BoolType,
    38  				},
    39  			},
    40  			{
    41  				Field: "sffg",
    42  				ExtendedType: ExtendedType{
    43  					Base: smartcontract.AnyType,
    44  				},
    45  			},
    46  		},
    47  	}
    48  	tcs := map[string]struct {
    49  		a           *ExtendedType
    50  		b           *ExtendedType
    51  		expectedRes bool
    52  	}{
    53  		"both nil": {
    54  			a:           nil,
    55  			b:           nil,
    56  			expectedRes: true,
    57  		},
    58  		"a is nil": {
    59  			a:           nil,
    60  			b:           &ExtendedType{},
    61  			expectedRes: false,
    62  		},
    63  		"b is nil": {
    64  			a:           &ExtendedType{},
    65  			b:           nil,
    66  			expectedRes: false,
    67  		},
    68  		"base mismatch": {
    69  			a: &ExtendedType{
    70  				Base: smartcontract.StringType,
    71  			},
    72  			b: &ExtendedType{
    73  				Base: smartcontract.IntegerType,
    74  			},
    75  			expectedRes: false,
    76  		},
    77  		"name mismatch": {
    78  			a: &ExtendedType{
    79  				Base: smartcontract.ArrayType,
    80  				Name: "q",
    81  			},
    82  			b: &ExtendedType{
    83  				Base: smartcontract.ArrayType,
    84  				Name: "w",
    85  			},
    86  			expectedRes: false,
    87  		},
    88  		"number of fields mismatch": {
    89  			a: &ExtendedType{
    90  				Base: smartcontract.ArrayType,
    91  				Name: "q",
    92  				Fields: []FieldExtendedType{
    93  					{
    94  						Field:        "IntField",
    95  						ExtendedType: ExtendedType{Base: smartcontract.IntegerType},
    96  					},
    97  				},
    98  			},
    99  			b: &ExtendedType{
   100  				Base: smartcontract.ArrayType,
   101  				Name: "w",
   102  				Fields: []FieldExtendedType{
   103  					{
   104  						Field:        "IntField",
   105  						ExtendedType: ExtendedType{Base: smartcontract.IntegerType},
   106  					},
   107  					{
   108  						Field:        "BoolField",
   109  						ExtendedType: ExtendedType{Base: smartcontract.BoolType},
   110  					},
   111  				},
   112  			},
   113  			expectedRes: false,
   114  		},
   115  		"field names mismatch": {
   116  			a: &ExtendedType{
   117  				Base: smartcontract.ArrayType,
   118  				Fields: []FieldExtendedType{
   119  					{
   120  						Field:        "IntField",
   121  						ExtendedType: ExtendedType{Base: smartcontract.IntegerType},
   122  					},
   123  				},
   124  			},
   125  			b: &ExtendedType{
   126  				Base: smartcontract.ArrayType,
   127  				Fields: []FieldExtendedType{
   128  					{
   129  						Field:        "BoolField",
   130  						ExtendedType: ExtendedType{Base: smartcontract.BoolType},
   131  					},
   132  				},
   133  			},
   134  			expectedRes: false,
   135  		},
   136  		"field types mismatch": {
   137  			a: &ExtendedType{
   138  				Base: smartcontract.ArrayType,
   139  				Fields: []FieldExtendedType{
   140  					{
   141  						Field:        "Field",
   142  						ExtendedType: ExtendedType{Base: smartcontract.IntegerType},
   143  					},
   144  				},
   145  			},
   146  			b: &ExtendedType{
   147  				Base: smartcontract.ArrayType,
   148  				Fields: []FieldExtendedType{
   149  					{
   150  						Field:        "Field",
   151  						ExtendedType: ExtendedType{Base: smartcontract.BoolType},
   152  					},
   153  				},
   154  			},
   155  			expectedRes: false,
   156  		},
   157  		"interface mismatch": {
   158  			a:           &ExtendedType{Interface: "iterator"},
   159  			b:           &ExtendedType{Interface: "unknown"},
   160  			expectedRes: false,
   161  		},
   162  		"value is nil": {
   163  			a: &ExtendedType{
   164  				Base: smartcontract.StringType,
   165  			},
   166  			b: &ExtendedType{
   167  				Base: smartcontract.StringType,
   168  			},
   169  			expectedRes: true,
   170  		},
   171  		"a value is not nil": {
   172  			a: &ExtendedType{
   173  				Base:  smartcontract.ArrayType,
   174  				Value: &ExtendedType{},
   175  			},
   176  			b: &ExtendedType{
   177  				Base: smartcontract.ArrayType,
   178  			},
   179  			expectedRes: false,
   180  		},
   181  		"b value is not nil": {
   182  			a: &ExtendedType{
   183  				Base: smartcontract.ArrayType,
   184  			},
   185  			b: &ExtendedType{
   186  				Base:  smartcontract.ArrayType,
   187  				Value: &ExtendedType{},
   188  			},
   189  			expectedRes: false,
   190  		},
   191  		"byte array tolerance for a": {
   192  			a: &ExtendedType{
   193  				Base: smartcontract.StringType,
   194  			},
   195  			b: &ExtendedType{
   196  				Base: smartcontract.ByteArrayType,
   197  			},
   198  			expectedRes: true,
   199  		},
   200  		"byte array tolerance for b": {
   201  			a: &ExtendedType{
   202  				Base: smartcontract.ByteArrayType,
   203  			},
   204  			b: &ExtendedType{
   205  				Base: smartcontract.StringType,
   206  			},
   207  			expectedRes: true,
   208  		},
   209  		"key mismatch": {
   210  			a: &ExtendedType{
   211  				Key: smartcontract.StringType,
   212  			},
   213  			b: &ExtendedType{
   214  				Key: smartcontract.IntegerType,
   215  			},
   216  			expectedRes: false,
   217  		},
   218  		"good nested": {
   219  			a:           &crazyT,
   220  			b:           &crazyT,
   221  			expectedRes: true,
   222  		},
   223  	}
   224  	for name, tc := range tcs {
   225  		t.Run(name, func(t *testing.T) {
   226  			assert.Equal(t, tc.expectedRes, tc.a.Equals(tc.b))
   227  		})
   228  	}
   229  }