}
// TODO: worth checking the signature? Probably not.
}
- if strings.HasSuffix(f.pkg, "_test") {
+ if isOutsideTest(f.pkg) {
outsideFileNames = append(outsideFileNames, f.name)
} else {
insideFileNames = append(insideFileNames, f.name)
if len(f.tests) == 0 && len(f.benchmarks) == 0 {
continue
}
- if strings.HasSuffix(f.pkg, "_test") {
+ if isOutsideTest(f.pkg) {
outsideTests = true
} else {
insideTests = true
}
}
+ // Rename the imports for the system under test,
+ // in case the tested package has the same name
+ // as any of the other imports, variables or methods.
if insideTests {
switch importPath {
case "testing":
case "main":
// Import path main is reserved, so import with
// explicit reference to ./_test/main instead.
- // Also, the file we are writing defines a function named main,
- // so rename this import to __main__ to avoid name conflict.
- fmt.Fprintf(b, "import __main__ %q\n", "./_test/main")
+ fmt.Fprintf(b, "import target %q\n", "./_test/main")
default:
- fmt.Fprintf(b, "import %q\n", importPath)
+ fmt.Fprintf(b, "import target %q\n", importPath)
}
}
if outsideTests {
- fmt.Fprintf(b, "import %q\n", "./_xtest_")
+ // It is possible to have both inside and outside tests
+ // at the same time, so a different import name is needed.
+ fmt.Fprintf(b, "import target_test %q\n", "./_xtest_")
}
fmt.Fprintf(b, "import %q\n", "testing")
- fmt.Fprintf(b, "import __os__ %q\n", "os") // rename in case tested package is called os
- fmt.Fprintf(b, "import __regexp__ %q\n", "regexp") // rename in case tested package is called regexp
- fmt.Fprintln(b) // for gofmt
+ fmt.Fprintf(b, "import %q\n", "os")
+ fmt.Fprintf(b, "import %q\n", "regexp")
+ fmt.Fprintln(b) // for gofmt
// Tests.
fmt.Fprintln(b, "var tests = []testing.InternalTest{")
for _, f := range files {
for _, t := range f.tests {
- fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, t, notMain(f.pkg), t)
+ fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, t, renamedPackage(f.pkg), t)
}
}
fmt.Fprintln(b, "}")
fmt.Fprintf(b, "var benchmarks = []testing.InternalBenchmark{")
for _, f := range files {
for _, bm := range f.benchmarks {
- fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, bm, notMain(f.pkg), bm)
+ fmt.Fprintf(b, "\t{\"%s.%s\", %s.%s},\n", f.pkg, bm, renamedPackage(f.pkg), bm)
}
}
fmt.Fprintln(b, "}")
fmt.Fprintf(b, "var examples = []testing.InternalExample{")
for _, f := range files {
for _, eg := range f.examples {
- fmt.Fprintf(b, "\t{%q, %s.%s, %q},\n", eg.name, f.pkg, eg.name, eg.output)
+ fmt.Fprintf(b, "\t{%q, %s.%s, %q},\n", eg.name, renamedPackage(f.pkg), eg.name, eg.output)
}
}
fmt.Fprintln(b, "}")
fmt.Fprintln(b, testBody)
}
-// notMain returns the package, renaming as appropriate if it's "main".
-func notMain(pkg string) string {
- if pkg == "main" {
- return "__main__"
+// renamedPackage returns the name under which the test package was imported.
+func renamedPackage(pkg string) string {
+ if isOutsideTest(pkg) {
+ return "target_test"
}
- return pkg
+ return "target"
+}
+
+func isOutsideTest(pkg string) bool {
+ return strings.HasSuffix(pkg, "_test")
}
// testBody is just copied to the output. It's the code that runs the tests.
var testBody = `
var matchPat string
-var matchRe *__regexp__.Regexp
+var matchRe *regexp.Regexp
-func matchString(pat, str string) (result bool, err __os__.Error) {
+func matchString(pat, str string) (result bool, err os.Error) {
if matchRe == nil || matchPat != pat {
matchPat = pat
- matchRe, err = __regexp__.Compile(matchPat)
+ matchRe, err = regexp.Compile(matchPat)
if err != nil {
return
}