forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise_test.go
135 lines (113 loc) · 3.36 KB
/
promise_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
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"testing"
v8 "rogchap.com/v8go"
)
func TestPromiseFulfilled(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
if _, err := v8.NewPromiseResolver(nil); err == nil {
t.Error("expected error with <nil> Context")
}
res1, _ := v8.NewPromiseResolver(ctx)
prom1 := res1.GetPromise()
if s := prom1.State(); s != v8.Pending {
t.Errorf("unexpected state for Promise, want Pending (0) got: %v", s)
}
var thenInfo *v8.FunctionCallbackInfo
prom1thenVal := prom1.Then(func(info *v8.FunctionCallbackInfo) *v8.Value {
thenInfo = info
return nil
})
prom1then, _ := prom1thenVal.AsPromise()
if prom1then.State() != v8.Pending {
t.Errorf("unexpected state for dependent Promise, want Pending got: %v", prom1then.State())
}
if thenInfo != nil {
t.Error("unexpected call of Then prior to resolving the promise")
}
val1, _ := v8.NewValue(iso, "foo")
res1.Resolve(val1)
if s := prom1.State(); s != v8.Fulfilled {
t.Fatalf("unexpected state for Promise, want Fulfilled (1) got: %v", s)
}
if result := prom1.Result(); result.String() != val1.String() {
t.Errorf("expected the Promise result to match the resolve value, but got: %s", result)
}
if thenInfo == nil {
t.Errorf("expected Then to be called, was not")
}
if len(thenInfo.Args()) != 1 || thenInfo.Args()[0].String() != "foo" {
t.Errorf("expected promise to be called with [foo] args, was: %+v", thenInfo.Args())
}
}
func TestPromiseRejected(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
res2, _ := v8.NewPromiseResolver(ctx)
val2, _ := v8.NewValue(iso, "Bad Foo")
res2.Reject(val2)
prom2 := res2.GetPromise()
if s := prom2.State(); s != v8.Rejected {
t.Fatalf("unexpected state for Promise, want Rejected (2) got: %v", s)
}
var thenInfo *v8.FunctionCallbackInfo
var then2Fulfilled, then2Rejected bool
prom2.
Catch(func(info *v8.FunctionCallbackInfo) *v8.Value {
thenInfo = info
return nil
}).
Then(
func(_ *v8.FunctionCallbackInfo) *v8.Value {
then2Fulfilled = true
return nil
},
func(_ *v8.FunctionCallbackInfo) *v8.Value {
then2Rejected = true
return nil
},
)
ctx.PerformMicrotaskCheckpoint()
if thenInfo == nil {
t.Fatalf("expected Then to be called on already-resolved promise, but was not")
}
if len(thenInfo.Args()) != 1 || thenInfo.Args()[0].String() != val2.String() {
t.Fatalf("expected [%v], was: %+v", val2, thenInfo.Args())
}
if !then2Fulfilled {
t.Fatalf("expected call to onFulfilled, got none")
}
if then2Rejected {
t.Fatalf("unexpectedly called onRejected")
}
}
func TestPromiseThenPanic(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
res, _ := v8.NewPromiseResolver(ctx)
prom := res.GetPromise()
t.Run("no callbacks", func(t *testing.T) {
defer func() { recover() }()
prom.Then()
t.Errorf("expected a panic")
})
t.Run("3 callbacks", func(t *testing.T) {
defer func() { recover() }()
fn := func(_ *v8.FunctionCallbackInfo) *v8.Value { return nil }
prom.Then(fn, fn, fn)
t.Errorf("expected a panic")
})
}