Skip to content

Commit d8cae8e

Browse files
author
Igor Polevoy
committed
#209 Implement operators and two operands for IfTag
1 parent 4153666 commit d8cae8e

File tree

2 files changed

+222
-19
lines changed

2 files changed

+222
-19
lines changed

javalite-templator/src/main/java/org/javalite/templator/tags/IfTag.java

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.javalite.common.Convert;
44
import org.javalite.templator.AbstractTag;
5+
import org.javalite.templator.ParseException;
56
import org.javalite.templator.Template;
7+
import org.javalite.templator.TemplateException;
68

79
import java.io.Writer;
810
import java.util.List;
@@ -17,8 +19,9 @@
1719
public class IfTag extends AbstractTag {
1820
private Template bodyTemplate;
1921
private boolean singleArgument = true;
20-
private String argumentLine;
22+
private String argument1name, argument2name, operator;
2123

24+
private List<String> operators = list("lt", "gt", "gt=", "lt=", "||", "&&", "==", "!=");
2225

2326
@Override
2427
public void setBody(String body) {
@@ -28,22 +31,100 @@ public void setBody(String body) {
2831

2932
@Override
3033
public void setArguments(String argumentLine) {
31-
this.argumentLine = argumentLine.trim();
34+
3235
String[] arguments = split(argumentLine, " ");
36+
37+
if (arguments.length != 1 && arguments.length != 3) {
38+
throw new ParseException("Incorrect number of arguments for <#if> tag. Either provide a single value or expression with two operands.");
39+
}
3340
singleArgument = arguments.length == 1;
41+
42+
if (singleArgument) {
43+
this.argument1name = argumentLine.trim();
44+
} else {
45+
argument1name = arguments[0].trim();
46+
operator = arguments[1].trim();
47+
argument2name = arguments[2].trim();
48+
if (!operators.contains(operator)) {
49+
throw new ParseException("Cannot parse operator for <#if> tag: " + operator + ". The following operators supported: " + operators);
50+
}
51+
}
3452
}
3553

3654
@Override
3755
public void process(Map values, Writer writer) {
3856

3957
boolean processBody = false;
4058

41-
if(singleArgument){
42-
Object singleArg = values.get(argumentLine);
43-
processBody = Convert.toBoolean(singleArg);
59+
if (singleArgument) {
60+
if (!values.containsKey(argument1name)) {
61+
throw new TemplateException("Bad argument for <#if> tag: '" + argument1name + "' cannot be null.");
62+
}
63+
Object singleArg = values.get(argument1name);
64+
try {
65+
processBody = Convert.toBoolean(singleArg);
66+
} catch (Exception e) {
67+
throw new TemplateException("Bad argument for <#if> tag: '" + argument1name + "' must be a boolean.");
68+
}
69+
} else {
70+
if (!values.containsKey(argument1name) || !values.containsKey(argument2name)) {
71+
throw new TemplateException("Bad arguments for <#if> tag: '" + argument1name + "' or '" +
72+
argument2name + "' cannot be null.");
73+
}
74+
75+
if (operator.equals("lt") ||
76+
operator.equals("lt") ||
77+
operator.equals("gt") ||
78+
operator.equals("lt=") ||
79+
operator.equals("gt=")
80+
) {
81+
Double left, right;
82+
try {
83+
left = Convert.toDouble(values.get(argument1name));
84+
right = Convert.toDouble(values.get(argument2name));
85+
} catch (TemplateException e) {
86+
throw new TemplateException("Bad arguments for <#if> tag: '" + argument1name + "' or '" +
87+
argument2name + "' must be numbers.");
88+
}
89+
90+
if (operator.equals("lt")) {
91+
processBody = left < right;
92+
} else if (operator.equals("gt")) {
93+
processBody = left > right;
94+
} else if (operator.equals("lt=")) {
95+
processBody = left <= right;
96+
} else if (operator.equals("gt=")) {
97+
processBody = left >= right;
98+
}
99+
}
100+
101+
if (operator.equals("==") || operator.equals("!=")) {
102+
if (operator.equals("==")) {
103+
processBody = values.get(argument1name).equals(values.get(argument2name));
104+
} else if (operator.equals("!=")) {
105+
processBody = !values.get(argument1name).equals(values.get(argument2name));
106+
}
107+
}
108+
109+
if (operator.equals("||") || operator.equals("&&")) {
110+
Boolean left, right;
111+
try {
112+
left = Convert.toBoolean(values.get(argument1name));
113+
right = Convert.toBoolean(values.get(argument2name));
114+
} catch (TemplateException e) {
115+
throw new TemplateException("Bad arguments for <#if> tag: '" + argument1name + "' or '" +
116+
argument2name + "' must be booleans.");
117+
}
118+
119+
if (operator.equals("||")) {
120+
processBody = left || right;
121+
} else if (operator.equals("&&")) {
122+
processBody = left && right;
123+
}
124+
}
44125
}
45126

46-
if(processBody){
127+
if (processBody) {
47128
bodyTemplate.process(values, writer);
48129
}
49130
}

javalite-templator/src/test/java/org/javalite/templator/TemplateSpec.java

Lines changed: 135 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Map;
1212

1313
import static java.lang.String.format;
14+
import static junit.framework.TestCase.fail;
1415
import static org.javalite.common.Collections.list;
1516
import static org.javalite.common.Collections.map;
1617
import static org.javalite.test.jspec.JSpec.a;
@@ -236,7 +237,7 @@ public void addHabits(String ... habitsAr) {
236237
}
237238

238239
@Test
239-
public void implementNestedTags() {
240+
public void shouldrenderNestedTags() {
240241
String source = Util.readResource("/nested.html");
241242

242243
Person4 jimmy = new Person4("Jimmy", "Henrdix");
@@ -273,27 +274,148 @@ public void shouldUseBuiltIn() {
273274

274275
}
275276

276-
@Test @Ignore //TODO
277-
public void implement() {
277+
@Test
278+
public void shouldRejectWrongNumberOfArguments() {
279+
try{
280+
new Template("<html><#if one two three four> tada </#if></html>");
281+
}catch(Exception e){
282+
a(e.getMessage()).shouldBeEqual("Incorrect number of arguments for <#if> tag. Either provide a single value or expression with two operands.");
283+
return;
284+
}
285+
fail();
286+
}
278287

279288

280-
/*
289+
@Test
290+
public void shouldRejectWrongIncorrectOperator() {
291+
try{
292+
new Template("<html><#if one bad_operator four> tada </#if></html>");
293+
}catch(Exception e){
294+
a(e.getMessage()).shouldBeEqual("Cannot parse operator for <#if> tag: bad_operator. The following operators supported: [lt, gt, gt=, lt=, ||, &&, ==, !=]");
295+
return;
296+
}
297+
fail();
298+
}
281299

282-
Implement two operand conditions with operators:
283300

284-
> //only for numeric types
285-
<
286-
>=
287-
<=
288-
||
289-
&&
290-
eq - this is for .equals
291301

292-
*/
302+
@Test
303+
public void shouldCompareNumbersWithLT() {
304+
305+
String source = "<html><#if two lt three> tada </#if></html>";
306+
Template template = new Template(source);
307+
StringWriter w = new StringWriter();
308+
template.process(map("two", 2, "three", 3), w);
309+
a(w.toString()).shouldBeEqual("<html> tada </html>");
310+
311+
source = "<html><#if three lt two> tada </#if></html>";
312+
template = new Template(source);
313+
w = new StringWriter();
314+
template.process(map("three", 3, "two", 2), w);
315+
a(w.toString()).shouldBeEqual("<html></html>");
316+
}
317+
318+
@Test
319+
public void shouldCompareNumbersWithLTE() {
293320

321+
String source = "<html><#if three lt= three> tada </#if></html>";
322+
Template template = new Template(source);
323+
StringWriter w = new StringWriter();
324+
template.process(map("three", 3, "three", 3), w);
325+
a(w.toString()).shouldBeEqual("<html> tada </html>");
326+
}
327+
328+
@Test
329+
public void shouldCompareNumbersWithGTE() {
330+
331+
String source = "<html><#if left gt= right> tada </#if></html>";
332+
Template template = new Template(source);
333+
StringWriter w = new StringWriter();
334+
template.process(map("left", 3, "right", 3), w);
335+
a(w.toString()).shouldBeEqual("<html> tada </html>");
336+
337+
338+
w = new StringWriter();
339+
template.process(map("left", 1, "right", 3), w);
340+
a(w.toString()).shouldBeEqual("<html></html>");
341+
}
342+
343+
@Test
344+
public void shouldCompareNumbersWithGT() {
345+
346+
String source = "<html><#if three gt two> tada </#if></html>";
347+
Template template = new Template(source);
348+
StringWriter w = new StringWriter();
349+
template.process(map("three", 3, "two", 2), w);
350+
a(w.toString()).shouldBeEqual("<html> tada </html>");
351+
352+
source = "<html><#if two gt three> tada </#if></html>";
353+
template = new Template(source);
354+
w = new StringWriter();
355+
template.process(map("two", 2, "three", 3), w);
356+
a(w.toString()).shouldBeEqual("<html></html>");
294357
}
295358

296359

360+
@Test
361+
public void shouldCompareObjectsWithEQ() {
362+
363+
String source = "<html><#if word1 == word2> tada </#if></html>";
364+
Template template = new Template(source);
365+
StringWriter w = new StringWriter();
366+
template.process(map("word1", "help", "word2", "help"), w);
367+
a(w.toString()).shouldBeEqual("<html> tada </html>");
368+
369+
w = new StringWriter();
370+
template.process(map("word1", "help!", "word2", "help?"), w);
371+
a(w.toString()).shouldBeEqual("<html></html>");
372+
}
373+
374+
@Test
375+
public void shouldCompareObjectsWithNEQ() {
376+
377+
String source = "<html><#if word1 != word2> tada </#if></html>";
378+
Template template = new Template(source);
379+
StringWriter w = new StringWriter();
380+
template.process(map("word1", "help!", "word2", "help?"), w);
381+
a(w.toString()).shouldBeEqual("<html> tada </html>");
382+
383+
w = new StringWriter();
384+
template.process(map("word1", "help", "word2", "help"), w);
385+
a(w.toString()).shouldBeEqual("<html></html>");
386+
}
387+
388+
389+
@Test
390+
public void shouldCompareBooleansWithOR() {
391+
392+
String source = "<html><#if cond1 || cond2> tada </#if></html>";
393+
Template template = new Template(source);
394+
StringWriter w = new StringWriter();
395+
template.process(map("cond1", true, "cond2", true), w);
396+
a(w.toString()).shouldBeEqual("<html> tada </html>");
397+
398+
w = new StringWriter();
399+
template.process(map("cond1", true, "cond2", false), w);
400+
a(w.toString()).shouldBeEqual("<html> tada </html>");
401+
}
402+
403+
@Test
404+
public void shouldCompareBooleansWithAND() {
405+
406+
String source = "<html><#if cond1 && cond2> tada </#if></html>";
407+
Template template = new Template(source);
408+
StringWriter w = new StringWriter();
409+
template.process(map("cond1", true, "cond2", true), w);
410+
a(w.toString()).shouldBeEqual("<html> tada </html>");
411+
412+
w = new StringWriter();
413+
template.process(map("cond1", true, "cond2", false), w);
414+
a(w.toString()).shouldBeEqual("<html></html>");
415+
}
416+
417+
418+
297419
@Test @Ignore //TODO
298420
public void implement1() {
299421
/*

0 commit comments

Comments
 (0)