forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_test.go
116 lines (101 loc) · 2.74 KB
/
object_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
// 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 (
"fmt"
"testing"
"rogchap.com/v8go"
)
func TestObjectSet(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext()
val, _ := ctx.RunScript("const foo = {}; foo", "")
obj, _ := val.AsObject()
obj.Set("bar", "baz")
baz, _ := ctx.RunScript("foo.bar", "")
if baz.String() != "baz" {
t.Errorf("unexpected value: %q", baz)
}
if err := obj.Set("", nil); err == nil {
t.Error("expected error but got <nil>")
}
if err := obj.Set("a", 0); err == nil {
t.Error("expected error but got <nil>")
}
obj.SetIdx(10, "ten")
if ten, _ := ctx.RunScript("foo[10]", ""); ten.String() != "ten" {
t.Errorf("unexpected value: %q", ten)
}
}
func TestObjectGet(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext()
val, _ := ctx.RunScript("const foo = { bar: 'baz'}; foo", "")
obj, _ := val.AsObject()
if bar, _ := obj.Get("bar"); bar.String() != "baz" {
t.Errorf("unexpected value: %q", bar)
}
if baz, _ := obj.Get("baz"); !baz.IsUndefined() {
t.Errorf("unexpected value: %q", baz)
}
ctx.RunScript("foo[5] = 5", "")
if five, _ := obj.GetIdx(5); five.Integer() != 5 {
t.Errorf("unexpected value: %q", five)
}
if u, _ := obj.GetIdx(55); !u.IsUndefined() {
t.Errorf("unexpected value: %q", u)
}
}
func TestObjectHas(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext()
val, _ := ctx.RunScript("const foo = {a: 1, '2': 2}; foo", "")
obj, _ := val.AsObject()
if !obj.Has("a") {
t.Error("expected true, got false")
}
if obj.Has("c") {
t.Error("expected false, got true")
}
if !obj.HasIdx(2) {
t.Error("expected true, got false")
}
if obj.HasIdx(1) {
t.Error("expected false, got true")
}
}
func TestObjectDelete(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext()
val, _ := ctx.RunScript("const foo = { bar: 'baz', '2': 2}; foo", "")
obj, _ := val.AsObject()
if !obj.Has("bar") {
t.Error("expected property to exist")
}
if !obj.Delete("bar") {
t.Error("expected delete to return true, got false")
}
if obj.Has("bar") {
t.Error("expected property to be deleted")
}
if !obj.DeleteIdx(2) {
t.Error("expected delete to return true, got false")
}
}
func ExampleObject_global() {
iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)
global := ctx.Global()
console, _ := v8go.NewObjectTemplate(iso)
logfn, _ := v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
fmt.Println(info.Args()[0])
return nil
})
console.Set("log", logfn)
consoleObj, _ := console.NewInstance(ctx)
global.Set("console", consoleObj)
ctx.RunScript("console.log('foo')", "")
// Output:
// foo
}