From 5811e43f3c5c7d1d5baeeedf55162fb1f34b31d9 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Mon, 10 Mar 2025 21:40:07 +0200 Subject: [PATCH] #2 --- main.go | 17 ++++++++++++++++- main_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 main_test.go diff --git a/main.go b/main.go index b9f616e..a588538 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,24 @@ package main -import "fmt" +import ( + "fmt" + "strings" +) func main() { 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 +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..9aa0f3d --- /dev/null +++ b/main_test.go @@ -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) + } + } + } +}