.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / go / ir / irutil / load_test.go
1 // Copyright 2015 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 irutil_test
6
7 import (
8         "bytes"
9         "go/ast"
10         "go/importer"
11         "go/parser"
12         "go/token"
13         "go/types"
14         "os"
15         "strings"
16         "testing"
17
18         "honnef.co/go/tools/go/ir/irutil"
19
20         "golang.org/x/tools/go/packages"
21 )
22
23 const hello = `package main
24
25 import "fmt"
26
27 func main() {
28         fmt.Println("Hello, world")
29 }
30 `
31
32 func TestBuildPackage(t *testing.T) {
33         // There is a more substantial test of BuildPackage and the
34         // IR program it builds in ../ir/builder_test.go.
35
36         fset := token.NewFileSet()
37         f, err := parser.ParseFile(fset, "hello.go", hello, 0)
38         if err != nil {
39                 t.Fatal(err)
40         }
41
42         pkg := types.NewPackage("hello", "")
43         irpkg, _, err := irutil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, pkg, []*ast.File{f}, 0)
44         if err != nil {
45                 t.Fatal(err)
46         }
47         if pkg.Name() != "main" {
48                 t.Errorf("pkg.Name() = %s, want main", pkg.Name())
49         }
50         if irpkg.Func("main") == nil {
51                 irpkg.WriteTo(os.Stderr)
52                 t.Errorf("irpkg has no main function")
53         }
54 }
55
56 func TestPackages(t *testing.T) {
57         cfg := &packages.Config{Mode: packages.LoadSyntax}
58         initial, err := packages.Load(cfg, "bytes")
59         if err != nil {
60                 t.Fatal(err)
61         }
62         if packages.PrintErrors(initial) > 0 {
63                 t.Fatal("there were errors")
64         }
65
66         prog, pkgs := irutil.Packages(initial, 0, nil)
67         bytesNewBuffer := pkgs[0].Func("NewBuffer")
68         bytesNewBuffer.Pkg.Build()
69
70         // We'll dump the IR of bytes.NewBuffer because it is small and stable.
71         out := new(bytes.Buffer)
72         bytesNewBuffer.WriteTo(out)
73
74         // For determinism, sanitize the location.
75         location := prog.Fset.Position(bytesNewBuffer.Pos()).String()
76         got := strings.Replace(out.String(), location, "$GOROOT/src/bytes/buffer.go:1", -1)
77
78         want := `
79 # Name: bytes.NewBuffer
80 # Package: bytes
81 # Location: $GOROOT/src/bytes/buffer.go:1
82 func NewBuffer(buf []byte) *Buffer:
83 b0: # entry
84         t1 = Parameter <[]byte> {buf}
85         t2 = HeapAlloc <*Buffer>
86         t3 = FieldAddr <*[]byte> [0] (buf) t2
87         Store {[]byte} t3 t1
88         Jump → b1
89
90 b1: ← b0 # exit
91         Return t2
92
93 `[1:]
94         if got != want {
95                 t.Errorf("bytes.NewBuffer IR = <<%s>>, want <<%s>>", got, want)
96         }
97 }
98
99 func TestBuildPackage_MissingImport(t *testing.T) {
100         fset := token.NewFileSet()
101         f, err := parser.ParseFile(fset, "bad.go", `package bad; import "missing"`, 0)
102         if err != nil {
103                 t.Fatal(err)
104         }
105
106         pkg := types.NewPackage("bad", "")
107         irpkg, _, err := irutil.BuildPackage(new(types.Config), fset, pkg, []*ast.File{f}, 0)
108         if err == nil || irpkg != nil {
109                 t.Fatal("BuildPackage succeeded unexpectedly")
110         }
111 }
112
113 func TestIssue28106(t *testing.T) {
114         // In go1.10, go/packages loads all packages from source, not
115         // export data, but does not type check function bodies of
116         // imported packages. This test ensures that we do not attempt
117         // to run the IR builder on functions without type information.
118         cfg := &packages.Config{Mode: packages.LoadSyntax}
119         pkgs, err := packages.Load(cfg, "runtime")
120         if err != nil {
121                 t.Fatal(err)
122         }
123         prog, _ := irutil.Packages(pkgs, 0, nil)
124         prog.Build() // no crash
125 }