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

Function TestUnifiedConstructorMapping

class_test.go:1597–1659  ·  view source on GitHub ↗

TestUnifiedConstructorMapping tests the unified constructor -> classID mapping

(t *testing.T)

Source from the content-addressed store, hash-verified

1595
1596// TestUnifiedConstructorMapping tests the unified constructor -> classID mapping
1597func TestUnifiedConstructorMapping(t *testing.T) {
1598 rt := NewRuntime()
1599 defer rt.Close()
1600 ctx := rt.NewContext()
1601 defer ctx.Close()
1602
1603 // Test manual class creation
1604 manualConstructor, manualClassID := createPointClass(ctx)
1605 if manualConstructor.IsException() {
1606 err := ctx.Exception()
1607 t.Fatalf("Failed to create manual Point class: %v", err)
1608 }
1609
1610 // Test reflection class creation
1611 reflectConstructor, reflectClassID := ctx.BindClass(&Point{})
1612 if reflectConstructor.IsException() {
1613 err := ctx.Exception()
1614 t.Fatalf("Failed to create reflection Point class: %v", err)
1615 }
1616
1617 // Verify both constructors are registered in the unified mapping
1618 manualRetrievedID, exists := getConstructorClassID(ctx, manualConstructor.ref)
1619 if !exists {
1620 t.Errorf("Manual constructor not found in unified mapping")
1621 }
1622 if manualRetrievedID != manualClassID {
1623 t.Errorf("Manual constructor classID mismatch: expected %d, got %d", manualClassID, manualRetrievedID)
1624 }
1625
1626 reflectRetrievedID, exists := getConstructorClassID(ctx, reflectConstructor.ref)
1627 if !exists {
1628 t.Errorf("Reflection constructor not found in unified mapping")
1629 }
1630 if reflectRetrievedID != reflectClassID {
1631 t.Errorf("Reflection constructor classID mismatch: expected %d, got %d", reflectClassID, reflectRetrievedID)
1632 }
1633
1634 // Test that both can create instances with the CallConstructor API
1635 ctx.Globals().Set("ManualPoint", manualConstructor)
1636 ctx.Globals().Set("ReflectPoint", reflectConstructor)
1637
1638 result := ctx.Eval(`
1639 let manual = new ManualPoint(1, 2);
1640 let reflect = new ReflectPoint();
1641 reflect.X = 3;
1642 reflect.Y = 4;
1643 [manual.x, manual.y, reflect.X, reflect.Y];
1644 `)
1645 defer result.Free()
1646 if result.IsException() {
1647 err := ctx.Exception()
1648 t.Fatalf("Failed to test unified mapping: %v", err)
1649 }
1650
1651 if result.GetIdx(0).ToFloat64() != 1.0 || result.GetIdx(1).ToFloat64() != 2.0 { // Updated: Use ToFloat64()
1652 t.Errorf("Manual constructor instance incorrect: got (%f, %f)",
1653 result.GetIdx(0).ToFloat64(), result.GetIdx(1).ToFloat64())
1654 }

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
ExceptionMethod · 0.95
BindClassMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
NewRuntimeFunction · 0.85
createPointClassFunction · 0.85
getConstructorClassIDFunction · 0.85
IsExceptionMethod · 0.80
SetMethod · 0.80

Tested by

no test coverage detected