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

Function TestModuleBuilder_Integration

module_test.go:540–641  ·  view source on GitHub ↗

============================================================================= INTEGRATION TESTS =============================================================================

(t *testing.T)

Source from the content-addressed store, hash-verified

538// =============================================================================
539
540func TestModuleBuilder_Integration(t *testing.T) {
541 useStableOwnerHooksForLegacySubtests(t)
542
543 rt := NewRuntime()
544 defer rt.Close()
545 ctx := rt.NewContext()
546 defer ctx.Close()
547
548 t.Run("MultipleModules", func(t *testing.T) {
549 // Create math module
550 addFunc := ctx.NewFunction(func(ctx *Context, this *Value, args []*Value) *Value {
551 if len(args) >= 2 {
552 return ctx.NewFloat64(args[0].ToFloat64() + args[1].ToFloat64())
553 }
554 return ctx.NewFloat64(0)
555 })
556
557 mathModule := NewModuleBuilder("math").
558 Export("add", addFunc).
559 Export("PI", ctx.NewFloat64(3.14159))
560
561 // Create utils module
562 utilsModule := NewModuleBuilder("utils").
563 Export("name", ctx.NewString("UtilsModule")).
564 Export("version", ctx.NewString("1.0.0"))
565
566 err := mathModule.Build(ctx)
567 require.NoError(t, err)
568
569 err = utilsModule.Build(ctx)
570 require.NoError(t, err)
571
572 // Test math module
573 mathResult := ctx.Eval(`
574 (async function() {
575 const { add, PI } = await import('math');
576 return add(PI, 1);
577 })()`, EvalAwait(true))
578 defer mathResult.Free()
579 require.False(t, mathResult.IsException())
580 require.InDelta(t, 4.14159, mathResult.ToFloat64(), 0.0001)
581
582 // Test utils module
583 utilsResult := ctx.Eval(`
584 (async function() {
585 const { name, version } = await import('utils');
586 return name + ' v' + version;
587 })()`, EvalAwait(true))
588 defer utilsResult.Free()
589 require.False(t, utilsResult.IsException())
590 require.Equal(t, "UtilsModule v1.0.0", utilsResult.ToString())
591 })
592
593 t.Run("ComplexModuleWithObjects", func(t *testing.T) {
594 config := ctx.NewObject()
595 config.Set("name", ctx.NewString("TestApp"))
596 config.Set("version", ctx.NewString("2.0.0"))
597 config.Set("debug", ctx.NewBool(true))

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
NewFunctionMethod · 0.95
NewFloat64Method · 0.95
NewStringMethod · 0.95
EvalMethod · 0.95
NewObjectMethod · 0.95
NewBoolMethod · 0.95
NewRuntimeFunction · 0.85
NewModuleBuilderFunction · 0.85

Tested by

no test coverage detected