/** * Formats the given parameters and arguments to a comma-separated list of {@code $parameterName=$argumentName}. * Arguments {@link String} representation are therefore treated specially. * * @param parameters used to for formatting * @param arguments to be formatted * @return the formatted {@link String} of the given {@link Parameter}{@code []} and {@link List}{@code <Object>} */ protected String formatAll(Parameter[] parameters, List<Object> arguments) { StringBuilder stringBuilder = new StringBuilder(); for (int idx = 0; idx < arguments.size(); idx++) { String parameterName = (parameters.length > idx) ? parameters[idx].getName() : "?"; Object argument = arguments.get(idx); stringBuilder.append(parameterName).append("=").append(format(argument)); if (idx < arguments.size() - 1) { stringBuilder.append(", "); } } return stringBuilder.toString(); }
@Test void testFormatForCustomObjectReplacesCarriageReturnsWithTheirPrintableCounterpartEvenIfWithText() { // Given: final TestToString argument = new TestToString("test\rtest\r"); // When: String result = underTest.format(argument); // Then: assertThat(result).isEqualTo("test\\rtest\\r"); }
@Test void testFormatForCustomObjectReplacesLineFeedsWithTheirPrintableCounterpartEvenIfWithText() { // Given: final TestToString argument = new TestToString("1\n2\n3"); // When: String result = underTest.format(argument); // Then: assertThat(result).isEqualTo("1\\n2\\n3"); }
@Test void testFormatForCustomObjectReplacesLineFeedWithTheirPrintableCounterpart() { // Given: final TestToString argument = new TestToString("\n"); // When: String result = underTest.format(argument); // Then: assertThat(result).isEqualTo("\\n"); }
@Test void testFormatForCustomObjectReplacesCarriageReturnWithTheirPrintableCounterpart() { // Given: final TestToString argument = new TestToString("\r"); // When: String result = underTest.format(argument); // Then: assertThat(result).isEqualTo("\\r"); }
@Test void testFormatForCustomObjectReplacesCarriageReturnsAndLineFeedsWithTheirPrintableCounterpart() { // Given: final TestToString argument = new TestToString( "A very\r\nlong text\nwith multiple\rdifferent newline\n\rvariations."); // When: String result = underTest.format(argument); // Then: assertThat(result).isEqualTo("A very\\r\\nlong text\\nwith multiple\\rdifferent newline\\n\\rvariations."); }
@Test void testFormatForCustomObjectReplacesNullFromToString() { // Given: final TestToString argument = new TestToString(null); // When: String result = underTest.format(argument); // Then: assertThat(result).isEqualTo(STRING_NULL); }