Skip to content

Commit e776dfe

Browse files
core: extract by regexp overload (#810)
1 parent b4a4f8a commit e776dfe

4 files changed

Lines changed: 25 additions & 3 deletions

File tree

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/CliRunResult.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.testingisdocumenting.webtau.reporter.WebTauStep;
2121
import org.testingisdocumenting.webtau.utils.RegexpUtils;
2222

23+
import java.util.regex.Pattern;
24+
2325
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;
2426
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.stringValue;
2527
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.tokenizedMessage;
@@ -50,14 +52,22 @@ public String getError() {
5052
}
5153

5254
public String extractFromOutputByRegexp(String regexp) {
55+
return extractFromOutputByRegexp(Pattern.compile(regexp));
56+
}
57+
58+
public String extractFromOutputByRegexp(Pattern regexp) {
5359
return extractFromSourceByRegexp("stdout", output, regexp);
5460
}
5561

5662
public String extractFromErrorByRegexp(String regexp) {
63+
return extractFromErrorByRegexp(Pattern.compile(regexp));
64+
}
65+
66+
public String extractFromErrorByRegexp(Pattern regexp) {
5767
return extractFromSourceByRegexp("stderr", error, regexp);
5868
}
5969

60-
private String extractFromSourceByRegexp(String sourceLabel, String source, String regexp) {
70+
private String extractFromSourceByRegexp(String sourceLabel, String source, Pattern regexp) {
6171
WebTauStep step = WebTauStep.createStep(null,
6272
tokenizedMessage(action("extracting text"), classifier("by regexp"), stringValue(regexp),
6373
FROM, urlValue(command), classifier(sourceLabel)),
@@ -68,7 +78,7 @@ FROM, urlValue(command), classifier(sourceLabel), COLON, stringValue(r)),
6878
return step.execute(StepReportOptions.SKIP_START);
6979
}
7080

71-
private String extractByRegexpStepImpl(String sourceLabel, String source, String regexp) {
81+
private String extractByRegexpStepImpl(String sourceLabel, String source, Pattern regexp) {
7282
String extracted = RegexpUtils.extractByRegexp(source, regexp);
7383
if (extracted == null) {
7484
throw new RuntimeException("can't find content to extract using regexp <" + regexp + "> from " +

webtau-filesystem/src/main/java/org/testingisdocumenting/webtau/fs/FileTextContent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.testingisdocumenting.webtau.utils.RegexpUtils;
3030

3131
import java.nio.file.Path;
32+
import java.util.regex.Pattern;
3233

3334
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;
3435
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.tokenizedMessage;
@@ -56,6 +57,10 @@ public ActualPath actualPath() {
5657
}
5758

5859
public String extractByRegexp(String regexp) {
60+
return extractByRegexp(Pattern.compile(regexp));
61+
}
62+
63+
public String extractByRegexp(Pattern regexp) {
5964
WebTauStep step = WebTauStep.createStep(null,
6065
tokenizedMessage(action("extracting text"), classifier("by regexp"), stringValue(regexp),
6166
FROM, urlValue(path)),
@@ -97,7 +102,7 @@ public String toString() {
97102
return getData();
98103
}
99104

100-
private String extractByRegexpStepImpl(String regexp) {
105+
private String extractByRegexpStepImpl(Pattern regexp) {
101106
String extracted = RegexpUtils.extractByRegexp(getData(), regexp);
102107
if (extracted == null) {
103108
throw new RuntimeException("can't find content to extract using regexp <" + regexp + "> from: " + path);

webtau-utils/src/main/java/org/testingisdocumenting/webtau/utils/RegexpUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public static String replaceAll(String source, Pattern regexp, Function<Matcher,
4040

4141
public static String extractByRegexp(String source, String regexp) {
4242
Pattern pattern = Pattern.compile(regexp);
43+
return extractByRegexp(source, pattern);
44+
}
45+
46+
public static String extractByRegexp(String source, Pattern pattern) {
4347
Matcher matcher = pattern.matcher(source);
4448
boolean found = matcher.find();
4549
if (!found) {

webtau-utils/src/test/groovy/org/testingisdocumenting/webtau/utils/RegexpUtilsTest.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class RegexpUtilsTest {
3232
Assert.assertEquals("123",
3333
RegexpUtils.extractByRegexp("line 1\nline 2\nhello id=123 ere\n", "id=(\\d+)"))
3434

35+
Assert.assertEquals("123",
36+
RegexpUtils.extractByRegexp("line 1\nline 2\nhello id=123 ere\n", ~/id=(\d+)/))
37+
3538
Assert.assertEquals(null,
3639
RegexpUtils.extractByRegexp("line 1\nline 2\nhello id=123 ere\n", "bid=(\\d+)"))
3740
}

0 commit comments

Comments
 (0)