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

Function TestValueBasics

value_test.go:17–61  ·  view source on GitHub ↗

TestValueBasics tests basic value creation and type checking

(t *testing.T)

Source from the content-addressed store, hash-verified

15
16// TestValueBasics tests basic value creation and type checking
17func TestValueBasics(t *testing.T) {
18 useStableOwnerHooksForLegacySubtests(t)
19
20 rt := NewRuntime()
21 defer rt.Close()
22 ctx := rt.NewContext()
23 defer ctx.Close()
24
25 // Test basic type creation and checking - Updated to use New* methods
26 testCases := []struct {
27 name string
28 createVal func() *Value // Changed to return pointer
29 checkFunc func(*Value) bool // Changed parameter to pointer
30 }{
31 {"Number", func() *Value { return ctx.NewInt32(42) }, func(v *Value) bool { return v.IsNumber() }},
32 {"String", func() *Value { return ctx.NewString("test") }, func(v *Value) bool { return v.IsString() }},
33 {"Boolean", func() *Value { return ctx.NewBool(true) }, func(v *Value) bool { return v.IsBool() }},
34 {"Null", func() *Value { return ctx.NewNull() }, func(v *Value) bool { return v.IsNull() }},
35 {"Undefined", func() *Value { return ctx.NewUndefined() }, func(v *Value) bool { return v.IsUndefined() }},
36 {"Uninitialized", func() *Value { return ctx.NewUninitialized() }, func(v *Value) bool { return v.IsUninitialized() }},
37 {"Object", func() *Value { return ctx.NewObject() }, func(v *Value) bool { return v.IsObject() }},
38 {"BigInt", func() *Value { return ctx.NewBigInt64(123456789) }, func(v *Value) bool { return v.IsBigInt() }},
39 }
40
41 for _, tc := range testCases {
42 t.Run(tc.name, func(t *testing.T) {
43 val := tc.createVal()
44 defer val.Free()
45 require.True(t, tc.checkFunc(val))
46 require.Equal(t, ctx, val.Context()) // Test Context() method
47 })
48 }
49
50 // Test JavaScript created values - FIXED: removed error handling
51 arr := ctx.Eval(`[1, 2, 3]`)
52 defer arr.Free()
53 require.False(t, arr.IsException()) // Check for exceptions instead of error
54 require.True(t, arr.IsArray())
55 require.True(t, arr.IsObject()) // Arrays are objects
56
57 sym := ctx.Eval(`Symbol('test')`)
58 defer sym.Free()
59 require.False(t, sym.IsException()) // Check for exceptions instead of error
60 require.True(t, sym.IsSymbol())
61}
62
63func TestValueComparisonAPIs(t *testing.T) {
64 useStableOwnerHooksForLegacySubtests(t)

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
NewInt32Method · 0.95
NewStringMethod · 0.95
NewBoolMethod · 0.95
NewNullMethod · 0.95
NewUndefinedMethod · 0.95
NewUninitializedMethod · 0.95
NewObjectMethod · 0.95
NewBigInt64Method · 0.95
EvalMethod · 0.95

Tested by

no test coverage detected