Debian Codegraph @

encoding_json_test.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "bytes"
5 "reflect"
6 "testing"
7 "time"
8 )
9
10 func Test_JSONWrite(t *testing.T) {
11 type args struct {
12 b *[]byte
13 c []byte
14 }
15 tests := []struct {
16 name string
17 args args
18 }{
19 // TODO: Add test cases.
20 }
21 for _, tt := range tests {
22 t.Run(tt.name, func(t *testing.T) {
23 })
24 }
25 }
26
27 func Test_JSONWriteActivity(t *testing.T) {
28 type args struct {
29 b *[]byte
30 a Activity
31 }
32 tests := []struct {
33 name string
34 args args
35 wantNotEmpty bool
36 }{
37 // TODO: Add test cases.
38 }
39 for _, tt := range tests {
40 t.Run(tt.name, func(t *testing.T) {
41 if gotNotEmpty := JSONWriteActivityValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty {
42 t.Errorf("JSONWriteActivityValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
43 }
44 })
45 }
46 }
47
48 func Test_JSONWriteBoolProp(t *testing.T) {
49 type args struct {
50 b *[]byte
51 n string
52 t bool
53 }
54 tests := []struct {
55 name string
56 args args
57 wantNotEmpty bool
58 }{
59 // TODO: Add test cases.
60 }
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 if gotNotEmpty := JSONWriteBoolProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
64 t.Errorf("JSONWriteBoolProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
65 }
66 })
67 }
68 }
69
70 func Test_JSONWriteComma(t *testing.T) {
71 type args struct {
72 b *[]byte
73 }
74 tests := []struct {
75 name string
76 args args
77 }{
78 // TODO: Add test cases.
79 }
80 for _, tt := range tests {
81 t.Run(tt.name, func(t *testing.T) {
82 })
83 }
84 }
85
86 func Test_JSONWriteDurationProp(t *testing.T) {
87 type args struct {
88 b *[]byte
89 n string
90 d time.Duration
91 }
92 tests := []struct {
93 name string
94 args args
95 wantNotEmpty bool
96 }{
97 // TODO: Add test cases.
98 }
99 for _, tt := range tests {
100 t.Run(tt.name, func(t *testing.T) {
101 if gotNotEmpty := JSONWriteDurationProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
102 t.Errorf("JSONWriteDurationProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
103 }
104 })
105 }
106 }
107
108 func Test_JSONWriteFloatProp(t *testing.T) {
109 type args struct {
110 b *[]byte
111 n string
112 f float64
113 }
114 tests := []struct {
115 name string
116 args args
117 wantNotEmpty bool
118 }{
119 // TODO: Add test cases.
120 }
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 if gotNotEmpty := JSONWriteFloatProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty {
124 t.Errorf("JSONWriteFloatProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
125 }
126 })
127 }
128 }
129
130 func Test_JSONWriteIRIProp(t *testing.T) {
131 type args struct {
132 b *[]byte
133 n string
134 i LinkOrIRI
135 }
136 tests := []struct {
137 name string
138 args args
139 wantNotEmpty bool
140 }{
141 // TODO: Add test cases.
142 }
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 if gotNotEmpty := JSONWriteIRIProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
146 t.Errorf("JSONWriteIRIProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
147 }
148 })
149 }
150 }
151
152 func Test_JSONWriteIntProp(t *testing.T) {
153 type args struct {
154 b *[]byte
155 n string
156 d int64
157 }
158 tests := []struct {
159 name string
160 args args
161 wantNotEmpty bool
162 }{
163 // TODO: Add test cases.
164 }
165 for _, tt := range tests {
166 t.Run(tt.name, func(t *testing.T) {
167 if gotNotEmpty := JSONWriteIntProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty {
168 t.Errorf("JSONWriteIntProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
169 }
170 })
171 }
172 }
173
174 func Test_JSONWriteIntransitiveActivity(t *testing.T) {
175 type args struct {
176 b *[]byte
177 i IntransitiveActivity
178 }
179 tests := []struct {
180 name string
181 args args
182 wantNotEmpty bool
183 }{
184 // TODO: Add test cases.
185 }
186 for _, tt := range tests {
187 t.Run(tt.name, func(t *testing.T) {
188 if gotNotEmpty := JSONWriteIntransitiveActivityValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
189 t.Errorf("JSONWriteIntransitiveActivityValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
190 }
191 })
192 }
193 }
194
195 func Test_JSONWriteItemCollection(t *testing.T) {
196 type args struct {
197 b *[]byte
198 col ItemCollection
199 }
200 tests := []struct {
201 name string
202 args args
203 wantNotEmpty bool
204 }{
205 // TODO: Add test cases.
206 }
207 for _, tt := range tests {
208 t.Run(tt.name, func(t *testing.T) {
209 if gotNotEmpty := JSONWriteItemCollectionValue(tt.args.b, tt.args.col, true); gotNotEmpty != tt.wantNotEmpty {
210 t.Errorf("JSONWriteItemCollectionValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
211 }
212 })
213 }
214 }
215
216 func Test_JSONWriteItemCollectionProp(t *testing.T) {
217 type args struct {
218 b *[]byte
219 n string
220 col ItemCollection
221 }
222 tests := []struct {
223 name string
224 args args
225 wantNotEmpty bool
226 }{
227 // TODO: Add test cases.
228 }
229 for _, tt := range tests {
230 t.Run(tt.name, func(t *testing.T) {
231 if gotNotEmpty := JSONWriteItemCollectionProp(tt.args.b, tt.args.n, tt.args.col, true); gotNotEmpty != tt.wantNotEmpty {
232 t.Errorf("JSONWriteItemCollectionProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
233 }
234 })
235 }
236 }
237
238 func Test_JSONWriteItemProp(t *testing.T) {
239 type args struct {
240 b *[]byte
241 n string
242 i Item
243 }
244 tests := []struct {
245 name string
246 args args
247 wantNotEmpty bool
248 }{
249 // TODO: Add test cases.
250 }
251 for _, tt := range tests {
252 t.Run(tt.name, func(t *testing.T) {
253 if gotNotEmpty := JSONWriteItemProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty {
254 t.Errorf("JSONWriteItemProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
255 }
256 })
257 }
258 }
259
260 func Test_JSONWriteNaturalLanguageProp(t *testing.T) {
261 type args struct {
262 b *[]byte
263 n string
264 nl NaturalLanguageValues
265 }
266 tests := []struct {
267 name string
268 args args
269 wantNotEmpty bool
270 }{
271 // TODO: Add test cases.
272 }
273 for _, tt := range tests {
274 t.Run(tt.name, func(t *testing.T) {
275 if gotNotEmpty := JSONWriteNaturalLanguageProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty {
276 t.Errorf("JSONWriteNaturalLanguageProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
277 }
278 })
279 }
280 }
281
282 func Test_JSONWriteObjectValue(t *testing.T) {
283 type args struct {
284 b *[]byte
285 o Object
286 }
287 tests := []struct {
288 name string
289 args args
290 wantNotEmpty bool
291 }{
292 // TODO: Add test cases.
293 }
294 for _, tt := range tests {
295 t.Run(tt.name, func(t *testing.T) {
296 if gotNotEmpty := JSONWriteObjectValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty {
297 t.Errorf("JSONWriteObjectValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
298 }
299 })
300 }
301 }
302
303 func Test_JSONWriteProp(t *testing.T) {
304 type args struct {
305 b *[]byte
306 name string
307 val []byte
308 }
309 tests := []struct {
310 name string
311 args args
312 wantNotEmpty bool
313 }{
314 // TODO: Add test cases.
315 }
316 for _, tt := range tests {
317 t.Run(tt.name, func(t *testing.T) {
318 if gotNotEmpty := JSONWriteProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty {
319 t.Errorf("JSONWriteProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
320 }
321 })
322 }
323 }
324
325 func Test_JSONWritePropName(t *testing.T) {
326 type args struct {
327 b *[]byte
328 s string
329 }
330 tests := []struct {
331 name string
332 args args
333 wantNotEmpty bool
334 }{
335 // TODO: Add test cases.
336 }
337 for _, tt := range tests {
338 t.Run(tt.name, func(t *testing.T) {
339 if gotNotEmpty := JSONWritePropName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
340 t.Errorf("JSONWritePropName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
341 }
342 })
343 }
344 }
345
346 func Test_JSONWriteQuestionValue(t *testing.T) {
347 type args struct {
348 b *[]byte
349 q Question
350 }
351 tests := []struct {
352 name string
353 args args
354 wantNotEmpty bool
355 }{
356 // TODO: Add test cases.
357 }
358 for _, tt := range tests {
359 t.Run(tt.name, func(t *testing.T) {
360 if gotNotEmpty := JSONWriteQuestionValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty {
361 t.Errorf("JSONWriteQuestionValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
362 }
363 })
364 }
365 }
366
367 func Test_JSONWriteStringValue(t *testing.T) {
368 type args struct {
369 b *[]byte
370 s string
371 }
372 tests := []struct {
373 name string
374 args args
375 wantNotEmpty bool
376 }{
377 // TODO: Add test cases.
378 }
379 for _, tt := range tests {
380 t.Run(tt.name, func(t *testing.T) {
381 if gotNotEmpty := JSONWriteStringValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
382 t.Errorf("JSONWriteStringValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
383 }
384 })
385 }
386 }
387
388 func Test_JSONWriteStringProp(t *testing.T) {
389 type args struct {
390 b *[]byte
391 n string
392 s string
393 }
394 tests := []struct {
395 name string
396 args args
397 wantNotEmpty bool
398 }{
399 // TODO: Add test cases.
400 }
401 for _, tt := range tests {
402 t.Run(tt.name, func(t *testing.T) {
403 if gotNotEmpty := JSONWriteStringProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
404 t.Errorf("JSONWriteStringProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
405 }
406 })
407 }
408 }
409
410 func Test_JSONWriteTimeProp(t *testing.T) {
411 type args struct {
412 b *[]byte
413 n string
414 t time.Time
415 }
416 tests := []struct {
417 name string
418 args args
419 wantNotEmpty bool
420 }{
421 // TODO: Add test cases.
422 }
423 for _, tt := range tests {
424 t.Run(tt.name, func(t *testing.T) {
425 if gotNotEmpty := JSONWriteTimeProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty {
426 t.Errorf("JSONWriteTimeProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
427 }
428 })
429 }
430 }
431
432 func Test_JSONWriteValue(t *testing.T) {
433 type args struct {
434 b *[]byte
435 s []byte
436 }
437 tests := []struct {
438 name string
439 args args
440 wantNotEmpty bool
441 }{
442 // TODO: Add test cases.
443 }
444 for _, tt := range tests {
445 t.Run(tt.name, func(t *testing.T) {
446 if gotNotEmpty := JSONWriteValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty {
447 t.Errorf("JSONWriteValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
448 }
449 })
450 }
451 }
452
453 func mockOb(id IRI, typ ActivityVocabularyType) LinkOrIRI {
454 ob := ObjectNew(typ)
455 ob.ID = id
456 return ob
457 }
458
459 func TestMarshalJSON(t *testing.T) {
460 tests := []struct {
461 name string
462 arg LinkOrIRI
463 want []byte
464 wantErr bool
465 }{
466 {
467 name: "empty",
468 arg: nil,
469 want: []byte("null"),
470 },
471 {
472 name: "Link to example.com",
473 arg: LinkNew("https://example.com", MentionType),
474 want: []byte(`{"id":"https://example.com","type":"Mention"}`),
475 },
476 {
477 name: "Note",
478 arg: mockOb("https://example.com", NoteType),
479 want: []byte(`{"id":"https://example.com","type":"Note"}`),
480 },
481 }
482 for _, tt := range tests {
483 t.Run(tt.name, func(t *testing.T) {
484 got, err := MarshalJSON(tt.arg)
485 if (err != nil) != tt.wantErr {
486 t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
487 return
488 }
489 if !reflect.DeepEqual(got, tt.want) {
490 t.Errorf("MarshalJSON() got = %s, want %s", got, tt.want)
491 }
492 })
493 }
494 }
495
496 func TestJSONWriteValue(t *testing.T) {
497 buff := func(s int) *[]byte {
498 b := make([]byte, 0, s)
499 return &b
500 }
501 type args struct {
502 b *[]byte
503 s []byte
504 }
505 tests := []struct {
506 name string
507 args args
508 want []byte
509 wantNotEmpty bool
510 }{
511 {
512 name: "empty",
513 args: args{
514 b: buff(0),
515 },
516 wantNotEmpty: false,
517 },
518 {
519 name: "do not escape quotes at start-end",
520 args: args{
521 b: buff(20),
522 s: []byte(`"https://example.com"`),
523 },
524 want: []byte(`"https://example.com"`),
525 wantNotEmpty: true,
526 },
527 {
528 name: "escape quotes inside string",
529 args: args{
530 b: buff(80),
531 s: []byte(`"application/ld+json; profile="https://www.w3.org/ns/activitystreams""`),
532 },
533 want: []byte(`"application/ld+json; profile="https://www.w3.org/ns/activitystreams""`),
534 wantNotEmpty: true,
535 },
536 }
537 for _, tt := range tests {
538 t.Run(tt.name, func(t *testing.T) {
539 gotNotEmpty := JSONWriteValue(tt.args.b, tt.args.s)
540 if gotNotEmpty != tt.wantNotEmpty {
541 t.Errorf("JSONWriteStringValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
542 }
543 if tt.wantNotEmpty && !bytes.Equal(*tt.args.b, tt.want) {
544 t.Errorf("JSONWriteValue() = %s, want %s", *tt.args.b, tt.want)
545 }
546 })
547 }
548 }
549
550 func buff(l int) *[]byte {
551 b := make([]byte, 0, l)
552 return &b
553 }
554
555 func TestJSONWriteStringValue(t *testing.T) {
556 type args struct {
557 b *[]byte
558 s string
559 }
560 tests := []struct {
561 name string
562 args args
563 want string
564 wantNotEmpty bool
565 }{
566 {
567 name: "empty",
568 args: args{},
569 want: "",
570 wantNotEmpty: false,
571 },
572 {
573 name: "escaped quote",
574 args: args{
575 b: buff(10),
576 s: `ana"are`,
577 },
578 want: `"ana\"are"`,
579 wantNotEmpty: true,
580 },
581 {
582 name: "already escaped quote",
583 args: args{
584 b: buff(10),
585 s: `ana\"are`,
586 },
587 want: `"ana\"are"`,
588 wantNotEmpty: true,
589 },
590 {
591 name: "already escaped quote and multiple other quotes",
592 args: args{
593 b: buff(10),
594 s: `ana\"""are`,
595 },
596 want: `"ana\"\"\"are"`,
597 wantNotEmpty: true,
598 },
599 {
600 name: "quote at the end",
601 args: args{
602 b: buff(10),
603 s: `anaare"`,
604 },
605 want: `"anaare\""`,
606 wantNotEmpty: true,
607 },
608 }
609 for _, tt := range tests {
610 t.Run(tt.name, func(t *testing.T) {
611 gotNotEmpty := JSONWriteStringValue(tt.args.b, tt.args.s)
612 if gotNotEmpty != tt.wantNotEmpty {
613 t.Errorf("JSONWriteStringValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
614 }
615 if tt.wantNotEmpty && tt.want != string(*tt.args.b) {
616 t.Errorf("JSONWriteStringValue() = %s, want %s", *tt.args.b, tt.want)
617 }
618 })
619 }
620 }
621
622 func TestJSONWriteActivityVocabularyTypes(t *testing.T) {
623 type args struct {
624 b *[]byte
625 v ActivityVocabularyTypes
626 }
627 tests := []struct {
628 name string
629 args args
630 want string
631 wantNotEmpty bool
632 }{
633 {
634 name: "empty",
635 args: args{},
636 want: "",
637 wantNotEmpty: false,
638 },
639 {
640 name: "single type",
641 args: args{
642 b: buff(10),
643 v: ActivityVocabularyTypes{
644 ActivityType,
645 },
646 },
647 want: `"Activity"`,
648 wantNotEmpty: true,
649 },
650 {
651 name: "multiple types",
652 args: args{
653 b: buff(21),
654 v: ActivityVocabularyTypes{
655 ActivityType,
656 AcceptType,
657 },
658 },
659 want: `["Activity","Accept"]`,
660 wantNotEmpty: true,
661 },
662 }
663 for _, tt := range tests {
664 t.Run(tt.name, func(t *testing.T) {
665 gotNotEmpty := JSONWriteActivityVocabularyTypes(tt.args.b, tt.args.v)
666 if gotNotEmpty != tt.wantNotEmpty {
667 t.Errorf("JSONWriteActivityVocabularyTypes() = %v, want %v", gotNotEmpty, tt.wantNotEmpty)
668 }
669 if tt.wantNotEmpty && tt.want != string(*tt.args.b) {
670 t.Errorf("JSONWriteActivityVocabularyTypes() = %s, want %s", *tt.args.b, tt.want)
671 }
672 })
673 }
674 }