Skip to content

Commit

Permalink
bug fix (lexer): if with operator is now robust to ignorable whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
DJCordhose committed Nov 7, 2017
1 parent 1d456d9 commit 43803eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@ Major Release 5.0.0
* Breaking Change: Removed all API deprecated in version 4
* Breaking Change: Switch to Java 8 language level
* Bug fix / Enhancement: if statement more robust (https://github.com/DJCordhose/jmte/issues/8)
* Refactoring / Cleanup: Removed all caliper tests and real life test
* Refactoring / Cleanup: Removed all caliper tests and real life test
* Deprecation: Configurable expression syntax, will be fixed to ${...}
* Enhancement: transform using a *null* template will yield *null*
15 changes: 12 additions & 3 deletions src/com/floreysoft/jmte/token/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private AbstractToken innerNextToken(final String untrimmedInput) {
negated = false;
ifExpression = objectExpression;
}
if (!ifExpression.contains("=") && !ifExpression.contains(";")) {
if (!input.contains("=") && !input.contains(";")) {
return new IfToken(ifExpression, negated);
} else {
// HACK: if the value we compare to contains a space, it is cut off
Expand All @@ -86,11 +86,16 @@ private AbstractToken innerNextToken(final String untrimmedInput) {
String operand = null;
if (hasCmp) {
operand = completeIfExpression.substring(posLastEq + 1);
// heuristic: when there is leading or trailing space and after that a quote begins,
// it must be ignorable white space
if (isQuoted(operand.trim())) {
operand = operand.trim();
}
// remove optional quotations
if (operand.startsWith("'") || operand.startsWith("\"")) {
if (isQuoted(operand)) {
operand = operand.substring(1, operand.length() - 1);
}
complexVariable = completeIfExpression.substring(0, posLastEq);
complexVariable = completeIfExpression.substring(0, posLastEq).trim();
} else {
complexVariable = completeIfExpression;
}
Expand Down Expand Up @@ -231,4 +236,8 @@ private AbstractToken innerNextToken(final String untrimmedInput) {

}

private static boolean isQuoted(String operand) {
return operand.startsWith("'") || operand.startsWith("\"");
}

}
14 changes: 14 additions & 0 deletions test/com/floreysoft/jmte/EngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ public String toString() {
DEFAULT_MODEL.put("bigDecimal1", new BigDecimal("1.0"));
}

@Test
public void transformRobustToNull() {
String output = newEngine()
.transform(null, new HashMap<>());
assertNull(output);
}

@Test
public void variableName() throws Exception {
Map<String, Object> simpleModel = new HashMap<String, Object>();
Expand Down Expand Up @@ -1731,6 +1738,13 @@ public void ifEqSpacesSample() throws Exception {
assertEquals("Filbert", output);
}

@Test
public void ifEqSpacesElseSample() throws Exception {
String input = "${if address = 'Filbert2'}${address}${else}NIX${end}";
String output = newEngine().transform(input, DEFAULT_MODEL);
assertEquals("NIX", output);
}

@Test
public void ifEqSpacesRendererSample() throws Exception {
String input = "${if address;string = 'Filbert'}${address}${else}NIX${end}";
Expand Down

0 comments on commit 43803eb

Please sign in to comment.