Code Kata Report

diff -r f07d495d0493 -r 37a8519b312f src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java --- a/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 22:55:18 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:08:23 2012 -0500 @@ -14,11 +14,18 @@ private Map<String, String> variableMap = new HashMap<String, String>(); private TemplateEngine templateEngine = new TemplateEngine(); @Test - public void simpleTemplateShouldMapCorrectly() throws Exception { + public void simpleSingleTemplateShouldMapCorrectly() throws Exception { variableMap.put("name", "Nick"); String returnedString = templateEngine.evaluate("Hello {$name}", variableMap); assertThat(returnedString, is(equalTo("Hello Nick"))); } + + @Test + public void simpleSingleVarWithMultipleTemplateShouldMapCorrectly() throws Exception { + variableMap.put("name", "Nick"); + String returnedString = templateEngine.evaluate("{$name}, Hello {$name}", variableMap); + assertThat(returnedString, is(equalTo("Nick, Hello Nick"))); + } } diff -r f07d495d0493 -r 37a8519b312f src/main/java/com/nicholas/tuck/katas/TemplateEngine.java --- a/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 22:55:18 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:08:23 2012 -0500 @@ -1,11 +1,15 @@ package com.nicholas.tuck.katas; import java.util.Map; public class TemplateEngine { public String evaluate(String template, Map<String, String> variableMap) { - return "Hello Nick"; + int beginningOfKey = template.indexOf("{$") + 2; + int endingOfKey = template.indexOf("}", beginningOfKey); + String key = template.substring(beginningOfKey, endingOfKey); + String returnString = template.replaceAll("\\{\\$.+?\\}", variableMap.get(key)); + return returnString; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 37a8519b312f -r be9a9fb3cab1 src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java --- a/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:08:23 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:09:36 2012 -0500 @@ -11,21 +11,20 @@ // Template Engine Code Kata // https://sites.google.com/site/tddproblems/all-problems-1/Template-engine public class TemplateEngineTest { private Map<String, String> variableMap = new HashMap<String, String>(); - private TemplateEngine templateEngine = new TemplateEngine(); @Test public void simpleSingleTemplateShouldMapCorrectly() throws Exception { variableMap.put("name", "Nick"); - String returnedString = templateEngine.evaluate("Hello {$name}", variableMap); + String returnedString = TemplateEngine.evaluate("Hello {$name}", variableMap); assertThat(returnedString, is(equalTo("Hello Nick"))); } @Test public void simpleSingleVarWithMultipleTemplateShouldMapCorrectly() throws Exception { variableMap.put("name", "Nick"); - String returnedString = templateEngine.evaluate("{$name}, Hello {$name}", variableMap); + String returnedString = TemplateEngine.evaluate("{$name}, Hello {$name}", variableMap); assertThat(returnedString, is(equalTo("Nick, Hello Nick"))); } } diff -r 37a8519b312f -r be9a9fb3cab1 src/main/java/com/nicholas/tuck/katas/TemplateEngine.java --- a/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:08:23 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:09:36 2012 -0500 @@ -1,15 +1,15 @@ package com.nicholas.tuck.katas; import java.util.Map; public class TemplateEngine { - public String evaluate(String template, Map<String, String> variableMap) { + public static String evaluate(String template, Map<String, String> variableMap) { int beginningOfKey = template.indexOf("{$") + 2; int endingOfKey = template.indexOf("}", beginningOfKey); String key = template.substring(beginningOfKey, endingOfKey); String returnString = template.replaceAll("\\{\\$.+?\\}", variableMap.get(key)); return returnString; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r be9a9fb3cab1 -r c1d68eed561e src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java --- a/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:09:36 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:22:44 2012 -0500 @@ -13,18 +13,34 @@ public class TemplateEngineTest { private Map<String, String> variableMap = new HashMap<String, String>(); @Test - public void simpleSingleTemplateShouldMapCorrectly() throws Exception { + public void simpleSingleTemplateShouldEvaluateCorrectly() throws Exception { variableMap.put("name", "Nick"); String returnedString = TemplateEngine.evaluate("Hello {$name}", variableMap); assertThat(returnedString, is(equalTo("Hello Nick"))); } @Test - public void simpleSingleVarWithMultipleTemplateShouldMapCorrectly() throws Exception { + public void simpleSingleVarWithMultipleTemplateShouldEvaluateCorrectly() throws Exception { variableMap.put("name", "Nick"); String returnedString = TemplateEngine.evaluate("{$name}, Hello {$name}", variableMap); assertThat(returnedString, is(equalTo("Nick, Hello Nick"))); } + + @Test + public void multipleVarsWithSingleTemplateShouldEvaluateCorrectly() throws Exception { + variableMap.put("firstName", "Nick"); + variableMap.put("lastName", "Tuck"); + String returnedString = TemplateEngine.evaluate("Howdy Mr. {$lastName}", variableMap); + assertThat(returnedString, is(equalTo("Howdy Mr. Tuck"))); + } + + @Test + public void multipleVarsWithMultipleTemplatesShouldEvaluateCorrectly() throws Exception { + variableMap.put("firstName", "Nick"); + variableMap.put("lastName", "Tuck"); + String returnedString = TemplateEngine.evaluate("Howdy {$firstName} {$lastName}", variableMap); + assertThat(returnedString, is(equalTo("Howdy Nick Tuck"))); + } } diff -r be9a9fb3cab1 -r c1d68eed561e src/main/java/com/nicholas/tuck/katas/TemplateEngine.java --- a/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:09:36 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:22:44 2012 -0500 @@ -1,15 +1,21 @@ package com.nicholas.tuck.katas; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class TemplateEngine { public static String evaluate(String template, Map<String, String> variableMap) { - int beginningOfKey = template.indexOf("{$") + 2; - int endingOfKey = template.indexOf("}", beginningOfKey); - String key = template.substring(beginningOfKey, endingOfKey); - String returnString = template.replaceAll("\\{\\$.+?\\}", variableMap.get(key)); + String returnString = template; + Pattern keyPattern = Pattern.compile("\\{\\$.+?\\}"); + Matcher keyMatcher = keyPattern.matcher(template); + while(keyMatcher.find()) { + String keyString = keyMatcher.group(0); + keyString = keyString.substring(2, keyString.length() - 1); + returnString = returnString.replaceFirst("\\{\\$.+?\\}", variableMap.get(keyString)); + } return returnString; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r c1d68eed561e -r d5917d4ec5a3 src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java --- a/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:22:44 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:31:18 2012 -0500 @@ -9,11 +9,11 @@ import static org.hamcrest.Matchers.*; // Template Engine Code Kata // https://sites.google.com/site/tddproblems/all-problems-1/Template-engine public class TemplateEngineTest { - + private Map<String, String> variableMap = new HashMap<String, String>(); @Test public void simpleSingleTemplateShouldEvaluateCorrectly() throws Exception { variableMap.put("name", "Nick"); @@ -41,6 +41,17 @@ variableMap.put("firstName", "Nick"); variableMap.put("lastName", "Tuck"); String returnedString = TemplateEngine.evaluate("Howdy {$firstName} {$lastName}", variableMap); assertThat(returnedString, is(equalTo("Howdy Nick Tuck"))); } + + @Test + public void noVariablesAndNoTemplateShouldReturnString() throws Exception { + String returnedString = TemplateEngine.evaluate("simple test", variableMap); + assertThat(returnedString, is(equalTo("simple test"))); + } + + @Test(expected = IllegalArgumentException.class) + public void templateWithNoCorrespondingVarShouldThrowIllegalArgumentException() throws Exception { + TemplateEngine.evaluate("Missing {$variable} exception", variableMap); + } } diff -r c1d68eed561e -r d5917d4ec5a3 src/main/java/com/nicholas/tuck/katas/TemplateEngine.java --- a/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:22:44 2012 -0500 +++ b/src/main/java/com/nicholas/tuck/katas/TemplateEngine.java Sun Apr 08 23:31:18 2012 -0500 @@ -1,21 +1,25 @@ package com.nicholas.tuck.katas; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TemplateEngine { public static String evaluate(String template, Map<String, String> variableMap) { String returnString = template; Pattern keyPattern = Pattern.compile("\\{\\$.+?\\}"); Matcher keyMatcher = keyPattern.matcher(template); while(keyMatcher.find()) { String keyString = keyMatcher.group(0); keyString = keyString.substring(2, keyString.length() - 1); - returnString = returnString.replaceFirst("\\{\\$.+?\\}", variableMap.get(keyString)); + String variable = variableMap.get(keyString); + if (variable == null) { + throw new IllegalArgumentException("No mapping found for key: " + keyString); + } + returnString = returnString.replaceFirst("\\{\\$.+?\\}", variable); } return returnString; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r d5917d4ec5a3 -r bc66d71206ce src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java --- a/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:31:18 2012 -0500 +++ b/src/test/java/com/nicholas/tuck/katas/TemplateEngineTest.java Sun Apr 08 23:43:26 2012 -0500 @@ -52,6 +52,13 @@ @Test(expected = IllegalArgumentException.class) public void templateWithNoCorrespondingVarShouldThrowIllegalArgumentException() throws Exception { TemplateEngine.evaluate("Missing {$variable} exception", variableMap); } + + @Test + public void complexTemplateShouldMapInnerTemplateOnly() throws Exception { + variableMap.put("name", "Nick"); + String returnedString = TemplateEngine.evaluate("Hello ${$name}}", variableMap); + assertThat(returnedString, is(equalTo("Hello $Nick}"))); + } }