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