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