Last active
December 16, 2015 01:59
-
-
Save NicMcPhee/5359230 to your computer and use it in GitHub Desktop.
Unfinished solution to the dictionary replacer kata (http://codingdojo.org/cgi-bin/wiki.pl?back=KataDictionaryReplacer) from the 10 April 2013 coding dojo at the University of Minnesota, Morris. We got a number of good tests written and can pass everything that just has a single tagged term, but time ran out before we got to dealing with the mor…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DictionaryReplacer { | |
def replace(string, dictionary){ | |
def returnValue | |
if (string.size() < 2){ | |
returnValue = string | |
} | |
else if (string[0]!='$'){ | |
def firstDollar = string.indexOf('$') | |
if (firstDollar == -1) { | |
returnValue = string | |
} | |
else { | |
def preString = string[0..firstDollar-1] | |
returnValue = preString + replace(string[firstDollar..-1], dictionary) | |
} | |
} | |
else{ | |
returnValue = doReplacement(string, dictionary) | |
} | |
return returnValue | |
} | |
def doReplacement(string, dictionary) { | |
def secondDollar = string.indexOf('$', 1) | |
def replacedTerm = dictionary[string[1..secondDollar-1]] | |
def needsReplacing = secondDollar!=-1 && replacedTerm | |
def returnValue = "" | |
if (needsReplacing) { | |
if (secondDollar == string.size()-1) { | |
returnValue=replacedTerm | |
} else { | |
returnValue = replacedTerm + replace(string[secondDollar+1..-1], dictionary) | |
} | |
} else { | |
returnValue = string | |
} | |
return returnValue | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class TestDictionaryReplacer extends Specification { | |
def "test empty string"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = "" | |
def dictionary = [:] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "" | |
} | |
def "test non-empty string with an empty dictionary"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = "some string" | |
def dictionary = [:] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "some string" | |
} | |
def "test one-word string with non-empty dictionary"() { | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = '$word$' | |
def dictionary = ['word' : "Up"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "Up" | |
} | |
def "test one-word string with new non-empty dictionary"() { | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = '$word$' | |
def dictionary = ['word' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "Down" | |
} | |
def "test one-word string with newer non-empty dictionary"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = '$phrase$' | |
def dictionary = ['phrase' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "Down" | |
} | |
def "test one-word string with non-empty dictionary and no match"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = '$phrase$' | |
def dictionary = ['blip' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == '$phrase$' | |
} | |
def "test string with letter in front"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'd$phrase$' | |
def dictionary = ['phrase' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "dDown" | |
} | |
def "test string without ending \$"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'd$phrase' | |
def dictionary = ['phrase' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "d\$phrase" | |
} | |
def "test string without ending dollar sign"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'd$phrase' | |
def dictionary = ['phras' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "d\$phrase" | |
} | |
def "test string with beginning and ending character"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'd$phrase$d' | |
def dictionary = ['phrase' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == "dDownd" | |
} | |
def "test non-empty string with a non-empty dictionary"() { | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'Hello, $name$ you are looking $adjective$.' | |
def dictionary = [name : "Brian", adjective : "sleepy"] | |
} | |
def "test string with three dollar signs"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'd$phrase$more stuff$' | |
def dictionary = ['phrase' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == 'dDownmore stuff$' | |
} | |
def "test string with three dollar signs and text at end"(){ | |
given: | |
def dictionaryReplacer = new DictionaryReplacer() | |
def stringInput = 'd$phrase$ and then some $stuff and then some' | |
def dictionary = ['phrase' : "Down"] | |
when: | |
def result = dictionaryReplacer.replace(stringInput, dictionary) | |
then: | |
result == 'dDown and then some $stuff and then some' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment