Skip to content

Instantly share code, notes, and snippets.

@jakelacey2012
Created August 6, 2017 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakelacey2012/01da922854a07abddf7058b1bafc069c to your computer and use it in GitHub Desktop.
Save jakelacey2012/01da922854a07abddf7058b1bafc069c to your computer and use it in GitHub Desktop.
Example of testing in go
package sum
// Ints returns sum of list integers.
func Ints(vs ...int) int {
return ints(vs)
}
func ints(vs []int) int {
if len(vs) == 0 {
return 0
}
return Ints(vs[1:]...) + vs[0]
}
////////////////////////////
package sum_test
import "testing"
import "github.com/jakelacey2012/testing"
func TestInt(t *testing.T) {
tt := []struct {
name string
numbers []int
sum int
}{
{"One to five", []int{1, 2, 3, 4, 5}, 15},
{"No Numbers", nil, 0},
{"one and minus one", []int{1, -1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
s := sum.Ints(tc.numbers...)
if s != tc.sum {
t.Fatalf("Sum of %v should be %v; got %v", tc.name, tc.sum, s)
}
})
}
}
//////////////////////////////////
package sum_test
import (
"fmt"
"github.com/jakelacey2012/testing"
)
func ExampleInts() {
s := sum.Ints(1, 2, 3, 4, 5)
fmt.Println("Sum of one to five is", s)
// Output:
// Sum of one to five is 15
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment