-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncify_test.go
78 lines (66 loc) · 1.84 KB
/
asyncify_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
package asyncify
import (
"errors"
"testing"
"time"
)
func TestPromise(t *testing.T) {
// Create a new promise
promise := Promise(func(resolve func(interface{}), reject func(error)) {
// Simulate an asynchronous operation
go func() {
// Resolve the promise with a value
resolve("hello")
}()
})
// Test the `Then` method
promise.Then(func(result interface{}) interface{} {
// Test that the result is correct
if result != "hello" {
t.Errorf("Expected result to be 'hello', but got '%v'", result)
}
// Return a new value
return "world"
}).Then(func(result interface{}) interface{} {
// Test that the result is correct
if result != "world" {
t.Errorf("Expected result to be 'world', but got '%v'", result)
}
// Return an error
return errors.New("oops")
}).Catch(func(err error) interface{} {
// Test that the error is correct
if err.Error() != "oops" {
t.Errorf("Expected error to be 'oops', but got '%v'", err.Error())
}
// Return a new value
return "caught"
}).Finally(func() {
// Test that the promise is fulfilled
if promise.state != fulfilled {
t.Error("Expected promise to be fulfilled")
}
// Close the test channel
close(promise.awaitChan)
})
// Wait for the promise to resolve
<-promise.awaitChan
}
func TestPromiseAwait(t *testing.T) {
// Create a new promise that resolves after 100 milliseconds
p := Promise(func(resolve func(interface{}), reject func(error)) {
time.Sleep(100 * time.Millisecond)
resolve("Hello, world!")
})
// Call Await on the promise and capture the result
result, err := p.Await()
// Check that there is no error
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
// Check that the result matches the expected value
expected := "Hello, world!"
if result != expected {
t.Errorf("Expected %v, but got: %v", expected, result)
}
}