forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
value_test.go
735 lines (667 loc) · 17.7 KB
/
value_test.go
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// Copyright 2019 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"bytes"
"fmt"
"math"
"math/big"
"reflect"
"runtime"
"testing"
v8 "rogchap.com/v8go"
)
func TestValueNewBaseCases(t *testing.T) {
t.Parallel()
if _, err := v8.NewValue(nil, ""); err == nil {
t.Error("expected error, but got <nil>")
}
iso := v8.NewIsolate()
defer iso.Dispose()
if _, err := v8.NewValue(iso, nil); err == nil {
t.Error("expected error, but got <nil>")
}
if _, err := v8.NewValue(iso, struct{}{}); err == nil {
t.Error("expected error, but got <nil>")
}
}
func TestValueNewUint8Array(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
iso := ctx.Isolate()
in := []uint8{1, 2, 3, 4, 5}
if val, err := v8.NewValue(iso, in); err != nil {
t.Fatalf("Error %v", err)
} else if !val.IsUint8Array() {
t.Errorf("Val is not []uint")
} else {
out := val.Uint8Array()
if len(out) != 5 {
t.Errorf("Expected array length 5, got %d", len(out))
}
for i := 0; i < 5; i++ {
if out[i] != in[i] {
t.Errorf("Wrong byte at %d", i)
}
}
}
}
func TestValueFormatting(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
defaultVerb string
defaultVerbFlag string
stringVerb string
quoteVerb string
}{
{"new Object()", "[object Object]", "#<Object>", "[object Object]", `"[object Object]"`},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
if s := fmt.Sprintf("%v", val); s != tt.defaultVerb {
t.Errorf("incorrect format for %%v: %s", s)
}
if s := fmt.Sprintf("%+v", val); s != tt.defaultVerbFlag {
t.Errorf("incorrect format for %%+v: %s", s)
}
if s := fmt.Sprintf("%s", val); s != tt.stringVerb {
t.Errorf("incorrect format for %%s: %s", s)
}
if s := fmt.Sprintf("%q", val); s != tt.quoteVerb {
t.Errorf("incorrect format for %%q: %s", s)
}
})
}
}
func TestValueString(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
name string
source string
out string
}{
{"Number", `13 * 2`, "26"},
{"String", `"string"`, "string"},
{"String with null character and non-latin unicode", `"a\x00Ω"`, "a\x00Ω"},
{"Object", `let obj = {}; obj`, "[object Object]"},
{"Function", `let fn = function(){}; fn`, "function(){}"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
result, _ := ctx.RunScript(tt.source, "test.js")
str := result.String()
if str != tt.out {
t.Errorf("unexpected result: expected %q, got %q", tt.out, str)
}
})
}
}
func TestNewValue(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
iso := ctx.Isolate()
defer iso.Dispose()
defer ctx.Close()
tests := []struct {
name string
input interface{}
predicate string
}{
{"string", "s\x00s\x00", `str => str === "s\x00s\x00"`},
{"int32", int32(36), `int => int === 36`},
{"bool", true, `b => b === true`},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
val, err := ctx.RunScript(tt.predicate, "test.js")
if err != nil {
t.Fatal(err)
}
fn, err := val.AsFunction()
if err != nil {
t.Fatal(err)
}
jsVal, err := v8.NewValue(iso, tt.input)
if err != nil {
t.Fatal(err)
}
result, err := fn.Call(ctx.Global(), jsVal)
if err != nil {
t.Fatal(err)
}
if !result.Boolean() {
t.Fatal("unexpected result: expected true, got false")
}
})
}
}
func TestValueDetailString(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
name string
source string
out string
}{
{"Number", `13 * 2`, "26"},
{"String", `"a string"`, "a string"},
{"Object", `let obj = {}; obj`, "#<Object>"},
{"Function", `let fn = function(){}; fn`, "function(){}"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
result, _ := ctx.RunScript(tt.source, "test.js")
str := result.DetailString()
if str != tt.out {
t.Errorf("unexpected result: expected %q, got %q", tt.out, str)
}
})
}
}
func TestValueBoolean(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
out bool
}{
{"true", true},
{"false", false},
{"1", true},
{"0", false},
{"null", false},
{"undefined", false},
{"''", false},
{"'foo'", true},
{"() => {}", true},
{"{}", false},
{"{foo:'bar'}", true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
if b := val.Boolean(); b != tt.out {
t.Errorf("unexpected value: expected %v, got %v", tt.out, b)
}
})
}
}
func TestValueConstants(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
tests := [...]struct {
source string
value *v8.Value
same bool
}{
{"undefined", v8.Undefined(iso), true},
{"null", v8.Null(iso), true},
{"undefined", v8.Null(iso), false},
}
for _, tt := range tests {
tt := tt
val, err := ctx.RunScript(tt.source, "test.js")
fatalIf(t, err)
if tt.value.SameValue(val) != tt.same {
t.Errorf("SameValue on JS `%s` and V8 value %+v didn't return %v",
tt.source, tt.value, tt.same)
}
}
}
func TestValueArrayIndex(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
idx uint32
ok bool
}{
{"1", 1, true},
{"0", 0, true},
{"-1", 0, false},
{"'1'", 1, true},
{"'-1'", 0, false},
{"'a'", 0, false},
{"[1]", 1, true},
{"['1']", 1, true},
{"[1, 1]", 0, false},
{"{}", 0, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
idx, ok := val.ArrayIndex()
if ok != tt.ok {
t.Errorf("unexpected ok: expected %v, got %v", tt.ok, ok)
}
if idx != tt.idx {
t.Errorf("unexpected array index: expected %v, got %v", tt.idx, idx)
}
})
}
}
func TestValueInt32(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
expected int32
}{
{"0", 0},
{"1", 1},
{"-1", -1},
{"'1'", 1},
{"1.5", 1},
{"-1.5", -1},
{"'a'", 0},
{"[1]", 1},
{"[1,1]", 0},
{"Infinity", 0},
{"Number.MAX_SAFE_INTEGER", -1},
{"Number.MIN_SAFE_INTEGER", 1},
{"Number.NaN", 0},
{"2_147_483_647", 1<<31 - 1},
{"-2_147_483_648", -1 << 31},
{"2_147_483_648", -1 << 31}, // overflow
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
if i32 := val.Int32(); i32 != tt.expected {
t.Errorf("unexpected value: expected %v, got %v", tt.expected, i32)
}
})
}
}
func TestValueInteger(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
expected int64
}{
{"0", 0},
{"1", 1},
{"-1", -1},
{"'1'", 1},
{"1.5", 1},
{"-1.5", -1},
{"'a'", 0},
{"[1]", 1},
{"[1,1]", 0},
{"Infinity", 1<<63 - 1},
{"Number.MAX_SAFE_INTEGER", 1<<53 - 1},
{"Number.MIN_SAFE_INTEGER", -(1<<53 - 1)},
{"Number.NaN", 0},
{"9_007_199_254_740_991", 1<<53 - 1},
{"-9_007_199_254_740_991", -(1<<53 - 1)},
{"9_223_372_036_854_775_810", 1<<63 - 1}, // does not overflow, pinned at 2^64 -1
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
if i64 := val.Integer(); i64 != tt.expected {
t.Errorf("unexpected value: expected %v, got %v", tt.expected, i64)
}
})
}
}
func TestValueNumber(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
expected float64
}{
{"0", 0},
{"1", 1},
{"-1", -1},
{"'1'", 1},
{"1.5", 1.5},
{"-1.5", -1.5},
{"'a'", math.NaN()},
{"[1]", 1},
{"[1,1]", math.NaN()},
{"Infinity", math.Inf(0)},
{"Number.MAX_VALUE", 1.7976931348623157e+308},
{"Number.MIN_VALUE", 5e-324},
{"Number.MAX_SAFE_INTEGER", 1<<53 - 1},
{"Number.NaN", math.NaN()},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
f64 := val.Number()
if math.IsNaN(tt.expected) {
if !math.IsNaN(f64) {
t.Errorf("unexpected value: expected NaN, got %v", f64)
}
return
}
if f64 != tt.expected {
t.Errorf("unexpected value: expected %v, got %v", tt.expected, f64)
}
})
}
}
func TestValueUint32(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.Close()
tests := [...]struct {
source string
expected uint32
}{
{"0", 0},
{"1", 1},
{"-1", 1<<32 - 1}, // overflow
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
val, _ := ctx.RunScript(tt.source, "test.js")
if u32 := val.Uint32(); u32 != tt.expected {
t.Errorf("unexpected value: expected %v, got %v", tt.expected, u32)
}
})
}
}
func TestValueBigInt(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
x, _ := new(big.Int).SetString("36893488147419099136", 10) // larger than a single word size (64bit)
tests := [...]struct {
source string
expected *big.Int
}{
{"BigInt(0)", &big.Int{}},
{"-1n", big.NewInt(-1)},
{"new BigInt(1)", nil}, // bad syntax
{"BigInt(Number.MAX_SAFE_INTEGER)", big.NewInt(1<<53 - 1)},
{"BigInt(Number.MIN_SAFE_INTEGER)", new(big.Int).Neg(big.NewInt(1<<53 - 1))},
{"BigInt(Number.MAX_SAFE_INTEGER) * 2n", big.NewInt(1<<54 - 2)},
{"BigInt(Number.MAX_SAFE_INTEGER) * 4096n", x},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
ctx := v8.NewContext(iso)
defer ctx.Close()
val, _ := ctx.RunScript(tt.source, "test.js")
b := val.BigInt()
if b == nil && tt.expected != nil {
t.Errorf("uexpected <nil> value")
return
}
if b != nil && tt.expected == nil {
t.Errorf("expected <nil>, but got value: %v", b)
return
}
if b != nil && b.Cmp(tt.expected) != 0 {
t.Errorf("unexpected value: expected %v, got %v", tt.expected, b)
}
})
}
}
func TestValueObject(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := ctx.RunScript("1", "")
if _, err := val.AsObject(); err == nil {
t.Error("Expected error but got <nil>")
}
if obj := val.Object(); obj.String() != "1" {
t.Errorf("unexpected object value: %v", obj)
}
}
func TestValuePromise(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := ctx.RunScript("1", "")
if _, err := val.AsPromise(); err == nil {
t.Error("Expected error but got <nil>")
}
if _, err := ctx.RunScript("new Promise(()=>{})", ""); err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func TestValueFunction(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := ctx.RunScript("1", "")
if _, err := val.AsFunction(); err == nil {
t.Error("Expected error but got <nil>")
}
val, err := ctx.RunScript("(a, b) => { return a + b; }", "")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if _, err := val.AsFunction(); err != nil {
t.Errorf("Expected success but got: %v", err)
}
}
func TestValueSameValue(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
objTempl := v8.NewObjectTemplate(iso)
obj1, err := objTempl.NewInstance(ctx)
fatalIf(t, err)
obj2, err := objTempl.NewInstance(ctx)
fatalIf(t, err)
if obj1.Value.SameValue(obj2.Value) != false {
t.Errorf("SameValue on two different values didn't return false")
}
if obj1.Value.SameValue(obj1.Value) != true {
t.Errorf("SameValue on two of the same value didn't return true")
}
}
func TestValueIsXXX(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
tests := [...]struct {
source string
assert func(*v8.Value) bool
}{
{"", (*v8.Value).IsUndefined},
{"let v; v", (*v8.Value).IsUndefined},
{"null", (*v8.Value).IsNull},
{"let v; v", (*v8.Value).IsNullOrUndefined},
{"let v = null; v", (*v8.Value).IsNullOrUndefined},
{"true", (*v8.Value).IsTrue},
{"false", (*v8.Value).IsFalse},
{"'name'", (*v8.Value).IsName},
{"Symbol()", (*v8.Value).IsName},
{`"double quote"`, (*v8.Value).IsString},
{"'single quote'", (*v8.Value).IsString},
{"`string literal`", (*v8.Value).IsString},
{"Symbol()", (*v8.Value).IsSymbol},
{"Symbol('foo')", (*v8.Value).IsSymbol},
{"() => {}", (*v8.Value).IsFunction},
{"function v() {}; v", (*v8.Value).IsFunction},
{"const v = function() {}; v", (*v8.Value).IsFunction},
{"console.log", (*v8.Value).IsFunction},
{"Object", (*v8.Value).IsFunction},
{"class Foo {}; Foo", (*v8.Value).IsFunction},
{"class Foo { bar() {} }; (new Foo()).bar", (*v8.Value).IsFunction},
{"function* v(){}; v", (*v8.Value).IsFunction},
{"async function v(){}; v", (*v8.Value).IsFunction},
{"Object()", (*v8.Value).IsObject},
{"new Object", (*v8.Value).IsObject},
{"var v = {}; v", (*v8.Value).IsObject},
{"10n", (*v8.Value).IsBigInt},
{"BigInt(1)", (*v8.Value).IsBigInt},
{"true", (*v8.Value).IsBoolean},
{"false", (*v8.Value).IsBoolean},
{"Boolean()", (*v8.Value).IsBoolean},
{"(new Boolean).valueOf()", (*v8.Value).IsBoolean},
{"1", (*v8.Value).IsNumber},
{"1.1", (*v8.Value).IsNumber},
{"1_1", (*v8.Value).IsNumber},
{".1", (*v8.Value).IsNumber},
{"2e4", (*v8.Value).IsNumber},
{"0x2", (*v8.Value).IsNumber},
{"NaN", (*v8.Value).IsNumber},
{"Infinity", (*v8.Value).IsNumber},
{"Number(1)", (*v8.Value).IsNumber},
{"(new Number()).valueOf()", (*v8.Value).IsNumber},
{"1", (*v8.Value).IsInt32},
{"-1", (*v8.Value).IsInt32},
{"1", (*v8.Value).IsUint32},
{"new Date", (*v8.Value).IsDate},
{"function foo(){ return arguments }; foo()", (*v8.Value).IsArgumentsObject},
{"Object(1n)", (*v8.Value).IsBigIntObject},
{"Object(1)", (*v8.Value).IsNumberObject},
{"new Number", (*v8.Value).IsNumberObject},
{"new String", (*v8.Value).IsStringObject},
{"Object('')", (*v8.Value).IsStringObject},
{"Object(Symbol())", (*v8.Value).IsSymbolObject},
{"Error()", (*v8.Value).IsNativeError},
{"TypeError()", (*v8.Value).IsNativeError},
{"SyntaxError()", (*v8.Value).IsNativeError},
{"/./", (*v8.Value).IsRegExp},
{"RegExp()", (*v8.Value).IsRegExp},
{"async function v(){}; v", (*v8.Value).IsAsyncFunction},
{"let v = async () => {}; v", (*v8.Value).IsAsyncFunction},
{"function* v(){}; v", (*v8.Value).IsGeneratorFunction},
{"function* v(){}; v()", (*v8.Value).IsGeneratorObject},
{"new Promise(()=>{})", (*v8.Value).IsPromise},
{"new Map", (*v8.Value).IsMap},
{"new Set", (*v8.Value).IsSet},
{"(new Map).entries()", (*v8.Value).IsMapIterator},
{"(new Set).entries()", (*v8.Value).IsSetIterator},
{"new WeakMap", (*v8.Value).IsWeakMap},
{"new WeakSet", (*v8.Value).IsWeakSet},
{"new Array", (*v8.Value).IsArray},
{"Array()", (*v8.Value).IsArray},
{"[]", (*v8.Value).IsArray},
{"new ArrayBuffer", (*v8.Value).IsArrayBuffer},
{"new Int8Array", (*v8.Value).IsArrayBufferView},
{"new Int8Array", (*v8.Value).IsTypedArray},
{"new Uint32Array", (*v8.Value).IsTypedArray},
{"new Uint8Array", (*v8.Value).IsUint8Array},
{"new Uint8ClampedArray", (*v8.Value).IsUint8ClampedArray},
{"new Int8Array", (*v8.Value).IsInt8Array},
{"new Uint16Array", (*v8.Value).IsUint16Array},
{"new Int16Array", (*v8.Value).IsInt16Array},
{"new Uint32Array", (*v8.Value).IsUint32Array},
{"new Int32Array", (*v8.Value).IsInt32Array},
{"new Float32Array", (*v8.Value).IsFloat32Array},
{"new Float64Array", (*v8.Value).IsFloat64Array},
{"new BigInt64Array", (*v8.Value).IsBigInt64Array},
{"new BigUint64Array", (*v8.Value).IsBigUint64Array},
{"new DataView(new ArrayBuffer)", (*v8.Value).IsDataView},
{"new SharedArrayBuffer", (*v8.Value).IsSharedArrayBuffer},
{"new Proxy({},{})", (*v8.Value).IsProxy},
}
for _, tt := range tests {
tt := tt
t.Run(tt.source, func(t *testing.T) {
ctx := v8.NewContext(iso)
defer ctx.Close()
val, err := ctx.RunScript(tt.source, "test.js")
if err != nil {
t.Fatalf("failed to run script: %v", err)
}
if !tt.assert(val) {
t.Errorf("value is false for %s", runtime.FuncForPC(reflect.ValueOf(tt.assert).Pointer()).Name())
}
})
}
}
func TestValueMarshalJSON(t *testing.T) {
t.Parallel()
iso := v8.NewIsolate()
defer iso.Dispose()
tests := [...]struct {
name string
val func(*v8.Context) *v8.Value
expected []byte
}{
{
"primitive",
func(ctx *v8.Context) *v8.Value {
val, _ := v8.NewValue(iso, int32(0))
return val
},
[]byte("0"),
},
{
"object",
func(ctx *v8.Context) *v8.Value {
val, _ := ctx.RunScript("let foo = {a:1, b:2}; foo", "test.js")
return val
},
[]byte(`{"a":1,"b":2}`),
},
{
"objectFunc",
func(ctx *v8.Context) *v8.Value {
val, _ := ctx.RunScript("let foo = {a:1, b:()=>{}}; foo", "test.js")
return val
},
[]byte(`{"a":1}`),
},
{
"nil",
func(ctx *v8.Context) *v8.Value { return nil },
[]byte(""),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
ctx := v8.NewContext(iso)
defer ctx.Close()
val := tt.val(ctx)
json, _ := val.MarshalJSON()
if !bytes.Equal(json, tt.expected) {
t.Errorf("unexpected JSON value: %s", string(json))
}
})
}
}