-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_test.go
65 lines (53 loc) · 1.18 KB
/
sub_test.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package assert
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// Basic assert example
func TestSub(t *testing.T) {
// act
result := Sub(4, 1)
// assert
assert.Equal(t, 3, result)
}
// Show the formatted message
func TestSub2(t *testing.T) {
// act
result := Sub(7, 2, 1)
// assert
assert.Equal(t, 4, result, "optional message %d != %d", result, 4)
}
// Show the wrapped test object syntax
func TestSub3(t *testing.T) {
// act
result := Sub(7, 9)
// assert
is := assert.New(t)
is.Equal(-2, result, "optional message %d != %d", result, -2)
}
// Using test tables is also possible
func TestSubTable(tt *testing.T) {
// arrange
type testCases struct {
n []int
expected int
}
tests := []testCases{
{n: []int{0, 0}, expected: 0},
{n: []int{2, 1}, expected: 1},
{n: []int{1, 2}, expected: -1},
{n: []int{4, 2, 1}, expected: 1},
}
for _, test := range tests {
testName := strings.Join(strings.Fields(fmt.Sprint(test.n)), " - ")
tt.Run(fmt.Sprintf("%s should equal %d", testName, test.expected), func(t *testing.T) {
// act
result := Sub(test.n...)
// assert
is := assert.New(t)
is.Equal(test.expected, result)
})
}
}