Skip to content

Commit 2fdbe37

Browse files
committed
test: parameterize unwrapEscapedQuotes scenarios
Refactor duplicate UnwrapEscapedQuotes execute tests into a MethodSource-based parameterized test and keep the case provider in the helper section for maintainability. Made-with: Cursor
1 parent 4c14c4a commit 2fdbe37

1 file changed

Lines changed: 16 additions & 43 deletions

File tree

boat-scaffold/src/test/java/com/backbase/oss/codegen/java/BoatSpringCodeGenTests.java

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@
4949
import java.util.Set;
5050
import java.util.UUID;
5151
import java.util.stream.Collectors;
52+
import java.util.stream.Stream;
5253
import org.apache.commons.io.FileUtils;
5354
import org.apache.commons.lang.UnhandledException;
5455
import org.hamcrest.Matchers;
5556
import org.jetbrains.annotations.NotNull;
5657
import org.junit.jupiter.api.BeforeAll;
5758
import org.junit.jupiter.api.Test;
5859
import org.junit.jupiter.params.ParameterizedTest;
60+
import org.junit.jupiter.params.provider.Arguments;
5961
import org.junit.jupiter.params.provider.CsvSource;
62+
import org.junit.jupiter.params.provider.MethodSource;
6063
import org.openapitools.codegen.CliOption;
6164
import org.openapitools.codegen.ClientOptInput;
6265
import org.openapitools.codegen.CodegenOperation;
@@ -98,56 +101,18 @@ void newLineIndent() throws IOException {
98101
assertThat(output.toString(), equalTo(String.format("__%n__Good%n__ morning,%n__ Dave%n")));
99102
}
100103

101-
@Test
102-
void unwrapEscapedQuotes_withNullInput_shouldWriteNothing() throws IOException {
103-
final BoatSpringCodeGen.UnwrapEscapedQuotes lambda = new BoatSpringCodeGen.UnwrapEscapedQuotes();
104-
final StringWriter output = new StringWriter();
105-
final Fragment frag = mock(Fragment.class);
106-
107-
when(frag.execute()).thenReturn(null);
108-
109-
lambda.execute(frag, output);
110-
111-
assertThat(output.toString(), equalTo(""));
112-
}
113-
114-
@Test
115-
void unwrapEscapedQuotes_withWrappedEscapedQuotes_shouldRemoveOuterEscapedQuotes() throws IOException {
116-
final BoatSpringCodeGen.UnwrapEscapedQuotes lambda = new BoatSpringCodeGen.UnwrapEscapedQuotes();
117-
final StringWriter output = new StringWriter();
118-
final Fragment frag = mock(Fragment.class);
119-
120-
when(frag.execute()).thenReturn("\\\"{\\\"message\\\":\\\"Bad Request\\\"}\\\"");
121-
122-
lambda.execute(frag, output);
123-
124-
assertThat(output.toString(), equalTo("{\\\"message\\\":\\\"Bad Request\\\"}"));
125-
}
126-
127-
@Test
128-
void unwrapEscapedQuotes_withDoubleEscapedQuotesAndNoWrapper_shouldOnlyNormalize() throws IOException {
129-
final BoatSpringCodeGen.UnwrapEscapedQuotes lambda = new BoatSpringCodeGen.UnwrapEscapedQuotes();
130-
final StringWriter output = new StringWriter();
131-
final Fragment frag = mock(Fragment.class);
132-
133-
when(frag.execute()).thenReturn("prefix\\\\\"quoted\\\\\"suffix");
134-
135-
lambda.execute(frag, output);
136-
137-
assertThat(output.toString(), equalTo("prefix\\\"quoted\\\"suffix"));
138-
}
139-
140-
@Test
141-
void unwrapEscapedQuotes_withShortEscapedQuoteToken_shouldNotUnwrap() throws IOException {
104+
@ParameterizedTest
105+
@MethodSource("unwrapEscapedQuotesCases")
106+
void unwrapEscapedQuotes_execute_shouldHandleAllScenarios(String input, String expectedOutput) throws IOException {
142107
final BoatSpringCodeGen.UnwrapEscapedQuotes lambda = new BoatSpringCodeGen.UnwrapEscapedQuotes();
143108
final StringWriter output = new StringWriter();
144109
final Fragment frag = mock(Fragment.class);
145110

146-
when(frag.execute()).thenReturn("\\\"");
111+
when(frag.execute()).thenReturn(input);
147112

148113
lambda.execute(frag, output);
149114

150-
assertThat(output.toString(), equalTo("\\\""));
115+
assertThat(output.toString(), equalTo(expectedOutput));
151116
}
152117

153118
@Test
@@ -644,4 +609,12 @@ private static void assertMethodCollectionReturnType(MethodDeclaration method, S
644609
.getTypeArguments().get().getFirst().get();
645610
assertEquals(itemType, collectionItemType.getName().toString());
646611
}
612+
613+
static Stream<Arguments> unwrapEscapedQuotesCases() {
614+
return Stream.of(
615+
Arguments.of((String) null, ""),
616+
Arguments.of("\\\"{\\\"message\\\":\\\"Bad Request\\\"}\\\"", "{\\\"message\\\":\\\"Bad Request\\\"}"),
617+
Arguments.of("prefix\\\\\"quoted\\\\\"suffix", "prefix\\\"quoted\\\"suffix"),
618+
Arguments.of("\\\"", "\\\""));
619+
}
647620
}

0 commit comments

Comments
 (0)