From 9dc4fd45951e7577fb9f1a58fd58b9f5e7200a8e Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 20 Jan 2025 18:04:19 -0500 Subject: [PATCH] Started adding tests from Python's RE test suite --- re_test.go | 68 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/re_test.go b/re_test.go index afebc2f..14f3892 100644 --- a/re_test.go +++ b/re_test.go @@ -110,6 +110,9 @@ var reTests = []struct { {`\d{3,4}`, "ababab555", []Group{{6, 9}}}, {`\bpaint\b`, "paints", []Group{}}, {`\b\w{5}\b`, "paint", []Group{{0, 5}}}, + {`[^\w]`, "abcdef1230[]qq';;'", []Group{{10, 11}, {11, 12}, {14, 15}, {15, 16}, {16, 17}, {17, 18}}}, + {`[^\W]`, "abcdef1230[]qq';;'", []Group{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {12, 13}, {13, 14}}}, + {`[\[\]]`, "a[b[l]]", []Group{{1, 2}, {3, 4}, {5, 6}, {6, 7}}}, // Unicode tests {`.+`, "úïäö´«åæïëòöê»éãçâï«úïòíñ", []Group{{0, 25}}}, @@ -156,6 +159,24 @@ var reTests = []struct { {"(?<=f)f+(?=f)", "fffff", []Group{{1, 4}}}, {"(?<=f)f+(?=f)", "fffffa", []Group{{1, 4}}}, + // Test cases from Python's RE test suite + {`[\1]`, "\x01", []Group{{0, 1}}}, + + {`\0`, "\x00", []Group{{0, 1}}}, + {`[\0a]`, "\x00", []Group{{0, 1}}}, + {`[\0a]`, "\x00", []Group{{0, 1}}}, + {`[a\0]`, "\x00", []Group{{0, 1}}}, + {`[^a\0]`, "\x00", []Group{}}, + + {`\a[\b]\f\n\r\t\v`, "\a\b\f\n\r\t\v", []Group{{0, 7}}}, + {`[\a][\b][\f][\n][\r][\t][\v]`, "\a\b\f\n\r\t\v", []Group{{0, 7}}}, + {`\u`, "", nil}, + {`\xff`, "ÿ", []Group{{0, 1}}}, + {`\x00ffffffffffffff`, "\xff", []Group{}}, + {`\x00f`, "\x0f", []Group{}}, + {`\x00fe`, "\xfe", []Group{}}, + {`^\w+=(\\[\000-\277]|[^\n\\])*`, "SRC=eval.c g.c blah blah blah \\\\\n\tapes.c", []Group{{0, 32}}}, + // Todo - add numeric range tests } @@ -183,6 +204,7 @@ var groupTests = []struct { {"(a?)a?", "ab", []Match{[]Group{{0, 1}, {0, 1}}, []Group{{1, 1}, {1, 1}}, []Group{{2, 2}, {2, 2}}}}, {"(a?)a?", "aa", []Match{[]Group{{0, 2}, {0, 1}}, []Group{{2, 2}, {2, 2}}}}, {"a((b.d){3})", "abfdbhdbid", []Match{[]Group{{0, 10}, {1, 10}, {7, 10}}}}, + {`(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)\071`, `abcdefghijkl9`, []Match{[]Group{{0, 13}, {0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 10}, {10, 11}, {11, 12}}}}, } func TestFindAllMatches(t *testing.T) { @@ -190,15 +212,18 @@ func TestFindAllMatches(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) { regComp, err := Compile(test.re) if err != nil { - panic(err) - } - matchIndices := FindAllMatches(regComp, test.str) - zeroGroups := make([]Group, len(matchIndices)) - for i, m := range matchIndices { - zeroGroups[i] = m[0] - } - if !slices.Equal(test.result, zeroGroups) { - t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups) + if test.result != nil { + panic(err) + } + } else { + matchIndices := FindAllMatches(regComp, test.str) + zeroGroups := make([]Group, len(matchIndices)) + for i, m := range matchIndices { + zeroGroups[i] = m[0] + } + if !slices.Equal(test.result, zeroGroups) { + t.Errorf("Wanted %v Got %v\n", test.result, zeroGroups) + } } }) } @@ -209,17 +234,20 @@ func TestFindString(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) { regComp, err := Compile(test.re) if err != nil { - panic(err) - } - foundString := FindString(regComp, test.str) - if len(test.result) == 0 { - if foundString != "" { - t.Errorf("Expected no match got %v\n", foundString) + if test.result != nil { + panic(err) } } else { - expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx] - if foundString != foundString { - t.Errorf("Wanted %v Got %v\n", expectedString, foundString) + foundString := FindString(regComp, test.str) + if len(test.result) == 0 { + if foundString != "" { + t.Errorf("Expected no match got %v\n", foundString) + } + } else { + expectedString := test.str[test.result[0].startIdx:test.result[0].endIdx] + if foundString != expectedString { + t.Errorf("Wanted %v Got %v\n", expectedString, foundString) + } } } }) @@ -231,7 +259,9 @@ func TestFindAllGroups(t *testing.T) { t.Run(test.re+" "+test.str, func(t *testing.T) { regComp, err := Compile(test.re) if err != nil { - panic(err) + if test.result != nil { + panic(err) + } } matchIndices := FindAllMatches(regComp, test.str) for i := range matchIndices {