-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult_test.go
51 lines (42 loc) · 1.12 KB
/
result_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
package routine
import (
"context"
"testing"
"github.com/aperturerobotics/util/promise"
)
// TestStateResultRoutine tests the state result routine functionality
func TestStateResultRoutine(t *testing.T) {
ctx := context.Background()
// Test successful case
sr, ctr := NewStateResultRoutine(func(ctx context.Context, st int) (string, error) {
return "value:" + string(rune(st+'0')), nil
})
// Set initial state and check result
if err := sr(ctx, 1); err != nil {
t.Fatal(err)
}
prom, _ := ctr.GetPromise()
res, err := prom.Await(ctx)
if err != nil {
t.Fatal(err)
}
if res != "value:1" {
t.Fatalf("expected value:1 got %v", res)
}
// Test with custom promise container
customCtr := promise.NewPromiseContainer[string]()
sr3 := NewStateResultRoutineWithPromiseContainer(func(ctx context.Context, st int) (string, error) {
return "custom:" + string(rune(st+'0')), nil
}, customCtr)
if err := sr3(ctx, 2); err != nil {
t.Fatal(err)
}
prom3, _ := customCtr.GetPromise()
res, err = prom3.Await(ctx)
if err != nil {
t.Fatal(err)
}
if res != "custom:2" {
t.Fatalf("expected custom:2 got %v", res)
}
}