Source file src/cmd/go/internal/gover/mod_test.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gover
     6  
     7  import (
     8  	"slices"
     9  	"strings"
    10  	"testing"
    11  
    12  	"golang.org/x/mod/module"
    13  )
    14  
    15  func TestIsToolchain(t *testing.T) { test1(t, isToolchainTests, "IsToolchain", IsToolchain) }
    16  
    17  var isToolchainTests = []testCase1[string, bool]{
    18  	{"go", true},
    19  	{"toolchain", true},
    20  	{"anything", false},
    21  	{"golang.org/toolchain", false},
    22  }
    23  
    24  func TestModCompare(t *testing.T) { test3(t, modCompareTests, "ModCompare", ModCompare) }
    25  
    26  var modCompareTests = []testCase3[string, string, string, int]{
    27  	{"go", "1.2", "1.3", -1},
    28  	{"go", "v1.2", "v1.3", 0}, // equal because invalid
    29  	{"go", "1.2", "1.2", 0},
    30  	{"toolchain", "go1.2", "go1.3", -1},
    31  	{"toolchain", "go1.2", "go1.2", 0},
    32  	{"toolchain", "1.2", "1.3", -1},  // accepted but non-standard
    33  	{"toolchain", "v1.2", "v1.3", 0}, // equal because invalid
    34  	{"rsc.io/quote", "v1.2", "v1.3", -1},
    35  	{"rsc.io/quote", "1.2", "1.3", 0}, // equal because invalid
    36  }
    37  
    38  func TestModIsValid(t *testing.T) { test2(t, modIsValidTests, "ModIsValid", ModIsValid) }
    39  
    40  var modIsValidTests = []testCase2[string, string, bool]{
    41  	{"go", "1.2", true},
    42  	{"go", "v1.2", false},
    43  	{"toolchain", "go1.2", true},
    44  	{"toolchain", "v1.2", false},
    45  	{"rsc.io/quote", "v1.2", true},
    46  	{"rsc.io/quote", "1.2", false},
    47  }
    48  
    49  func TestModSort(t *testing.T) {
    50  	test1(t, modSortTests, "ModSort", func(list []module.Version) []module.Version {
    51  		out := slices.Clone(list)
    52  		ModSort(out)
    53  		return out
    54  	})
    55  }
    56  
    57  var modSortTests = []testCase1[[]module.Version, []module.Version]{
    58  	{
    59  		mvl(`z v1.1; a v1.2; a v1.1; go 1.3; toolchain 1.3; toolchain 1.2; go 1.2`),
    60  		mvl(`a v1.1; a v1.2; go 1.2; go 1.3; toolchain 1.2; toolchain 1.3; z v1.1`),
    61  	},
    62  }
    63  
    64  func mvl(s string) []module.Version {
    65  	var list []module.Version
    66  	for _, f := range strings.Split(s, ";") {
    67  		f = strings.TrimSpace(f)
    68  		path, vers, _ := strings.Cut(f, " ")
    69  		list = append(list, module.Version{Path: path, Version: vers})
    70  	}
    71  	return list
    72  }
    73  

View as plain text