2008-10-09 19:40:53 -07:00
|
|
|
// 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.
|
|
|
|
|
|
2008-11-19 15:38:46 -08:00
|
|
|
package regexp
|
2008-10-09 19:40:53 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os";
|
|
|
|
|
"regexp";
|
2008-11-19 15:38:46 -08:00
|
|
|
"testing";
|
2008-10-09 19:40:53 -07:00
|
|
|
)
|
|
|
|
|
|
2009-02-13 14:48:32 -08:00
|
|
|
var good_re = []string(
|
2008-10-14 17:45:49 -07:00
|
|
|
``,
|
|
|
|
|
`.`,
|
|
|
|
|
`^.$`,
|
|
|
|
|
`a`,
|
|
|
|
|
`a*`,
|
|
|
|
|
`a+`,
|
|
|
|
|
`a?`,
|
|
|
|
|
`a|b`,
|
|
|
|
|
`a*|b*`,
|
|
|
|
|
`(a*|b)(c*|d)`,
|
|
|
|
|
`[a-z]`,
|
|
|
|
|
`[a-abc-c\-\]\[]`,
|
|
|
|
|
`[a-z]+`,
|
|
|
|
|
`[]`,
|
|
|
|
|
`[abc]`,
|
|
|
|
|
`[^1234]`,
|
2009-02-13 14:48:32 -08:00
|
|
|
)
|
2008-10-14 16:32:43 -07:00
|
|
|
|
2009-01-06 13:54:53 -08:00
|
|
|
// TODO: nice to do this with a map
|
2009-01-15 17:22:15 -08:00
|
|
|
type stringError struct {
|
2008-10-14 16:32:43 -07:00
|
|
|
re string;
|
|
|
|
|
err *os.Error;
|
|
|
|
|
}
|
2009-02-13 14:48:32 -08:00
|
|
|
var bad_re = []stringError(
|
|
|
|
|
stringError( `*`, regexp.ErrBareClosure ),
|
|
|
|
|
stringError( `(abc`, regexp.ErrUnmatchedLpar ),
|
|
|
|
|
stringError( `abc)`, regexp.ErrUnmatchedRpar ),
|
|
|
|
|
stringError( `x[a-z`, regexp.ErrUnmatchedLbkt ),
|
|
|
|
|
stringError( `abc]`, regexp.ErrUnmatchedRbkt ),
|
|
|
|
|
stringError( `[z-a]`, regexp.ErrBadRange ),
|
|
|
|
|
stringError( `abc\`, regexp.ErrExtraneousBackslash ),
|
|
|
|
|
stringError( `a**`, regexp.ErrBadClosure ),
|
|
|
|
|
stringError( `a*+`, regexp.ErrBadClosure ),
|
|
|
|
|
stringError( `a??`, regexp.ErrBadClosure ),
|
|
|
|
|
stringError( `*`, regexp.ErrBareClosure ),
|
|
|
|
|
stringError( `\x`, regexp.ErrBadBackslash ),
|
|
|
|
|
)
|
2008-10-14 16:32:43 -07:00
|
|
|
|
2009-01-15 17:22:15 -08:00
|
|
|
type vec []int;
|
2008-10-14 16:32:43 -07:00
|
|
|
|
2009-01-15 17:22:15 -08:00
|
|
|
type tester struct {
|
2008-10-14 16:32:43 -07:00
|
|
|
re string;
|
|
|
|
|
text string;
|
2009-01-15 17:22:15 -08:00
|
|
|
match vec;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-13 14:48:32 -08:00
|
|
|
var matches = []tester (
|
|
|
|
|
tester( ``, "", vec(0,0) ),
|
|
|
|
|
tester( `a`, "a", vec(0,1) ),
|
|
|
|
|
tester( `x`, "y", vec() ),
|
|
|
|
|
tester( `b`, "abc", vec(1,2) ),
|
|
|
|
|
tester( `.`, "a", vec(0,1) ),
|
|
|
|
|
tester( `.*`, "abcdef", vec(0,6) ),
|
|
|
|
|
tester( `^abcd$`, "abcd", vec(0,4) ),
|
|
|
|
|
tester( `^bcd'`, "abcdef", vec() ),
|
|
|
|
|
tester( `^abcd$`, "abcde", vec() ),
|
|
|
|
|
tester( `a+`, "baaab", vec(1,4) ),
|
|
|
|
|
tester( `a*`, "baaab", vec(0,0) ),
|
|
|
|
|
tester( `[a-z]+`, "abcd", vec(0,4) ),
|
|
|
|
|
tester( `[^a-z]+`, "ab1234cd", vec(2,6) ),
|
|
|
|
|
tester( `[a\-\]z]+`, "az]-bcz", vec(0,4) ),
|
|
|
|
|
tester( `[日本語]+`, "日本語日本語", vec(0,18) ),
|
|
|
|
|
tester( `()`, "", vec(0,0, 0,0) ),
|
|
|
|
|
tester( `(a)`, "a", vec(0,1, 0,1) ),
|
|
|
|
|
tester( `(.)(.)`, "æ—¥a", vec(0,4, 0,3, 3,4) ),
|
|
|
|
|
tester( `(.*)`, "", vec(0,0, 0,0) ),
|
|
|
|
|
tester( `(.*)`, "abcd", vec(0,4, 0,4) ),
|
|
|
|
|
tester( `(..)(..)`, "abcd", vec(0,4, 0,2, 2,4) ),
|
|
|
|
|
tester( `(([^xyz]*)(d))`, "abcd", vec(0,4, 0,4, 0,3, 3,4) ),
|
|
|
|
|
tester( `((a|b|c)*(d))`, "abcd", vec(0,4, 0,4, 2,3, 3,4) ),
|
|
|
|
|
tester( `(((a|b|c)*)(d))`, "abcd", vec(0,4, 0,4, 0,3, 2,3, 3,4) ),
|
|
|
|
|
tester( `a*(|(b))c*`, "aacc", vec(0,4, 2,2, -1,-1) ),
|
|
|
|
|
)
|
2008-10-14 16:32:43 -07:00
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func compileTest(t *testing.T, expr string, error *os.Error) regexp.Regexp {
|
2008-10-14 16:32:43 -07:00
|
|
|
re, err := regexp.Compile(expr);
|
|
|
|
|
if err != error {
|
2008-11-19 15:38:46 -08:00
|
|
|
t.Error("compiling `", expr, "`; unexpected error: ", err.String());
|
2008-10-14 16:32:43 -07:00
|
|
|
}
|
|
|
|
|
return re
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func printVec(t *testing.T, m []int) {
|
2008-12-18 22:37:22 -08:00
|
|
|
l := len(m);
|
2008-10-14 17:45:49 -07:00
|
|
|
if l == 0 {
|
2008-11-19 15:38:46 -08:00
|
|
|
t.Log("\t<no match>");
|
2008-10-14 17:45:49 -07:00
|
|
|
} else {
|
2008-12-18 22:37:22 -08:00
|
|
|
for i := 0; i < l; i = i+2 {
|
2008-11-19 15:38:46 -08:00
|
|
|
t.Log("\t", m[i], ",", m[i+1])
|
2008-10-14 17:45:49 -07:00
|
|
|
}
|
2008-10-14 16:32:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func printStrings(t *testing.T, m []string) {
|
2009-01-06 13:54:53 -08:00
|
|
|
l := len(m);
|
|
|
|
|
if l == 0 {
|
|
|
|
|
t.Log("\t<no match>");
|
|
|
|
|
} else {
|
|
|
|
|
for i := 0; i < l; i = i+2 {
|
|
|
|
|
t.Logf("\t%q", m[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func equal(m1, m2 []int) bool {
|
2008-12-18 22:37:22 -08:00
|
|
|
l := len(m1);
|
|
|
|
|
if l != len(m2) {
|
2008-10-14 16:32:43 -07:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
|
if m1[i] != m2[i] {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func equalStrings(m1, m2 []string) bool {
|
2009-01-06 13:54:53 -08:00
|
|
|
l := len(m1);
|
|
|
|
|
if l != len(m2) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
|
if m1[i] != m2[i] {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func executeTest(t *testing.T, expr string, str string, match []int) {
|
|
|
|
|
re := compileTest(t, expr, nil);
|
2008-11-19 15:38:46 -08:00
|
|
|
if re == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2008-10-14 16:32:43 -07:00
|
|
|
m := re.Execute(str);
|
2009-01-16 10:34:36 -08:00
|
|
|
if !equal(m, match) {
|
2009-01-06 13:54:53 -08:00
|
|
|
t.Error("Execute failure on `", expr, "` matching `", str, "`:");
|
2009-01-16 10:34:36 -08:00
|
|
|
printVec(t, m);
|
2008-11-19 15:38:46 -08:00
|
|
|
t.Log("should be:");
|
2009-01-16 10:34:36 -08:00
|
|
|
printVec(t, match);
|
2008-10-14 16:32:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestGoodCompile(t *testing.T) {
|
2008-10-14 16:32:43 -07:00
|
|
|
for i := 0; i < len(good_re); i++ {
|
2009-01-16 10:34:36 -08:00
|
|
|
compileTest(t, good_re[i], nil);
|
2008-10-14 16:32:43 -07:00
|
|
|
}
|
2008-11-19 15:38:46 -08:00
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestBadCompile(t *testing.T) {
|
2008-10-14 16:32:43 -07:00
|
|
|
for i := 0; i < len(bad_re); i++ {
|
2009-01-16 10:34:36 -08:00
|
|
|
compileTest(t, bad_re[i].re, bad_re[i].err)
|
2008-10-14 16:32:43 -07:00
|
|
|
}
|
2008-11-19 15:38:46 -08:00
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestExecute(t *testing.T) {
|
2009-01-06 13:54:53 -08:00
|
|
|
for i := 0; i < len(matches); i++ {
|
|
|
|
|
test := &matches[i];
|
2009-01-16 10:34:36 -08:00
|
|
|
executeTest(t, test.re, test.text, test.match)
|
2009-01-06 13:54:53 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func matchTest(t *testing.T, expr string, str string, match []int) {
|
|
|
|
|
re := compileTest(t, expr, nil);
|
2009-01-06 13:54:53 -08:00
|
|
|
if re == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m := re.Match(str);
|
|
|
|
|
if m != (len(match) > 0) {
|
|
|
|
|
t.Error("Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestMatch(t *testing.T) {
|
2008-10-14 16:32:43 -07:00
|
|
|
for i := 0; i < len(matches); i++ {
|
2008-11-19 15:38:46 -08:00
|
|
|
test := &matches[i];
|
2009-01-16 10:34:36 -08:00
|
|
|
matchTest(t, test.re, test.text, test.match)
|
2008-10-09 19:40:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-01-06 13:54:53 -08:00
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func matchStringsTest(t *testing.T, expr string, str string, match []int) {
|
|
|
|
|
re := compileTest(t, expr, nil);
|
2009-01-06 13:54:53 -08:00
|
|
|
if re == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2009-01-06 15:19:02 -08:00
|
|
|
strs := make([]string, len(match)/2);
|
2009-01-06 13:54:53 -08:00
|
|
|
for i := 0; i < len(match); i++ {
|
|
|
|
|
strs[i/2] = str[match[i] : match[i+1]]
|
|
|
|
|
}
|
|
|
|
|
m := re.MatchStrings(str);
|
2009-01-16 10:34:36 -08:00
|
|
|
if !equalStrings(m, strs) {
|
2009-01-06 13:54:53 -08:00
|
|
|
t.Error("MatchStrings failure on `", expr, "` matching `", str, "`:");
|
2009-01-16 10:34:36 -08:00
|
|
|
printStrings(t, m);
|
2009-01-06 13:54:53 -08:00
|
|
|
t.Log("should be:");
|
2009-01-16 10:34:36 -08:00
|
|
|
printStrings(t, strs);
|
2009-01-06 13:54:53 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestMatchStrings(t *testing.T) {
|
2009-01-06 13:54:53 -08:00
|
|
|
for i := 0; i < len(matches); i++ {
|
|
|
|
|
test := &matches[i];
|
2009-01-16 10:34:36 -08:00
|
|
|
matchTest(t, test.re, test.text, test.match)
|
2009-01-06 13:54:53 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 10:34:36 -08:00
|
|
|
func matchFunctionTest(t *testing.T, expr string, str string, match []int) {
|
2009-01-06 13:54:53 -08:00
|
|
|
m, err := Match(expr, str);
|
|
|
|
|
if err == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if m != (len(match) > 0) {
|
|
|
|
|
t.Error("function Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 14:40:40 -08:00
|
|
|
func TestMatchFunction(t *testing.T) {
|
2009-01-06 13:54:53 -08:00
|
|
|
for i := 0; i < len(matches); i++ {
|
|
|
|
|
test := &matches[i];
|
2009-01-16 10:34:36 -08:00
|
|
|
matchFunctionTest(t, test.re, test.text, test.match)
|
2009-01-06 13:54:53 -08:00
|
|
|
}
|
|
|
|
|
}
|