Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
== Changelog

=== 1.9.2 (released 05.02.2025)
* support "as" error description in AssertJ (#61)

=== 1.9.1 (released 26.09.2024)
* Bump pdfbox from 3.0.2 to 3.0.3 (#41)
* Bump junit from 5.10.3 to 5.11.1 (#43) (#45)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {

group = 'com.codeborne'
archivesBaseName = 'pdf-test'
version = '1.9.1'
version = '1.9.2'

defaultTasks 'test', 'install'

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/codeborne/pdftest/Spaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codeborne.pdftest;

import java.util.stream.Stream;

public class Spaces {
public static String reduce(String text) {
return text.replaceAll("[\\s\\n\\r\u00a0]+", " ").trim();
}

public static String[] reduce(String text, String... texts) {
return Stream.concat(Stream.of(text), Stream.of(texts))
.map(Spaces::reduce)
.toArray(String[]::new);
}
}
25 changes: 10 additions & 15 deletions src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
package com.codeborne.pdftest.assertj;

import com.codeborne.pdftest.PDF;
import com.codeborne.pdftest.matchers.ContainsExactText;
import com.codeborne.pdftest.matchers.ContainsText;
import com.codeborne.pdftest.matchers.ContainsTextCaseInsensitive;
import com.codeborne.pdftest.matchers.DoesNotContainExactText;
import com.codeborne.pdftest.matchers.DoesNotContainText;
import com.codeborne.pdftest.matchers.MatchesText;
import com.codeborne.pdftest.Spaces;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.internal.Strings;

import java.util.regex.Pattern;

import static org.hamcrest.MatcherAssert.assertThat;

public class PdfAssert extends AbstractAssert<PdfAssert, PDF> {
private final Strings strings = Strings.instance();

public PdfAssert(PDF actual) {
super(actual, PdfAssert.class);
}

public PdfAssert containsText(String text, String... texts) {
isNotNull();
assertThat(actual, new ContainsText(text, texts));
strings.assertContains(info, Spaces.reduce(actual.text), Spaces.reduce(text, texts));
return this;
}

public PdfAssert doesNotContainText(String text, String... texts) {
isNotNull();
assertThat(actual, new DoesNotContainText(text, texts));
strings.assertDoesNotContain(info, Spaces.reduce(actual.text), Spaces.reduce(text, texts));
return this;
}

public PdfAssert containsExactText(String substring) {
isNotNull();
assertThat(actual, new ContainsExactText(substring));
strings.assertContains(info, actual.text, substring);
return this;
}

public PdfAssert doesNotContainExactText(String substring) {
isNotNull();
assertThat(actual, new DoesNotContainExactText(substring));
strings.assertDoesNotContain(info, actual.text, substring);
return this;
}

public PdfAssert containsTextCaseInsensitive(String substring) {
isNotNull();
assertThat(actual, new ContainsTextCaseInsensitive(substring));
strings.assertContainsIgnoringCase(info, Spaces.reduce(actual.text), Spaces.reduce(substring));
return this;
}

Expand All @@ -54,8 +50,7 @@ public PdfAssert matchesText(String regex) {

public PdfAssert matchesText(Pattern regex) {
isNotNull();
assertThat(actual, new MatchesText(regex));
strings.assertMatches(info, Spaces.reduce(actual.text), regex);
return this;
}

}
5 changes: 3 additions & 2 deletions src/main/java/com/codeborne/pdftest/matchers/PDFMatcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codeborne.pdftest.matchers;

import com.codeborne.pdftest.PDF;
import com.codeborne.pdftest.Spaces;
import org.hamcrest.Description;
import org.hamcrest.SelfDescribing;
import org.hamcrest.TypeSafeMatcher;
Expand All @@ -11,12 +12,12 @@

abstract class PDFMatcher extends TypeSafeMatcher<PDF> implements SelfDescribing {
protected String reduceSpaces(String text) {
return text.replaceAll("[\\s\\n\\r\u00a0]+", " ").trim();
return Spaces.reduce(text);
}

protected void buildErrorMessage(Description description, String text, String[] texts) {
if (texts.length > 0) {
List<String> reducedStrings = Arrays.stream(texts).map(this::reduceSpaces).collect(Collectors.toList());
List<String> reducedStrings = Arrays.stream(texts).map(Spaces::reduce).collect(Collectors.toList());
reducedStrings.add(0, reduceSpaces(text));
description.appendValueList("", ", ", "", reducedStrings);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ public void shouldNotIgnoreWhitespaces() throws IOException {
public void errorDescription() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));

assertThatThrownBy(() -> {
assertThat(pdf).containsExactText("Goodbye word");
})
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\n\"");
assertThatThrownBy(() -> assertThat(pdf).containsExactText("Goodbye World"))
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("%nExpecting actual:%n \"Hello World%n\"%nto contain:%n \"Goodbye World\" "));
}

@Test
public void errorDescription_as() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));

assertThatThrownBy(() -> assertThat(pdf).as("The World has changed").containsExactText("Goodbye World"))
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("[The World has changed] %nExpecting actual:%n \"Hello World%n\"%nto contain:%n \"Goodbye World\" "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ public void shouldIgnoreWhitespaces() throws IOException {
@Test
public void errorDescription() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
assertThatThrownBy(() -> {
assertThat(pdf).containsTextCaseInsensitive("Goodbye word");
})
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\"");
assertThatThrownBy(() -> assertThat(pdf).containsTextCaseInsensitive("Goodbye World"))
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("%nExpecting actual:%n \"Hello World\"%nto contain:%n \"Goodbye World\"%n (ignoring case)"));
}

@Test
public void errorDescription_as() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
assertThatThrownBy(() -> assertThat(pdf).as("The World has changed").containsTextCaseInsensitive("Goodbye World"))
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("[The World has changed] %nExpecting actual:%n \"Hello World\"%nto contain:%n \"Goodbye World\"%n (ignoring case)"));
}
}
28 changes: 23 additions & 5 deletions src/test/java/com/codeborne/pdftest/assertj/ContainsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,33 @@ public void shouldIgnoreWhitespaces() {
@Test
public void errorDescriptionForSingleParameter() {
assertThatThrownBy(() -> assertThat(minimalPdf).containsText("Goodbye word"))
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\"");
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("%nExpecting actual:%n" +
" \"Hello World\"%n" +
"to contain:%n" +
" \"Goodbye word\" "));
}

@Test
public void errorDescription_as() {
assertThatThrownBy(() -> assertThat(minimalPdf).as("The World has changed").containsText("Goodbye word"))
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("[The World has changed] %nExpecting actual:%n" +
" \"Hello World\"%n" +
"to contain:%n" +
" \"Goodbye word\" "));
}

@Test
public void errorDescriptionForMultipleParameters() {
assertThatThrownBy(() -> assertThat(minimalPdf).containsText("Goodbye word", "Privet"))
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpected: a PDF containing \"Goodbye word\", \"Privet\"\n but: was \"Hello World\"");
assertThatThrownBy(() -> assertThat(minimalPdf).containsText("Goodbye word", "Privet", "Hello World"))
.isInstanceOf(AssertionError.class)
.hasMessage(String.format("%nExpecting actual:%n" +
" \"Hello World\"%n" +
"to contain:%n" +
" [\"Goodbye word\", \"Privet\", \"Hello World\"]%n" +
"but could not find:%n" +
" [\"Goodbye word\", \"Privet\"]%n "));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ public void canUseSoftAssertions() throws IOException {
.hasMessageContaining("Multiple Failures (4 failures)")
.isInstanceOfSatisfying(MultipleFailuresError.class, e -> {
assertThat(e.getFailures()).hasSize(4);
assertThat(e.getFailures().get(0)).hasMessageContaining("Expected: a PDF containing \"one\"");
assertThat(e.getFailures().get(1)).hasMessageContaining("Expected: a PDF containing \"two\"");
assertThat(e.getFailures().get(2)).hasMessageContaining("Expected: a PDF containing \"three\"");
assertThat(e.getFailures().get(3)).hasMessageContaining("Expected: a PDF containing \"four\"");
assertThat(e.getFailures().get(0)).hasMessageStartingWith(message("one"));
assertThat(e.getFailures().get(1)).hasMessageStartingWith(message("two"));
assertThat(e.getFailures().get(2)).hasMessageStartingWith(message("three"));
assertThat(e.getFailures().get(3)).hasMessageStartingWith(message("four"));
});
}

private static String message(String expected) {
return String.format("%n" +
"Expecting actual:%n" +
" \"Hello World%n\"%n" +
"to contain:%n" +
" \"" + expected + "\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ public void errorDescriptionForSingleParameter() {

@Test
public void errorDescriptionForMultipleParameters() {
assertThatThrownBy(() -> Assertions.assertThat(minimalPdf).containsText("Goodbye word", "Privet"))
assertThatThrownBy(() -> Assertions.assertThat(minimalPdf).containsText("Goodbye word", "Privet", "Hello World"))
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpected: a PDF containing \"Goodbye word\", \"Privet\"\n but: was \"Hello World\"");
.hasMessage(String.format("%nExpecting actual:%n" +
" \"Hello World\"%nto contain:%n" +
" [\"Goodbye word\", \"Privet\", \"Hello World\"]%n" +
"but could not find:%n" +
" [\"Goodbye word\", \"Privet\"]%n "));
}

@Test
Expand Down
Loading