test: add a simple test suite
This commit is contained in:
parent
57782b0903
commit
ca7afe1f34
2 changed files with 84 additions and 12 deletions
|
@ -12,27 +12,24 @@ func main() {
|
||||||
flag.BoolVar(&all, "all", false, "reveal the code words of all the letters in the English alphabet.")
|
flag.BoolVar(&all, "all", false, "reveal the code words of all the letters in the English alphabet.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var input string
|
|
||||||
|
|
||||||
if all {
|
if all {
|
||||||
input = fullAlphabet()
|
fmt.Print(phoneticLetters(fullAlphabet()))
|
||||||
} else {
|
} else {
|
||||||
args := flag.Args()
|
fmt.Print(phoneticLetters(flag.Args()...))
|
||||||
for i := range args {
|
|
||||||
input = input + args[i]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(phoneticLetters(input))
|
|
||||||
}
|
|
||||||
|
|
||||||
// phoneticLetters returns a string of all the code words for the given set of letters.
|
// phoneticLetters returns a string of all the code words for the given set of letters.
|
||||||
func phoneticLetters(letters string) string {
|
func phoneticLetters(input... string) string {
|
||||||
letters = strings.ToLower(letters)
|
var characters string
|
||||||
|
|
||||||
|
for i := range input {
|
||||||
|
characters = characters + strings.ToLower(input[i])
|
||||||
|
}
|
||||||
|
|
||||||
b := strings.Builder{}
|
b := strings.Builder{}
|
||||||
|
|
||||||
for _, r := range letters {
|
for _, r := range characters {
|
||||||
switch r {
|
switch r {
|
||||||
case 'a':
|
case 'a':
|
||||||
b.WriteString("A - Alpha\n")
|
b.WriteString("A - Alpha\n")
|
||||||
|
|
75
phonetic_letters_test.go
Normal file
75
phonetic_letters_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue