|
| 1 | +package io.swagger.codegen.mustache; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.when; |
| 4 | +import static org.testng.Assert.assertEquals; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.StringWriter; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.Mockito; |
| 13 | +import org.mockito.MockitoAnnotations; |
| 14 | +import org.testng.annotations.AfterMethod; |
| 15 | +import org.testng.annotations.BeforeMethod; |
| 16 | +import org.testng.annotations.Test; |
| 17 | + |
| 18 | +import com.samskivert.mustache.Template.Fragment; |
| 19 | + |
| 20 | +public class SplitStringLambdaTest { |
| 21 | + private static final String INPUT_STRING = "1112223334"; |
| 22 | + |
| 23 | + private static final Map<Integer, String> EXPECTED_OUTPUTS; |
| 24 | + static { |
| 25 | + EXPECTED_OUTPUTS = new HashMap<>(); |
| 26 | + EXPECTED_OUTPUTS.put(2, |
| 27 | + String.format( |
| 28 | + "new StringBuilder(%d).append(\"11\").append(\"12\").append(\"22\").append(\"33\").append(\"34\").toString()", |
| 29 | + INPUT_STRING.length())); |
| 30 | + EXPECTED_OUTPUTS.put(3, |
| 31 | + String.format( |
| 32 | + "new StringBuilder(%d).append(\"111\").append(\"222\").append(\"333\").append(\"4\").toString()", |
| 33 | + INPUT_STRING.length())); |
| 34 | + } |
| 35 | + |
| 36 | + private static final String INPUT_QUOTED_STRING = "1\\\"11\\\"2223\\\"334"; |
| 37 | + private static final String INPUT_QUOTED_OUTPUT = String.format( |
| 38 | + "new StringBuilder(%d).append(\"1\\\"\").append(\"11\").append(\"\\\"2\").append(\"223\").append(\"\\\"3\").append(\"34\").toString()", |
| 39 | + INPUT_QUOTED_STRING.length()); |
| 40 | + |
| 41 | + @Mock |
| 42 | + private Fragment fragment; |
| 43 | + |
| 44 | + @BeforeMethod |
| 45 | + public void init() { |
| 46 | + MockitoAnnotations.initMocks(this); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterMethod |
| 50 | + public void reset() { |
| 51 | + Mockito.reset(fragment); |
| 52 | + } |
| 53 | + |
| 54 | + private void testString(String input, int maxLength, String expected) throws IOException { |
| 55 | + when(fragment.execute()).thenReturn(input); |
| 56 | + |
| 57 | + StringWriter output = new StringWriter(); |
| 58 | + new SplitStringLambda(maxLength).execute(fragment, output); |
| 59 | + assertEquals(output.toString(), expected); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSplitGroupsOf2() throws IOException { |
| 64 | + int maxLength = 2; |
| 65 | + testString(INPUT_STRING, maxLength, EXPECTED_OUTPUTS.get(maxLength)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testSplitGroupsOf3() throws IOException { |
| 70 | + int maxLength = 3; |
| 71 | + testString(INPUT_STRING, maxLength, EXPECTED_OUTPUTS.get(maxLength)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testSplitQuotedString() throws IOException { |
| 76 | + int maxLength = 3; |
| 77 | + testString(INPUT_QUOTED_STRING, maxLength, INPUT_QUOTED_OUTPUT); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testShortString() throws IOException { |
| 82 | + testString(INPUT_STRING, INPUT_STRING.length(), String.format("\"%s\"", INPUT_STRING)); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments