-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
49 lines (41 loc) · 1.08 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
package just_test
import (
"github.com/kazhuravlev/just"
"github.com/stretchr/testify/require"
"os"
"testing"
)
type SomeType struct {
ID int `json:"id"`
}
func TestJsonParseType(t *testing.T) {
t.Run("valid_type", func(t *testing.T) {
res, err := just.JsonParseType[SomeType]([]byte(`{"id":42}`))
require.NoError(t, err)
require.Equal(t, SomeType{ID: 42}, *res)
})
t.Run("invalid_type", func(t *testing.T) {
res, err := just.JsonParseType[SomeType]([]byte(`42`))
require.Error(t, err)
require.Nil(t, res)
})
}
func TestJsonParseTypeF(t *testing.T) {
t.Run("valid_type", func(t *testing.T) {
tmpFile, err := os.CreateTemp(os.TempDir(), "test-json-file")
require.NoError(t, err)
{
_, err := tmpFile.Write([]byte(`{"id":42}`))
require.NoError(t, err)
tmpFile.Close()
}
res, err := just.JsonParseTypeF[SomeType](tmpFile.Name())
require.NoError(t, err)
require.Equal(t, SomeType{ID: 42}, *res)
})
t.Run("invalid_type", func(t *testing.T) {
res, err := just.JsonParseTypeF[SomeType]("/path/not-exists/png")
require.Error(t, err)
require.Nil(t, res)
})
}