From c46aec7ab9b7f8dc57c3251c816bb925ed6af06e Mon Sep 17 00:00:00 2001 From: nprimo Date: Tue, 21 May 2024 14:22:14 +0100 Subject: [PATCH] feat(lastword): update tests and solution to be a function instead of program --- solutions/lastword.go | 13 ++++++++++ solutions/lastword/main.go | 16 ------------ tests/lastword_test/main.go | 52 +++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 solutions/lastword.go delete mode 100644 solutions/lastword/main.go diff --git a/solutions/lastword.go b/solutions/lastword.go new file mode 100644 index 00000000..62700247 --- /dev/null +++ b/solutions/lastword.go @@ -0,0 +1,13 @@ +package solutions + +import ( + "strings" +) + +func LastWord(s string) string { + words := strings.Fields(s) + if len(words) > 0 { + return words[len(words)-1] + "\n" + } + return "\n" +} diff --git a/solutions/lastword/main.go b/solutions/lastword/main.go deleted file mode 100644 index 711b1340..00000000 --- a/solutions/lastword/main.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - if len(os.Args) == 2 { - words := strings.Fields(os.Args[1]) - if len(words) > 0 { - fmt.Println(words[len(words)-1]) - } - } -} diff --git a/tests/lastword_test/main.go b/tests/lastword_test/main.go index b340804c..6c9dd03a 100644 --- a/tests/lastword_test/main.go +++ b/tests/lastword_test/main.go @@ -1,25 +1,43 @@ 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" + "log" + "reflect" + student "student" ) -func main() { - args := []string{ - "FOR PONY", - "this ... is sparta, then again, maybe not", - " ", - " lorem,ipsum ", - } - - args = append(args, random.StrSlice(chars.Words)...) +var testCases = []struct { + in string + want string +}{ + { + in: " ", + want: "\n", + }, + { + in: "FOR PONY", + want: "PONY\n", + }, + { + in: "this ... is sparta, then again, maybe not", + want: "not\n", + }, + { + in: " lorem,ipsum ", + want: "lorem,ipsum\n", + }, +} - for _, v := range args { - challenge.Program("lastword", v) +func main() { + for _, tc := range testCases { + got := student.LastWord(tc.in) + if !reflect.DeepEqual(got, tc.want) { + log.Fatalf("%s(%q) == %q instead of %q\n", + "RevConcatAlternate", + tc.in, + got, + tc.want, + ) + } } - - challenge.Program("lastword", "a", "b") - challenge.Program("lastword") }