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

Function TestReflectionBasicBinding

class_reflect_test.go:142–187  ·  view source on GitHub ↗

============================================================================= BASIC REFLECTION TESTS =============================================================================

(t *testing.T)

Source from the content-addressed store, hash-verified

140// =============================================================================
141
142func TestReflectionBasicBinding(t *testing.T) {
143 rt := NewRuntime()
144 defer rt.Close()
145 ctx := rt.NewContext()
146 defer ctx.Close()
147
148 // Test basic class binding
149 constructor, classID := ctx.BindClass(&Person{})
150 require.False(t, constructor.IsException())
151 require.NotEqual(t, uint32(0), classID)
152
153 ctx.Globals().Set("Person", constructor)
154
155 // Test instance creation and accessor access
156 result := ctx.Eval(`
157 let person = new Person();
158 person.firstName = "John";
159 person.lastName = "Doe";
160 person.age = 30;
161 person.salary = 50000.0;
162 person.isActive = true;
163
164 [
165 typeof person,
166 person.firstName,
167 person.lastName,
168 person.age,
169 person.salary,
170 person.isActive,
171 typeof person.secret, // Should be undefined (js:"-")
172 typeof person.private // Should be undefined (private field)
173 ];
174 `)
175 defer result.Free()
176
177 require.False(t, result.IsException())
178
179 require.Equal(t, "object", result.GetIdx(0).ToString())
180 require.Equal(t, "John", result.GetIdx(1).ToString())
181 require.Equal(t, "Doe", result.GetIdx(2).ToString())
182 require.Equal(t, int32(30), result.GetIdx(3).ToInt32())
183 require.Equal(t, 50000.0, result.GetIdx(4).ToFloat64())
184 require.True(t, result.GetIdx(5).ToBool())
185 require.Equal(t, "undefined", result.GetIdx(6).ToString()) // Changed: String() → ToString()
186 require.Equal(t, "undefined", result.GetIdx(7).ToString()) // Changed: String() → ToString()
187}
188
189func TestReflectionMethodCalls(t *testing.T) {
190 rt := NewRuntime()

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
BindClassMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
NewRuntimeFunction · 0.85
IsExceptionMethod · 0.80
SetMethod · 0.80
EqualMethod · 0.80
GetIdxMethod · 0.80
ToInt32Method · 0.80

Tested by

no test coverage detected