560ebad5c97a9cc0296f461d4f35a95bbe5795e0
[platform/upstream/gcc48.git] / libgo / go / go / build / build_test.go
1 // Copyright 2011 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 build
6
7 import (
8         "os"
9         "path/filepath"
10         "runtime"
11         "testing"
12 )
13
14 func TestMatch(t *testing.T) {
15         ctxt := Default
16         what := "default"
17         match := func(tag string) {
18                 if !ctxt.match(tag) {
19                         t.Errorf("%s context should match %s, does not", what, tag)
20                 }
21         }
22         nomatch := func(tag string) {
23                 if ctxt.match(tag) {
24                         t.Errorf("%s context should NOT match %s, does", what, tag)
25                 }
26         }
27
28         match(runtime.GOOS + "," + runtime.GOARCH)
29         match(runtime.GOOS + "," + runtime.GOARCH + ",!foo")
30         nomatch(runtime.GOOS + "," + runtime.GOARCH + ",foo")
31
32         what = "modified"
33         ctxt.BuildTags = []string{"foo"}
34         match(runtime.GOOS + "," + runtime.GOARCH)
35         match(runtime.GOOS + "," + runtime.GOARCH + ",foo")
36         nomatch(runtime.GOOS + "," + runtime.GOARCH + ",!foo")
37         match(runtime.GOOS + "," + runtime.GOARCH + ",!bar")
38         nomatch(runtime.GOOS + "," + runtime.GOARCH + ",bar")
39         nomatch("!")
40 }
41
42 func TestDotSlashImport(t *testing.T) {
43         p, err := ImportDir("testdata/other", 0)
44         if err != nil {
45                 t.Fatal(err)
46         }
47         if len(p.Imports) != 1 || p.Imports[0] != "./file" {
48                 t.Fatalf("testdata/other: Imports=%v, want [./file]", p.Imports)
49         }
50
51         p1, err := Import("./file", "testdata/other", 0)
52         if err != nil {
53                 t.Fatal(err)
54         }
55         if p1.Name != "file" {
56                 t.Fatalf("./file: Name=%q, want %q", p1.Name, "file")
57         }
58         dir := filepath.Clean("testdata/other/file") // Clean to use \ on Windows
59         if p1.Dir != dir {
60                 t.Fatalf("./file: Dir=%q, want %q", p1.Name, dir)
61         }
62 }
63
64 func TestLocalDirectory(t *testing.T) {
65         cwd, err := os.Getwd()
66         if err != nil {
67                 t.Fatal(err)
68         }
69
70         p, err := ImportDir(cwd, 0)
71         if err != nil {
72                 t.Fatal(err)
73         }
74         if p.ImportPath != "go/build" {
75                 t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build")
76         }
77 }