MCPcopy Create free account
hub / github.com/buke/quickjs-go / TestModuleBuilder_ValueSpec

Function TestModuleBuilder_ValueSpec

module_test.go:73–400  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

71}
72
73func TestModuleBuilder_ValueSpec(t *testing.T) {
74 useStableOwnerHooksForLegacySubtests(t)
75
76 rt := NewRuntime(WithModuleImport(true))
77 defer rt.Close()
78 ctx := rt.NewContext()
79 defer ctx.Close()
80
81 findModuleBuilderSnapshot := func(moduleName string) (*ModuleBuilder, bool) {
82 var snapshot *ModuleBuilder
83 ctx.handleStore.handles.Range(func(_, value interface{}) bool {
84 h, ok := value.(cgo.Handle)
85 if !ok {
86 return true
87 }
88 mb, ok := h.Value().(*ModuleBuilder)
89 if ok && mb != nil && mb.name == moduleName {
90 snapshot = mb
91 return false
92 }
93 return true
94 })
95 return snapshot, snapshot != nil
96 }
97
98 t.Run("ExportLiteralAndMarshalSpec", func(t *testing.T) {
99 module := NewModuleBuilder("value-spec-literal").
100 ExportLiteral("num", int64(42)).
101 ExportLiteral("none", nil).
102 ExportValue("cfg", MarshalSpec{Value: map[string]interface{}{"name": "cfg"}})
103
104 err := module.Build(ctx)
105 require.NoError(t, err)
106
107 result := ctx.Eval(`
108 (async function() {
109 const { num, none, cfg } = await import('value-spec-literal');
110 return num + ':' + (none === null) + ':' + cfg.name;
111 })()
112 `, EvalAwait(true))
113 defer result.Free()
114 require.False(t, result.IsException())
115 require.Equal(t, "42:true:cfg", result.ToString())
116 })
117
118 t.Run("FactorySpec", func(t *testing.T) {
119 module := NewModuleBuilder("value-spec-factory").
120 ExportValue("msg", FactorySpec{Factory: func(ctx *Context) (*Value, error) {
121 return ctx.NewString("hello-factory"), nil
122 }})
123
124 err := module.Build(ctx)
125 require.NoError(t, err)
126
127 result := ctx.Eval(`
128 (async function() {
129 const { msg } = await import('value-spec-factory');
130 return msg;

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
EvalMethod · 0.95
NewStringMethod · 0.95
ExceptionMethod · 0.95
NewFunctionMethod · 0.95
NewUndefinedMethod · 0.95
RunGCMethod · 0.95
NewRuntimeFunction · 0.85
WithModuleImportFunction · 0.85

Tested by

no test coverage detected