github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/pkg/gnomod/preprocess_test.go (about)

     1  package gnomod
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"golang.org/x/mod/modfile"
     8  	"golang.org/x/mod/module"
     9  )
    10  
    11  func TestRemoveRequireDups(t *testing.T) {
    12  	for _, tc := range []struct {
    13  		desc     string
    14  		in       []*modfile.Require
    15  		expected []*modfile.Require
    16  	}{
    17  		{
    18  			desc: "no_duplicate",
    19  			in: []*modfile.Require{
    20  				{
    21  					Mod: module.Version{
    22  						Path:    "x.y/w",
    23  						Version: "v1.0.0",
    24  					},
    25  				},
    26  				{
    27  					Mod: module.Version{
    28  						Path:    "x.y/z",
    29  						Version: "v1.1.0",
    30  					},
    31  				},
    32  			},
    33  			expected: []*modfile.Require{
    34  				{
    35  					Mod: module.Version{
    36  						Path:    "x.y/w",
    37  						Version: "v1.0.0",
    38  					},
    39  				},
    40  				{
    41  					Mod: module.Version{
    42  						Path:    "x.y/z",
    43  						Version: "v1.1.0",
    44  					},
    45  				},
    46  			},
    47  		},
    48  		{
    49  			desc: "one_duplicate",
    50  			in: []*modfile.Require{
    51  				{
    52  					Mod: module.Version{
    53  						Path:    "x.y/w",
    54  						Version: "v1.0.0",
    55  					},
    56  				},
    57  				{
    58  					Mod: module.Version{
    59  						Path:    "x.y/w",
    60  						Version: "v1.0.0",
    61  					},
    62  				},
    63  				{
    64  					Mod: module.Version{
    65  						Path:    "x.y/z",
    66  						Version: "v1.1.0",
    67  					},
    68  				},
    69  			},
    70  			expected: []*modfile.Require{
    71  				{
    72  					Mod: module.Version{
    73  						Path:    "x.y/w",
    74  						Version: "v1.0.0",
    75  					},
    76  				},
    77  				{
    78  					Mod: module.Version{
    79  						Path:    "x.y/z",
    80  						Version: "v1.1.0",
    81  					},
    82  				},
    83  			},
    84  		},
    85  		{
    86  			desc: "multiple_duplicate",
    87  			in: []*modfile.Require{
    88  				{
    89  					Mod: module.Version{
    90  						Path:    "x.y/w",
    91  						Version: "v1.0.0",
    92  					},
    93  				},
    94  				{
    95  					Mod: module.Version{
    96  						Path:    "x.y/w",
    97  						Version: "v1.0.0",
    98  					},
    99  				},
   100  				{
   101  					Mod: module.Version{
   102  						Path:    "x.y/z",
   103  						Version: "v1.1.0",
   104  					},
   105  				},
   106  				{
   107  					Mod: module.Version{
   108  						Path:    "x.y/w",
   109  						Version: "v1.2.0",
   110  					},
   111  				},
   112  			},
   113  			expected: []*modfile.Require{
   114  				{
   115  					Mod: module.Version{
   116  						Path:    "x.y/z",
   117  						Version: "v1.1.0",
   118  					},
   119  				},
   120  				{
   121  					Mod: module.Version{
   122  						Path:    "x.y/w",
   123  						Version: "v1.2.0",
   124  					},
   125  				},
   126  			},
   127  		},
   128  	} {
   129  		t.Run(tc.desc, func(t *testing.T) {
   130  			in := tc.in
   131  			removeRequireDups(&in)
   132  
   133  			assert.Equal(t, tc.expected, in)
   134  		})
   135  	}
   136  }
   137  
   138  func TestRemoveReplaceDups(t *testing.T) {
   139  	for _, tc := range []struct {
   140  		desc     string
   141  		in       []*modfile.Replace
   142  		expected []*modfile.Replace
   143  	}{
   144  		{
   145  			desc: "no_duplicate",
   146  			in: []*modfile.Replace{
   147  				{
   148  					Old: module.Version{
   149  						Path:    "x.y/w",
   150  						Version: "v1.0.0",
   151  					},
   152  				},
   153  				{
   154  					Old: module.Version{
   155  						Path:    "x.y/z",
   156  						Version: "v1.1.0",
   157  					},
   158  				},
   159  			},
   160  			expected: []*modfile.Replace{
   161  				{
   162  					Old: module.Version{
   163  						Path:    "x.y/w",
   164  						Version: "v1.0.0",
   165  					},
   166  				},
   167  				{
   168  					Old: module.Version{
   169  						Path:    "x.y/z",
   170  						Version: "v1.1.0",
   171  					},
   172  				},
   173  			},
   174  		},
   175  		{
   176  			desc: "one_duplicate",
   177  			in: []*modfile.Replace{
   178  				{
   179  					Old: module.Version{
   180  						Path:    "x.y/w",
   181  						Version: "v1.0.0",
   182  					},
   183  					Syntax: &modfile.Line{
   184  						Token: []string{"1"},
   185  					},
   186  				},
   187  				{
   188  					Old: module.Version{
   189  						Path:    "x.y/w",
   190  						Version: "v1.0.0",
   191  					},
   192  					Syntax: &modfile.Line{
   193  						Token: []string{"2"},
   194  					},
   195  				},
   196  				{
   197  					Old: module.Version{
   198  						Path:    "x.y/z",
   199  						Version: "v1.1.0",
   200  					},
   201  					Syntax: &modfile.Line{
   202  						Token: []string{"3"},
   203  					},
   204  				},
   205  			},
   206  			expected: []*modfile.Replace{
   207  				{
   208  					Old: module.Version{
   209  						Path:    "x.y/w",
   210  						Version: "v1.0.0",
   211  					},
   212  					Syntax: &modfile.Line{
   213  						Token: []string{"2"},
   214  					},
   215  				},
   216  				{
   217  					Old: module.Version{
   218  						Path:    "x.y/z",
   219  						Version: "v1.1.0",
   220  					},
   221  					Syntax: &modfile.Line{
   222  						Token: []string{"3"},
   223  					},
   224  				},
   225  			},
   226  		},
   227  		{
   228  			desc: "multiple_duplicate",
   229  			in: []*modfile.Replace{
   230  				{
   231  					Old: module.Version{
   232  						Path:    "x.y/w",
   233  						Version: "v1.0.0",
   234  					},
   235  					Syntax: &modfile.Line{
   236  						Token: []string{"1"},
   237  					},
   238  				},
   239  				{
   240  					Old: module.Version{
   241  						Path:    "x.y/w",
   242  						Version: "v1.0.0",
   243  					},
   244  					Syntax: &modfile.Line{
   245  						Token: []string{"2"},
   246  					},
   247  				},
   248  				{
   249  					Old: module.Version{
   250  						Path:    "x.y/z",
   251  						Version: "v1.1.0",
   252  					},
   253  					Syntax: &modfile.Line{
   254  						Token: []string{"3"},
   255  					},
   256  				},
   257  				{
   258  					Old: module.Version{
   259  						Path:    "x.y/z",
   260  						Version: "v1.1.0",
   261  					},
   262  					Syntax: &modfile.Line{
   263  						Token: []string{"4"},
   264  					},
   265  				},
   266  				{
   267  					Old: module.Version{
   268  						Path:    "x.y/w",
   269  						Version: "v1.2.0",
   270  					},
   271  					Syntax: &modfile.Line{
   272  						Token: []string{"5"},
   273  					},
   274  				},
   275  			},
   276  			expected: []*modfile.Replace{
   277  				{
   278  					Old: module.Version{
   279  						Path:    "x.y/w",
   280  						Version: "v1.0.0",
   281  					},
   282  					Syntax: &modfile.Line{
   283  						Token: []string{"2"},
   284  					},
   285  				},
   286  				{
   287  					Old: module.Version{
   288  						Path:    "x.y/z",
   289  						Version: "v1.1.0",
   290  					},
   291  					Syntax: &modfile.Line{
   292  						Token: []string{"4"},
   293  					},
   294  				},
   295  				{
   296  					Old: module.Version{
   297  						Path:    "x.y/w",
   298  						Version: "v1.2.0",
   299  					},
   300  					Syntax: &modfile.Line{
   301  						Token: []string{"5"},
   302  					},
   303  				},
   304  			},
   305  		},
   306  	} {
   307  		t.Run(tc.desc, func(t *testing.T) {
   308  			in := tc.in
   309  			removeReplaceDups(&in)
   310  
   311  			assert.Equal(t, tc.expected, in)
   312  		})
   313  	}
   314  }