]> git.r.bdr.sh - rbdr/mobius/blob - hotline/news_test.go
a2102b23c79959147d0446252857fffcc2de0c5d
[rbdr/mobius] / hotline / news_test.go
1 package hotline
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func TestNewsCategoryListData15_MarshalBinary(t *testing.T) {
9 type fields struct {
10 Type [2]byte
11 Name string
12 Articles map[uint32]*NewsArtData
13 SubCats map[string]NewsCategoryListData15
14 Count []byte
15 AddSN [4]byte
16 DeleteSN [4]byte
17 GUID [16]byte
18 }
19 tests := []struct {
20 name string
21 fields fields
22 wantData []byte
23 wantErr bool
24 }{
25 {
26 name: "returns expected bytes when type is a bundle",
27 fields: fields{
28 Type: [2]byte{0x00, 0x02},
29 Articles: map[uint32]*NewsArtData{
30 uint32(1): {
31 Title: "",
32 Poster: "",
33 Data: "",
34 },
35 },
36 Name: "foo",
37 },
38 wantData: []byte{
39 0x00, 0x02,
40 0x00, 0x01,
41 0x03,
42 0x66, 0x6f, 0x6f,
43 },
44 wantErr: false,
45 },
46 {
47 name: "returns expected bytes when type is a category",
48 fields: fields{
49 Type: [2]byte{0x00, 0x03},
50 Articles: map[uint32]*NewsArtData{
51 uint32(1): {
52 Title: "",
53 Poster: "",
54 Data: "",
55 },
56 },
57 Name: "foo",
58 },
59 wantData: []byte{
60 0x00, 0x03,
61 0x00, 0x01,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x01,
64 0x00, 0x00, 0x00, 0x02,
65 0x03,
66 0x66, 0x6f, 0x6f,
67 },
68 wantErr: false,
69 },
70 }
71 for _, tt := range tests {
72 t.Run(tt.name, func(t *testing.T) {
73 newscat := &NewsCategoryListData15{
74 Type: tt.fields.Type,
75 Name: tt.fields.Name,
76 Articles: tt.fields.Articles,
77 SubCats: tt.fields.SubCats,
78 AddSN: tt.fields.AddSN,
79 DeleteSN: tt.fields.DeleteSN,
80 GUID: tt.fields.GUID,
81 }
82 gotData, err := newscat.MarshalBinary()
83 if newscat.Type == [2]byte{0, 3} {
84 // zero out the random GUID before comparison
85 for i := 4; i < 20; i++ {
86 gotData[i] = 0
87 }
88 }
89 if (err != nil) != tt.wantErr {
90 t.Errorf("MarshalBinary() error = %v, wantErr %v", err, tt.wantErr)
91 return
92 }
93 if !reflect.DeepEqual(gotData, tt.wantData) {
94 t.Errorf("MarshalBinary() gotData = %v, want %v", gotData, tt.wantData)
95 }
96 })
97 }
98 }