-
Notifications
You must be signed in to change notification settings - Fork 0
/
adt_test.go
52 lines (38 loc) · 1.38 KB
/
adt_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
package adt_test
import (
"adtExample/adt"
"adtExample/lib"
"github.com/stretchr/testify/require"
"testing"
)
func TestExecutingWithTwoTypes(t *testing.T) {
typeACalled := false
typeBCalled := false
do := adt.UnionExecutor().
WithTypeA(func(typeA lib.TypeA) { typeACalled = true }).
WithTypeB(func(typeB lib.TypeB) { typeBCalled = true })
do.Exec(adt.UnionOf(lib.TypeA{}))
require.Equal(t, typeACalled, true)
require.Equal(t, typeBCalled, false)
typeACalled = false
typeBCalled = false
do.Exec(adt.UnionOf(lib.TypeB{}))
require.Equal(t, typeACalled, false)
require.Equal(t, typeBCalled, true)
}
func TestMapWithTwoTypes(t *testing.T) {
stringMapper := adt.UnionMapper[string]().
WithTypeA(func(typeA lib.TypeA) string { return "type-a" }).
WithTypeB(func(typeB lib.TypeB) string { return "type-b" })
typeAMappedString := stringMapper.Map(adt.UnionOf(lib.TypeA{}))
typeBMappedString := stringMapper.Map(adt.UnionOf(lib.TypeB{}))
require.Equal(t, "type-a", typeAMappedString)
require.Equal(t, "type-b", typeBMappedString)
intMapper := adt.UnionMapper[int]().
WithTypeA(func(typeA lib.TypeA) int { return 1000 }).
WithTypeB(func(typeB lib.TypeB) int { return 2000 })
typeAMappedInt := intMapper.Map(adt.UnionOf(lib.TypeA{}))
typeBMappedInt := intMapper.Map(adt.UnionOf(lib.TypeB{}))
require.Equal(t, 1000, typeAMappedInt)
require.Equal(t, 2000, typeBMappedInt)
}