-
Notifications
You must be signed in to change notification settings - Fork 22
/
cor_test.go
124 lines (94 loc) · 2.39 KB
/
cor_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package fpgo
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func logMessage(args ...interface{}) {
enable := false
if enable {
fmt.Println(args...)
}
}
func TestCorYield(t *testing.T) {
var expectedInt, actualInt int
var testee *CorDef[interface{}]
var wg sync.WaitGroup
expectedInt = 5
actualInt = 0
wg.Add(1)
// Cor c1
var c1 *CorDef[interface{}]
c1 = Cor.New(func() {
self := c1
logMessage(self, "c1 effect")
initVal := self.YieldRef(nil)
logMessage(self, "c1 initVal", initVal)
v, _ := Maybe.Just(initVal).ToInt()
// v := 0
receive := self.YieldRef((v + 1))
logMessage(self, "c1 yield initVal+1 & receive", receive)
logMessage(self, "c1", self.YieldRef(nil))
})
// Testee
testee = Cor.New(func() {
self := testee
var v int
var m MaybeDef[interface{}]
logMessage(self, "cor", "initialized")
v, _ = Maybe.Just(self.YieldRef(nil)).ToInt()
actualInt = v + 1
v, _ = Maybe.Just(self.YieldFromIO(MonadIO.Just(1).ObserveOn(Handler.GetDefault()))).ToInt()
logMessage(self, "s", 5)
actualInt += v
logMessage(self, "s", 6)
logMessage(self, "c1", c1.IsDone())
logMessage(self, "c1", c1.IsStarted())
m = Maybe.Just(self.YieldFrom(c1, nil)).ToMaybe()
logMessage(self, "c1", c1.IsDone())
logMessage(m)
v, _ = m.ToInt()
actualInt += v
logMessage(self, "received", v)
wg.Done()
})
c1.StartWithVal(1)
testee.StartWithVal(1)
wg.Wait()
assert.Equal(t, expectedInt, actualInt)
}
func TestCorDoNotation(t *testing.T) {
var expectedInt int
var actual interface{}
expectedInt = 3
// Cor c1
var c1 *CorDef[interface{}]
c1 = Cor.NewAndStart(func() {
self := c1
val := self.YieldRef((1))
Maybe.Just(val).ToInt()
logMessage(self, "c1 val", val)
})
// Testee
actual = Cor.DoNotation(func(self *CorDef[interface{}]) interface{} {
logMessage(self, "Do Notation", "init")
var result int
v := 0
var m MaybeDef[interface{}]
result = v + 1
logMessage(self, "Do Notation", "v", v)
logMessage(self, "Do Notation", "result", result)
h := Handler.New()
defer h.Close()
v, _ = Maybe.Just(self.YieldFromIO(MonadIO.Just(1).ObserveOn(h))).ToInt()
result += v
logMessage(self, "Do Notation", "result", result)
m = Maybe.Just(self.YieldFrom(c1, nil)).ToMaybe()
v, _ = m.ToInt()
result += v
logMessage(self, "Do Notation", "result", result)
return (result)
})
assert.Equal(t, expectedInt, (actual))
}