@@ -17,7 +17,14 @@ limitations under the License.
17
17
package json2
18
18
19
19
import (
20
+ "fmt"
20
21
"testing"
22
+
23
+ "github.com/stretchr/testify/assert"
24
+ "github.com/stretchr/testify/require"
25
+
26
+ "google.golang.org/protobuf/encoding/protojson"
27
+ "google.golang.org/protobuf/types/known/emptypb"
21
28
)
22
29
23
30
func TestUnmarshal (t * testing.T ) {
@@ -37,14 +44,50 @@ func TestUnmarshal(t *testing.T) {
37
44
err : "" ,
38
45
}}
39
46
for _ , tcase := range tcases {
40
- out := make (map [string ]any )
47
+ out := make (map [string ]interface {} )
41
48
err := Unmarshal ([]byte (tcase .in ), & out )
49
+
42
50
got := ""
43
51
if err != nil {
44
52
got = err .Error ()
45
53
}
46
- if got != tcase .err {
47
- t .Errorf ("Unmarshal(%v) err: %v, want %v" , tcase .in , got , tcase .err )
48
- }
54
+ assert .Equal (t , tcase .err , got , "Unmarshal(%v) err" , tcase .in )
55
+ }
56
+ }
57
+
58
+ func TestUnmarshalProto (t * testing.T ) {
59
+ protoData := & emptypb.Empty {}
60
+ protoJSONData , err := protojson .Marshal (protoData )
61
+ assert .Nil (t , err , "protojson.Marshal error" )
62
+
63
+ tcase := struct {
64
+ in string
65
+ out * emptypb.Empty
66
+ }{
67
+ in : string (protoJSONData ),
68
+ out : & emptypb.Empty {},
69
+ }
70
+
71
+ err = Unmarshal ([]byte (tcase .in ), tcase .out )
72
+
73
+ assert .Nil (t , err , "Unmarshal(%v) protobuf message" , tcase .in )
74
+ assert .Equal (t , protoData , tcase .out , "Unmarshal(%v) protobuf message result" , tcase .in )
75
+ }
76
+
77
+ func TestAnnotate (t * testing.T ) {
78
+ tcases := []struct {
79
+ data []byte
80
+ err error
81
+ }{
82
+ {
83
+ data : []byte ("invalid JSON" ),
84
+ err : fmt .Errorf ("line: 1, position 1: invalid character 'i' looking for beginning of value" ),
85
+ },
86
+ }
87
+
88
+ for _ , tcase := range tcases {
89
+ err := annotate (tcase .data , tcase .err )
90
+
91
+ require .Equal (t , tcase .err , err , "annotate(%s, %v) error" , string (tcase .data ), tcase .err )
49
92
}
50
93
}
0 commit comments