-
Notifications
You must be signed in to change notification settings - Fork 7
/
encoding_test.go
46 lines (34 loc) · 1002 Bytes
/
encoding_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
package typeid_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"go.jetify.com/typeid"
)
func TestJSON(t *testing.T) {
str := "prefix_00041061050r3gg28a1c60t3gf"
tid := typeid.Must(typeid.FromString(str))
encoded, err := json.Marshal(tid)
assert.NoError(t, err)
assert.Equal(t, `"`+str+`"`, string(encoded))
var decoded typeid.AnyID
err = json.Unmarshal(encoded, &decoded)
assert.NoError(t, err)
assert.Equal(t, tid, decoded)
assert.Equal(t, str, decoded.String())
}
func TestJSON_Subtype(t *testing.T) {
str := "user_00041061050r3gg28a1c60t3gf"
tid := typeid.Must(typeid.Parse[UserID](str))
encoded, err := json.Marshal(tid)
assert.NoError(t, err)
assert.Equal(t, `"`+str+`"`, string(encoded))
var decoded UserID
err = json.Unmarshal(encoded, &decoded)
assert.NoError(t, err)
assert.Equal(t, tid, decoded)
assert.Equal(t, str, decoded.String())
var wrongType AccountID
err = json.Unmarshal(encoded, &wrongType)
assert.Error(t, err)
}