github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/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  		`<head>
    52  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    53  		</head>`,
    54  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    55  	},
    56  	{
    57  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    58  		<body>`,
    59  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    60  	},
    61  	{
    62  		`<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    63  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    64  	},
    65  	{
    66  		// XML doesn't like <div style=position:relative>.
    67  		`<!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>`,
    68  		[]metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}},
    69  	},
    70  }
    71  
    72  func TestParseMetaGoImports(t *testing.T) {
    73  	for i, tt := range parseMetaGoImportsTests {
    74  		out, err := parseMetaGoImports(strings.NewReader(tt.in))
    75  		if err != nil {
    76  			t.Errorf("test#%d: %v", i, err)
    77  			continue
    78  		}
    79  		if !reflect.DeepEqual(out, tt.out) {
    80  			t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
    81  		}
    82  	}
    83  }