-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_test.go
88 lines (82 loc) · 2.05 KB
/
calc_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package calc_lib
import (
"fmt"
"testing"
)
func TestAddition_Calculate(t *testing.T) {
tests := []struct{ a, b, want int }{
{a: 0, b: 0, want: 0},
{a: 0, b: 1, want: 1},
{a: 1, b: 1, want: 2},
{a: 2, b: 3, want: 5},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d + %d = %d", tt.a, tt.b, tt.want), func(t *testing.T) {
addition := &Addition{}
got := addition.Calculate(tt.a, tt.b)
if got != tt.want {
t.Errorf("Calculate() = %v, want %v", got, tt.want)
}
})
}
}
func TestSubtraction_Calculate(t *testing.T) {
tests := []struct{ a, b, want int }{
{a: 0, b: 0, want: 0},
{a: 0, b: 1, want: -1},
{a: 1, b: 1, want: 0},
{a: 2, b: 3, want: -1},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d + %d = %d", tt.a, tt.b, tt.want), func(t *testing.T) {
operation := &Subtraction{}
got := operation.Calculate(tt.a, tt.b)
if got != tt.want {
t.Errorf("Calculate() = %v, want %v", got, tt.want)
}
})
}
}
func TestMultiplication_Calculate(t *testing.T) {
tests := []struct{ a, b, want int }{
{a: 0, b: 0, want: 0},
{a: 0, b: 1, want: 0},
{a: 1, b: 1, want: 1},
{a: 2, b: 3, want: 6},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d + %d = %d", tt.a, tt.b, tt.want), func(t *testing.T) {
operation := &Multiplication{}
got := operation.Calculate(tt.a, tt.b)
if got != tt.want {
t.Errorf("Calculate() = %v, want %v", got, tt.want)
}
})
}
}
func TestDivision_Calculate(t *testing.T) {
tests := []struct{ a, b, want int }{
{a: 0, b: 5, want: 0},
{a: 0, b: 1, want: 0},
{a: 1, b: 1, want: 1},
{a: 6, b: 3, want: 2},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d + %d = %d", tt.a, tt.b, tt.want), func(t *testing.T) {
operation := &Division{}
got := operation.Calculate(tt.a, tt.b)
if got != tt.want {
t.Errorf("Calculate() = %v, want %v", got, tt.want)
}
})
}
}
func TestDivision_ByZeroPanics(t *testing.T) {
division := &Division{}
defer func() {
if r := recover(); r == nil {
t.Fatalf("Division by zero did not panic")
}
}()
division.Calculate(0, 0)
}