Skip to content

Commit

Permalink
Add tests for regex character classes
Browse files Browse the repository at this point in the history
  • Loading branch information
grobinson-grafana committed Dec 6, 2024
1 parent e0bd4e0 commit f7df209
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions matcher/parse/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\\\"\"",
Expand Down
1 change: 1 addition & 0 deletions matcher/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (p *parser) parseMatcher(l *lexer) (parseFunc, error) {
if t, err = p.expect(l, tokenUnquoted, tokenQuoted); err != nil {
return nil, fmt.Errorf("%w: %w", err, errNoLabelValue)
}

matchValue, err = t.unquote()
if err != nil {
return nil, fmt.Errorf("%d:%d: %s: invalid input", t.columnStart, t.columnEnd, t.value)
Expand Down
8 changes: 8 additions & 0 deletions matcher/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+\"}",
Expand Down Expand Up @@ -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}",
Expand Down

0 comments on commit f7df209

Please sign in to comment.