Tag testing

Testing with Go

An integral part of the development licefycle is writing tests. Write tests early, write tests often as they say. Go makes it very easy to write tests. The following post will go through the basics of testing with Go. Let’s say we have a program that counts the number of vowels, consonants, digits and everything else within an input string. It may look something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package main import ( "flag" "fmt" ) // CountChars counts the number of characters in a string func CountChars(str string) (int, int, int, int) { vcount := 0 ccount := 0 dcount := 0 ocount := 0 for i := 0; i < len(str); i++ { switch str[i] { case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U': vcount++ case 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z': ccount++ case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0': dcount++ default: ocount++ } } return vcount, ccount, dcount, ocount } func main() { inputStr := flag.