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

Function TestPropertyVsAccessorBehavior

class_test.go:596–678  ·  view source on GitHub ↗

NEW: TestPropertyVsAccessorBehavior tests the behavioral differences between Properties and Accessors

(t *testing.T)

Source from the content-addressed store, hash-verified

594
595// NEW: TestPropertyVsAccessorBehavior tests the behavioral differences between Properties and Accessors
596func TestPropertyVsAccessorBehavior(t *testing.T) {
597 rt := NewRuntime()
598 defer rt.Close()
599
600 context := rt.NewContext()
601 defer context.Close()
602
603 // Create and register Point class
604 pointConstructor, _ := createPointClass(context)
605 if pointConstructor.IsException() {
606 defer pointConstructor.Free()
607 err := context.Exception()
608 t.Fatalf("Failed to create Point class: %v", err)
609 }
610
611 // Register Point class globally
612 context.Globals().Set("Point", pointConstructor)
613
614 // Test behavioral differences between properties and accessors
615 result := context.Eval(`
616 let p = new Point(5, 10);
617
618 // Test property behavior (direct data storage)
619 let originalVersion = p.version;
620 p.version = "2.0.0"; // Direct assignment to property
621 let newVersion = p.version;
622
623 // Test accessor behavior (function calls)
624 let originalX = p.x;
625 p.x = 15; // Calls setter function
626 let newX = p.x; // Calls getter function
627
628 // Test property descriptor differences
629 let versionDesc = Object.getOwnPropertyDescriptor(p, 'version');
630 let xDesc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(p), 'x');
631
632 [
633 originalVersion, // "1.0.0"
634 newVersion, // "2.0.0" (direct property assignment)
635 originalX, // 5 (from constructor)
636 newX, // 15 (from setter)
637 typeof versionDesc.value, // "string" (data property has value)
638 typeof versionDesc.get, // "undefined" (data property has no getter)
639 typeof xDesc.value, // "undefined" (accessor has no value)
640 typeof xDesc.get // "function" (accessor has getter)
641 ];
642 `)
643 defer result.Free()
644 if result.IsException() {
645 err := context.Exception()
646 t.Fatalf("Failed to evaluate property vs accessor behavior: %v", err)
647 }
648
649 // Check property behavior (direct data storage)
650 if result.GetIdx(0).ToString() != "1.0.0" { // Updated: Use ToString()
651 t.Errorf("Expected original version '1.0.0', got '%s'", result.GetIdx(0).ToString())
652 }
653 if result.GetIdx(1).ToString() != "2.0.0" { // Updated: Use ToString()

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
ExceptionMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
NewRuntimeFunction · 0.85
createPointClassFunction · 0.85
IsExceptionMethod · 0.80
SetMethod · 0.80
GetIdxMethod · 0.80
ToFloat64Method · 0.80

Tested by

no test coverage detected