github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/cmd/go/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 "cmd/go/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 out []metaImport 37 }{ 38 { 39 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 40 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 41 }, 42 { 43 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 44 <meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`, 45 []metaImport{ 46 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 47 {"baz/quux", "git", "http://github.com/rsc/baz/quux"}, 48 }, 49 }, 50 { 51 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 52 <meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`, 53 []metaImport{ 54 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 55 }, 56 }, 57 { 58 `<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux"> 59 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 60 []metaImport{ 61 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 62 }, 63 }, 64 { 65 `<head> 66 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 67 </head>`, 68 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 69 }, 70 { 71 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 72 <body>`, 73 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 74 }, 75 { 76 `<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 77 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 78 }, 79 { 80 // XML doesn't like <div style=position:relative>. 81 `<!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>`, 82 []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}}, 83 }, 84 } 85 86 func TestParseMetaGoImports(t *testing.T) { 87 for i, tt := range parseMetaGoImportsTests { 88 out, err := parseMetaGoImports(strings.NewReader(tt.in)) 89 if err != nil { 90 t.Errorf("test#%d: %v", i, err) 91 continue 92 } 93 if !reflect.DeepEqual(out, tt.out) { 94 t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out) 95 } 96 } 97 }