Skip to content

Commit

Permalink
Fix popState to honor expression start char
Browse files Browse the repository at this point in the history
Fixes #647
  • Loading branch information
mtdowling committed Dec 2, 2020
1 parent c82e379 commit 43593eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,11 @@ public CodeWriter popState() {
// back into a parent state.
popped.builder.setLength(0);
popped.builder.append(result);
} else {
} else if (!result.isEmpty()) {
// Sections can be added that are just placeholders. In those cases,
// do not write anything unless the section emitted a non-empty string.
// Note that this has already been formatted, so escape "$".
writeOptional(result.replace("$", "$$"));
// This has already been formatted, so writeWithNoFormatting is used.
writeWithNoFormatting(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,21 @@ public void poppedSectionsEscapeDollars() {
assertThat(result, equalTo("$Hello\n"));
}

@Test
public void poppedSectionsEscapeCustomExpressionStarts() {
CodeWriter writer = CodeWriter.createDefault();
String result = writer
.setExpressionStart('#')
.pushState("foo")
.write("##Hello")
.write("$Hello")
.write("$$Hello")
.popState()
.toString();

assertThat(result, equalTo("#Hello\n$Hello\n$$Hello\n"));
}

@Test
public void canWriteInline() {
String result = CodeWriter.createDefault()
Expand Down

0 comments on commit 43593eb

Please sign in to comment.