Skip to content

Instantly share code, notes, and snippets.

@thatisuday
Last active May 11, 2019 08:14
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 thatisuday/433f342695946148926f0846ef692b02 to your computer and use it in GitHub Desktop.
Save thatisuday/433f342695946148926f0846ef692b02 to your computer and use it in GitHub Desktop.
package calc
import "testing"
// test case for Add function
func TestMathAdd( t *testing.T ) {
_, result := Add(1, 2, 3) // expected result 6
if result != 6 {
t.Errorf( "Add(1,2,3) FAILED, expected %v but got value %v", 6, result )
} else {
t.Logf( "Add(1,2,3) PASSED, expected %v and got value %v", 6, result )
}
}
package calc
import "testing"
// define input-result struct type
type TestDataItem struct {
inputs []int // inputs to `Add` function
result int // result of `Add` function
hasError bool // does `Add` function returns error
}
// test case for Add function
func TestMathAdd( t *testing.T ) {
// input-result data items
dataItems := []TestDataItem{
{ []int{1,2,3}, 6, false},
{ []int{99,99}, 198, false},
{ []int{1,1,5,5,6}, 18, false},
{ []int{1}, 0, true},
}
for _, item := range dataItems {
err, result := Add(item.inputs...) // get result of `Add` function
if item.hasError {
// expected an error
if err == nil {
t.Errorf( "Add() with args %v : FAILED, expected an error but got value '%v'", item.inputs, result )
} else {
t.Logf( "Add() with args %v : PASSED, expected an error and got an error '%v'", item.inputs, err )
}
} else {
// expected a value
if result != item.result {
t.Errorf( "Add() with args %v : FAILED, expected %v but got value '%v'", item.inputs, item.result, result )
} else {
t.Logf( "Add() with args %v : PASSED, expected %v and got value '%v'", item.inputs, item.result, result )
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment