-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathfunctojs_test.go
More file actions
718 lines (607 loc) · 21 KB
/
Copy pathfunctojs_test.go
File metadata and controls
718 lines (607 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package qjs_test
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"unsafe"
"github.com/fastschema/qjs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Calculator struct {
Name string
}
func (c Calculator) Add(a, b int) int {
return a + b
}
func (c Calculator) Multiply(a, b float64) (float64, error) {
return a * b, nil
}
func (c Calculator) Sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
func (c Calculator) GetName() string {
return c.Name
}
func (c *Calculator) SetName(name string) error {
if name == "" {
return errors.New("name cannot be empty")
}
c.Name = name
return nil
}
func createAndRegisterJSFunc(t *testing.T, ctx *qjs.Context, name string, fn any) *qjs.Value {
jsFunc := must(qjs.FuncToJS(ctx, fn))
assert.True(t, jsFunc.IsFunction())
ctx.Global().SetPropertyStr(name, jsFunc)
return jsFunc
}
func TestBasicConversion(t *testing.T) {
_, ctx := setupTestContext(t)
t.Run("InvalidTypes", func(t *testing.T) {
_, err := qjs.FuncToJS(ctx, "not a function")
require.Error(t, err)
assert.Contains(t, err.Error(), "expected GO type function")
})
t.Run("FunctionTypes", func(t *testing.T) {
// Simple function
jsFunc := createAndRegisterJSFunc(t, ctx, "add", func(a, b int) int { return a + b })
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code("add(5, 3)")))
defer result.Free()
assert.Equal(t, int32(8), result.Int32())
// Function with error
jsFunc2 := createAndRegisterJSFunc(t, ctx, "divide", func(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
})
defer jsFunc2.Free()
result2 := must(ctx.Eval("test.js", qjs.Code("divide(10, 2)")))
defer result2.Free()
assert.InEpsilon(t, float64(5), result2.Float64(), 0.0001)
_, err := ctx.Eval("test.js", qjs.Code("divide(10, 0)"))
require.Error(t, err)
assert.Contains(t, err.Error(), "division by zero")
// Error-only return
jsFunc3 := createAndRegisterJSFunc(t, ctx, "errorOnly", func(shouldError bool) error {
if shouldError {
return errors.New("test error")
}
return nil
})
defer jsFunc3.Free()
result3 := must(ctx.Eval("test.js", qjs.Code("errorOnly(false)")))
defer result3.Free()
assert.True(t, result3.IsUndefined())
// No return value
jsFunc4 := createAndRegisterJSFunc(t, ctx, "print", func(msg string) { fmt.Printf("Message: %s\n", msg) })
defer jsFunc4.Free()
result4 := must(ctx.Eval("test.js", qjs.Code(`print("hello")`)))
defer result4.Free()
assert.True(t, result4.IsUndefined())
})
t.Run("NilHandling", func(t *testing.T) {
// Nil value
result := must(qjs.FuncToJS(ctx, nil))
defer result.Free()
assert.True(t, result.IsNull())
// Nil function
var nilFunc func()
jsFunc := must(qjs.FuncToJS(ctx, nilFunc))
defer jsFunc.Free()
assert.True(t, jsFunc.IsNull())
// Nil function pointer
var nilFuncPtr *func()
jsNilPtr := must(qjs.FuncToJS(ctx, nilFuncPtr))
defer jsNilPtr.Free()
assert.True(t, jsNilPtr.IsNull())
})
t.Run("FunctionPointer", func(t *testing.T) {
addFunc := func(a, b int) int { return a + b }
funcPtr := &addFunc
jsFunc := createAndRegisterJSFunc(t, ctx, "funcPtr", funcPtr)
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code("funcPtr(7, 8)")))
defer result.Free()
assert.Equal(t, int32(15), result.Int32())
})
}
func TestParameters(t *testing.T) {
_, ctx := setupTestContext(t)
t.Run("ParametersCountMismatch", func(t *testing.T) {
createAndRegisterJSFunc(t, ctx, "manyParamFunc", func(a, b, c, d, e int) int {
return a + b + c + d + e
})
result := must(ctx.Eval("test.js", qjs.Code(`manyParamFunc(1, 2, 3, 4, 5)`)))
defer result.Free()
assert.Equal(t, int32(15), result.Int32())
createAndRegisterJSFunc(t, ctx, "paramFunc", func(a, b, c int) int {
return a + b + c
})
result2 := must(ctx.Eval("test.js", qjs.Code(`paramFunc(10)`)))
defer result2.Free()
assert.Equal(t, int32(10), result2.Int32())
createAndRegisterJSFunc(t, ctx, "paramFunc2", func(a int) int {
return a * 2
})
result3 := must(ctx.Eval("test.js", qjs.Code(`paramFunc2(5, 10, 15)`)))
defer result3.Free()
assert.Equal(t, int32(10), result3.Int32())
})
t.Run("InvalidParameterTypes", func(t *testing.T) {
goFunc, err := qjs.FuncToJS(ctx, func(x int, y ...int) int {
return len(y) + 1
})
assert.NoError(t, err)
defer goFunc.Free()
ctx.Global().SetPropertyStr("testFunc", goFunc)
_, err = ctx.Eval("test.js", qjs.Code(`testFunc("invalid", 2)`))
require.Error(t, err)
})
t.Run("BasicParameterTypes", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "basicTypesFunc", func(s string, i int, f float64, b bool) string {
return fmt.Sprintf("string:%s,int:%d,float:%.1f,bool:%t", s, i, f, b)
})
result := must(ctx.Eval("test.js", qjs.Code(`basicTypesFunc("test", 42, 3.14, true)`)))
defer result.Free()
assert.Equal(t, "string:test,int:42,float:3.1,bool:true", result.String())
})
t.Run("ComplexParameterTypes", func(t *testing.T) {
t.Run("MapParameters", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "processData", func(data map[string]any) (string, error) {
if len(data) == 0 {
return "", errors.New("empty data")
}
return fmt.Sprintf("processed %d items, value=%v", len(data), data["value"]), nil
})
result := must(ctx.Eval("test.js", qjs.Code(`processData({name: "test", value: 42})`)))
defer result.Free()
assert.Equal(t, "processed 2 items, value=42", result.String())
_, err := ctx.Eval("test.js", qjs.Code(`processData({})`))
require.Error(t, err)
assert.Contains(t, err.Error(), "empty data")
})
t.Run("ArrayAndSliceParameters", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "arrayFunc", func(arr [3]int) string {
return fmt.Sprintf("array: %v", arr)
})
result := must(ctx.Eval("test.js", qjs.Code(`arrayFunc([1, 2, 3])`)))
defer result.Free()
assert.Contains(t, result.String(), "array:")
})
})
t.Run("PointerParameters", func(t *testing.T) {
t.Run("BasicPointerHandling", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "pointerFunc", func(ptr *int) string {
if ptr == nil {
return "nil"
}
return fmt.Sprintf("value: %d", *ptr)
})
result1 := must(ctx.Eval("test.js", qjs.Code(`pointerFunc(42)`)))
defer result1.Free()
assert.Equal(t, "value: 42", result1.String())
result2 := must(ctx.Eval("test.js", qjs.Code(`pointerFunc(null)`)))
defer result2.Free()
assert.Equal(t, "nil", result2.String())
result3 := must(ctx.Eval("test.js", qjs.Code(`pointerFunc(undefined)`)))
defer result3.Free()
assert.Equal(t, "nil", result3.String())
})
t.Run("VariousPointerTypes", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "ptrStringFunc", func(ptr *string) string {
if ptr == nil {
return "nil"
}
return fmt.Sprintf("value: %s", *ptr)
})
result4 := must(ctx.Eval("test.js", qjs.Code(`ptrStringFunc("hello")`)))
defer result4.Free()
assert.Equal(t, "value: hello", result4.String())
_ = createAndRegisterJSFunc(t, ctx, "ptrParamFunc", func(ptr *float64) string {
if ptr == nil {
return "nil"
}
return fmt.Sprintf("value: %.2f", *ptr)
})
result5 := must(ctx.Eval("test.js", qjs.Code(`ptrParamFunc(3.14159)`)))
defer result5.Free()
assert.Equal(t, "value: 3.14", result5.String())
})
t.Run("PointerConversionErrors", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "ptrFunc", func(ptr *string) string {
if ptr == nil {
return "nil"
}
return *ptr
})
_, err := ctx.Eval("test.js", qjs.Code(`ptrFunc(new Date())`))
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot convert JS function argument")
})
})
t.Run("InterfaceParameters", func(t *testing.T) {
t.Run("BasicInterfaceHandling", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "interfaceFunc", func(value any) string {
return fmt.Sprintf("%T: %v", value, value)
})
tests := []struct {
input string
contains string
}{
{`interfaceFunc(42)`, "42"},
{`interfaceFunc("hello")`, "hello"},
{`interfaceFunc(true)`, "true"},
{`interfaceFunc({a: 1, b: "test"})`, "map"},
}
for _, test := range tests {
result := must(ctx.Eval("test.js", qjs.Code(test.input)))
defer result.Free()
assert.Contains(t, result.String(), test.contains)
}
})
t.Run("InterfaceTypeDetection", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "anyFunc", func(value any) string {
return fmt.Sprintf("%T: %v", value, value)
})
// Test whole number conversion to int
result := must(ctx.Eval("test.js", qjs.Code(`anyFunc(42)`)))
defer result.Free()
assert.Contains(t, result.String(), "int64: 42")
// Test fractional number conversion to float64
result2 := must(ctx.Eval("test.js", qjs.Code(`anyFunc(3.14)`)))
defer result2.Free()
assert.Contains(t, result2.String(), "float64")
assert.Contains(t, result2.String(), "3.14")
})
})
t.Run("FunctionParameters", func(t *testing.T) {
t.Run("CallbackFunctions", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "higherOrderFunc", func(callback func(int) (int, error), value int) (int, error) {
return callback(value)
})
result := must(ctx.Eval("test.js", qjs.Code(`higherOrderFunc(x => x * 2, 21)`)))
defer result.Free()
assert.Equal(t, int32(42), result.Int32())
_, err := ctx.Eval("test.js", qjs.Code(`higherOrderFunc(x => { throw new Error("callback error"); }, 10)`))
require.Error(t, err)
assert.Contains(t, err.Error(), "callback error")
})
t.Run("FunctionConversionErrors", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "callbackTest", func(callback func() error) error {
return callback()
})
_, err2 := ctx.Eval("test.js", qjs.Code(`callbackTest(42)`))
require.Error(t, err2)
assert.Contains(t, err2.Error(), "cannot convert JS function argument")
})
})
t.Run("VariadicParameters", func(t *testing.T) {
t.Run("BasicVariadicFunction", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "sumFunc", func(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
})
result1 := must(ctx.Eval("test.js", qjs.Code("sumFunc(1, 2, 3, 4, 5)")))
defer result1.Free()
assert.Equal(t, int32(15), result1.Int32())
result2 := must(ctx.Eval("test.js", qjs.Code("sumFunc()")))
defer result2.Free()
assert.Equal(t, int32(0), result2.Int32())
result3 := must(ctx.Eval("test.js", qjs.Code("sumFunc(42)")))
defer result3.Free()
assert.Equal(t, int32(42), result3.Int32())
})
t.Run("MixedVariadicFunction", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "partialSumFunc", func(prefix string, nums ...int) string {
total := 0
for _, num := range nums {
total += num
}
return fmt.Sprintf("%s: %d", prefix, total)
})
result1 := must(ctx.Eval("test.js", qjs.Code(`partialSumFunc("Total", 1, 2, 3)`)))
defer result1.Free()
assert.Equal(t, "Total: 6", result1.String())
result2 := must(ctx.Eval("test.js", qjs.Code(`partialSumFunc("Total")`)))
defer result2.Free()
assert.Equal(t, "Total: 0", result2.String())
})
t.Run("VariadicWithAnyType", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "formatFunc", func(format string, args ...any) string {
return fmt.Sprintf(format, args...)
})
result := must(ctx.Eval("test.js", qjs.Code(`formatFunc("Hello %s, you are %d years old", "John", 25)`)))
defer result.Free()
assert.Equal(t, "Hello John, you are 25 years old", result.String())
})
t.Run("VariadicConversionErrors", func(t *testing.T) {
_ = createAndRegisterJSFunc(t, ctx, "variadicIntFunc", func(nums ...int8) string {
total := int8(0)
for _, num := range nums {
total += num
}
return fmt.Sprintf("total: %d", total)
})
_, err := ctx.Eval("test.js", qjs.Code(`variadicIntFunc(1, 2, 1000)`))
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot convert JS function argument")
})
})
}
func TestReturnValues(t *testing.T) {
_, ctx := setupTestContext(t)
t.Run("BasicReturnTypes", func(t *testing.T) {
t.Run("SingleReturn", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "nilFunc", func() any {
return nil
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`nilFunc()`)))
defer result.Free()
assert.True(t, result.IsNull())
})
t.Run("FunctionReturn", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "returningFunc", func() func(int) int {
return func(x int) int {
return x * 3
}
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`const func = returningFunc(); func(4)`)))
defer result.Free()
assert.Equal(t, int32(12), result.Int32())
})
})
t.Run("SingleReturnWithNilError", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "singleReturnNilError", func() (string, error) {
return "single", nil
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`singleReturnNilError()`)))
defer result.Free()
assert.Equal(t, "single", result.String())
})
t.Run("SingleReturnWithNonNilError", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "onlyNilError", func() error {
return nil
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`onlyNilError()`)))
defer result.Free()
assert.True(t, result.IsUndefined())
})
t.Run("TwoReturnsWithNilError", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "twoReturnsNilError", func() (string, int, error) {
return "success", 100, nil
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`twoReturnsNilError()`)))
defer result.Free()
assert.True(t, result.IsArray())
elem0 := result.GetPropertyIndex(0)
defer elem0.Free()
assert.Equal(t, "success", elem0.String())
elem1 := result.GetPropertyIndex(1)
defer elem1.Free()
assert.Equal(t, int32(100), elem1.Int32())
})
t.Run("MultipleNonErrorReturns", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "threeReturns", func() (string, int, bool) {
return "test", 123, true
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`threeReturns()`)))
defer result.Free()
assert.True(t, result.IsArray())
elem0 := result.GetPropertyIndex(0)
defer elem0.Free()
assert.Equal(t, "test", elem0.String())
elem1 := result.GetPropertyIndex(1)
defer elem1.Free()
assert.Equal(t, int32(123), elem1.Int32())
elem2 := result.GetPropertyIndex(2)
defer elem2.Free()
assert.Equal(t, true, elem2.Bool())
})
t.Run("FiveReturnsWithError", func(t *testing.T) {
jsFunc := createAndRegisterJSFunc(t, ctx, "fiveReturns", func() (int, string, bool, float64, error) {
return 1, "two", true, 4.5, nil
})
defer jsFunc.Free()
result := must(ctx.Eval("test.js", qjs.Code(`fiveReturns()`)))
defer result.Free()
assert.True(t, result.IsArray())
// Should return array with 4 elements (error is popped)
arrayLen := result.GetPropertyStr("length")
defer arrayLen.Free()
assert.Equal(t, int32(4), arrayLen.Int32())
elem0 := result.GetPropertyIndex(0)
defer elem0.Free()
assert.Equal(t, int32(1), elem0.Int32())
elem1 := result.GetPropertyIndex(1)
defer elem1.Free()
assert.Equal(t, "two", elem1.String())
elem2 := result.GetPropertyIndex(2)
defer elem2.Free()
assert.Equal(t, true, elem2.Bool())
elem3 := result.GetPropertyIndex(3)
defer elem3.Free()
assert.Equal(t, 4.5, elem3.Float64())
})
t.Run("UnsupportedReturnTypes", func(t *testing.T) {
t.Run("UnsafePointerReturnTypes", func(t *testing.T) {
_, err4 := qjs.FuncToJS(ctx, func() *unsafe.Pointer {
return nil
})
require.Error(t, err4)
assert.Contains(t, err4.Error(), "cannot convert Go func return 'unsafe.Pointer")
_, err5 := qjs.FuncToJS(ctx, func() **unsafe.Pointer {
return nil
})
require.Error(t, err5)
assert.Contains(t, err5.Error(), "cannot convert Go func return 'unsafe.Pointer")
})
t.Run("ArrayWithUnsupportedElements", func(t *testing.T) {
_, err3 := qjs.FuncToJS(ctx, func() [2]unsafe.Pointer {
return [2]unsafe.Pointer{}
})
require.Error(t, err3)
assert.Contains(t, err3.Error(), "cannot convert Go func return 'array: unsafe.Pointer' to JS")
})
t.Run("StructWithUnsupportedFields", func(t *testing.T) {
type StructWithUnsafePtr struct {
Name string
Ptr unsafe.Pointer
}
_, err2 := qjs.FuncToJS(ctx, func() StructWithUnsafePtr {
return StructWithUnsafePtr{Name: "test", Ptr: unsafe.Pointer(&[]byte{1}[0])}
})
require.Error(t, err2)
assert.Contains(t, err2.Error(), "cannot convert Go 'qjs_test.StructWithUnsafePtr.Ptr' to JS")
assert.Contains(t, err2.Error(), "cannot convert Go func return 'unsafe.Pointer")
// Nested validation
type StructWithNestedUnsafePtr struct {
Field struct {
PtrField unsafe.Pointer
}
}
_, err5 := qjs.FuncToJS(ctx, func() StructWithNestedUnsafePtr {
return StructWithNestedUnsafePtr{}
})
require.Error(t, err5)
assert.Contains(t, err5.Error(), "cannot convert Go 'qjs_test.StructWithNestedUnsafePtr.Field' to JS")
assert.Contains(t, err5.Error(), "cannot convert Go func return 'unsafe.Pointer")
})
t.Run("ComplexNestedValidation", func(t *testing.T) {
_, err := qjs.FuncToJS(ctx, func() map[string]unsafe.Pointer {
return nil
})
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot convert Go func return 'map value: unsafe.Pointer' to JS")
type StructWithUnsafeField struct {
Field unsafe.Pointer
}
_, err2 := qjs.FuncToJS(ctx, func() StructWithUnsafeField {
return StructWithUnsafeField{}
})
require.Error(t, err2)
assert.Contains(t, err2.Error(), "cannot convert Go 'qjs_test.StructWithUnsafeField.Field' to JS")
assert.Contains(t, err2.Error(), "cannot convert Go func return 'unsafe.Pointer'")
})
})
}
func TestMethodBinding(t *testing.T) {
_, ctx := setupTestContext(t)
t.Run("StructMethodBinding", func(t *testing.T) {
t.Run("ValueReceiverMethods", func(t *testing.T) {
calc := Calculator{Name: "MyCalculator"}
jsAddMethod := createAndRegisterJSFunc(t, ctx, "calcAdd", calc.Add)
defer jsAddMethod.Free()
result := must(ctx.Eval("test.js", qjs.Code("calcAdd(10, 20)")))
defer result.Free()
assert.Equal(t, int32(30), result.Int32())
jsMultiplyMethod := createAndRegisterJSFunc(t, ctx, "calcMultiply", calc.Multiply)
defer jsMultiplyMethod.Free()
result2 := must(ctx.Eval("test.js", qjs.Code("calcMultiply(3.5, 2.0)")))
defer result2.Free()
assert.Equal(t, float64(7), result2.Float64())
jsSumMethod := createAndRegisterJSFunc(t, ctx, "calcSum", calc.Sum)
defer jsSumMethod.Free()
result3 := must(ctx.Eval("test.js", qjs.Code("calcSum(1, 2, 3)")))
defer result3.Free()
assert.Equal(t, int32(6), result3.Int32())
})
t.Run("PointerReceiverMethods", func(t *testing.T) {
calc := &Calculator{Name: "TestCalc"}
jsSetNameMethod := createAndRegisterJSFunc(t, ctx, "setName", calc.SetName)
defer jsSetNameMethod.Free()
result := must(ctx.Eval("test.js", qjs.Code(`setName("NewName")`)))
defer result.Free()
assert.True(t, result.IsUndefined() || result.IsNull())
assert.Equal(t, "NewName", calc.Name)
_, err := ctx.Eval("test.js", qjs.Code(`setName("")`))
require.Error(t, err)
assert.Contains(t, err.Error(), "name cannot be empty")
})
})
}
func TestCreateNonNilSample(t *testing.T) {
tests := []struct {
name string
input any
expectedType any
shouldBeNotNil bool
testFuncExecution bool
}{
{
name: "Context type",
input: context.Background(),
expectedType: context.Background(),
shouldBeNotNil: true,
},
{
name: "Pointer type",
input: (*int)(nil),
expectedType: (*int)(nil),
shouldBeNotNil: false, // Pointer itself can be nil but sample won't be
},
{
name: "Array type",
input: [3]string{},
expectedType: [3]string{},
shouldBeNotNil: true,
},
{
name: "Slice type",
input: []string{},
expectedType: []string{},
shouldBeNotNil: true,
},
{
name: "Map type",
input: map[string]int{},
expectedType: map[string]int{},
shouldBeNotNil: true,
},
{
name: "Chan type",
input: (chan int)(nil),
expectedType: (chan int)(nil),
shouldBeNotNil: true,
},
{
name: "Func type",
input: (func(int) int)(nil),
expectedType: (func(int) int)(nil),
shouldBeNotNil: true,
testFuncExecution: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sample := qjs.CreateNonNilSample(reflect.TypeOf(tt.input))
assert.IsType(t, tt.expectedType, sample)
if tt.shouldBeNotNil {
assert.NotNil(t, sample)
}
// Special test for function execution
if tt.testFuncExecution {
dummyFunc := sample.(func(int) int)
result := dummyFunc(42)
assert.Equal(t, 0, result) // Should return zero value
}
})
}
}