github.com/fjballest/golang@v0.0.0-20151209143359-e4c5fe594ca8/src/cmd/go/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 main
     6  
     7  import (
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  var foldDupTests = []struct {
    14  	list   []string
    15  	f1, f2 string
    16  }{
    17  	{stringList("math/rand", "math/big"), "", ""},
    18  	{stringList("math", "strings"), "", ""},
    19  	{stringList("strings"), "", ""},
    20  	{stringList("strings", "strings"), "strings", "strings"},
    21  	{stringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"},
    22  }
    23  
    24  func TestFoldDup(t *testing.T) {
    25  	for _, tt := range foldDupTests {
    26  		f1, f2 := foldDup(tt.list)
    27  		if f1 != tt.f1 || f2 != tt.f2 {
    28  			t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2)
    29  		}
    30  	}
    31  }
    32  
    33  var parseMetaGoImportsTests = []struct {
    34  	in  string
    35  	out []metaImport
    36  }{
    37  	{
    38  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    39  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    40  	},
    41  	{
    42  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    43  		<meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
    44  		[]metaImport{
    45  			{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
    46  			{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
    47  		},
    48  	},
    49  	{
    50  		`<head>
    51  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    52  		</head>`,
    53  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    54  	},
    55  	{
    56  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    57  		<body>`,
    58  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    59  	},
    60  }
    61  
    62  func TestParseMetaGoImports(t *testing.T) {
    63  	for i, tt := range parseMetaGoImportsTests {
    64  		out, err := parseMetaGoImports(strings.NewReader(tt.in))
    65  		if err != nil {
    66  			t.Errorf("test#%d: %v", i, err)
    67  			continue
    68  		}
    69  		if !reflect.DeepEqual(out, tt.out) {
    70  			t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
    71  		}
    72  	}
    73  }
    74  
    75  func TestSharedLibName(t *testing.T) {
    76  	// TODO(avdva) - make these values platform-specific
    77  	prefix := "lib"
    78  	suffix := ".so"
    79  	testData := []struct {
    80  		args      []string
    81  		pkgs      []*Package
    82  		expected  string
    83  		expectErr bool
    84  	}{
    85  		{
    86  			[]string{"std"},
    87  			[]*Package{},
    88  			"std",
    89  			false,
    90  		},
    91  		{
    92  			[]string{"std", "cmd"},
    93  			[]*Package{},
    94  			"std,cmd",
    95  			false,
    96  		},
    97  		{
    98  			[]string{},
    99  			[]*Package{&Package{ImportPath: "gopkg.in/somelib"}},
   100  			"gopkg.in-somelib",
   101  			false,
   102  		},
   103  		{
   104  			[]string{"./..."},
   105  			[]*Package{&Package{ImportPath: "somelib"}},
   106  			"somelib",
   107  			false,
   108  		},
   109  		{
   110  			[]string{"../somelib", "../somelib"},
   111  			[]*Package{&Package{ImportPath: "somelib"}},
   112  			"somelib",
   113  			false,
   114  		},
   115  		{
   116  			[]string{"../lib1", "../lib2"},
   117  			[]*Package{&Package{ImportPath: "gopkg.in/lib1"}, &Package{ImportPath: "gopkg.in/lib2"}},
   118  			"gopkg.in-lib1,gopkg.in-lib2",
   119  			false,
   120  		},
   121  		{
   122  			[]string{"./..."},
   123  			[]*Package{
   124  				&Package{ImportPath: "gopkg.in/dir/lib1"},
   125  				&Package{ImportPath: "gopkg.in/lib2"},
   126  				&Package{ImportPath: "gopkg.in/lib3"},
   127  			},
   128  			"gopkg.in-dir-lib1,gopkg.in-lib2,gopkg.in-lib3",
   129  			false,
   130  		},
   131  		{
   132  			[]string{"std", "../lib2"},
   133  			[]*Package{},
   134  			"",
   135  			true,
   136  		},
   137  		{
   138  			[]string{"all", "./"},
   139  			[]*Package{},
   140  			"",
   141  			true,
   142  		},
   143  		{
   144  			[]string{"cmd", "fmt"},
   145  			[]*Package{},
   146  			"",
   147  			true,
   148  		},
   149  	}
   150  	for _, data := range testData {
   151  		computed, err := libname(data.args, data.pkgs)
   152  		if err != nil {
   153  			if !data.expectErr {
   154  				t.Errorf("libname returned an error %q, expected a name", err.Error())
   155  			}
   156  		} else if data.expectErr {
   157  			t.Errorf("libname returned %q, expected an error", computed)
   158  		} else {
   159  			expected := prefix + data.expected + suffix
   160  			if expected != computed {
   161  				t.Errorf("libname returned %q, expected %q", computed, expected)
   162  			}
   163  		}
   164  	}
   165  }