Exemplo n.º 1
0
func TestClasses(t *testing.T) {
	if !java.IsAvailable() {
		t.Skipf("java importer is not available")
	}
	runTest(t, []string{
		"golang.org/x/mobile/bind/testpkg/javapkg",
	}, "", "ClassesTest")
}
Exemplo n.º 2
0
func TestGenGoJavaWrappers(t *testing.T) {
	if !java.IsAvailable() {
		t.Skipf("java is not available")
	}
	for _, filename := range javaTests {
		var buf bytes.Buffer
		refs := fileRefs(t, filename, "Java/")
		classes, err := java.Import("", "", refs)
		if err != nil {
			t.Fatal(err)
		}
		tmpGopath, err := ioutil.TempDir(os.TempDir(), "gomobile-bind-test-")
		if err != nil {
			t.Fatal(err)
		}
		defer os.RemoveAll(tmpGopath)
		cg := genJavaPackages(t, tmpGopath, classes, &buf)
		pkg := typeCheck(t, filename, tmpGopath)
		cg.GenGo()
		testGenGo(t, filename, &buf, pkg)
	}
}
Exemplo n.º 3
0
func TestGenJava(t *testing.T) {
	allTests := tests
	if java.IsAvailable() {
		allTests = append(append([]string{}, allTests...), javaTests...)
	}
	for _, filename := range allTests {
		refs := fileRefs(t, filename, "Java/")
		classes, err := java.Import("", "", refs)
		if err != nil {
			t.Fatal(err)
		}
		var cg *ClassGen
		tmpGopath := ""
		var buf bytes.Buffer
		if len(classes) > 0 {
			tmpGopath, err = ioutil.TempDir(os.TempDir(), "gomobile-bind-test-")
			if err != nil {
				t.Fatal(err)
			}
			defer os.RemoveAll(tmpGopath)
			cg = genJavaPackages(t, tmpGopath, classes, new(bytes.Buffer))
			cg.Buf = &buf
		}
		pkg := typeCheck(t, filename, tmpGopath)
		g := &JavaGen{
			Generator: &Generator{
				Printer: &Printer{Buf: &buf, IndentEach: []byte("    ")},
				Fset:    fset,
				AllPkg:  []*types.Package{pkg},
				Pkg:     pkg,
			},
		}
		g.Init(classes)
		testCases := []struct {
			suffix string
			gen    func() error
		}{
			{
				".java.golden",
				func() error {
					for i := range g.ClassNames() {
						if err := g.GenClass(i); err != nil {
							return err
						}
					}
					return g.GenJava()
				},
			},
			{
				".java.c.golden",
				func() error {
					if cg != nil {
						cg.GenC()
					}
					return g.GenC()
				},
			},
			{
				".java.h.golden",
				func() error {
					if cg != nil {
						cg.GenH()
					}
					return g.GenH()
				},
			},
		}

		for _, tc := range testCases {
			buf.Reset()
			if err := tc.gen(); err != nil {
				t.Errorf("%s: %v", filename, err)
				continue
			}
			out := writeTempFile(t, "generated"+tc.suffix, buf.Bytes())
			defer os.Remove(out)
			golden := filename[:len(filename)-len(".go")] + tc.suffix
			if diffstr := diff(golden, out); diffstr != "" {
				t.Errorf("%s: does not match Java golden:\n%s", filename, diffstr)

				if *updateFlag {
					t.Logf("Updating %s...", golden)
					if err := exec.Command("/bin/cp", out, golden).Run(); err != nil {
						t.Errorf("Update failed: %s", err)
					}
				}

			}
		}
	}
}