From ec9a21e1274fb6de29f825790bb57b0c7eeb49e0 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Fri, 6 Dec 2024 13:34:21 +0000 Subject: [PATCH] Add tests for regex character classes (#4155) Signed-off-by: George Robinson --- matcher/parse/lexer_test.go | 26 ++++++++++++++++++++++++++ matcher/parse/parse_test.go | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/matcher/parse/lexer_test.go b/matcher/parse/lexer_test.go index fcc9b5bf74..62bec55e30 100644 --- a/matcher/parse/lexer_test.go +++ b/matcher/parse/lexer_test.go @@ -372,6 +372,32 @@ func TestLexer_Scan(t *testing.T) { columnEnd: 13, }, }}, + }, { + name: "quoted with regex digit character class", + input: "\"\\d+\"", + expected: []token{{ + kind: tokenQuoted, + value: "\"\\d+\"", + position: position{ + offsetStart: 0, + offsetEnd: 5, + columnStart: 0, + columnEnd: 5, + }, + }}, + }, { + name: "quoted with escaped regex digit character class", + input: "\"\\\\d+\"", + expected: []token{{ + kind: tokenQuoted, + value: "\"\\\\d+\"", + position: position{ + offsetStart: 0, + offsetEnd: 6, + columnStart: 0, + columnEnd: 6, + }, + }}, }, { name: "quoted with escaped quotes", input: "\"hello \\\"world\\\"\"", diff --git a/matcher/parse/parse_test.go b/matcher/parse/parse_test.go index 1aba66853c..9a5b6955cf 100644 --- a/matcher/parse/parse_test.go +++ b/matcher/parse/parse_test.go @@ -99,6 +99,10 @@ func TestMatchers(t *testing.T) { name: "match regex in quotes", input: "{\"foo\"=~\"[a-z]+\"}", expected: labels.Matchers{mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+")}, + }, { + name: "match regex digit in quotes", + input: "{\"foo\"=~\"\\\\d+\"}", + expected: labels.Matchers{mustNewMatcher(t, labels.MatchRegexp, "foo", "\\d+")}, }, { name: "doesn't match regex in quotes", input: "{\"foo\"!~\"[a-z]+\"}", @@ -203,6 +207,10 @@ func TestMatchers(t *testing.T) { name: "invalid escape sequence", input: "{foo=\"bar\\w\"}", error: "5:12: \"bar\\w\": invalid input", + }, { + name: "invalid escape sequence regex digits", + input: "{\"foo\"=~\"\\d+\"}", + error: "8:13: \"\\d+\": invalid input", }, { name: "no unquoted escape sequences", input: "{foo=bar\\n}",