]>
Commit | Line | Data |
---|---|---|
72dd37f1 JH |
1 | package hotline |
2 | ||
3 | import ( | |
48ecb30d | 4 | "github.com/stretchr/testify/assert" |
72dd37f1 JH |
5 | "testing" |
6 | ) | |
7 | ||
8 | func TestNewsCategoryListData15_MarshalBinary(t *testing.T) { | |
9 | type fields struct { | |
9cf66aea | 10 | Type [2]byte |
72dd37f1 JH |
11 | Name string |
12 | Articles map[uint32]*NewsArtData | |
13 | SubCats map[string]NewsCategoryListData15 | |
14 | Count []byte | |
0ed51327 JH |
15 | AddSN [4]byte |
16 | DeleteSN [4]byte | |
17 | GUID [16]byte | |
72dd37f1 JH |
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{ | |
9cf66aea | 28 | Type: [2]byte{0x00, 0x02}, |
72dd37f1 JH |
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{ | |
9cf66aea | 49 | Type: [2]byte{0x00, 0x03}, |
72dd37f1 JH |
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, | |
48ecb30d JH |
63 | 0x00, 0x00, 0x00, 0x00, |
64 | 0x00, 0x00, 0x00, 0x00, | |
72dd37f1 JH |
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, | |
72dd37f1 JH |
78 | AddSN: tt.fields.AddSN, |
79 | DeleteSN: tt.fields.DeleteSN, | |
80 | GUID: tt.fields.GUID, | |
81 | } | |
82 | gotData, err := newscat.MarshalBinary() | |
9cf66aea | 83 | if newscat.Type == [2]byte{0, 3} { |
72dd37f1 JH |
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 | } | |
48ecb30d | 93 | if !assert.Equal(t, tt.wantData, gotData) { |
72dd37f1 JH |
94 | t.Errorf("MarshalBinary() gotData = %v, want %v", gotData, tt.wantData) |
95 | } | |
96 | }) | |
97 | } | |
98 | } |