test: add a simple test suite

This commit is contained in:
Dan Anglin 2024-01-08 11:24:28 +00:00
parent 57782b0903
commit ca7afe1f34
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 84 additions and 12 deletions

View file

@ -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")

75
phonetic_letters_test.go Normal file
View file

@ -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)
}
}