]>
Commit | Line | Data |
---|---|---|
72dd37f1 JH |
1 | package hotline |
2 | ||
3 | import ( | |
48ecb30d | 4 | "github.com/stretchr/testify/assert" |
d9bc63a1 | 5 | "github.com/stretchr/testify/mock" |
a2ef262a | 6 | "io" |
72dd37f1 JH |
7 | "testing" |
8 | ) | |
9 | ||
d9bc63a1 JH |
10 | type mockThreadNewsMgr struct { |
11 | mock.Mock | |
12 | } | |
13 | ||
14 | func (m *mockThreadNewsMgr) ListArticles(newsPath []string) NewsArtListData { | |
15 | args := m.Called(newsPath) | |
16 | ||
17 | return args.Get(0).(NewsArtListData) | |
18 | } | |
19 | ||
20 | func (m *mockThreadNewsMgr) GetArticle(newsPath []string, articleID uint32) *NewsArtData { | |
21 | args := m.Called(newsPath, articleID) | |
22 | ||
23 | return args.Get(0).(*NewsArtData) | |
24 | } | |
25 | func (m *mockThreadNewsMgr) DeleteArticle(newsPath []string, articleID uint32, recursive bool) error { | |
26 | args := m.Called(newsPath, articleID, recursive) | |
27 | ||
28 | return args.Error(0) | |
29 | } | |
30 | ||
31 | func (m *mockThreadNewsMgr) PostArticle(newsPath []string, parentArticleID uint32, article NewsArtData) error { | |
32 | args := m.Called(newsPath, parentArticleID, article) | |
33 | ||
34 | return args.Error(0) | |
35 | } | |
36 | func (m *mockThreadNewsMgr) CreateGrouping(newsPath []string, name string, itemType [2]byte) error { | |
37 | args := m.Called(newsPath, name, itemType) | |
38 | ||
39 | return args.Error(0) | |
40 | } | |
41 | ||
42 | func (m *mockThreadNewsMgr) GetCategories(paths []string) []NewsCategoryListData15 { | |
43 | args := m.Called(paths) | |
44 | ||
45 | return args.Get(0).([]NewsCategoryListData15) | |
46 | } | |
47 | ||
48 | func (m *mockThreadNewsMgr) NewsItem(newsPath []string) NewsCategoryListData15 { | |
49 | args := m.Called(newsPath) | |
50 | ||
51 | return args.Get(0).(NewsCategoryListData15) | |
52 | } | |
53 | ||
54 | func (m *mockThreadNewsMgr) DeleteNewsItem(newsPath []string) error { | |
55 | args := m.Called(newsPath) | |
56 | ||
57 | return args.Error(0) | |
58 | } | |
59 | ||
72dd37f1 JH |
60 | func TestNewsCategoryListData15_MarshalBinary(t *testing.T) { |
61 | type fields struct { | |
9cf66aea | 62 | Type [2]byte |
72dd37f1 JH |
63 | Name string |
64 | Articles map[uint32]*NewsArtData | |
65 | SubCats map[string]NewsCategoryListData15 | |
66 | Count []byte | |
0ed51327 JH |
67 | AddSN [4]byte |
68 | DeleteSN [4]byte | |
69 | GUID [16]byte | |
72dd37f1 JH |
70 | } |
71 | tests := []struct { | |
72 | name string | |
73 | fields fields | |
74 | wantData []byte | |
75 | wantErr bool | |
76 | }{ | |
77 | { | |
78 | name: "returns expected bytes when type is a bundle", | |
79 | fields: fields{ | |
9cf66aea | 80 | Type: [2]byte{0x00, 0x02}, |
72dd37f1 JH |
81 | Articles: map[uint32]*NewsArtData{ |
82 | uint32(1): { | |
83 | Title: "", | |
84 | Poster: "", | |
85 | Data: "", | |
86 | }, | |
87 | }, | |
88 | Name: "foo", | |
89 | }, | |
90 | wantData: []byte{ | |
91 | 0x00, 0x02, | |
92 | 0x00, 0x01, | |
93 | 0x03, | |
94 | 0x66, 0x6f, 0x6f, | |
95 | }, | |
96 | wantErr: false, | |
97 | }, | |
98 | { | |
99 | name: "returns expected bytes when type is a category", | |
100 | fields: fields{ | |
9cf66aea | 101 | Type: [2]byte{0x00, 0x03}, |
72dd37f1 JH |
102 | Articles: map[uint32]*NewsArtData{ |
103 | uint32(1): { | |
104 | Title: "", | |
105 | Poster: "", | |
106 | Data: "", | |
107 | }, | |
108 | }, | |
109 | Name: "foo", | |
110 | }, | |
111 | wantData: []byte{ | |
112 | 0x00, 0x03, | |
113 | 0x00, 0x01, | |
114 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
48ecb30d JH |
115 | 0x00, 0x00, 0x00, 0x00, |
116 | 0x00, 0x00, 0x00, 0x00, | |
72dd37f1 JH |
117 | 0x03, |
118 | 0x66, 0x6f, 0x6f, | |
119 | }, | |
120 | wantErr: false, | |
121 | }, | |
122 | } | |
123 | for _, tt := range tests { | |
124 | t.Run(tt.name, func(t *testing.T) { | |
125 | newscat := &NewsCategoryListData15{ | |
126 | Type: tt.fields.Type, | |
127 | Name: tt.fields.Name, | |
128 | Articles: tt.fields.Articles, | |
129 | SubCats: tt.fields.SubCats, | |
72dd37f1 JH |
130 | AddSN: tt.fields.AddSN, |
131 | DeleteSN: tt.fields.DeleteSN, | |
132 | GUID: tt.fields.GUID, | |
133 | } | |
a2ef262a | 134 | gotData, err := io.ReadAll(newscat) |
9cf66aea | 135 | if newscat.Type == [2]byte{0, 3} { |
72dd37f1 JH |
136 | // zero out the random GUID before comparison |
137 | for i := 4; i < 20; i++ { | |
138 | gotData[i] = 0 | |
139 | } | |
140 | } | |
141 | if (err != nil) != tt.wantErr { | |
142 | t.Errorf("MarshalBinary() error = %v, wantErr %v", err, tt.wantErr) | |
143 | return | |
144 | } | |
48ecb30d | 145 | if !assert.Equal(t, tt.wantData, gotData) { |
72dd37f1 JH |
146 | t.Errorf("MarshalBinary() gotData = %v, want %v", gotData, tt.wantData) |
147 | } | |
148 | }) | |
149 | } | |
150 | } |