Last active
November 1, 2016 18:54
-
-
Save acapps/a6979089a212ba68d31012d0dfa2ab75 to your computer and use it in GitHub Desktop.
Example implementation of Zipwhip's Stop Handling. One file, as it is meant to run on the Go Playground
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
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
testStop() | |
} | |
func stop(input string) bool { | |
trimmed := strings.TrimSpace(input) // Remove whitespace characters: space, tab, newline | |
words := strings.Split(trimmed, " ") // Split the input into words by the space character | |
if len(words) == 1 { // Only if "STOP" is alone do we count it. | |
if strings.ToUpper(words[0]) == "STOP" { | |
return true | |
} | |
} | |
return false | |
} | |
func testStop() { | |
inputs := map[string]bool{ | |
"stop": true, | |
" stop": true, | |
"\tstop": true, | |
"\nstop": true, | |
"stop ": true, | |
"stop\t": true, | |
"stop\n": true, | |
"stop it": false, | |
"Hey, stop it.": false, | |
} | |
for input, expectedResult := range inputs { | |
actualResult := stop(input) | |
if actualResult == expectedResult { | |
fmt.Println(formatTestOutput("PASS", input, expectedResult, actualResult)) | |
continue | |
} | |
fmt.Print(formatTestOutput("FAIL", input, expectedResult, actualResult)) | |
} | |
} | |
func formatTestOutput(result string, input string, expectedResult bool, actualResult bool) string { | |
var strFormat = "[%s] - Input: %-15s %25s %-10s %s {%t}" | |
input = strings.Replace(input, "\n", "\\n", -1) | |
input = strings.Replace(input, "\t", "\\t", -1) | |
return fmt.Sprintf(strFormat, result, fmt.Sprintf("[%s]", input), "ExpectedResult:", fmt.Sprintf("{%t}", expectedResult), "ActualResult:", actualResult) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment