-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_template_test.go
executable file
·158 lines (139 loc) · 3.35 KB
/
object_template_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2020 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 (
"math/big"
"runtime"
"testing"
v8 "github.com/nzhenev/v8go"
)
func TestObjectTemplate(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
obj := v8.NewObjectTemplate(iso)
setError := func(t *testing.T, err error) {
if err != nil {
t.Errorf("failed to set property: %v", err)
}
}
val, _ := v8.NewValue(iso, "bar")
objVal := v8.NewObjectTemplate(iso)
bigbigint, _ := new(big.Int).SetString("36893488147419099136", 10) // larger than a single word size (64bit)
bigbignegint, _ := new(big.Int).SetString("-36893488147419099136", 10)
tests := [...]struct {
name string
value interface{}
}{
{"str", "foo"},
{"i32", int32(1)},
{"u32", uint32(1)},
{"i64", int64(1)},
{"u64", uint64(1)},
{"float64", float64(1)},
{"bigint", big.NewInt(1)},
{"biguint", new(big.Int).SetUint64(1 << 63)},
{"bigbigint", bigbigint},
{"bigbignegint", bigbignegint},
{"bool", true},
{"val", val},
{"obj", objVal},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
setError(t, obj.Set(tt.name, tt.value, 0))
})
}
}
func TestObjectTemplate_panic_on_nil_isolate(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err == nil {
t.Error("expected panic")
}
}()
v8.NewObjectTemplate(nil)
}
func TestGlobalObjectTemplate(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
tests := [...]struct {
global func() *v8.ObjectTemplate
source string
validate func(t *testing.T, val *v8.Value)
}{
{
func() *v8.ObjectTemplate {
gbl := v8.NewObjectTemplate(iso)
gbl.Set("foo", "bar")
return gbl
},
"foo",
func(t *testing.T, val *v8.Value) {
if !val.IsString() {
t.Errorf("expect value %q to be of type String", val)
return
}
if val.String() != "bar" {
t.Errorf("unexpected value: %v", val)
}
},
},
{
func() *v8.ObjectTemplate {
foo := v8.NewObjectTemplate(iso)
foo.Set("bar", "baz")
gbl := v8.NewObjectTemplate(iso)
gbl.Set("foo", foo)
return gbl
},
"foo.bar",
func(t *testing.T, val *v8.Value) {
if val.String() != "baz" {
t.Errorf("unexpected value: %v", val)
}
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
ctx := v8.NewContext(iso, tt.global())
val, err := ctx.RunScript(tt.source, "test.js")
if err != nil {
t.Fatalf("unexpected error running script: %v", err)
}
tt.validate(t, val)
ctx.Close()
})
}
}
func TestObjectTemplateNewInstance(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
tmpl := v8.NewObjectTemplate(iso)
if _, err := tmpl.NewInstance(nil); err == nil {
t.Error("expected error but got <nil>")
}
tmpl.Set("foo", "bar")
ctx := v8.NewContext(iso)
defer ctx.Close()
obj, _ := tmpl.NewInstance(ctx)
if foo, _ := obj.Get("foo"); foo.String() != "bar" {
t.Errorf("unexpected value for object property: %v", foo)
}
}
func TestObjectTemplate_garbageCollection(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
tmpl := v8.NewObjectTemplate(iso)
tmpl.Set("foo", "bar")
ctx := v8.NewContext(iso, tmpl)
ctx.Close()
iso.Dispose()
runtime.GC()
}