|
| 1 | +/* |
| 2 | + * Copyright 2026-Present Datadog, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datadoghq.reggie.runtime; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | + |
| 21 | +import com.datadoghq.reggie.Reggie; |
| 22 | +import com.datadoghq.reggie.ReggieOptions; |
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.Arguments; |
| 28 | +import org.junit.jupiter.params.provider.MethodSource; |
| 29 | + |
| 30 | +/** |
| 31 | + * Regression coverage for B12: quantifier nodes in the prefix before a capturing backref group. |
| 32 | + * After the fix these patterns route natively via VARIABLE_CAPTURE_BACKREF. |
| 33 | + * |
| 34 | + * <p>Patterns require variable-length group content (e.g. {@code (b+)}, {@code ([0-9]+)}) so they |
| 35 | + * are detected as VARIABLE_CAPTURE_BACKREF; the prefix quantifier (e.g. {@code a*}, {@code x{3}}) |
| 36 | + * is what previously caused the fallback. |
| 37 | + */ |
| 38 | +class B12QuantifierPrefixTest { |
| 39 | + |
| 40 | + private static final ReggieOptions WITH_FALLBACK = |
| 41 | + ReggieOptions.builder().allowJdkFallback().build(); |
| 42 | + |
| 43 | + static Stream<Arguments> quantifierPrefixPatterns() { |
| 44 | + return Stream.of( |
| 45 | + Arguments.of("a*(b+)\\1", "bb"), |
| 46 | + Arguments.of("a*(b+)\\1", "abb"), |
| 47 | + Arguments.of("a*(b+)\\1", "aabb"), |
| 48 | + Arguments.of("a*(b+)\\1", "aac"), |
| 49 | + Arguments.of("a+(b+)\\1", "abb"), |
| 50 | + Arguments.of("a+(b+)\\1", "aabb"), |
| 51 | + Arguments.of("a+(b+)\\1", "bb"), |
| 52 | + Arguments.of("[0-9]*([a-z]+)\\1", "aa"), |
| 53 | + Arguments.of("[0-9]*([a-z]+)\\1", "1aa"), |
| 54 | + Arguments.of("[0-9]*([a-z]+)\\1", "123aa"), |
| 55 | + Arguments.of("[0-9]*([a-z]+)\\1", "ab"), |
| 56 | + Arguments.of("x{3}(a+)\\1", "xxxaa"), |
| 57 | + Arguments.of("x{3}(a+)\\1", "xxaa"), |
| 58 | + Arguments.of("x{3}(a+)\\1", "xxxxaa")); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest(name = "[{index}] pat={0} in={1}") |
| 62 | + @MethodSource("quantifierPrefixPatterns") |
| 63 | + void agreesWithJdk(String pat, String in) { |
| 64 | + ReggieMatcher reggie = Reggie.compile(pat, WITH_FALLBACK); |
| 65 | + Pattern jdk = Pattern.compile(pat); |
| 66 | + String ctx = "pat=" + pat + " in=" + repr(in); |
| 67 | + assertEquals(jdk.matcher(in).matches(), reggie.matches(in), "matches() " + ctx); |
| 68 | + assertEquals(jdk.matcher(in).find(), reggie.find(in), "find() " + ctx); |
| 69 | + Matcher jm = jdk.matcher(in); |
| 70 | + boolean jFound = jm.find(); |
| 71 | + MatchResult rf = reggie.findMatch(in); |
| 72 | + assertEquals(jFound, rf != null, "findMatch() null " + ctx); |
| 73 | + if (jFound && rf != null) { |
| 74 | + assertEquals(jm.start(), rf.start(), "findMatch() start " + ctx); |
| 75 | + assertEquals(jm.end(), rf.end(), "findMatch() end " + ctx); |
| 76 | + if (jm.groupCount() >= 1 && jm.start(1) != -1 && rf.start(1) != -1) { |
| 77 | + assertEquals(jm.start(1), rf.start(1), "g1 start " + ctx); |
| 78 | + assertEquals(jm.end(1), rf.end(1), "g1 end " + ctx); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @ParameterizedTest(name = "[{index}] pat={0} in={1}") |
| 84 | + @MethodSource("quantifierPrefixPatterns") |
| 85 | + void routesToNative(String pat, String in) { |
| 86 | + assertFalse( |
| 87 | + Reggie.compile(pat) instanceof JavaRegexFallbackMatcher, |
| 88 | + "expected native matcher for: " + pat); |
| 89 | + } |
| 90 | + |
| 91 | + private static String repr(String s) { |
| 92 | + return s.isEmpty() ? "(empty)" : "\"" + s.replace("\n", "\\n") + "\""; |
| 93 | + } |
| 94 | +} |
0 commit comments