From ca7afe1f340f7f0ba5622613a530342df8aa81b0 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 8 Jan 2024 11:24:28 +0000 Subject: [PATCH] test: add a simple test suite --- phonetic_letters.go | 21 +++++------ phonetic_letters_test.go | 75 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 phonetic_letters_test.go diff --git a/phonetic_letters.go b/phonetic_letters.go index 818e4e6..57e9143 100644 --- a/phonetic_letters.go +++ b/phonetic_letters.go @@ -12,27 +12,24 @@ func main() { flag.BoolVar(&all, "all", false, "reveal the code words of all the letters in the English alphabet.") flag.Parse() - var input string - if all { - input = fullAlphabet() + fmt.Print(phoneticLetters(fullAlphabet())) } else { - args := flag.Args() - for i := range args { - input = input + args[i] - } + fmt.Print(phoneticLetters(flag.Args()...)) } - - fmt.Print(phoneticLetters(input)) } // phoneticLetters returns a string of all the code words for the given set of letters. -func phoneticLetters(letters string) string { - letters = strings.ToLower(letters) +func phoneticLetters(input... string) string { + var characters string + + for i := range input { + characters = characters + strings.ToLower(input[i]) + } b := strings.Builder{} - for _, r := range letters { + for _, r := range characters { switch r { case 'a': b.WriteString("A - Alpha\n") diff --git a/phonetic_letters_test.go b/phonetic_letters_test.go new file mode 100644 index 0000000..6502297 --- /dev/null +++ b/phonetic_letters_test.go @@ -0,0 +1,75 @@ +package main + +import "testing" + +func TestPhoneticLetters(t *testing.T) { + t.Log("This is the test suite for phonetic-letters") + t.Run("Test the application with one string", testWithOneString) + t.Run("Test the application with two strings", testWithTwoStrings) + t.Run("Test the application with numbers and symbols", testWithNumbersAndSymbols) +} + +func testWithOneString(t *testing.T) { + input := "phonetic" + + got := phoneticLetters(input) + + want := `P - Papa +H - Hotel +O - Oscar +N - November +E - Echo +T - Tango +I - India +C - Charlie +` + + if want != got { + t.Errorf("Unexpected result returned from phoneticLetters(); want %s, got %s", want, got) + } +} + +func testWithTwoStrings(t *testing.T) { + input := []string{"three", "coins"} + + got := phoneticLetters(input...) + + want := `T - Tango +H - Hotel +R - Romeo +E - Echo +E - Echo +C - Charlie +O - Oscar +I - India +N - November +S - Sierra +` + + if want != got { + t.Errorf("Unexpected result returned from phoneticLetters(); want %s, got %s", want, got) + } +} + +func testWithNumbersAndSymbols(t *testing.T) { + input := "Hello, 8 Worlds!" + + got := phoneticLetters(input) + + want := `H - Hotel +E - Echo +L - Lima +L - Lima +O - Oscar +W - Whiskey +O - Oscar +R - Romeo +L - Lima +D - Delta +S - Sierra +` + + if want != got { + t.Errorf("Unexpected result returned from phoneticLetters(); want %s, got %s", want, got) + } +}