-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshort_test.go
More file actions
68 lines (58 loc) · 1.55 KB
/
short_test.go
File metadata and controls
68 lines (58 loc) · 1.55 KB
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
package x_test
import (
"fmt"
"testing"
. "github.com/jad21/x"
)
func TestIf(t *testing.T) {
if got := If(true, "yes", "no"); got != "yes" {
t.Errorf("If(true, 'yes', 'no') = %v; want 'yes'", got)
}
if got := If(false, "yes", "no"); got != "no" {
t.Errorf("If(false, 'yes', 'no') = %v; want 'no'", got)
}
if got := If(true, 1, 2); got != 1 {
t.Errorf("If(true, 1, 2) = %v; want 1", got)
}
if got := If(false, 1, 2); got != 2 {
t.Errorf("If(false, 1, 2) = %v; want 2", got)
}
}
func TestIfFn(t *testing.T) {
var str *string
defaultFn := func() string { return "no" }
yesFn := func() string { return *str }
if got := IfFn(str != nil, yesFn, defaultFn); got != "no" {
t.Errorf("If(true, nil, 'no') = %v; want 'no'", got)
}
yes := "yes"
str = &yes
if got := IfFn(str != nil, yesFn, defaultFn); got != "yes" {
t.Errorf("If(false, *'yes', 'no') = %v; want 'yes'", got)
}
}
func TestSwitchMap(t *testing.T) {
cases := map[int]string{
1: "one",
2: "two",
3: "three",
}
tests := []struct {
key int
expected string
defaultValue string
}{
{key: 1, expected: "one", defaultValue: "default"},
{key: 2, expected: "two", defaultValue: "default"},
{key: 3, expected: "three", defaultValue: "default"},
{key: 4, expected: "default", defaultValue: "default"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("key=%d", tt.key), func(t *testing.T) {
result := SwitchMap(tt.key, cases, tt.defaultValue)
if result != tt.expected {
t.Errorf("SwitchMap(%d) = %v; want %v", tt.key, result, tt.expected)
}
})
}
}