Debian Codegraph @

object_test.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "bytes"
5 "reflect"
6 "testing"
7 "time"
8
9 "github.com/google/go-cmp/cmp"
10 "github.com/valyala/fastjson"
11 )
12
13 func TestObjectNew(t *testing.T) {
14 testValue := ID("test")
15 testType := ArticleType
16
17 o := ObjectNew(testType)
18 o.ID = testValue
19
20 if o.ID != testValue {
21 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
22 }
23 if !o.Match(testType) {
24 t.Errorf("APObject Type '%v' different than expected '%v'", o.GetType(), testType)
25 }
26
27 n := ObjectNew(NilType)
28 n.ID = testValue
29 if n.ID != testValue {
30 t.Errorf("APObject Id '%v' different than expected '%v'", n.ID, testValue)
31 }
32 if !n.Match(ObjectType) {
33 t.Errorf("APObject Type '%v' different than expected '%v'", n.GetType(), ObjectType)
34 }
35 }
36
37 func TestObject_IsLink(t *testing.T) {
38 o := ObjectNew(ObjectType)
39 o.ID = "test"
40 if o.IsLink() {
41 t.Errorf("%#v should not be a valid link", o.Type)
42 }
43 m := ObjectNew(AcceptType)
44 m.ID = "test"
45 if m.IsLink() {
46 t.Errorf("%#v should not be a valid link", m.Type)
47 }
48 }
49
50 func TestObject_IsObject(t *testing.T) {
51 o := ObjectNew(ObjectType)
52 o.ID = "test"
53 if !o.IsObject() {
54 t.Errorf("%#v should be a valid object", o.Type)
55 }
56 m := ObjectNew(AcceptType)
57 m.ID = "test"
58 if !m.IsObject() {
59 t.Errorf("%#v should be a valid object", m.Type)
60 }
61 }
62
63 func TestObjectsArr_Append(t *testing.T) {
64 d := make(ItemCollection, 0)
65
66 val := Object{ID: ID("grrr")}
67
68 _ = d.Append(val)
69
70 if len(d) != 1 {
71 t.Errorf("Objects array should have exactly an element")
72 }
73 if !reflect.DeepEqual(d[0], val) {
74 t.Errorf("First item in object array does not match %q", val.ID)
75 }
76 }
77
78 func TestRecipients(t *testing.T) {
79 bob := PersonNew("bob")
80 alice := PersonNew("alice")
81 foo := OrganizationNew("foo")
82 bar := GroupNew("bar")
83
84 first := make(ItemCollection, 0)
85 if len(first) != 0 {
86 t.Errorf("Objects array should have exactly an element")
87 }
88
89 _ = first.Append(bob)
90 _ = first.Append(alice)
91 _ = first.Append(foo)
92 _ = first.Append(bar)
93 if len(first) != 4 {
94 t.Errorf("Objects array should have exactly 4(four) elements, not %d", len(first))
95 }
96
97 _ = first.Append(bar)
98 _ = first.Append(alice)
99 _ = first.Append(foo)
100 _ = first.Append(bob)
101 if len(first) != 4 {
102 t.Errorf("Objects array should have exactly 4(eight) elements, not %d", len(first))
103 }
104
105 ItemCollectionDeduplication(&first)
106 if len(first) != 4 {
107 t.Errorf("Objects array should have exactly 4(four) elements, not %d", len(first))
108 }
109
110 second := make(ItemCollection, 0)
111 _ = second.Append(bar)
112 _ = second.Append(foo)
113
114 ItemCollectionDeduplication(&first, &second)
115 if len(first) != 4 {
116 t.Errorf("First Objects array should have exactly 8(eight) elements, not %d", len(first))
117 }
118 if len(second) != 0 {
119 t.Errorf("Second Objects array should have exactly 0(zero) elements, not %d", len(second))
120 }
121 }
122
123 func validateEmptyObject(o Object, t *testing.T) {
124 if o.ID != "" {
125 t.Errorf("Unmarshaled object %T should have empty ID, received %q", o, o.ID)
126 }
127 if HasTypes(o) {
128 t.Errorf("Unmarshaled object %T should have empty Type, received %v", o, o.GetType())
129 }
130 if o.AttributedTo != nil {
131 t.Errorf("Unmarshaled object %T should have empty AttributedTo, received %q", o, o.AttributedTo)
132 }
133 if len(o.Name) != 0 {
134 t.Errorf("Unmarshaled object %T should have empty Name, received %q", o, o.Name)
135 }
136 if len(o.Summary) != 0 {
137 t.Errorf("Unmarshaled object %T should have empty Summary, received %q", o, o.Summary)
138 }
139 if len(o.Content) != 0 {
140 t.Errorf("Unmarshaled object %T should have empty Content, received %q", o, o.Content)
141 }
142 if o.URL != nil {
143 t.Errorf("Unmarshaled object %T should have empty URL, received %v", o, o.URL)
144 }
145 if o.Icon != nil {
146 t.Errorf("Unmarshaled object %T should have empty Icon, received %v", o, o.Icon)
147 }
148 if o.Image != nil {
149 t.Errorf("Unmarshaled object %T should have empty Image, received %v", o, o.Image)
150 }
151 if !o.Published.IsZero() {
152 t.Errorf("Unmarshaled object %T should have empty Published, received %q", o, o.Published)
153 }
154 if !o.StartTime.IsZero() {
155 t.Errorf("Unmarshaled object %T should have empty StartTime, received %q", o, o.StartTime)
156 }
157 if !o.Updated.IsZero() {
158 t.Errorf("Unmarshaled object %T should have empty Updated, received %q", o, o.Updated)
159 }
160 if !o.EndTime.IsZero() {
161 t.Errorf("Unmarshaled object %T should have empty EndTime, received %q", o, o.EndTime)
162 }
163 if o.Duration != 0 {
164 t.Errorf("Unmarshaled object %T should have empty Duration, received %q", o, o.Duration)
165 }
166 if len(o.To) > 0 {
167 t.Errorf("Unmarshaled object %T should have empty To, received %q", o, o.To)
168 }
169 if len(o.Bto) > 0 {
170 t.Errorf("Unmarshaled object %T should have empty Bto, received %q", o, o.Bto)
171 }
172 if len(o.CC) > 0 {
173 t.Errorf("Unmarshaled object %T should have empty CC, received %q", o, o.CC)
174 }
175 if len(o.BCC) > 0 {
176 t.Errorf("Unmarshaled object %T should have empty BCC, received %q", o, o.BCC)
177 }
178 validateEmptySource(o.Source, t)
179 }
180
181 func validateEmptySource(s Source, t *testing.T) {
182 if s.MediaType != "" {
183 t.Errorf("Unmarshalled object %T should have empty Source.MediaType, received %q", s, s.MediaType)
184 }
185 if s.Content != nil {
186 t.Errorf("Unmarshalled object %T should have empty Source.Content, received %q", s, s.Content)
187 }
188 }
189
190 func TestObject_UnmarshalJSON(t *testing.T) {
191 o := Object{}
192
193 dataEmpty := []byte("{}")
194 _ = o.UnmarshalJSON(dataEmpty)
195 validateEmptyObject(o, t)
196 }
197
198 func TestMimeType_UnmarshalJSON(t *testing.T) {
199 m := MimeType("")
200 dataEmpty := []byte("")
201
202 _ = m.UnmarshalJSON(dataEmpty)
203 if m != "" {
204 t.Errorf("Unmarshaled object %T should be an empty string, received %q", m, m)
205 }
206 }
207
208 func TestMimeType_GobDecode(t *testing.T) {
209 tests := []struct {
210 name string
211 m MimeType
212 data []byte
213 wantErr bool
214 }{
215 {
216 name: "empty",
217 m: "",
218 data: []byte{},
219 wantErr: false,
220 },
221 {
222 name: "some mime-type",
223 m: "application/json",
224 data: gobValue([]byte("application/json")),
225 wantErr: false,
226 },
227 }
228 for _, tt := range tests {
229 t.Run(tt.name, func(t *testing.T) {
230 if err := tt.m.GobDecode(tt.data); (err != nil) != tt.wantErr {
231 t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr)
232 }
233 })
234 }
235 }
236
237 func TestMimeType_GobEncode(t *testing.T) {
238 tests := []struct {
239 name string
240 m MimeType
241 want []byte
242 wantErr bool
243 }{
244 {
245 name: "empty",
246 m: "",
247 want: []byte{},
248 wantErr: false,
249 },
250 {
251 name: "some mime-type",
252 m: "application/json",
253 want: gobValue([]byte("application/json")),
254 wantErr: false,
255 },
256 }
257 for _, tt := range tests {
258 t.Run(tt.name, func(t *testing.T) {
259 got, err := tt.m.GobEncode()
260 if (err != nil) != tt.wantErr {
261 t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr)
262 return
263 }
264 if !reflect.DeepEqual(got, tt.want) {
265 t.Errorf("GobEncode() got = %v, want %v", got, tt.want)
266 }
267 })
268 }
269 }
270
271 func TestLangRefValue_UnmarshalText(t *testing.T) {
272 t.Skipf("TODO")
273 }
274
275 func TestLangRef_UnmarshalText(t *testing.T) {
276 l := NilLangRef
277 dataEmpty := []byte("")
278
279 if _ = l.UnmarshalText(dataEmpty); l != NilLangRef {
280 t.Errorf("Unmarshaled object %T should be an empty string, received %q", l, l)
281 }
282 }
283
284 func TestObject_GetID(t *testing.T) {
285 a := Object{}
286 testVal := "crash$"
287 a.ID = ID(testVal)
288 if string(a.GetID()) != testVal {
289 t.Errorf("%T should return %q, Received %q", a.GetID, testVal, a.GetID())
290 }
291 }
292
293 func TestObject_GetLink(t *testing.T) {
294 a := Object{}
295 testVal := "crash$"
296 a.ID = ID(testVal)
297 if string(a.GetLink()) != testVal {
298 t.Errorf("%T should return %q, Received %q", a.GetLink, testVal, a.GetLink())
299 }
300 }
301
302 func TestObject_GetType(t *testing.T) {
303 a := Object{}
304 a.Type = ActorType
305 if !a.Match(ActorType) {
306 t.Errorf("%T should return %q, Received %q", a.GetType(), ActorType, a.GetType())
307 }
308 }
309
310 func TestToObject(t *testing.T) {
311 tests := []struct {
312 name string
313 it LinkOrIRI
314 want *Object
315 wantErr error
316 }{
317 {
318 name: "empty",
319 },
320 {
321 name: "Valid Object",
322 it: Object{ID: "test", Type: UpdateType},
323 want: &Object{ID: "test", Type: UpdateType},
324 },
325 {
326 name: "Valid *Object",
327 it: &Object{ID: "test", Type: CreateType},
328 want: &Object{ID: "test", Type: CreateType},
329 },
330 {
331 name: "Valid Place",
332 it: Place{ID: "test", Type: PlaceType},
333 want: &Object{ID: "test", Type: PlaceType},
334 },
335 {
336 name: "Valid *Place",
337 it: &Place{ID: "test", Type: PlaceType},
338 want: &Object{ID: "test", Type: PlaceType},
339 },
340 {
341 name: "Valid Profile",
342 it: Profile{ID: "test", Type: ProfileType},
343 want: &Object{ID: "test", Type: ProfileType},
344 },
345 {
346 name: "Valid *Profile",
347 it: &Profile{ID: "test", Type: ProfileType},
348 want: &Object{ID: "test", Type: ProfileType},
349 },
350 {
351 name: "Valid Relationship",
352 it: Relationship{ID: "test", Type: RelationshipType},
353 want: &Object{ID: "test", Type: RelationshipType},
354 },
355 {
356 name: "Valid *Relationship",
357 it: &Relationship{ID: "test", Type: RelationshipType},
358 want: &Object{ID: "test", Type: RelationshipType},
359 },
360 {
361 name: "Valid Tombstone",
362 it: Tombstone{ID: "test", Type: TombstoneType},
363 want: &Object{ID: "test", Type: TombstoneType},
364 },
365 {
366 name: "Valid *Tombstone",
367 it: &Tombstone{ID: "test", Type: TombstoneType},
368 want: &Object{ID: "test", Type: TombstoneType},
369 },
370 {
371 name: "Valid Activity",
372 it: &Activity{ID: "test", Type: CreateType},
373 want: &Object{ID: "test", Type: CreateType},
374 },
375 {
376 name: "Valid IntransitiveActivity",
377 it: &IntransitiveActivity{ID: "test", Type: ArriveType},
378 want: &Object{ID: "test", Type: ArriveType},
379 },
380 {
381 name: "Valid Question",
382 it: &Question{ID: "test", Type: QuestionType},
383 want: &Object{ID: "test", Type: QuestionType},
384 },
385 {
386 name: "Valid OrderedCollection",
387 it: OrderedCollection{ID: "test", Type: OrderedCollectionType},
388 want: &Object{ID: "test", Type: OrderedCollectionType},
389 },
390 {
391 name: "Valid *OrderedCollection",
392 it: &OrderedCollection{ID: "test", Type: OrderedCollectionType},
393 want: &Object{ID: "test", Type: OrderedCollectionType},
394 },
395 {
396 name: "Valid OrderedCollectionPage",
397 it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType},
398 want: &Object{ID: "test", Type: OrderedCollectionPageType},
399 },
400 {
401 name: "Valid *OrderedCollectionPage",
402 it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType},
403 want: &Object{ID: "test", Type: OrderedCollectionPageType},
404 },
405 {
406 name: "Valid OrderedCollection",
407 it: OrderedCollection{ID: "test", Type: OrderedCollectionType},
408 want: &Object{ID: "test", Type: OrderedCollectionType},
409 },
410 {
411 name: "Valid *OrderedCollection",
412 it: &OrderedCollection{ID: "test", Type: OrderedCollectionType},
413 want: &Object{ID: "test", Type: OrderedCollectionType},
414 },
415 {
416 name: "Valid OrderedCollectionPage",
417 it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType},
418 want: &Object{ID: "test", Type: OrderedCollectionPageType},
419 },
420 {
421 name: "Valid *OrderedCollectionPage",
422 it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType},
423 want: &Object{ID: "test", Type: OrderedCollectionPageType},
424 },
425 {
426 name: "IRI",
427 it: IRI("https://example.com"),
428 wantErr: ErrorInvalidType[Object](IRI("")),
429 },
430 {
431 name: "IRIs",
432 it: IRIs{IRI("https://example.com")},
433 wantErr: ErrorInvalidType[Object](IRIs{}),
434 },
435 {
436 name: "ItemCollection",
437 it: ItemCollection{},
438 wantErr: ErrorInvalidType[Object](ItemCollection{}),
439 },
440 }
441 for _, tt := range tests {
442 t.Run(tt.name, func(t *testing.T) {
443 got, err := ToObject(tt.it)
444 if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) {
445 t.Errorf("ToObject() error = %v, wantErr %v", err, tt.wantErr)
446 return
447 }
448 if !cmp.Equal(got, tt.want) {
449 t.Errorf("ToObject() got = %s", cmp.Diff(tt.want, got))
450 }
451 })
452 }
453 }
454
455 func TestToObject1(t *testing.T) {
456 tests := []struct {
457 name string
458 arg Item
459 want Item
460 wantErr bool
461 }{
462 {
463 name: "Actor with ID, type, w/o extra properties",
464 arg: &Actor{ID: "https://example.com", Type: PersonType},
465 want: &Object{ID: "https://example.com", Type: PersonType},
466 },
467 {
468 name: "Actor with ID, type, w/ extra properties",
469 arg: &Actor{ID: "https://example.com", Type: PersonType, Endpoints: &Endpoints{
470 OauthAuthorizationEndpoint: IRI("https://example.com/oauth"),
471 }},
472 want: &Object{ID: "https://example.com", Type: PersonType},
473 },
474 {
475 name: "Place w/o extra properties",
476 arg: &Place{ID: "https://example.com", Type: PlaceType},
477 want: &Object{ID: "https://example.com", Type: PlaceType},
478 },
479 {
480 name: "Place w/ extra properties",
481 arg: &Place{ID: "https://example.com", Type: PlaceType, Accuracy: 0.22, Altitude: 66.6},
482 want: &Object{ID: "https://example.com", Type: PlaceType},
483 },
484 {
485 name: "Profile w/o extra properties",
486 arg: &Profile{ID: "https://example.com", Type: ProfileType},
487 want: &Object{ID: "https://example.com", Type: ProfileType},
488 },
489 {
490 name: "Profile w/ extra properties",
491 arg: &Profile{ID: "https://example.com", Type: ProfileType, Describes: IRI("https://alt.example.com/")},
492 want: &Object{ID: "https://example.com", Type: ProfileType},
493 },
494 {
495 name: "Tombstone w/o extra properties",
496 arg: &Tombstone{ID: "https://example.com", Type: TombstoneType},
497 want: &Object{ID: "https://example.com", Type: TombstoneType},
498 },
499 {
500 name: "Tombstone w/ extra properties",
501 arg: &Tombstone{ID: "https://example.com", Type: TombstoneType, FormerType: GroupType, Deleted: time.Now()},
502 want: &Object{ID: "https://example.com", Type: TombstoneType},
503 },
504 {
505 name: "Create w/o extra properties",
506 arg: &Create{ID: "https://example.com", Type: CreateType},
507 want: &Object{ID: "https://example.com", Type: CreateType},
508 },
509 {
510 name: "Create w/ extra properties",
511 arg: &Create{ID: "https://example.com", Type: CreateType, Actor: IRI("https://example.com/1")},
512 want: &Object{ID: "https://example.com", Type: CreateType},
513 },
514 {
515 name: "Question w/o extra properties",
516 arg: &Question{ID: "https://example.com", Type: QuestionType},
517 want: &Object{ID: "https://example.com", Type: QuestionType},
518 },
519 {
520 name: "Question w/ extra properties",
521 arg: &Question{ID: "https://example.com", Type: QuestionType, AnyOf: ItemCollection{IRI("https://example.com")}},
522 want: &Object{ID: "https://example.com", Type: QuestionType},
523 },
524 }
525 for _, tt := range tests {
526 t.Run(tt.name, func(t *testing.T) {
527 arg := tt.arg.(Item)
528 got, err := ToObject(arg)
529 if (err != nil) != tt.wantErr {
530 t.Errorf("ToObject() error = %v, wantErr %v", err, tt.wantErr)
531 return
532 }
533 if !ItemsEqual(tt.want, got) {
534 t.Errorf("ToObject() got = %v, expected %v", got, tt.want)
535 }
536 })
537 }
538 }
539
540 func TestFlattenObjectProperties(t *testing.T) {
541 t.Skipf("TODO")
542 }
543
544 func TestObject_Recipients(t *testing.T) {
545 t.Skipf("TODO")
546 }
547
548 func TestRelationship_Recipients(t *testing.T) {
549 t.Skipf("TODO")
550 }
551
552 func TestTombstone_Recipients(t *testing.T) {
553 t.Skipf("TODO")
554 }
555
556 func TestSource_UnmarshalJSON(t *testing.T) {
557 s := Source{}
558
559 dataEmpty := []byte("{}")
560 _ = s.UnmarshalJSON(dataEmpty)
561 validateEmptySource(s, t)
562 }
563
564 func TestGetAPSource(t *testing.T) {
565 data := []byte(`{"source": {"content": "test", "mediaType": "text/plain" }}`)
566
567 par := fastjson.Parser{}
568 val, _ := par.ParseBytes(data)
569 a := GetAPSource(val)
570
571 if a.Content.First().String() != "test" {
572 t.Errorf("Content didn't match test value. Received %q, expecting %q", a.Content, "test")
573 }
574 if a.MediaType != "text/plain" {
575 t.Errorf("Content didn't match test value. Received %q, expecting %q", a.MediaType, "text/plain")
576 }
577 }
578
579 func TestObject_Clean(t *testing.T) {
580 t.Skip("TODO")
581 }
582
583 func TestObject_IsCollection(t *testing.T) {
584 t.Skip("TODO")
585 }
586
587 func TestActivityVocabularyType_MarshalJSON(t *testing.T) {
588 t.Skip("TODO")
589 }
590
591 func TestActivityVocabularyType_GobDecode(t *testing.T) {
592 tests := []struct {
593 name string
594 t ActivityVocabularyType
595 data []byte
596 wantErr bool
597 }{
598 {
599 name: "empty",
600 t: "",
601 data: []byte{},
602 wantErr: false,
603 },
604 {
605 name: "some activity type",
606 t: PersonType,
607 data: gobValue([]byte("Person")),
608 wantErr: false,
609 },
610 }
611 for _, tt := range tests {
612 t.Run(tt.name, func(t *testing.T) {
613 if err := tt.t.GobDecode(tt.data); (err != nil) != tt.wantErr {
614 t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr)
615 }
616 })
617 }
618 }
619
620 func TestActivityVocabularyType_GobEncode(t *testing.T) {
621 tests := []struct {
622 name string
623 t ActivityVocabularyType
624 want []byte
625 wantErr bool
626 }{
627 {
628 name: "empty",
629 t: "",
630 want: []byte{},
631 wantErr: false,
632 },
633 {
634 name: "some activity type",
635 t: ActivityType,
636 want: []byte("Activity"),
637 wantErr: false,
638 },
639 }
640 for _, tt := range tests {
641 t.Run(tt.name, func(t *testing.T) {
642 got, err := tt.t.GobEncode()
643 if (err != nil) != tt.wantErr {
644 t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr)
645 return
646 }
647 if !reflect.DeepEqual(got, tt.want) {
648 t.Errorf("GobEncode() got = %v, want %v", got, tt.want)
649 }
650 })
651 }
652 }
653
654 func TestObject_MarshalJSON(t *testing.T) {
655 type fields struct {
656 ID ID
657 Type Typer
658 Name NaturalLanguageValues
659 Attachment Item
660 AttributedTo Item
661 Audience ItemCollection
662 Content NaturalLanguageValues
663 Context Item
664 MediaType MimeType
665 EndTime time.Time
666 Generator Item
667 Icon Item
668 Image Item
669 InReplyTo Item
670 Location Item
671 Preview Item
672 Published time.Time
673 Replies Item
674 StartTime time.Time
675 Summary NaturalLanguageValues
676 Tag ItemCollection
677 Updated time.Time
678 URL Item
679 To ItemCollection
680 Bto ItemCollection
681 CC ItemCollection
682 BCC ItemCollection
683 Duration time.Duration
684 Likes Item
685 Shares Item
686 Source Source
687 }
688 tests := []struct {
689 name string
690 fields fields
691 want [][]byte
692 wantErr bool
693 }{
694 {
695 name: "Empty",
696 fields: fields{},
697 want: nil,
698 wantErr: false,
699 },
700 {
701 name: "JustID",
702 fields: fields{
703 ID: ID("example.com"),
704 },
705 want: [][]byte{[]byte(`{"id":"example.com"}`)},
706 wantErr: false,
707 },
708 {
709 name: "JustType",
710 fields: fields{
711 Type: ActivityVocabularyType("myType"),
712 },
713 want: [][]byte{[]byte(`{"type":"myType"}`)},
714 wantErr: false,
715 },
716 {
717 name: "JustOneName",
718 fields: fields{
719 Name: NaturalLanguageValues{
720 NilLangRef: Content("ana"),
721 },
722 },
723 want: [][]byte{[]byte(`{"name":"ana"}`)},
724 wantErr: false,
725 },
726 {
727 name: "MoreNames",
728 fields: fields{
729 Name: NaturalLanguageValues{
730 English: Content("anna"),
731 French: Content("anne"),
732 },
733 },
734 want: [][]byte{
735 []byte(`{"nameMap":{"en":"anna","fr":"anne"}}`),
736 []byte(`{"nameMap":{"fr":"anne","en":"anna"}}`),
737 },
738 wantErr: false,
739 },
740 {
741 name: "JustOneSummary",
742 fields: fields{
743 Summary: NaturalLanguageValues{
744 NilLangRef: Content("test summary"),
745 },
746 },
747 want: [][]byte{[]byte(`{"summary":"test summary"}`)},
748 wantErr: false,
749 },
750 {
751 name: "MoreSummaryEntries",
752 fields: fields{
753 Summary: NaturalLanguageValues{
754 English: Content("test summary"),
755 French: Content("teste summary"),
756 },
757 },
758 want: [][]byte{
759 []byte(`{"summaryMap":{"en":"test summary","fr":"teste summary"}}`),
760 []byte(`{"summaryMap":{"fr":"teste summary","en":"test summary"}}`),
761 },
762 wantErr: false,
763 },
764 {
765 name: "JustOneContent",
766 fields: fields{
767 Content: NaturalLanguageValues{
768 NilLangRef: Content("test content"),
769 },
770 },
771 want: [][]byte{[]byte(`{"content":"test content"}`)},
772 wantErr: false,
773 },
774 {
775 name: "MoreContentEntries",
776 fields: fields{
777 Content: NaturalLanguageValues{
778 English: Content("test content"),
779 French: Content("teste content"),
780 },
781 },
782 want: [][]byte{
783 []byte(`{"contentMap":{"en":"test content","fr":"teste content"}}`),
784 []byte(`{"contentMap":{"fr":"teste content","en":"test content"}}`),
785 },
786 wantErr: false,
787 },
788 {
789 name: "MediaType",
790 fields: fields{
791 MediaType: MimeType("text/stupid"),
792 },
793 want: [][]byte{[]byte(`{"mediaType":"text/stupid"}`)},
794 wantErr: false,
795 },
796 {
797 name: "Attachment",
798 fields: fields{
799 Attachment: &Object{
800 ID: "some example",
801 Type: VideoType,
802 },
803 },
804 want: [][]byte{[]byte(`{"attachment":{"id":"some example","type":"Video"}}`)},
805 wantErr: false,
806 },
807 {
808 name: "AttributedTo",
809 fields: fields{
810 AttributedTo: &Actor{
811 ID: "http://example.com/ana",
812 Type: PersonType,
813 },
814 },
815 want: [][]byte{[]byte(`{"attributedTo":{"id":"http://example.com/ana","type":"Person"}}`)},
816 wantErr: false,
817 },
818 {
819 name: "AttributedToDouble",
820 fields: fields{
821 AttributedTo: ItemCollection{
822 &Actor{
823 ID: "http://example.com/ana",
824 Type: PersonType,
825 },
826 &Actor{
827 ID: "http://example.com/GGG",
828 Type: GroupType,
829 },
830 },
831 },
832 want: [][]byte{[]byte(`{"attributedTo":[{"id":"http://example.com/ana","type":"Person"},{"id":"http://example.com/GGG","type":"Group"}]}`)},
833 wantErr: false,
834 },
835 {
836 name: "Source",
837 fields: fields{
838 Source: Source{
839 MediaType: MimeType("text/plain"),
840 Content: NaturalLanguageValues{},
841 },
842 },
843 want: [][]byte{[]byte(`{"source":{"mediaType":"text/plain"}}`)},
844 wantErr: false,
845 },
846 }
847 for _, tt := range tests {
848 t.Run(tt.name, func(t *testing.T) {
849 o := Object{
850 ID: tt.fields.ID,
851 Type: tt.fields.Type,
852 Name: tt.fields.Name,
853 Attachment: tt.fields.Attachment,
854 AttributedTo: tt.fields.AttributedTo,
855 Audience: tt.fields.Audience,
856 Content: tt.fields.Content,
857 Context: tt.fields.Context,
858 MediaType: tt.fields.MediaType,
859 EndTime: tt.fields.EndTime,
860 Generator: tt.fields.Generator,
861 Icon: tt.fields.Icon,
862 Image: tt.fields.Image,
863 InReplyTo: tt.fields.InReplyTo,
864 Location: tt.fields.Location,
865 Preview: tt.fields.Preview,
866 Published: tt.fields.Published,
867 Replies: tt.fields.Replies,
868 StartTime: tt.fields.StartTime,
869 Summary: tt.fields.Summary,
870 Tag: tt.fields.Tag,
871 Updated: tt.fields.Updated,
872 URL: tt.fields.URL,
873 To: tt.fields.To,
874 Bto: tt.fields.Bto,
875 CC: tt.fields.CC,
876 BCC: tt.fields.BCC,
877 Duration: tt.fields.Duration,
878 Likes: tt.fields.Likes,
879 Shares: tt.fields.Shares,
880 Source: tt.fields.Source,
881 }
882 got, err := o.MarshalJSON()
883 if (err != nil) != tt.wantErr {
884 t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
885 return
886 }
887 found := got == nil
888 for _, wantBytes := range tt.want {
889 if bytes.Equal(got, wantBytes) {
890 found = true
891 }
892 }
893 if !found {
894 t.Errorf("MarshalJSON() got = %s, want %s", got, tt.want)
895 }
896 })
897 }
898 }
899
900 func TestSource_MarshalJSON(t *testing.T) {
901 type fields struct {
902 Content NaturalLanguageValues
903 MediaType MimeType
904 }
905 tests := []struct {
906 name string
907 fields fields
908 want [][]byte
909 wantErr bool
910 }{
911 {
912 name: "Empty",
913 fields: fields{},
914 want: nil,
915 wantErr: false,
916 },
917 {
918 name: "MediaType",
919 fields: fields{
920 MediaType: MimeType("blank"),
921 },
922 want: [][]byte{[]byte(`{"mediaType":"blank"}`)},
923 wantErr: false,
924 },
925 {
926 name: "OneContentValue",
927 fields: fields{
928 Content: NaturalLanguageValues{
929 Und: Content("test"),
930 },
931 },
932 want: [][]byte{[]byte(`{"content":"test"}`)},
933 wantErr: false,
934 },
935 {
936 name: "MultipleContentValues",
937 fields: fields{
938 Content: NaturalLanguageValues{
939 English: Content("test"),
940 French: Content("teste"),
941 },
942 },
943 want: [][]byte{
944 []byte(`{"contentMap":{"en":"test","fr":"teste"}}`),
945 []byte(`{"contentMap":{"fr":"teste","en":"test"}}`),
946 },
947 wantErr: false,
948 },
949 }
950 for _, tt := range tests {
951 t.Run(tt.name, func(t *testing.T) {
952 s := Source{
953 Content: tt.fields.Content,
954 MediaType: tt.fields.MediaType,
955 }
956 got, err := s.MarshalJSON()
957 if (err != nil) != tt.wantErr {
958 t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
959 return
960 }
961 found := got == nil
962 for _, wantBytes := range tt.want {
963 if bytes.Equal(got, wantBytes) {
964 found = true
965 }
966 }
967 if !found {
968 t.Errorf("MarshalJSON() got = %s, want %s", got, tt.want)
969 }
970 })
971 }
972 }
973
974 func TestObject_Equals(t *testing.T) {
975 type fields struct {
976 ID ID
977 Type Typer
978 Name NaturalLanguageValues
979 Attachment Item
980 AttributedTo Item
981 Audience ItemCollection
982 Content NaturalLanguageValues
983 Context Item
984 MediaType MimeType
985 EndTime time.Time
986 Generator Item
987 Icon Item
988 Image Item
989 InReplyTo Item
990 Location Item
991 Preview Item
992 Published time.Time
993 Replies Item
994 StartTime time.Time
995 Summary NaturalLanguageValues
996 Tag ItemCollection
997 Updated time.Time
998 URL Item
999 To ItemCollection
1000 Bto ItemCollection
1001 CC ItemCollection
1002 BCC ItemCollection
1003 Duration time.Duration
1004 Likes Item
1005 Shares Item
1006 Source Source
1007 }
1008 tests := []struct {
1009 name string
1010 fields fields
1011 arg Item
1012 want bool
1013 }{
1014 {
1015 name: "equal-empty-object",
1016 fields: fields{},
1017 arg: Object{},
1018 want: true,
1019 },
1020 {
1021 name: "equal-object-just-id",
1022 fields: fields{ID: "test"},
1023 arg: Object{ID: "test"},
1024 want: true,
1025 },
1026 {
1027 name: "equal-object-id",
1028 fields: fields{ID: "test", URL: IRI("example.com")},
1029 arg: Object{ID: "test"},
1030 want: true,
1031 },
1032 {
1033 name: "equal-false-with-id-and-url",
1034 fields: fields{ID: "test"},
1035 arg: Object{ID: "test", URL: IRI("example.com")},
1036 want: false,
1037 },
1038 {
1039 name: "not a valid object",
1040 fields: fields{ID: "http://example.com"},
1041 arg: Link{ID: "http://example.com"},
1042 want: false,
1043 },
1044 }
1045 for _, tt := range tests {
1046 t.Run(tt.name, func(t *testing.T) {
1047 o := Object{
1048 ID: tt.fields.ID,
1049 Type: tt.fields.Type,
1050 Name: tt.fields.Name,
1051 Attachment: tt.fields.Attachment,
1052 AttributedTo: tt.fields.AttributedTo,
1053 Audience: tt.fields.Audience,
1054 Content: tt.fields.Content,
1055 Context: tt.fields.Context,
1056 MediaType: tt.fields.MediaType,
1057 EndTime: tt.fields.EndTime,
1058 Generator: tt.fields.Generator,
1059 Icon: tt.fields.Icon,
1060 Image: tt.fields.Image,
1061 InReplyTo: tt.fields.InReplyTo,
1062 Location: tt.fields.Location,
1063 Preview: tt.fields.Preview,
1064 Published: tt.fields.Published,
1065 Replies: tt.fields.Replies,
1066 StartTime: tt.fields.StartTime,
1067 Summary: tt.fields.Summary,
1068 Tag: tt.fields.Tag,
1069 Updated: tt.fields.Updated,
1070 URL: tt.fields.URL,
1071 To: tt.fields.To,
1072 Bto: tt.fields.Bto,
1073 CC: tt.fields.CC,
1074 BCC: tt.fields.BCC,
1075 Duration: tt.fields.Duration,
1076 Likes: tt.fields.Likes,
1077 Shares: tt.fields.Shares,
1078 Source: tt.fields.Source,
1079 }
1080 if got := o.Equals(tt.arg); got != tt.want {
1081 t.Errorf("Equal() = %v, want %v", got, tt.want)
1082 }
1083 })
1084 }
1085 }
1086
1087 func TestObject_GobEncode(t *testing.T) {
1088 type fields struct {
1089 ID ID
1090 Type Typer
1091 Name NaturalLanguageValues
1092 Attachment Item
1093 AttributedTo Item
1094 Audience ItemCollection
1095 Content NaturalLanguageValues
1096 Context Item
1097 MediaType MimeType
1098 EndTime time.Time
1099 Generator Item
1100 Icon Item
1101 Image Item
1102 InReplyTo Item
1103 Location Item
1104 Preview Item
1105 Published time.Time
1106 Replies Item
1107 StartTime time.Time
1108 Summary NaturalLanguageValues
1109 Tag ItemCollection
1110 Updated time.Time
1111 URL Item
1112 To ItemCollection
1113 Bto ItemCollection
1114 CC ItemCollection
1115 BCC ItemCollection
1116 Duration time.Duration
1117 Likes Item
1118 Shares Item
1119 Source Source
1120 }
1121 tests := []struct {
1122 name string
1123 fields fields
1124 wantErr bool
1125 }{
1126 {
1127 name: "empty",
1128 fields: fields{},
1129 wantErr: false,
1130 },
1131 {
1132 name: "with ID",
1133 fields: fields{ID: ID("https://example.com")},
1134 wantErr: false,
1135 },
1136 {
1137 name: "with ID, type",
1138 fields: fields{ID: ID("https://example.com"), Type: ObjectType},
1139 wantErr: false,
1140 },
1141 {
1142 name: "with ID, type, name",
1143 fields: fields{ID: ID("https://example.com"), Type: ObjectType, Name: NaturalLanguageValues{English: Content("ana")}},
1144 wantErr: false,
1145 },
1146 {
1147 name: "with Source",
1148 fields: fields{Source: Source{MediaType: "image/svg+xml", Content: NaturalLanguageValues{NilLangRef: Content("data:image/svg+xml,%3csvg%3e %3c/svg%3e")}}},
1149 wantErr: false,
1150 },
1151 {
1152 name: "with IRI AttributedTo",
1153 fields: fields{AttributedTo: IRI("https://example.com/1")},
1154 wantErr: false,
1155 },
1156 {
1157 name: "with multiple IRIs AttributedTo",
1158 fields: fields{AttributedTo: ItemCollection{IRI("https://example.com/1"), IRI("https://example.com/2")}},
1159 wantErr: false,
1160 },
1161 {
1162 name: "with single object AttributedTo",
1163 fields: fields{AttributedTo: Object{ID: "https://example.com/1"}},
1164 wantErr: false,
1165 },
1166 }
1167 for _, tt := range tests {
1168 t.Run(tt.name, func(t *testing.T) {
1169 o := Object{
1170 ID: tt.fields.ID,
1171 Type: tt.fields.Type,
1172 Name: tt.fields.Name,
1173 Attachment: tt.fields.Attachment,
1174 AttributedTo: tt.fields.AttributedTo,
1175 Audience: tt.fields.Audience,
1176 Content: tt.fields.Content,
1177 Context: tt.fields.Context,
1178 MediaType: tt.fields.MediaType,
1179 EndTime: tt.fields.EndTime,
1180 Generator: tt.fields.Generator,
1181 Icon: tt.fields.Icon,
1182 Image: tt.fields.Image,
1183 InReplyTo: tt.fields.InReplyTo,
1184 Location: tt.fields.Location,
1185 Preview: tt.fields.Preview,
1186 Published: tt.fields.Published,
1187 Replies: tt.fields.Replies,
1188 StartTime: tt.fields.StartTime,
1189 Summary: tt.fields.Summary,
1190 Tag: tt.fields.Tag,
1191 Updated: tt.fields.Updated,
1192 URL: tt.fields.URL,
1193 To: tt.fields.To,
1194 Bto: tt.fields.Bto,
1195 CC: tt.fields.CC,
1196 BCC: tt.fields.BCC,
1197 Duration: tt.fields.Duration,
1198 Likes: tt.fields.Likes,
1199 Shares: tt.fields.Shares,
1200 Source: tt.fields.Source,
1201 }
1202 got, err := o.GobEncode()
1203 if (err != nil) != tt.wantErr {
1204 t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr)
1205 return
1206 }
1207 ob := Object{}
1208 if err = ob.GobDecode(got); (err != nil) != tt.wantErr {
1209 t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr)
1210 return
1211 }
1212 if !ItemsEqual(ob, o) {
1213 t.Errorf("GobEncode() got = %s", cmp.Diff(ob, o))
1214 }
1215 })
1216 }
1217 }
1218
1219 type reflectTest[T Objects | Links] struct {
1220 name string
1221 arg any
1222 want *T
1223 wantErr bool
1224 }
1225
1226 func Test_reflectedItemByType_Object(t *testing.T) {
1227 tests := []reflectTest[Object]{
1228 {
1229 name: "empty object",
1230 arg: &Object{},
1231 want: &Object{},
1232 },
1233 {
1234 name: "object with ID",
1235 arg: &Object{ID: "https://example.com"},
1236 want: &Object{ID: "https://example.com"},
1237 },
1238 {
1239 name: "object with ID, type",
1240 arg: &Object{ID: "https://example.com", Type: ArticleType},
1241 want: &Object{ID: "https://example.com", Type: ArticleType},
1242 },
1243 }
1244 for _, tt := range tests {
1245 t.Run(tt.name, func(t *testing.T) {
1246 arg := tt.arg.(Item)
1247 got, err := reflectItemToType[Object](arg)
1248 if (err != nil) != tt.wantErr {
1249 t.Errorf("reflectItemToType() error = %v, wantErr %v", err, tt.wantErr)
1250 return
1251 }
1252 if !ItemsEqual(tt.want, got) {
1253 t.Errorf("reflectItemToType() got = %v, expected %v", got, tt.want)
1254 }
1255 })
1256 }
1257 }