forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
67 lines (57 loc) · 1.35 KB
/
json_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
// Copyright 2021 Roger Chapman and the v8 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 (
"fmt"
"testing"
v8 "rogchap.com/v8go"
)
func TestJSONParse(t *testing.T) {
t.Parallel()
if _, err := v8.JSONParse(nil, "{}"); err == nil {
t.Error("expected error but got <nil>")
}
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
_, err := v8.JSONParse(ctx, "{")
if err == nil {
t.Error("expected error but got <nil>")
return
}
if _, ok := err.(*v8.JSError); !ok {
t.Errorf("expected error to be of type JSError, got: %T", err)
}
}
func TestJSONStringify(t *testing.T) {
t.Parallel()
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
if _, err := v8.JSONStringify(ctx, nil); err == nil {
t.Error("expected error but got <nil>")
}
}
func ExampleJSONParse() {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := v8.JSONParse(ctx, `{"foo": "bar"}`)
fmt.Println(val)
// Output:
// [object Object]
}
func ExampleJSONStringify() {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := v8.JSONParse(ctx, `{
"a": 1,
"b": "foo"
}`)
jsonStr, _ := v8.JSONStringify(ctx, val)
fmt.Println(jsonStr)
// Output:
// {"a":1,"b":"foo"}
}