Skip to content

Commit 64dfb66

Browse files
author
Sam Pullara
committed
add a test for simpleobjecthandler dot notation and a fix
1 parent 684bd8a commit 64dfb66

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

compiler/src/main/java/com/github/mustachejava/reflect/SimpleObjectHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public Object call(Object[] scopes) throws GuardException {
6767
Wrapper wrapper = find(name.substring(0, index), subscope);
6868
if (wrapper != null) {
6969
scope = wrapper.call(subscope);
70+
if (scope == null) {
71+
continue;
72+
}
7073
subscope = new Object[]{scope};
7174
return find(name.substring(index + 1), new Object[]{subscope}).call(subscope);
7275
}

compiler/src/test/java/com/github/mustachejava/InterpreterTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.mustachejava;
22

3+
import com.github.mustachejava.reflect.SimpleObjectHandler;
34
import com.github.mustachejava.util.CapturingMustacheVisitor;
45
import com.github.mustachejavabenchmarks.JsonCapturer;
56
import com.github.mustachejavabenchmarks.JsonInterpreterTest;
@@ -97,6 +98,31 @@ int taxed_value() {
9798
assertEquals(getContents(root, "simple.txt"), sw.toString());
9899
}
99100

101+
private class OkGenerator {
102+
public boolean isItOk() {
103+
return true;
104+
}
105+
}
106+
107+
public void testNestedAccessWithSimpleObjectHandler() throws IOException {
108+
assertEquals(getOutput(false), getOutput(true));
109+
}
110+
111+
private String getOutput(final boolean setObjectHandler) {
112+
final DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory();
113+
if (setObjectHandler) {
114+
mustacheFactory.setObjectHandler(new SimpleObjectHandler());
115+
}
116+
final Mustache defaultMustache = mustacheFactory.compile(new StringReader("{{#okGenerator.isItOk}}{{okGenerator.isItOk}}{{/okGenerator.isItOk}}"), "Test template");
117+
final Map<String, Object> params = new HashMap<String, Object>();
118+
params.put("okGenerator", new OkGenerator());
119+
final Writer writer = new StringWriter();
120+
defaultMustache.execute(writer, params);
121+
return writer.toString();
122+
123+
}
124+
125+
100126
public void testMultipleWrappers() throws MustacheException, IOException, ExecutionException, InterruptedException {
101127
MustacheFactory c = createMustacheFactory();
102128
Mustache m = c.compile("simple.html");

indy/src/test/java/IndyDemo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static void main(String[] args) throws Throwable {
2121
timeReflection(indyDemo);
2222
timeReflectionCached(indyDemo);
2323
timeDirect(indyDemo);
24+
System.out.println("-----------------");
2425
}
2526
}
2627

0 commit comments

Comments
 (0)