File tree Expand file tree Collapse file tree 2 files changed +76
-0
lines changed
modules/swagger-codegen/src
main/java/io/swagger/codegen/mustache
test/java/io/swagger/codegen/mustache Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 1+ package io .swagger .codegen .mustache ;
2+
3+ import java .io .IOException ;
4+ import java .io .Writer ;
5+
6+ import com .samskivert .mustache .Mustache ;
7+ import com .samskivert .mustache .Template .Fragment ;
8+
9+ /**
10+ * Replaces duplicate whitespace characters in a fragment with single space.
11+ *
12+ * Register:
13+ *
14+ * <pre>
15+ * additionalProperties.put("lambdaTrimWhitespace", new TrimWhitespaceLambda());
16+ * </pre>
17+ *
18+ * Use:
19+ *
20+ * <pre>
21+ * {{#lambdaTrimWhitespace}}{{summary}}{{/lambdaTrimWhitespace}}
22+ * </pre>
23+ */
24+ public class TrimWhitespaceLambda implements Mustache .Lambda {
25+ private static final String SINGLE_SPACE = " " ;
26+
27+ private static final String WHITESPACE_REGEX = "\\ s+" ;
28+
29+ @ Override
30+ public void execute (Fragment fragment , Writer writer ) throws IOException {
31+ writer .write (fragment .execute ().replaceAll (WHITESPACE_REGEX , SINGLE_SPACE ));
32+ }
33+
34+ }
Original file line number Diff line number Diff line change 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+
9+ import org .mockito .Mock ;
10+ import org .mockito .Mockito ;
11+ import org .mockito .MockitoAnnotations ;
12+ import org .testng .annotations .AfterMethod ;
13+ import org .testng .annotations .BeforeMethod ;
14+ import org .testng .annotations .Test ;
15+
16+ import com .samskivert .mustache .Template .Fragment ;
17+
18+ public class TrimWhitespaceLambdaTest {
19+
20+ @ Mock
21+ private Fragment fragment ;
22+
23+ @ BeforeMethod
24+ public void init () {
25+ MockitoAnnotations .initMocks (this );
26+ }
27+
28+ @ AfterMethod
29+ public void reset () {
30+ Mockito .reset (fragment );
31+ }
32+
33+ @ Test
34+ public void testTrimWhitespace () throws IOException {
35+ when (fragment .execute ()).thenReturn ("\t a b\t \t c \t " );
36+
37+ StringWriter output = new StringWriter ();
38+ new TrimWhitespaceLambda ().execute (fragment , output );
39+ assertEquals (output .toString (), " a b c " );
40+ }
41+
42+ }
You can’t perform that action at this time.
0 commit comments