3 Star 1 Fork 0

Gitee 极速下载/otto

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/robertkrimen/otto
克隆/下载
script_test.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
Steven Hartland 提交于 2024-04-13 17:05 . chore: update ci versions (#519)
package otto
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestScript(t *testing.T) {
tt(t, func() {
vm := New()
script, err := vm.Compile("xyzzy", `var abc; if (!abc) abc = 0; abc += 2; abc;`)
require.NoError(t, err)
str := script.String()
is(str, "// xyzzy\nvar abc; if (!abc) abc = 0; abc += 2; abc;")
val, err := vm.Run(script)
require.NoError(t, err)
is(val, 2)
// TODO(steve): Fix the underlying issues as to why this returns early.
if true {
return
}
tmp, err := script.marshalBinary()
require.NoError(t, err)
is(len(tmp), 1228)
{
script2 := &Script{}
err = script2.unmarshalBinary(tmp)
require.NoError(t, err)
is(script2.String(), str)
val, err = vm.Run(script2)
require.NoError(t, err)
is(val, 4)
tmp, err = script2.marshalBinary()
require.NoError(t, err)
is(len(tmp), 1228)
}
{
script2 := &Script{}
err = script2.unmarshalBinary(tmp)
require.NoError(t, err)
is(script2.String(), str)
val2, err2 := vm.Run(script2)
require.NoError(t, err2)
is(val2, 6)
tmp, err2 = script2.marshalBinary()
require.NoError(t, err2)
is(len(tmp), 1228)
}
{
version := scriptVersion
scriptVersion = "bogus"
script2 := &Script{}
err = script2.unmarshalBinary(tmp)
is(err, "version mismatch")
is(script2.String(), "// \n")
is(script2.version, "")
is(script2.program == nil, true)
is(script2.filename, "")
is(script2.src, "")
scriptVersion = version
}
})
}
func TestFunctionCall_CallerLocation(t *testing.T) {
tt(t, func() {
vm := New()
err := vm.Set("loc", func(call FunctionCall) Value {
return toValue(call.CallerLocation())
})
require.NoError(t, err)
script, err := vm.Compile("somefile.js", `var where = loc();`)
require.NoError(t, err)
_, err = vm.Run(script)
require.NoError(t, err)
where, err := vm.Get("where")
require.NoError(t, err)
is(where, "somefile.js:1:13")
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/otto.git
git@gitee.com:mirrors/otto.git
mirrors
otto
otto
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385