forked from loafle/openapi-generator-original
Fix indent lambda using \n as line break (#16464)
* fix indent lambda using \n as line break * update tests * more fix
This commit is contained in:
parent
71b33db7ee
commit
c74ed98282
@ -109,7 +109,8 @@ public class IndentedLambda implements Mustache.Lambda {
|
|||||||
|
|
||||||
String prefixedIndention = StringUtils.repeat(new String(Character.toChars(spaceCode)), prefixSpaceCount);
|
String prefixedIndention = StringUtils.repeat(new String(Character.toChars(spaceCode)), prefixSpaceCount);
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String[] lines = text.split(System.lineSeparator());
|
// use \n instead of System.lineSeparator (e.g. \r\n in Windows) as templates use \n
|
||||||
|
String[] lines = text.split("\n");
|
||||||
for (int i = 0; i < lines.length; i++) {
|
for (int i = 0; i < lines.length; i++) {
|
||||||
String line = lines[i];
|
String line = lines[i];
|
||||||
// Mustache will apply correct indentation to the first line of a template (to match declaration location).
|
// Mustache will apply correct indentation to the first line of a template (to match declaration location).
|
||||||
@ -121,9 +122,9 @@ public class IndentedLambda implements Mustache.Lambda {
|
|||||||
|
|
||||||
sb.append(line);
|
sb.append(line);
|
||||||
|
|
||||||
// We've split on the system's line separator. We don't want to add an additional trailing line.
|
// We've split on \n. We don't want to add an additional trailing line.
|
||||||
if (i < lines.length - 1) {
|
if (i < lines.length - 1) {
|
||||||
sb.append(System.lineSeparator());
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.write(sb.toString());
|
writer.write(sb.toString());
|
||||||
|
@ -6,18 +6,17 @@ import org.testng.annotations.Test;
|
|||||||
|
|
||||||
public class IndentedLambdaTest extends LambdaTest {
|
public class IndentedLambdaTest extends LambdaTest {
|
||||||
|
|
||||||
String lineSeparator = System.lineSeparator();
|
String lineSeparator = "\n";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultIndentTest() {
|
public void defaultIndentTest() {
|
||||||
// Given
|
// Given
|
||||||
Map<String, Object> ctx = context("indented", new IndentedLambda());
|
Map<String, Object> ctx = context("indented", new IndentedLambda());
|
||||||
String lineSeparator = System.lineSeparator();
|
|
||||||
|
|
||||||
// When & Then
|
// When & Then
|
||||||
// IndentedLambda applies indentation from second line on of a template.
|
// IndentedLambda applies indentation from second line on of a template.
|
||||||
test("first line" + lineSeparator + " second line",
|
test("first line" + lineSeparator + " second line",
|
||||||
"{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx);
|
"{{#indented}}first line" + lineSeparator + "second line{{/indented}}", ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -28,7 +27,7 @@ public class IndentedLambdaTest extends LambdaTest {
|
|||||||
// When & Then
|
// When & Then
|
||||||
// IndentedLambda applies indentation from second line on of a template.
|
// IndentedLambda applies indentation from second line on of a template.
|
||||||
test("first line" + lineSeparator + " second line",
|
test("first line" + lineSeparator + " second line",
|
||||||
"{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx);
|
"{{#indented}}first line" + lineSeparator + "second line{{/indented}}", ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user