-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCond_test.go
48 lines (45 loc) · 1.22 KB
/
Cond_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
package gohelper
import (
"fmt"
"testing"
)
func TestIf(t *testing.T) {
var tests = []struct {
expr bool
succ any
fail any
out any
}{
{1 == 1, true, false, true},
{2 == 2, true, false, true},
{true, "ok", "no", "ok"},
{0 > -1, 123, 456, 123},
{1 != 1, "equal", "not equal", "not equal"},
{3.14 == 3.14, true, false, true},
{-3.14 != -3.14, true, false, false},
{"🐶" == "🐶", true, false, true},
}
for _, tt := range tests {
got := If(tt.expr, tt.succ, tt.fail)
switch got.(type) {
case bool:
if got.(bool) != tt.out {
t.Errorf("If(%v, %v, %v) = %v, want: %v", tt.expr, tt.succ, tt.fail, got.(bool), tt.out)
}
case string:
if got.(string) != tt.out {
t.Errorf("If(%v, %v, %v) = %v, want: %v", tt.expr, tt.succ, tt.fail, got.(string), tt.out)
}
case int, int8, int16, int32, int64:
if fmt.Sprintf("%d", got) != fmt.Sprintf("%d", tt.out) {
t.Errorf("If(%v, %v, %v) = %v, want: %v", tt.expr, tt.succ, tt.fail, got, tt.out)
}
case uint, uint8, uint16, uint32, uint64:
if fmt.Sprintf("%d", got) != fmt.Sprintf("%d", tt.out) {
t.Errorf("If(%v, %v, %v) = %v, want: %v", tt.expr, tt.succ, tt.fail, got, tt.out)
}
default:
t.Error("unsupported type")
}
}
}