Skip to content

Commit

Permalink
fix(#347): fix the bug with related to instance field message
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 16, 2024
1 parent 00bf518 commit e43278e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ Optional<String> asString() {
final Optional<String> result;
if (this.expr.isStringLiteralExpr()) {
result = Optional.of(this.expr.asStringLiteralExpr().asString());
} else if (this.expr.isNameExpr() || this.expr.isMethodCallExpr()) {
} else if (
this.expr.isNameExpr()
|| this.expr.isMethodCallExpr()
|| this.expr.isFieldAccessExpr()
) {
result = new UnknownMessage().message();
} else {
result = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,31 @@ void ignoresJUnitAssertions() {
);
}

@Test
void findsExplanationMessage() {
final AssertionOfHamcrest first = JavaTestClasses.TEST_WITH_HAMCREST_ASSERTIONS
.method("checksTheCaseFrom357issue")
.statements()
.map(AssertionOfHamcrest::new)
.filter(AssertionOfHamcrest::isAssertion)
.findFirst().orElseThrow(() -> new AssertionError("No assertions found"));
MatcherAssert.assertThat(
String.format("We expect that assertion has a valid message", first),
first.explanation().orElseThrow(() -> new AssertionError("No explanation found")),
Matchers.equalTo("Unknown message. The message will be known only in runtime")
);
}

@SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts")
@Test
void checksCorrectlyOnLineHitters() {
final List<AssertionOfHamcrest> assertions =
JavaTestClasses.HAMCREST_ASSERT_TRUE_LINE_HITTER
.method("checksHitter")
.statements()
.map(AssertionOfHamcrest::new)
.filter(AssertionOfHamcrest::isLineHitter)
.collect(Collectors.toList());
.method("checksHitter")
.statements()
.map(AssertionOfHamcrest::new)
.filter(AssertionOfHamcrest::isLineHitter)
.collect(Collectors.toList());
MatcherAssert.assertThat(
String.format("%s contains two line hitters", assertions),
assertions,
Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/TestWithHamcrestAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ class TestWithHamcrestAssertions {
*/
private final static Supplier<String> DEFAULT_SUPPLIER = () -> TestWithJUnitAssertions.DEFAULT_EXPLANATION;

/**
* Static final message for assertions.
*/
private static final String MSG = "MESSAGE";

@Test
void checksTheCaseFrom357issue() {
// This test were added to check the issue #357
// You can read more about it here:
// https://github.com/volodya-lombrozo/jtcop/issues/347
MatcherAssert.assertThat(
TestWithHamcrestAssertions.MSG,
"1",
Matchers.equalTo("1")
);
}

@Test
void withMessages() {
MatcherAssert.assertThat(
Expand Down

0 comments on commit e43278e

Please sign in to comment.