github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/comparison/equal_test.go (about)

     1  package comparison
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  type equalitorArgs struct {
    10  	a interface{}
    11  	b interface{}
    12  }
    13  type equalitorWants struct {
    14  	equal bool
    15  }
    16  type equalitorTest struct {
    17  	description string
    18  	args        equalitorArgs
    19  	wants       equalitorWants
    20  }
    21  
    22  var (
    23  	standardSuite = []equalitorTest{
    24  		{
    25  			description: "EmptyStructs/True",
    26  			args: equalitorArgs{
    27  				a: struct{}{},
    28  				b: struct{}{},
    29  			},
    30  			wants: equalitorWants{
    31  				equal: true,
    32  			},
    33  		},
    34  		{
    35  			description: "Structs/Exported/True",
    36  			args: equalitorArgs{
    37  				a: struct {
    38  					Animal string
    39  				}{
    40  					Animal: "hippo",
    41  				},
    42  				b: struct {
    43  					Animal string
    44  				}{
    45  					Animal: "hippo",
    46  				},
    47  			},
    48  			wants: equalitorWants{
    49  				equal: true,
    50  			},
    51  		},
    52  		{
    53  			description: "Structs/Exported/False",
    54  			args: equalitorArgs{
    55  				a: struct {
    56  					Animal string
    57  				}{
    58  					Animal: "hippo",
    59  				},
    60  				b: struct {
    61  					Animal string
    62  				}{
    63  					Animal: "meerkat",
    64  				},
    65  			},
    66  			wants: equalitorWants{
    67  				equal: false,
    68  			},
    69  		},
    70  		{
    71  			description: "Slices/Strings/Empty/True",
    72  			args: equalitorArgs{
    73  				a: []string{},
    74  				b: []string{},
    75  			},
    76  			wants: equalitorWants{
    77  				equal: true,
    78  			},
    79  		},
    80  		{
    81  			description: "Slices/Strings/Sequence/True",
    82  			args: equalitorArgs{
    83  				a: []string{"hippo", "meerkat"},
    84  				b: []string{"hippo", "meerkat"},
    85  			},
    86  			wants: equalitorWants{
    87  				equal: true,
    88  			},
    89  		},
    90  		{
    91  			description: "Slices/Strings/Sequence/False",
    92  			args: equalitorArgs{
    93  				a: []string{"hippo", "meerkat"},
    94  				b: []string{"meerkat", "hippo"},
    95  			},
    96  			wants: equalitorWants{
    97  				equal: false,
    98  			},
    99  		},
   100  		{
   101  			description: "Slices/Strings/Sequence/LengthChange/False",
   102  			args: equalitorArgs{
   103  				a: []string{"hippo", "meerkat"},
   104  				b: []string{"hippo", "meerkat", "otter"},
   105  			},
   106  			wants: equalitorWants{
   107  				equal: false,
   108  			},
   109  		},
   110  		{
   111  			description: "Slices/Structs/Exported/Sequence/True",
   112  			args: equalitorArgs{
   113  				a: []struct {
   114  					Animal string
   115  				}{
   116  					{Animal: "hippo"},
   117  					{Animal: "meerkat"},
   118  				},
   119  				b: []struct {
   120  					Animal string
   121  				}{
   122  					{Animal: "hippo"},
   123  					{Animal: "meerkat"},
   124  				},
   125  			},
   126  			wants: equalitorWants{
   127  				equal: true,
   128  			},
   129  		},
   130  		{
   131  			description: "Slices/Structs/Exported/Sequence/False",
   132  			args: equalitorArgs{
   133  				a: []struct {
   134  					Animal string
   135  				}{
   136  					{Animal: "hippo"},
   137  					{Animal: "meerkat"},
   138  				},
   139  				b: []struct {
   140  					Animal string
   141  				}{
   142  					{Animal: "meerkat"},
   143  					{Animal: "hippo"},
   144  				},
   145  			},
   146  			wants: equalitorWants{
   147  				equal: false,
   148  			},
   149  		},
   150  		{
   151  			description: "Slices/Structs/Exported/Sequence/LengthChange/False",
   152  			args: equalitorArgs{
   153  				a: []struct {
   154  					Animal string
   155  				}{
   156  					{Animal: "hippo"},
   157  					{Animal: "meerkat"},
   158  				},
   159  				b: []struct {
   160  					Animal string
   161  				}{
   162  					{Animal: "hippo"},
   163  					{Animal: "meerkat"},
   164  					{Animal: "otter"},
   165  				},
   166  			},
   167  			wants: equalitorWants{
   168  				equal: false,
   169  			},
   170  		},
   171  		{
   172  			description: "Slice/Structs/Strings/MismatchedTypes/False",
   173  			args: equalitorArgs{
   174  				a: []struct {
   175  					Animal string
   176  				}{
   177  					{Animal: "hippo"},
   178  					{Animal: "meerkat"},
   179  				},
   180  				b: []string{"hippo", "meerkat"},
   181  			},
   182  			wants: equalitorWants{
   183  				equal: false,
   184  			},
   185  		},
   186  		{
   187  			description: "Struct/int/MismatchedTypes/False",
   188  			args: equalitorArgs{
   189  				a: struct {
   190  					Animal string
   191  				}{
   192  					Animal: "hippo",
   193  				},
   194  				b: 5,
   195  			},
   196  			wants: equalitorWants{
   197  				equal: false,
   198  			},
   199  		},
   200  		{
   201  			description: "Struct/nil/MismatchedTypes/False",
   202  			args: equalitorArgs{
   203  				a: struct {
   204  					Animal string
   205  				}{
   206  					Animal: "hippo",
   207  				},
   208  				b: nil,
   209  			},
   210  			wants: equalitorWants{
   211  				equal: false,
   212  			},
   213  		},
   214  		{
   215  			description: "Map/Strings/True",
   216  			args: equalitorArgs{
   217  				a: map[string]int{
   218  					"hippo":   64,
   219  					"meerkat": 32,
   220  				},
   221  				b: map[string]int{
   222  					"hippo":   64,
   223  					"meerkat": 32,
   224  				},
   225  			},
   226  			wants: equalitorWants{
   227  				equal: true,
   228  			},
   229  		},
   230  		{
   231  			description: "Map/Strings/Set/True",
   232  			args: equalitorArgs{
   233  				a: map[string]int{
   234  					"hippo":   64,
   235  					"meerkat": 32,
   236  				},
   237  				b: map[string]int{
   238  					"meerkat": 32,
   239  					"hippo":   64,
   240  				},
   241  			},
   242  			wants: equalitorWants{
   243  				equal: true,
   244  			},
   245  		},
   246  	}
   247  )
   248  
   249  func RunEqualitorSuite(equalitor Equalitor, suite []equalitorTest, t *testing.T) {
   250  	for _, tt := range suite {
   251  		t.Run(tt.description, func(t *testing.T) {
   252  			// Check equality and ensure symetry
   253  			require.Equal(t, tt.wants.equal, equalitor.Equal(tt.args.a, tt.args.b))
   254  			require.Equal(t, tt.wants.equal, equalitor.Equal(tt.args.b, tt.args.a))
   255  		})
   256  	}
   257  }
   258  
   259  func TestNewHashEqualitor(t *testing.T) {
   260  	// Run the standard test suite
   261  	equalitor := NewHashEqualitor()
   262  	RunEqualitorSuite(equalitor, standardSuite, t)
   263  
   264  	// Run custom tests for the specific Equalitor
   265  	type Animal struct {
   266  		Name string
   267  	}
   268  	suite := []equalitorTest{
   269  		{
   270  			description: "Structs/Slices/NoSetTag/False",
   271  			args: equalitorArgs{
   272  				a: struct {
   273  					Animals []Animal
   274  				}{
   275  					Animals: []Animal{
   276  						{Name: "hippo"},
   277  						{Name: "meerkat"},
   278  					},
   279  				},
   280  				b: struct {
   281  					Animals []Animal
   282  				}{
   283  					Animals: []Animal{
   284  						{Name: "meerkat"},
   285  						{Name: "hippo"},
   286  					},
   287  				},
   288  			},
   289  			wants: equalitorWants{
   290  				equal: false,
   291  			},
   292  		},
   293  		{
   294  			description: "Structs/Slices/SetTag/True",
   295  			args: equalitorArgs{
   296  				a: struct {
   297  					Animals []Animal `hash:"set"`
   298  				}{
   299  					Animals: []Animal{
   300  						{Name: "hippo"},
   301  						{Name: "meerkat"},
   302  					},
   303  				},
   304  				b: struct {
   305  					Animals []Animal `hash:"set"`
   306  				}{
   307  					Animals: []Animal{
   308  						{Name: "meerkat"},
   309  						{Name: "hippo"},
   310  					},
   311  				},
   312  			},
   313  			wants: equalitorWants{
   314  				equal: true,
   315  			},
   316  		},
   317  		{
   318  			description: "Structs/Field/Changed/NoIgnoreTag/False",
   319  			args: equalitorArgs{
   320  				a: struct {
   321  					Animal
   322  					Age int
   323  				}{
   324  					Animal: Animal{
   325  						Name: "hippo",
   326  					},
   327  					Age: 27,
   328  				},
   329  				b: struct {
   330  					Animal
   331  					Age int
   332  				}{
   333  					Animal: Animal{
   334  						Name: "hippo",
   335  					},
   336  					Age: 28,
   337  				},
   338  			},
   339  			wants: equalitorWants{
   340  				equal: false,
   341  			},
   342  		},
   343  		{
   344  			description: "Structs/Field/Changed/IgnoreTag/True",
   345  			args: equalitorArgs{
   346  				a: struct {
   347  					Animal
   348  					Age int `hash:"ignore"`
   349  				}{
   350  					Animal: Animal{
   351  						Name: "hippo",
   352  					},
   353  					Age: 27,
   354  				},
   355  				b: struct {
   356  					Animal
   357  					Age int `hash:"ignore"`
   358  				}{
   359  					Animal: Animal{
   360  						Name: "hippo",
   361  					},
   362  					Age: 28,
   363  				},
   364  			},
   365  			wants: equalitorWants{
   366  				equal: true,
   367  			},
   368  		},
   369  	}
   370  	RunEqualitorSuite(equalitor, suite, t)
   371  }