改完之后「某个行为在哪测」就是查同名文件:
lazyglobal_test.go → engine_globals_test.go
static_test.go → script_static_test.go
scope_pool_test.go → script_vm_test.go 它测的是 VM 池,不是作用域
esmwrap_test.go → bundle_finalize_test.go
missing_global_test.go → errors_hints_test.go
entry_test.go → bundle_entry_test.go 并收下 bundle_test.go 里
那三个 TestEntry_*
删掉 5 组复制粘贴。其中 TestNew_同实例调用串行 和 TestInstance_调用串行 **函数体
完全一致**,只差 mustCompile 的文件名参数 counter.js / counter.ts。Instance 的
测试全部归 instance_test.go 并统一前缀成 TestInstance_,scope_test.go 只留作用域。
vmstate_test.go + govalue_test.go → goja_facts_test.go。这两个测的都是 **goja
本身**,不是这个库:Program 能不能给多个 Runtime 共用、顶层变量跟不跟 Runtime 走、
Go 的切片进到脚本里是不是真数组。它们是「为什么这个库要这么设计」的实验记录,
goja 行为变了会先红。原来那个名字看不出这层定位。
helper 集中到 helper_test.go:newEngine / mustCompile 被十几个文件依赖,原来住在
script_test.go 里,那个名字暗示「测 Script」,找不到。
caller_example_test.go 并进 caller_test.go——同一个类型没必要两个文件。
overlay_test.go 和 script_vm_test.go 保持内部测试,各写了一句为什么:前者要读
Engine.loader 和 isPrepared,后者要直接断言 vmHandle.scoped。我试过把 overlay
挪成外部测试,挪不动。
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package jscriptx_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.fsdpf.net/go/jscriptx"
|
|
)
|
|
|
|
// 各个测试文件共用的脚手架。放一起是因为它们被十几个文件依赖——
|
|
// 原来住在 script_test.go 里,那个名字暗示「测 Script」,找不到。
|
|
|
|
// newEngine 造一个测试用引擎,默认不打日志(避免测试输出被 console 刷屏)。
|
|
func newEngine(t *testing.T, opts ...jscriptx.Option) *jscriptx.Engine {
|
|
t.Helper()
|
|
opts = append([]jscriptx.Option{jscriptx.WithLogger(nil)}, opts...)
|
|
e, err := jscriptx.New(opts...)
|
|
if err != nil {
|
|
t.Fatalf("New 失败: %v", err)
|
|
}
|
|
t.Cleanup(e.Close)
|
|
return e
|
|
}
|
|
|
|
func mustCompile(t *testing.T, e *jscriptx.Engine, name, src string) *jscriptx.Script {
|
|
t.Helper()
|
|
s, err := e.Compile(name, src)
|
|
if err != nil {
|
|
t.Fatalf("编译 %s 失败: %v", name, err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// toNumber 把脚本返回的数字统一成 float64:goja 会按数值是否为整数
|
|
// 导出成 int64 或 float64,测试里不必纠结这个差异。
|
|
func toNumber(t *testing.T, v any) float64 {
|
|
t.Helper()
|
|
switch n := v.(type) {
|
|
case int64:
|
|
return float64(n)
|
|
case float64:
|
|
return n
|
|
default:
|
|
t.Fatalf("不是数字: %#v", v)
|
|
return 0
|
|
}
|
|
}
|