Skip to content

Commit

Permalink
fixed escape token, added in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Synapticloop authored and Synapticloop committed Mar 10, 2016
1 parent 314a978 commit 4b8496b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
group = 'synapticloop'
archivesBaseName = 'templar'
description = """Templar templating engine"""
version = '1.2.0'
version = '1.2.1'

sourceCompatibility = 1.7
targetCompatibility = 1.7
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/synapticloop/templar/token/EscapeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.StringTokenizer;

import synapticloop.templar.exception.ParseException;
import synapticloop.templar.utils.TemplarContext;
import synapticloop.templar.utils.Tokeniser;

public class EscapeToken extends Token {
Expand All @@ -31,20 +32,26 @@ public EscapeToken(String value, StringTokenizer stringTokenizer, Tokeniser toke

StringBuilder stringBuilder = new StringBuilder();

if(stringTokenizer.hasMoreTokens()) {
while(stringTokenizer.hasMoreTokens()) {
String nextToken = stringTokenizer.nextToken();
if("}".equals(nextToken)) {
value = stringBuilder.toString();
this.value = stringBuilder.toString();
return;
} else {
stringBuilder.append(nextToken);
}
} else {
throw new ParseException("Could not find end token marker '}' for the escape token, and we are out of tokens", this);
}
}

throw new ParseException("Could not find end token marker '}' for the escape token, and we are out of tokens", this);
}

@Override
public String render(TemplarContext templarContext) {
return(this.value);
}

@Override
public String toString() {
return(super.toString("ESCAPE", value));
return(super.toString("ESCAPE", this.value));
}
}
51 changes: 51 additions & 0 deletions src/test/java/synapticloop/templar/token/EscapeTokenTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package synapticloop.templar.token;

/*
* Copyright (c) 2012-2015 synapticloop.
* All rights reserved.
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENCE shipped with
* this source code or binaries.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* Licence for the specific language governing permissions and limitations
* under the Licence.
*/

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import synapticloop.templar.Parser;
import synapticloop.templar.exception.ParseException;
import synapticloop.templar.exception.RenderException;

public class EscapeTokenTest {
@Before
public void setup() {
}

@Test(expected = ParseException.class)
public void testNoEndToken() throws ParseException {
new Parser("{\\");
}

@Test(expected = ParseException.class)
public void testNoEndTokenWithOtherTokens() throws ParseException {
new Parser("{\\ blah-di-blah {{{");
}

@Test
public void testEscape() throws ParseException, RenderException {
Parser parser = new Parser("{\\ }");
System.out.println(parser.toString());
assertEquals("<ESCAPE@1:2 ( )/>", parser.toString());
assertEquals(" ", parser.render());
}
}

0 comments on commit 4b8496b

Please sign in to comment.