github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/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/load" 9 "cmd/go/internal/str" 10 "reflect" 11 "strings" 12 "testing" 13 ) 14 15 var foldDupTests = []struct { 16 list []string 17 f1, f2 string 18 }{ 19 {str.StringList("math/rand", "math/big"), "", ""}, 20 {str.StringList("math", "strings"), "", ""}, 21 {str.StringList("strings"), "", ""}, 22 {str.StringList("strings", "strings"), "strings", "strings"}, 23 {str.StringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"}, 24 } 25 26 func TestFoldDup(t *testing.T) { 27 for _, tt := range foldDupTests { 28 f1, f2 := str.FoldDup(tt.list) 29 if f1 != tt.f1 || f2 != tt.f2 { 30 t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2) 31 } 32 } 33 } 34 35 var parseMetaGoImportsTests = []struct { 36 in string 37 out []metaImport 38 }{ 39 { 40 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 41 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 42 }, 43 { 44 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 45 <meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`, 46 []metaImport{ 47 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 48 {"baz/quux", "git", "http://github.com/rsc/baz/quux"}, 49 }, 50 }, 51 { 52 `<head> 53 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 54 </head>`, 55 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 56 }, 57 { 58 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 59 <body>`, 60 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 61 }, 62 { 63 `<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 64 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 65 }, 66 { 67 // XML doesn't like <div style=position:relative>. 68 `<!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>`, 69 []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}}, 70 }, 71 } 72 73 func TestParseMetaGoImports(t *testing.T) { 74 for i, tt := range parseMetaGoImportsTests { 75 out, err := parseMetaGoImports(strings.NewReader(tt.in)) 76 if err != nil { 77 t.Errorf("test#%d: %v", i, err) 78 continue 79 } 80 if !reflect.DeepEqual(out, tt.out) { 81 t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out) 82 } 83 } 84 } 85 86 func pkgImportPath(path string) *load.Package { 87 return &load.Package{ 88 PackagePublic: load.PackagePublic{ 89 ImportPath: path, 90 }, 91 } 92 }