1 Star 0 Fork 0

似是故人来/v8go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
object_template_test.go 3.38 KB
一键复制 编辑 原始数据 按行查看 历史
zhengkaikai 提交于 2022-09-29 23:55 . trace log
// 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 "gitee.com/hasika/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.NewContextWithOptions(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)
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.NewContextWithOptions(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.NewContextWithOptions(iso, tmpl)
ctx.Close()
iso.Dispose()
runtime.GC()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hasika/v8go.git
[email protected]:hasika/v8go.git
hasika
v8go
v8go
master

搜索帮助