Skip to content

Instantly share code, notes, and snippets.

@Chavao
Created October 31, 2012 02:28
Show Gist options
  • Save Chavao/3984453 to your computer and use it in GitHub Desktop.
Save Chavao/3984453 to your computer and use it in GitHub Desktop.
A simple template engine covered by tests
package br.chavao.template;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* @author Chavão <[email protected]>
*/
public class Template {
private String content;
private Map<String, String> target;
public Template() {
this.content = new String();
this.target = new HashMap<String, String>();
}
public Template(String content) {
super();
this.content = content;
}
public Template setTarget(String key, Integer value) {
return this.setTarget(key, String.valueOf(value));
}
public Template setContent(String content) {
if (content.isEmpty()) {
throw new IllegalArgumentException("Content must be setted");
}
this.content = content;
return this;
}
public Template setTarget(String key, String value) {
if (key.isEmpty()) {
throw new IllegalArgumentException("Key must be setted");
}
this.target.put(key, value);
return this;
}
public String render() {
if (this.content.isEmpty()) {
throw new RuntimeException("Content must be setted before runs render");
}
Iterator it = this.target.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
String key = pairs.getKey().toString();
String value = pairs.getValue().toString();
this.content = this.content.replaceAll("\\{\\{" + key + "\\}\\}", value);
it.remove();
}
return this.content;
}
}
package br.chavao.template;
import org.junit.*;
import static org.junit.Assert.*;
import org.junit.rules.ExpectedException;
/**
*
* @author Chavão <[email protected]>
*/
public class TemplateTeste {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void setTargetShouldThrowsAnExceptionWhenKeyIsNull() {
thrown.expect(IllegalArgumentException.class);
Template t = new Template();
t.setTarget("", "Value");
}
@Test
public void returnedValueShouldBeTheSameObject() {
Template t = new Template();
Template targetReturned = t.setTarget("Key", "Value");
assertEquals(targetReturned, t);
Template contentReturned = t.setContent("Content");
assertEquals(contentReturned, t);
}
@Test
public void setTargetShouldAcceptAnIntegerValue() {
Template t = new Template();
Template returned = t.setTarget("Key", 15);
assertEquals(t, returned);
}
@Test
public void constructorShouldAcceptAStringValue() {
Template t = new Template("John Doe");
assertNotNull(t);
}
@Test
public void setContentShouldThrowsAnExceptionWhenContentIsNull() {
thrown.expect(IllegalArgumentException.class);
Template t = new Template();
t.setContent("");
}
@Test
public void renderShouldThrowsAnExceptionWhenContentIsNull() {
thrown.expect(RuntimeException.class);
Template t = new Template();
t.render();
}
@Test
public void renderShouldRetrieveAParsedTemplate() {
Template t = new Template();
t.setContent("<span> {{name}} </span> {{age}}.");
t.setTarget("name", "John Doe");
t.setTarget("age", 25);
String renderedReturned = t.render();
assertEquals(renderedReturned, "<span> John Doe </span> 25.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment