forked from robfig/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_template_test.go
136 lines (123 loc) · 3.07 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
// 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"
"testing"
"rogchap.com/v8go"
)
func TestObjectTemplate(t *testing.T) {
t.Parallel()
_, err := v8go.NewObjectTemplate(nil)
if err == nil {
t.Fatal("expected error but got <nil>")
}
iso, _ := v8go.NewIsolate()
obj, err := v8go.NewObjectTemplate(iso)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
setError := func(t *testing.T, err error) {
if err != nil {
t.Errorf("failed to set property: %v", err)
}
}
val, _ := v8go.NewValue(iso, "bar")
objVal, _ := v8go.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) {
t.Parallel()
setError(t, obj.Set(tt.name, tt.value, 0))
})
}
}
func TestGlobalObjectTemplate(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
tests := [...]struct {
global func() *v8go.ObjectTemplate
source string
validate func(t *testing.T, val *v8go.Value)
}{
{
func() *v8go.ObjectTemplate {
gbl, _ := v8go.NewObjectTemplate(iso)
gbl.Set("foo", "bar")
return gbl
},
"foo",
func(t *testing.T, val *v8go.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() *v8go.ObjectTemplate {
foo, _ := v8go.NewObjectTemplate(iso)
foo.Set("bar", "baz")
gbl, _ := v8go.NewObjectTemplate(iso)
gbl.Set("foo", foo)
return gbl
},
"foo.bar",
func(t *testing.T, val *v8go.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) {
t.Parallel()
ctx, _ := v8go.NewContext(iso, tt.global())
val, err := ctx.RunScript(tt.source, "test.js")
if err != nil {
t.Fatalf("unexpected error runing script: %v", err)
}
tt.validate(t, val)
})
}
}
func TestObjectTemplateNewInstance(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
tmpl, _ := v8go.NewObjectTemplate(iso)
if _, err := tmpl.NewInstance(nil); err == nil {
t.Error("expected error but got <nil>")
}
tmpl.Set("foo", "bar")
ctx, _ := v8go.NewContext(iso)
obj, _ := tmpl.NewInstance(ctx)
if foo, _ := obj.Get("foo"); foo.String() != "bar" {
t.Errorf("unexpected value for object property: %v", foo)
}
}