Skip to content

Commit

Permalink
Fix variable name starting with operator (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwolaq authored Nov 17, 2020
1 parent 2634c1c commit 8520b2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ private void buildOperatorRegex() {
*/
char nextChar = operator.charAt(operator.length() - 1);
if (Character.isLetter(nextChar) || Character.getType(nextChar) == Character.LETTER_NUMBER) {
regex.append("(?![a-zA-Z])");
regex.append("(?![a-zA-Z0-9_])");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ void testTokenizeExpression() {
assertThat(tokenStream.peek(3).getValue()).isNull();
}

/**
* Test tokenizing an expression, e.g. {{ expression }}
*/
@Test
void testVariableNameStartingWithOperator() {
Loader<String> loader = new StringLoader();
Reader templateReader = loader.getReader("{{ is_active + contains0 }}");

TokenStream tokenStream = this.lexer.tokenize(templateReader, this.TEMPLATE_NAME);

int i = 0;
assertThat(tokenStream.peek(i).getType()).isEqualTo(Type.PRINT_START);
assertThat(tokenStream.peek(i++).getValue()).isNull();

assertThat(tokenStream.peek(i).getType()).isEqualTo(Type.NAME);
assertThat(tokenStream.peek(i++).getValue()).isEqualTo("is_active");

assertThat(tokenStream.peek(i).getType()).isEqualTo(Type.OPERATOR);
assertThat(tokenStream.peek(i++).getValue()).isEqualTo("+");

assertThat(tokenStream.peek(i).getType()).isEqualTo(Type.NAME);
assertThat(tokenStream.peek(i++).getValue()).isEqualTo("contains0");

assertThat(tokenStream.peek(i).getType()).isEqualTo(Type.PRINT_END);
assertThat(tokenStream.peek(i++).getValue()).isEqualTo("}}");

assertThat(tokenStream.peek(i).getType()).isEqualTo(Type.EOF);
assertThat(tokenStream.peek(i++).getValue()).isNull();
}

/**
* Test tokenizing Punctuation, such as the dot in item.itemType
*/
Expand Down

0 comments on commit 8520b2f

Please sign in to comment.