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

Function TestBasicClassCreation

class_test.go:126–160  ·  view source on GitHub ↗

TestBasicClassCreation tests basic class creation and registration

(t *testing.T)

Source from the content-addressed store, hash-verified

124
125// TestBasicClassCreation tests basic class creation and registration
126func TestBasicClassCreation(t *testing.T) {
127 rt := NewRuntime()
128 defer rt.Close()
129
130 context := rt.NewContext()
131 defer context.Close()
132
133 // Create Point class
134 pointConstructor, _ := createPointClass(context)
135 if pointConstructor.IsException() {
136 defer pointConstructor.Free()
137 err := context.Exception()
138 t.Fatalf("Failed to create Point class: %v", err)
139 }
140
141 // Register Point class globally
142 // Note: Once set as global property, Globals will manage the memory automatically
143 context.Globals().Set("Point", pointConstructor)
144
145 // Test basic constructor call
146 result := context.Eval(`
147 let p = new Point(3, 4);
148 p.norm();
149 `)
150 defer result.Free()
151 if result.IsException() {
152 err := context.Exception()
153 t.Fatalf("Failed to evaluate basic constructor test: %v", err)
154 }
155
156 expected := 5.0 // sqrt(3^2 + 4^2) = 5
157 if math.Abs(result.ToFloat64()-expected) > 0.001 { // Updated: Use ToFloat64()
158 t.Errorf("Expected norm to be %f, got %f", expected, result.ToFloat64())
159 }
160}
161
162// TestConstructorFunctionality tests constructor with different parameter counts
163func TestConstructorFunctionality(t *testing.T) {

Callers

nothing calls this directly

Calls 12

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
ToFloat64Method · 0.80
FreeMethod · 0.45

Tested by

no test coverage detected