github.com/champo/mobile@v0.0.0-20190107162257-dc0771356504/cmd/gobind/gobind_test.go (about) 1 // Copyright 2016 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 "bytes" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "os/exec" 13 "path/filepath" 14 "testing" 15 ) 16 17 var tests = []struct { 18 name string 19 lang string 20 pkg string 21 goos string 22 }{ 23 {"ObjC-Testpkg", "objc", "golang.org/x/mobile/bind/testdata/testpkg", ""}, 24 {"Java-Testpkg", "java", "golang.org/x/mobile/bind/testdata/testpkg", ""}, 25 {"Go-Testpkg", "go", "golang.org/x/mobile/bind/testdata/testpkg", ""}, 26 {"Java-Javapkg", "java", "golang.org/x/mobile/bind/testdata/testpkg/javapkg", "android"}, 27 {"Go-Javapkg", "go", "golang.org/x/mobile/bind/testdata/testpkg/javapkg", "android"}, 28 {"Go-Javapkg", "go,java,objc", "golang.org/x/mobile/bind/testdata/cgopkg", "android"}, 29 } 30 31 func installGobind() error { 32 if out, err := exec.Command("go", "install", "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil { 33 return fmt.Errorf("gobind install failed: %v: %s", err, out) 34 } 35 return nil 36 } 37 38 func runGobind(lang, pkg, goos string) error { 39 cmd := exec.Command("gobind", "-lang", lang, pkg) 40 if goos != "" { 41 cmd.Env = append(os.Environ(), "GOOS="+goos) 42 cmd.Env = append(os.Environ(), "CGO_ENABLED=1") 43 } 44 if out, err := cmd.CombinedOutput(); err != nil { 45 return fmt.Errorf("gobind -lang %s %s failed: %v: %s", lang, pkg, err, out) 46 } 47 return nil 48 } 49 50 func TestGobind(t *testing.T) { 51 if err := installGobind(); err != nil { 52 t.Fatal(err) 53 } 54 for _, test := range tests { 55 t.Run(test.name, func(t *testing.T) { 56 if err := runGobind(test.lang, test.pkg, test.goos); err != nil { 57 t.Error(err) 58 } 59 }) 60 } 61 } 62 63 func TestDocs(t *testing.T) { 64 if err := installGobind(); err != nil { 65 t.Fatal(err) 66 } 67 // Create a fake package for doc.go 68 tmpdir, err := ioutil.TempDir("", "gobind-test-") 69 if err != nil { 70 t.Fatal(err) 71 } 72 defer os.RemoveAll(tmpdir) 73 docPkg := filepath.Join(tmpdir, "src", "doctest") 74 if err := os.MkdirAll(docPkg, 0700); err != nil { 75 t.Fatal(err) 76 } 77 const docsrc = ` 78 package doctest 79 80 // This is a comment. 81 type Struct struct{ 82 }` 83 if err := ioutil.WriteFile(filepath.Join(docPkg, "doc.go"), []byte(docsrc), 0700); err != nil { 84 t.Fatal(err) 85 } 86 87 const comment = "This is a comment." 88 for _, lang := range []string{"java", "objc"} { 89 cmd := exec.Command("gobind", "-lang", lang, "doctest") 90 cmd.Env = append(os.Environ(), "GOROOT="+tmpdir) 91 out, err := cmd.CombinedOutput() 92 if err != nil { 93 t.Errorf("gobind -lang %s failed: %v: %s", lang, err, out) 94 continue 95 } 96 if bytes.Index(out, []byte(comment)) == -1 { 97 t.Errorf("gobind output for language %s did not contain the comment %q", lang, comment) 98 } 99 } 100 } 101 102 func BenchmarkGobind(b *testing.B) { 103 if err := installGobind(); err != nil { 104 b.Fatal(err) 105 } 106 for _, test := range tests { 107 b.Run(test.name, func(b *testing.B) { 108 for i := 0; i < b.N; i++ { 109 if err := runGobind(test.lang, test.pkg, test.goos); err != nil { 110 b.Error(err) 111 } 112 } 113 }) 114 } 115 }