2016-01-06 12:56:13 -07:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-08-05 17:42:54 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// No testdata on Android.
|
|
|
|
|
|
|
|
// +build !android
|
|
|
|
|
2014-04-02 10:24:55 -06:00
|
|
|
package eg_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
2018-08-10 10:49:16 -06:00
|
|
|
"go/constant"
|
2014-04-02 10:24:55 -06:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2015-12-29 11:06:30 -07:00
|
|
|
"go/types"
|
2014-04-02 10:24:55 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/go/loader"
|
|
|
|
"golang.org/x/tools/refactor/eg"
|
2014-04-02 10:24:55 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(adonovan): more tests:
|
|
|
|
// - of command-line tool
|
|
|
|
// - of all parts of syntax
|
|
|
|
// - of applying a template to a package it imports:
|
|
|
|
// the replacement syntax should use unqualified names for its objects.
|
|
|
|
|
|
|
|
var (
|
|
|
|
updateFlag = flag.Bool("update", false, "update the golden files")
|
|
|
|
verboseFlag = flag.Bool("verbose", false, "show matcher information")
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
t.Skipf("skipping test on %q (no /usr/bin/diff)", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := loader.Config{
|
2015-02-23 15:03:22 -07:00
|
|
|
Fset: token.NewFileSet(),
|
|
|
|
ParserMode: parser.ParseComments,
|
2014-04-02 10:24:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Each entry is a single-file package.
|
|
|
|
// (Multi-file packages aren't interesting for this test.)
|
|
|
|
// Order matters: each non-template package is processed using
|
|
|
|
// the preceding template package.
|
|
|
|
for _, filename := range []string{
|
|
|
|
"testdata/A.template",
|
|
|
|
"testdata/A1.go",
|
|
|
|
"testdata/A2.go",
|
|
|
|
|
|
|
|
"testdata/B.template",
|
|
|
|
"testdata/B1.go",
|
|
|
|
|
|
|
|
"testdata/C.template",
|
|
|
|
"testdata/C1.go",
|
|
|
|
|
|
|
|
"testdata/D.template",
|
|
|
|
"testdata/D1.go",
|
|
|
|
|
|
|
|
"testdata/E.template",
|
|
|
|
"testdata/E1.go",
|
|
|
|
|
2014-08-25 14:26:54 -06:00
|
|
|
"testdata/F.template",
|
|
|
|
"testdata/F1.go",
|
|
|
|
|
2015-05-22 13:53:13 -06:00
|
|
|
"testdata/G.template",
|
|
|
|
"testdata/G1.go",
|
|
|
|
|
|
|
|
"testdata/H.template",
|
|
|
|
"testdata/H1.go",
|
|
|
|
|
2017-11-14 16:37:00 -07:00
|
|
|
"testdata/I.template",
|
|
|
|
"testdata/I1.go",
|
|
|
|
|
|
|
|
"testdata/J.template",
|
|
|
|
"testdata/J1.go",
|
|
|
|
|
2014-04-02 10:24:55 -06:00
|
|
|
"testdata/bad_type.template",
|
|
|
|
"testdata/no_before.template",
|
|
|
|
"testdata/no_after_return.template",
|
|
|
|
"testdata/type_mismatch.template",
|
|
|
|
"testdata/expr_type_mismatch.template",
|
|
|
|
} {
|
|
|
|
pkgname := strings.TrimSuffix(filepath.Base(filename), ".go")
|
2015-01-30 11:30:23 -07:00
|
|
|
conf.CreateFromFilenames(pkgname, filename)
|
2014-04-02 10:24:55 -06:00
|
|
|
}
|
|
|
|
iprog, err := conf.Load()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var xform *eg.Transformer
|
|
|
|
for _, info := range iprog.Created {
|
|
|
|
file := info.Files[0]
|
|
|
|
filename := iprog.Fset.File(file.Pos()).Name() // foo.go
|
|
|
|
|
|
|
|
if strings.HasSuffix(filename, "template") {
|
|
|
|
// a new template
|
|
|
|
shouldFail, _ := info.Pkg.Scope().Lookup("shouldFail").(*types.Const)
|
2015-06-01 13:21:03 -06:00
|
|
|
xform, err = eg.NewTransformer(iprog.Fset, info.Pkg, file, &info.Info, *verboseFlag)
|
2014-04-02 10:24:55 -06:00
|
|
|
if err != nil {
|
|
|
|
if shouldFail == nil {
|
|
|
|
t.Errorf("NewTransformer(%s): %s", filename, err)
|
2018-08-10 10:49:16 -06:00
|
|
|
} else if want := constant.StringVal(shouldFail.Val()); !strings.Contains(err.Error(), want) {
|
2014-04-02 10:24:55 -06:00
|
|
|
t.Errorf("NewTransformer(%s): got error %q, want error %q", filename, err, want)
|
|
|
|
}
|
|
|
|
} else if shouldFail != nil {
|
|
|
|
t.Errorf("NewTransformer(%s) succeeded unexpectedly; want error %q",
|
|
|
|
filename, shouldFail.Val())
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if xform == nil {
|
|
|
|
t.Errorf("%s: no previous template", filename)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply previous template to this package
|
|
|
|
n := xform.Transform(&info.Info, info.Pkg, file)
|
|
|
|
if n == 0 {
|
|
|
|
t.Errorf("%s: no matches", filename)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
got := filename + "t" // foo.got
|
|
|
|
golden := filename + "lden" // foo.golden
|
|
|
|
|
|
|
|
// Write actual output to foo.got.
|
|
|
|
if err := eg.WriteAST(iprog.Fset, got, file); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-04-10 11:17:20 -06:00
|
|
|
defer os.Remove(got)
|
2014-04-02 10:24:55 -06:00
|
|
|
|
|
|
|
// Compare foo.got with foo.golden.
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
cmd = exec.Command("/bin/diff", "-c", golden, got)
|
|
|
|
default:
|
2014-04-21 16:35:16 -06:00
|
|
|
cmd = exec.Command("/usr/bin/diff", "-u", golden, got)
|
2014-04-02 10:24:55 -06:00
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd.Stdout = buf
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Errorf("eg tests for %s failed: %s.\n%s\n", filename, err, buf)
|
|
|
|
|
|
|
|
if *updateFlag {
|
|
|
|
t.Logf("Updating %s...", golden)
|
|
|
|
if err := exec.Command("/bin/cp", got, golden).Run(); err != nil {
|
|
|
|
t.Errorf("Update failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|