Commit 4e9cc085 authored by Rob Pike's avatar Rob Pike

testing: eliminate testing/regexp

Rather than updating the stripped-down regexp implementation embedded
in testing, delete it by passing the one function we need from the package
main file created by gotest.

R=rsc
CC=golang-dev
https://golang.org/cl/2761043
parent 1f4d54ea
......@@ -147,6 +147,7 @@ importpath=$(gomake -s importpath)
echo 'import "./_xtest_"'
fi
echo 'import "testing"'
echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
# test array
echo
echo 'var tests = []testing.Test{'
......@@ -166,8 +167,8 @@ importpath=$(gomake -s importpath)
# body
echo
echo 'func main() {'
echo ' testing.Main(tests)'
echo ' testing.RunBenchmarks(benchmarks)'
echo ' testing.Main(__regexp__.MatchString, tests)'
echo ' testing.RunBenchmarks(__regexp__.MatchString, benchmarks)'
echo '}'
}>_testmain.go
......
......@@ -151,6 +151,7 @@ NOTEST=\
rand\
runtime/pprof\
syscall\
testing\
testing/iotest\
try\
../libcgo\
......
......@@ -11,6 +11,7 @@ import (
"log"
"os"
"reflect"
"regexp"
"testing"
)
......@@ -89,9 +90,9 @@ func (a test) run(t *testing.T, name string) {
}
func match(t *testing.T, err os.Error, pat string) bool {
ok, errstr := testing.MatchString(pat, err.String())
if errstr != "" {
t.Fatalf("compile regexp %s: %v", pat, errstr)
ok, err1 := regexp.MatchString(pat, err.String())
if err1 != nil {
t.Fatalf("compile regexp %s: %v", pat, err1)
}
return ok
}
......
......@@ -10,6 +10,7 @@ import (
"io"
"os"
"reflect"
"regexp"
"strings"
"testing"
"utf8"
......@@ -101,7 +102,7 @@ func (x *Xs) Scan(state ScanState, verb int) os.Error {
if err != nil {
return err
}
if !testing.MustCompile("^" + string(verb) + "+$").MatchString(tok) {
if !regexp.MustCompile("^" + string(verb) + "+$").MatchString(tok) {
return os.ErrorString("syntax error for xs")
}
*x = Xs(tok)
......@@ -385,7 +386,7 @@ func TestScanf(t *testing.T) {
func TestScanOverflow(t *testing.T) {
// different machines and different types report errors with different strings.
re := testing.MustCompile("overflow|too large|out of range|not representable")
re := regexp.MustCompile("overflow|too large|out of range|not representable")
for _, test := range overflowTests {
_, err := Sscan(test.text, test.in)
if err == nil {
......
......@@ -23,7 +23,7 @@ func TestTempFile(t *testing.T) {
t.Errorf("TempFile(dir, `ioutil_test`) = %v, %v", f, err)
}
if f != nil {
re := testing.MustCompile("^" + regexp.QuoteMeta(dir) + "/ioutil_test[0-9]+$")
re := regexp.MustCompile("^" + regexp.QuoteMeta(dir) + "/ioutil_test[0-9]+$")
if !re.MatchString(f.Name()) {
t.Errorf("TempFile(`"+dir+"`, `ioutil_test`) created bad name %s", f.Name())
}
......
......@@ -7,7 +7,6 @@ include ../../Make.inc
TARG=testing
GOFILES=\
benchmark.go\
regexp.go\
testing.go\
include ../../Make.pkg
......@@ -148,18 +148,18 @@ func (b *B) run() {
// An internal function but exported because it is cross-package; part of the implementation
// of gotest.
func RunBenchmarks(benchmarks []Benchmark) {
func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), benchmarks []Benchmark) {
// If no flag was specified, don't run benchmarks.
if len(*matchBenchmarks) == 0 {
return
}
re, err := CompileRegexp(*matchBenchmarks)
if err != "" {
println("invalid regexp for -benchmarks:", err)
os.Exit(1)
}
for _, Benchmark := range benchmarks {
if !re.MatchString(Benchmark.Name) {
matched, err := matchString(*matchBenchmarks, Benchmark.Name)
if err != nil {
println("invalid regexp for -benchmarks:", err)
os.Exit(1)
}
if !matched {
continue
}
b := &B{benchmark: Benchmark}
......
This diff is collapsed.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testing
var good_re = []string{
``,
`.`,
`^.$`,
`a`,
`a*`,
`a+`,
`a?`,
`a|b`,
`a*|b*`,
`(a*|b)(c*|d)`,
`[a-z]`,
`[a-abc-c\-\]\[]`,
`[a-z]+`,
`[]`,
`[abc]`,
`[^1234]`,
`[^\n]`,
}
// TODO: nice to do this with a map
type stringError struct {
re string
err string
}
var bad_re = []stringError{
{`*`, ErrBareClosure},
{`(abc`, ErrUnmatchedLpar},
{`abc)`, ErrUnmatchedRpar},
{`x[a-z`, ErrUnmatchedLbkt},
{`abc]`, ErrUnmatchedRbkt},
{`[z-a]`, ErrBadRange},
{`abc\`, ErrExtraneousBackslash},
{`a**`, ErrBadClosure},
{`a*+`, ErrBadClosure},
{`a??`, ErrBadClosure},
{`*`, ErrBareClosure},
{`\x`, ErrBadBackslash},
}
type vec []int
type tester struct {
re string
text string
match vec
}
var matches = []tester{
{``, "", vec{0, 0}},
{`a`, "a", vec{0, 1}},
{`x`, "y", vec{}},
{`b`, "abc", vec{1, 2}},
{`.`, "a", vec{0, 1}},
{`.*`, "abcdef", vec{0, 6}},
{`^abcd$`, "abcd", vec{0, 4}},
{`^bcd'`, "abcdef", vec{}},
{`^abcd$`, "abcde", vec{}},
{`a+`, "baaab", vec{1, 4}},
{`a*`, "baaab", vec{0, 0}},
{`[a-z]+`, "abcd", vec{0, 4}},
{`[^a-z]+`, "ab1234cd", vec{2, 6}},
{`[a\-\]z]+`, "az]-bcz", vec{0, 4}},
{`[^\n]+`, "abcd\n", vec{0, 4}},
{`[日本語]+`, "日本語日本語", vec{0, 18}},
{`()`, "", vec{0, 0, 0, 0}},
{`(a)`, "a", vec{0, 1, 0, 1}},
{`(.)(.)`, "日a", vec{0, 4, 0, 3, 3, 4}},
{`(.*)`, "", vec{0, 0, 0, 0}},
{`(.*)`, "abcd", vec{0, 4, 0, 4}},
{`(..)(..)`, "abcd", vec{0, 4, 0, 2, 2, 4}},
{`(([^xyz]*)(d))`, "abcd", vec{0, 4, 0, 4, 0, 3, 3, 4}},
{`((a|b|c)*(d))`, "abcd", vec{0, 4, 0, 4, 2, 3, 3, 4}},
{`(((a|b|c)*)(d))`, "abcd", vec{0, 4, 0, 4, 0, 3, 2, 3, 3, 4}},
{`a*(|(b))c*`, "aacc", vec{0, 4, 2, 2, -1, -1}},
}
func compileTest(t *T, expr string, error string) *Regexp {
re, err := CompileRegexp(expr)
if err != error {
t.Error("compiling `", expr, "`; unexpected error: ", err)
}
return re
}
func TestGoodCompile(t *T) {
for i := 0; i < len(good_re); i++ {
compileTest(t, good_re[i], "")
}
}
func TestBadCompile(t *T) {
for i := 0; i < len(bad_re); i++ {
compileTest(t, bad_re[i].re, bad_re[i].err)
}
}
func matchTest(t *T, expr string, str string, match []int) {
re := compileTest(t, expr, "")
if re == nil {
return
}
m := re.MatchString(str)
if m != (len(match) > 0) {
t.Error("MatchString failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0)
}
// now try bytes
m = re.Match([]byte(str))
if m != (len(match) > 0) {
t.Error("Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0)
}
}
func TestMatch(t *T) {
for i := 0; i < len(matches); i++ {
test := &matches[i]
matchTest(t, test.re, test.text, test.match)
}
}
func matchFunctionTest(t *T, expr string, str string, match []int) {
m, err := MatchString(expr, str)
if err == "" {
return
}
if m != (len(match) > 0) {
t.Error("function Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0)
}
}
func TestMatchFunction(t *T) {
for i := 0; i < len(matches); i++ {
test := &matches[i]
matchFunctionTest(t, test.re, test.text, test.match)
}
}
......@@ -135,19 +135,19 @@ func tRunner(t *T, test *Test) {
// An internal function but exported because it is cross-package; part of the implementation
// of gotest.
func Main(tests []Test) {
func Main(matchString func(pat, str string) (bool, os.Error), tests []Test) {
flag.Parse()
ok := true
if len(tests) == 0 {
println("testing: warning: no tests to run")
}
re, err := CompileRegexp(*match)
if err != "" {
println("invalid regexp for -match:", err)
os.Exit(1)
}
for i := 0; i < len(tests); i++ {
if !re.MatchString(tests[i].Name) {
matched, err := matchString(*match, tests[i].Name)
if err != nil {
println("invalid regexp for -match:", err)
os.Exit(1)
}
if !matched {
continue
}
if *chatty {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment