-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.8.1: added matcher
matchesText(regex)
- Loading branch information
Showing
10 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/codeborne/pdftest/matchers/MatchesText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.codeborne.pdftest.matchers; | ||
|
||
import com.codeborne.pdftest.PDF; | ||
import org.hamcrest.Description; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class MatchesText extends PDFMatcher { | ||
private final Pattern expectedText; | ||
|
||
public MatchesText(Pattern expectedText) { | ||
this.expectedText = expectedText; | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(PDF item) { | ||
String reducedPdfText = reduceSpaces(item.text); | ||
return expectedText.matcher(reducedPdfText).matches(); | ||
} | ||
|
||
@Override | ||
protected void describeMismatchSafely(PDF item, Description mismatchDescription) { | ||
mismatchDescription.appendText("was \"").appendText(reduceSpaces(item.text)).appendText("\""); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("a PDF matching "); | ||
buildErrorMessage(description, expectedText.toString(), new String[0]); | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.codeborne.pdftest; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import static com.codeborne.pdftest.PDF.matchesText; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class MatchRegexTest { | ||
@Test | ||
public void verifyTextWithRegex() throws IOException { | ||
URL url = getClass().getClassLoader().getResource("50quickideas.pdf"); | ||
assertThat(new PDF(url), matchesText(".*50 Quick Ideas.+50 Quick Ideas.*")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/java/com/codeborne/pdftest/assertj/MatchRegexTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.codeborne.pdftest.assertj; | ||
|
||
import com.codeborne.pdftest.PDF; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.codeborne.pdftest.assertj.Assertions.assertThat; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class MatchRegexTest { | ||
@Test | ||
public void verifyTextWithRegex() throws IOException { | ||
PDF pdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf"))); | ||
assertThat(pdf).matchesText(".*50 Quick Ideas.+50 Quick Ideas.*"); | ||
assertThat(pdf).matchesText("50 Quick Ideas.*Gojko Adzic.*2014-03-25.*©2013 - 2014.*"); | ||
} | ||
} |