From 2951069fbd7afcb65457db30dde9dc0fd8ce5f80 Mon Sep 17 00:00:00 2001 From: Bert Jacobs Date: Wed, 24 Jul 2019 19:33:24 +0200 Subject: [PATCH] Use platform-specific line separators The `System.lineSeparator()` function will return the end of line separator appropriate for the current runtime, i.e. "\r\n" on Windows and "\n" on Unix. --- hamcrest/src/main/java/org/hamcrest/MatcherAssert.java | 6 ++++-- .../src/test/java/org/hamcrest/MatcherAssertTest.java | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/MatcherAssert.java b/hamcrest/src/main/java/org/hamcrest/MatcherAssert.java index 049e1df3..bc001ebb 100644 --- a/hamcrest/src/main/java/org/hamcrest/MatcherAssert.java +++ b/hamcrest/src/main/java/org/hamcrest/MatcherAssert.java @@ -10,9 +10,11 @@ public static void assertThat(String reason, T actual, Matcher ma if (!matcher.matches(actual)) { Description description = new StringDescription(); description.appendText(reason) - .appendText("\nExpected: ") + .appendText(System.lineSeparator()) + .appendText("Expected: ") .appendDescriptionOf(matcher) - .appendText("\n but: "); + .appendText(System.lineSeparator()) + .appendText(" but: "); matcher.describeMismatch(actual, description); throw new AssertionError(description.toString()); diff --git a/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java b/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java index b57c4d77..8d88daa8 100644 --- a/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java +++ b/hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java @@ -12,8 +12,9 @@ public final class MatcherAssertTest { includesDescriptionOfTestedValueInErrorMessage() { String expected = "expected"; String actual = "actual"; + String endLine = System.lineSeparator(); - String expectedMessage = "identifier\nExpected: \"expected\"\n but: was \"actual\""; + String expectedMessage = "identifier" + endLine + "Expected: \"expected\"" + endLine + " but: was \"actual\""; try { assertThat("identifier", actual, equalTo(expected)); @@ -30,8 +31,9 @@ public final class MatcherAssertTest { descriptionCanBeElided() { String expected = "expected"; String actual = "actual"; + String endLine = System.lineSeparator(); - String expectedMessage = "\nExpected: \"expected\"\n but: was \"actual\""; + String expectedMessage = endLine + "Expected: \"expected\"" + endLine + " but: was \"actual\""; try { assertThat(actual, equalTo(expected)); @@ -78,7 +80,8 @@ public void describeMismatch(Object item, Description mismatchDescription) { } }; - String expectedMessage = "\nExpected: Something cool\n but: Not cool"; + String endLine = System.lineSeparator(); + String expectedMessage = endLine + "Expected: Something cool" + endLine + " but: Not cool"; try { assertThat("Value", matcherWithCustomMismatchDescription);