-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition_test.go
73 lines (56 loc) · 2.27 KB
/
condition_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
package lo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTernary(t *testing.T) {
is := assert.New(t)
result1 := Ternary(true, "a", "b")
result2 := Ternary(false, "a", "b")
is.Equal(result1, "a")
is.Equal(result2, "b")
}
func TestIfElse(t *testing.T) {
is := assert.New(t)
result1 := If(true, 1).ElseIf(false, 2).Else(3)
result2 := If(true, 1).ElseIf(true, 2).Else(3)
result3 := If(false, 1).ElseIf(true, 2).Else(3)
result4 := If(false, 1).ElseIf(false, 2).Else(3)
is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, 2)
is.Equal(result4, 3)
}
func TestIfFElseF(t *testing.T) {
is := assert.New(t)
result1 := IfF(true, func() int { return 1 }).ElseIfF(false, func() int { return 2 }).ElseF(func() int { return 3 })
result2 := IfF(true, func() int { return 1 }).ElseIfF(true, func() int { return 2 }).ElseF(func() int { return 3 })
result3 := IfF(false, func() int { return 1 }).ElseIfF(true, func() int { return 2 }).ElseF(func() int { return 3 })
result4 := IfF(false, func() int { return 1 }).ElseIfF(false, func() int { return 2 }).ElseF(func() int { return 3 })
is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, 2)
is.Equal(result4, 3)
}
func TestSwitchCase(t *testing.T) {
is := assert.New(t)
result1 := Switch[int, int](42).Case(42, 1).Case(1, 2).Default(3)
result2 := Switch[int, int](42).Case(42, 1).Case(42, 2).Default(3)
result3 := Switch[int, int](42).Case(1, 1).Case(42, 2).Default(3)
result4 := Switch[int, int](42).Case(1, 1).Case(1, 2).Default(3)
is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, 2)
is.Equal(result4, 3)
}
func TestSwitchCaseF(t *testing.T) {
is := assert.New(t)
result1 := Switch[int, int](42).CaseF(42, func() int { return 1 }).CaseF(1, func() int { return 2 }).DefaultF(func() int { return 3 })
result2 := Switch[int, int](42).CaseF(42, func() int { return 1 }).CaseF(42, func() int { return 2 }).DefaultF(func() int { return 3 })
result3 := Switch[int, int](42).CaseF(1, func() int { return 1 }).CaseF(42, func() int { return 2 }).DefaultF(func() int { return 3 })
result4 := Switch[int, int](42).CaseF(1, func() int { return 1 }).CaseF(1, func() int { return 2 }).DefaultF(func() int { return 3 })
is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, 2)
is.Equal(result4, 3)
}