This commit is contained in:
17
main.go
17
main.go
@@ -1,9 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
fmt.Println("Hello, World!")
|
fmt.Println("Hello, World!")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanInput(text string) []string {
|
||||||
|
// trim input string
|
||||||
|
text = strings.TrimSpace(text)
|
||||||
|
// convert to lowercase
|
||||||
|
text = strings.ToLower(text)
|
||||||
|
// replace all double spaces into single space
|
||||||
|
text = strings.Replace(text, " ", " ", -1)
|
||||||
|
// split by space
|
||||||
|
separated := strings.Split(text, " ")
|
||||||
|
return separated
|
||||||
|
}
|
||||||
|
|||||||
34
main_test.go
Normal file
34
main_test.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCleanInput(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
input string
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: " hello world ",
|
||||||
|
expected: []string{"hello", "world"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "Charmander Bulbasaur PIKACHU",
|
||||||
|
expected: []string{"charmander", "bulbasaur", "pikachu"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
actual := cleanInput(c.input)
|
||||||
|
|
||||||
|
if len(actual) != len(c.expected) {
|
||||||
|
t.Errorf("cleanInput(%q) == %q, want %q", c.input, actual, c.expected)
|
||||||
|
}
|
||||||
|
for i := range actual {
|
||||||
|
word := actual[i]
|
||||||
|
expectedWord := c.expected[i]
|
||||||
|
if word != expectedWord {
|
||||||
|
t.Errorf("cleanInput(%q) == %q, want %q", c.input, word, expectedWord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user