-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
50 lines (39 loc) · 943 Bytes
/
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
//go:build js
package goji
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// customType is a type used to test marshalling
type customType struct {
Name string `json:"name"`
Age int
}
func TestMarshalJS(t *testing.T) {
expect := customType{
Name: "Alice",
Age: 42,
}
// marshal into a js.Value
value, err := MarshalJS(expect)
require.NoError(t, err)
// value should be preserved and json tags respected
assert.Equal(t, expect.Name, value.Get("name").String())
assert.Equal(t, expect.Age, value.Get("Age").Int())
}
func TestUnmarshalJS(t *testing.T) {
expect := customType{
Name: "Bob",
Age: 41,
}
// marshal into a js.Value
value, err := MarshalJS(expect)
require.NoError(t, err)
// unmarshal back into a struct
var actual customType
err = UnmarshalJS(value, &actual)
require.NoError(t, err)
// should be the same
assert.Equal(t, expect, actual)
}