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

Function TestContextBasics

context_test.go:24–89  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22}
23
24func TestContextBasics(t *testing.T) {
25 newTestContext := func(t *testing.T) *Context {
26 rt := NewRuntime()
27 ctx := rt.NewContext()
28 require.NotNil(t, ctx)
29 t.Cleanup(func() {
30 ctx.Close()
31 rt.Close()
32 })
33 return ctx
34 }
35
36 // Test Runtime() method
37 ctx := newTestContext(t)
38 require.NotNil(t, ctx.Runtime())
39
40 // Test basic value creation
41 t.Run("ValueCreation", func(t *testing.T) {
42 ctx := newTestContext(t)
43 values := []struct {
44 name string
45 createVal func() *Value // Changed to return pointer
46 checkFunc func(*Value) bool // Changed parameter to pointer
47 }{
48 {"Null", func() *Value { return ctx.NewNull() }, func(v *Value) bool { return v.IsNull() }},
49 {"Undefined", func() *Value { return ctx.NewUndefined() }, func(v *Value) bool { return v.IsUndefined() }},
50 {"Uninitialized", func() *Value { return ctx.NewUninitialized() }, func(v *Value) bool { return v.IsUninitialized() }},
51 {"Bool", func() *Value { return ctx.NewBool(true) }, func(v *Value) bool { return v.IsBool() }},
52 {"Int32", func() *Value { return ctx.NewInt32(-42) }, func(v *Value) bool { return v.IsNumber() }},
53 {"Int64", func() *Value { return ctx.NewInt64(1234567890) }, func(v *Value) bool { return v.IsNumber() }},
54 {"Uint32", func() *Value { return ctx.NewUint32(42) }, func(v *Value) bool { return v.IsNumber() }},
55 {"Uint64", func() *Value { return ctx.NewUint64(uint64(1) << 32) }, func(v *Value) bool { return v.IsNumber() && v.ToFloat64() == float64(uint64(1)<<32) }},
56 {"BigInt64", func() *Value { return ctx.NewBigInt64(9223372036854775807) }, func(v *Value) bool { return v.IsBigInt() }},
57 {"BigUint64", func() *Value { return ctx.NewBigUint64(18446744073709551615) }, func(v *Value) bool { return v.IsBigInt() }},
58 {"Float64", func() *Value { return ctx.NewFloat64(3.14159) }, func(v *Value) bool { return v.IsNumber() }},
59 {"String", func() *Value { return ctx.NewString("test") }, func(v *Value) bool { return v.IsString() }},
60 {"Object", func() *Value { return ctx.NewObject() }, func(v *Value) bool { return v.IsObject() }},
61 }
62
63 for _, tc := range values {
64 val := tc.createVal()
65 require.NotNil(t, val, tc.name)
66 defer val.Free()
67 require.True(t, tc.checkFunc(val), tc.name)
68 }
69 })
70
71 // Test ArrayBuffer with different data sizes
72 t.Run("ArrayBuffer", func(t *testing.T) {
73 ctx := newTestContext(t)
74 testCases := [][]byte{
75 {1, 2, 3, 4, 5},
76 {},
77 nil,
78 }
79
80 for i, data := range testCases {
81 ab := ctx.NewArrayBuffer(data)

Callers

nothing calls this directly

Calls 15

NewContextMethod · 0.95
CloseMethod · 0.95
CloseMethod · 0.95
RuntimeMethod · 0.95
NewNullMethod · 0.95
NewUndefinedMethod · 0.95
NewUninitializedMethod · 0.95
NewBoolMethod · 0.95
NewInt32Method · 0.95
NewInt64Method · 0.95
NewUint32Method · 0.95
NewUint64Method · 0.95

Tested by

no test coverage detected