Skip to content

Commit d960167

Browse files
committed
Fixed Templates chained method bug so order does not matter
1 parent 5a2f976 commit d960167

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/com/googlecode/totallylazy/template/Templates.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
public class Templates implements Renderers {
2121
private final Function2<? super String, ? super Renderers, ? extends Renderer<?>> missing;
22-
private final ConcurrentMap<String, Renderer<Object>> renderers = new ConcurrentHashMap<>();
23-
private volatile CompositeRenderer implicits = CompositeRenderer.compositeRenderer();
22+
private final ConcurrentMap<String, Renderer<Object>> renderers;
23+
private volatile CompositeRenderer implicits;
2424

25-
private Templates(Function2<? super String, ? super Renderers, ? extends Renderer<?>> missing) {
25+
public Templates(Function2<? super String, ? super Renderers, ? extends Renderer<?>> missing, ConcurrentMap<String, Renderer<Object>> renderers, CompositeRenderer implicits) {
2626
this.missing = missing;
27+
this.renderers = renderers;
28+
this.implicits = implicits;
2729
}
2830

2931
public static Templates templates() {
@@ -43,7 +45,7 @@ public static Templates templates(Renderers parent) {
4345
}
4446

4547
public static Templates templates(Function2<? super String, ? super Renderers, ? extends Renderer<?>> missing) {
46-
return new Templates(missing);
48+
return new Templates(missing, new ConcurrentHashMap<>(), CompositeRenderer.compositeRenderer());
4749
}
4850

4951
public Templates addDefault() {
@@ -82,7 +84,7 @@ public Renderer<Object> get(String name) {
8284
}
8385

8486
public Templates extension(String value) {
85-
return new Templates((s, r) -> missing.apply(s + "." + value, r));
87+
return new Templates((s, r) -> missing.apply(s + "." + value, r), renderers, implicits);
8688
}
8789

8890
public Templates logger(Appendable logger) {
@@ -93,6 +95,6 @@ public Templates logger(Appendable logger) {
9395
logger.append(format("Unable to load template '%s' because: %s%n", s, e.getMessage()));
9496
return (instance, appendable) -> appendable;
9597
}
96-
});
98+
}, renderers, implicits);
9799
}
98100
}

test/com/googlecode/totallylazy/template/TemplatesTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,20 @@ public void canUseTemplatesFromClassPathAndDefaultTemplates() throws Exception {
6161
@Test
6262
public void doesNotThrowWhenASubTemplateIsNotFoundButReturnsEmptyStringAndLogs() throws Exception {
6363
StringBuilder builder = new StringBuilder();
64-
Templates templates = templates(getClass()).logger(builder).extension("st").add("works", ignore -> "Other templates still work");
65-
String result = templates.get("error").render(map());
64+
Templates templates = templates(getClass()).add("works", ignore -> "Other templates still work").logger(builder);
65+
String result = templates.get("error.st").render(map());
6666
assertThat(result, is("Other templates still work\nSub template returned ''"));
6767
String log = builder.toString();
68-
assertThat(log, startsWith("Unable to load template 'foo.st' because: "));
68+
assertThat(log, startsWith("Unable to load template 'foo' because: "));
6969
assertThat(log, not(contains("Unable to load template 'works' because: ")));
7070
}
71+
72+
@Test
73+
public void orderOfChainedMethodsDoNotMatter() throws Exception {
74+
Templates templates1 = templates(getClass()).add("works", ignore -> "Other templates still work").logger(new StringBuilder());
75+
assertThat(templates1.get("error.st").render(map()), is("Other templates still work\nSub template returned ''"));
76+
77+
Templates templates2 = templates(getClass()).logger(new StringBuilder()).add("works", ignore -> "Other templates still work");
78+
assertThat(templates2.get("error.st").render(map()), is("Other templates still work\nSub template returned ''"));
79+
}
7180
}

0 commit comments

Comments
 (0)