@@ -120,3 +120,55 @@ func TestUnmarshalJSON(t *testing.T) {
120120 }
121121 }
122122}
123+
124+ func TestMarshalJSON (t * testing.T ) {
125+ ts , err := time .Parse ("2006-01-02T15:04:05Z07:00" , "2019-11-04T23:01:10.112Z" )
126+ if err != nil {
127+ t .Fatalf ("Error parsing time: %v." , err )
128+ }
129+ var tests = []struct {
130+ name string
131+ metadata Metadata
132+ want []byte
133+ }{
134+ {
135+ name : "MetadataWithResource" ,
136+ metadata : Metadata {
137+ EventID : "1234567" ,
138+ Timestamp : ts ,
139+ EventType : "google.pubsub.topic.publish" ,
140+ Resource : & Resource {
141+ Service : "pubsub.googleapis.com" ,
142+ Name : "mytopic" ,
143+ Type : "type.googleapis.com/google.pubsub.v1.PubsubMessage" ,
144+ },
145+ },
146+ },
147+ {
148+ name : "MetadataWithString" ,
149+ metadata : Metadata {
150+ EventID : "1234567" ,
151+ Timestamp : ts ,
152+ EventType : "google.pubsub.topic.publish" ,
153+ Resource : & Resource {
154+ RawPath : "projects/myproject/mytopic" ,
155+ },
156+ },
157+ },
158+ }
159+
160+ for _ , tc := range tests {
161+ b , err := json .Marshal (& tc .metadata )
162+ if err != nil {
163+ t .Errorf ("MarshalJSON(%s) error: %v" , tc .name , err )
164+ }
165+
166+ var m Metadata
167+ if err := json .Unmarshal (b , & m ); err != nil {
168+ t .Errorf ("MarshalJSON(%s) error: %v" , tc .name , err )
169+ }
170+ if ! cmp .Equal (m , tc .metadata ) {
171+ t .Errorf ("MarshalJSON(%s) error: got %v, want %v" , tc .name , m , tc .metadata )
172+ }
173+ }
174+ }
0 commit comments