github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/get/pkg_test.go (about) 1 // Copyright 2014 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 get 6 7 import ( 8 "github.com/gagliardetto/golang-go/cmd/go/not-internal/str" 9 "reflect" 10 "strings" 11 "testing" 12 ) 13 14 var foldDupTests = []struct { 15 list []string 16 f1, f2 string 17 }{ 18 {str.StringList("math/rand", "math/big"), "", ""}, 19 {str.StringList("math", "strings"), "", ""}, 20 {str.StringList("strings"), "", ""}, 21 {str.StringList("strings", "strings"), "strings", "strings"}, 22 {str.StringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"}, 23 } 24 25 func TestFoldDup(t *testing.T) { 26 for _, tt := range foldDupTests { 27 f1, f2 := str.FoldDup(tt.list) 28 if f1 != tt.f1 || f2 != tt.f2 { 29 t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2) 30 } 31 } 32 } 33 34 var parseMetaGoImportsTests = []struct { 35 in string 36 mod ModuleMode 37 out []metaImport 38 }{ 39 { 40 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 41 IgnoreMod, 42 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 43 }, 44 { 45 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 46 <meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`, 47 IgnoreMod, 48 []metaImport{ 49 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 50 {"baz/quux", "git", "http://github.com/rsc/baz/quux"}, 51 }, 52 }, 53 { 54 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 55 <meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`, 56 IgnoreMod, 57 []metaImport{ 58 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 59 }, 60 }, 61 { 62 `<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux"> 63 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 64 IgnoreMod, 65 []metaImport{ 66 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 67 }, 68 }, 69 { 70 `<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux"> 71 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 72 PreferMod, 73 []metaImport{ 74 {"foo/bar", "mod", "http://github.com/rsc/baz/quux"}, 75 }, 76 }, 77 { 78 `<head> 79 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 80 </head>`, 81 IgnoreMod, 82 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 83 }, 84 { 85 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 86 <body>`, 87 IgnoreMod, 88 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 89 }, 90 { 91 `<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 92 IgnoreMod, 93 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 94 }, 95 { 96 // XML doesn't like <div style=position:relative>. 97 `<!doctype html><title>Page Not Found</title><meta name=go-import content="chitin.io/chitin git https://github.com/chitin-io/chitin"><div style=position:relative>DRAFT</div>`, 98 IgnoreMod, 99 []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}}, 100 }, 101 { 102 `<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x"> 103 <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master"> 104 `, 105 IgnoreMod, 106 []metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x"}}, 107 }, 108 { 109 `<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x"> 110 <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master"> 111 `, 112 PreferMod, 113 []metaImport{ 114 {"myitcv.io/blah2", "mod", "https://raw.githubusercontent.com/myitcv/pubx/master"}, 115 {"myitcv.io", "git", "https://github.com/myitcv/x"}, 116 }, 117 }, 118 } 119 120 func TestParseMetaGoImports(t *testing.T) { 121 for i, tt := range parseMetaGoImportsTests { 122 out, err := parseMetaGoImports(strings.NewReader(tt.in), tt.mod) 123 if err != nil { 124 t.Errorf("test#%d: %v", i, err) 125 continue 126 } 127 if !reflect.DeepEqual(out, tt.out) { 128 t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out) 129 } 130 } 131 }