-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition_test.go
59 lines (45 loc) · 1.7 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
package lo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTernary(t *testing.T) {
is := assert.New(t)
result1 := Ternary[string](true, "a", "b")
result2 := Ternary[string](false, "a", "b")
is.Equal(result1, "a")
is.Equal(result2, "b")
}
func TestIfElse(t *testing.T) {
is := assert.New(t)
result1 := If[int](true, 1).ElseIf(false, 2).Else(3)
result2 := If[int](true, 1).ElseIf(true, 2).Else(3)
result3 := If[int](false, 1).ElseIf(true, 2).Else(3)
result4 := If[int](false, 1).ElseIf(false, 2).Else(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)
}