Skip to content

Commit

Permalink
Improved Templates test
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Oct 19, 2017
1 parent 2d73ab2 commit 5a2f976
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/com/googlecode/totallylazy/template/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Templates logger(Appendable logger) {
try {
return missing.apply(s, r);
} catch (Exception e) {
logger.append(format("Unable to load template '%s' because: %s", s, e.getMessage()));
logger.append(format("Unable to load template '%s' because: %s%n", s, e.getMessage()));
return (instance, appendable) -> appendable;
}
});
Expand Down
13 changes: 9 additions & 4 deletions test/com/googlecode/totallylazy/template/TemplatesTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.googlecode.totallylazy.template;

import com.googlecode.totallylazy.Strings;
import com.googlecode.totallylazy.predicates.Predicates;
import org.junit.Test;

import java.io.PrintWriter;

import static com.googlecode.totallylazy.Assert.assertThat;
import static com.googlecode.totallylazy.Maps.map;
import static com.googlecode.totallylazy.Strings.contains;
import static com.googlecode.totallylazy.Strings.startsWith;
import static com.googlecode.totallylazy.predicates.Predicates.contains;
import static com.googlecode.totallylazy.predicates.Predicates.instanceOf;
import static com.googlecode.totallylazy.predicates.Predicates.is;
import static com.googlecode.totallylazy.predicates.Predicates.not;
import static com.googlecode.totallylazy.template.Templates.templates;

public class TemplatesTest {
Expand Down Expand Up @@ -57,10 +60,12 @@ public void canUseTemplatesFromClassPathAndDefaultTemplates() throws Exception {

@Test
public void doesNotThrowWhenASubTemplateIsNotFoundButReturnsEmptyStringAndLogs() throws Exception {
StringBuilder log = new StringBuilder();
Templates templates = templates(getClass()).logger(log).extension("st");
StringBuilder builder = new StringBuilder();
Templates templates = templates(getClass()).logger(builder).extension("st").add("works", ignore -> "Other templates still work");
String result = templates.get("error").render(map());
assertThat(result, is("Sub template returned ''"));
assertThat(log.toString(), startsWith("Unable to load template 'foo.st' because: "));
assertThat(result, is("Other templates still work\nSub template returned ''"));
String log = builder.toString();
assertThat(log, startsWith("Unable to load template 'foo.st' because: "));
assertThat(log, not(contains("Unable to load template 'works' because: ")));
}
}
1 change: 1 addition & 0 deletions test/com/googlecode/totallylazy/template/error.st
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
$works()$
Sub template returned '$foo()$'

0 comments on commit 5a2f976

Please sign in to comment.