From d3440537009fecb4bdadb081827387e3355620b8 Mon Sep 17 00:00:00 2001 From: nprimo Date: Wed, 29 May 2024 14:57:39 +0100 Subject: [PATCH] feat(firstword): update solution and test to match new subject --- solutions/firstword.go | 12 ++++++++++++ solutions/firstword/main.go | 16 ---------------- tests/firstword_test/main.go | 36 ++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 solutions/firstword.go delete mode 100644 solutions/firstword/main.go diff --git a/solutions/firstword.go b/solutions/firstword.go new file mode 100644 index 00000000..1a60f081 --- /dev/null +++ b/solutions/firstword.go @@ -0,0 +1,12 @@ +package solutions + +import "strings" + +func FirstWord(s string) string { + words := strings.Fields(s) + res := "\n" + if len(words) > 0 { + res = words[0] + res + } + return res +} diff --git a/solutions/firstword/main.go b/solutions/firstword/main.go deleted file mode 100644 index 34474f8f..00000000 --- a/solutions/firstword/main.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - if len(os.Args) == 2 { - s := strings.Split(strings.TrimSpace(os.Args[1]), " ")[0] - if s != "" { - fmt.Println(s) - } - } -} diff --git a/tests/firstword_test/main.go b/tests/firstword_test/main.go index e291696f..647c83f0 100644 --- a/tests/firstword_test/main.go +++ b/tests/firstword_test/main.go @@ -1,22 +1,30 @@ package main import ( - "github.com/01-edu/go-tests/lib/challenge" - "github.com/01-edu/go-tests/lib/chars" - "github.com/01-edu/go-tests/lib/random" + "fmt" + "os" + student "student" ) +var testCases = []struct { + in string + want string +}{ + {"", "\n"}, + {" a as", "a\n"}, + {" f d", "f\n"}, + {" asd ad", "asd\n"}, + {" salut !!! ", "salut\n"}, + {" salut ! ! !", "salut\n"}, + {"salut ! !", "salut\n"}, +} + func main() { - table := append(random.StrSlice(chars.Words), - "", - " a as", - " f d", - " asd ad", - " salut !!! ", - " salut ! ! !", - "salut ! !", - ) - for _, s := range table { - challenge.Program("firstword", s) + for _, tc := range testCases { + got := student.FirstWord(tc.in) + if got != tc.want { + fmt.Printf("FirstWord(%q) = %q instead of %q\n", tc.in, got, tc.want) + os.Exit(1) + } } }