github.com/Ptt-official-app/go-bbs@v0.12.0/pttbbs/fav_test.go (about) 1 // Copyright 2020 Pichu Chen, The PTT APP Authors 2 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package pttbbs 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "fmt" 21 "io/ioutil" 22 "strings" 23 "testing" 24 "time" 25 ) 26 27 var time0 = time.Unix(int64(0), 0) 28 29 func TestUnmarshalFavItem(t *testing.T) { 30 data := []byte{3, 1, 2} 31 item, _, err := UnmarshalFavItem(data, 0) 32 if err != nil || item == nil { 33 t.Fatal("Failed to parse Line") 34 } 35 line, ok := item.Item.(*FavLineItem) 36 if !ok { 37 t.Error("Parsed item should be a FavLineItem") 38 } 39 if item.FavType != FavItemTypeLine { 40 t.Errorf("Line type should be FavItemTypeLine but was %d", item.FavType) 41 } 42 if line.LineID != 2 { 43 t.Errorf("Line should have ID 2 but was %d", line.LineID) 44 } 45 46 encoded, err := item.MarshalBinary() 47 if bytes.Compare(data, encoded) != 0 { 48 t.Errorf("Encoded FavLineItem should be equal to the input") 49 } 50 } 51 52 func TestOpenFavFile(t *testing.T) { 53 type testCase struct { 54 filename string 55 expected *FavFile 56 } 57 testCases := []*testCase{ 58 { 59 filename: "01.fav", 60 expected: &FavFile{ 61 Version: 3363, 62 Folder: &FavFolder{ 63 NAlloc: 10, 64 DataTail: 2, 65 NBoards: 2, 66 NLines: 0, 67 NFolders: 0, 68 LineID: 0, 69 FolderID: 0, 70 FavItems: []*FavItem{ 71 { 72 FavType: 1, 73 FavAttr: 1, 74 Item: &FavBoardItem{ 75 BoardID: 14, 76 LastVisit: time0, 77 Attr: 0, 78 }, 79 }, 80 { 81 FavType: 1, 82 FavAttr: 1, 83 Item: &FavBoardItem{ 84 BoardID: 6, 85 LastVisit: time0, 86 Attr: 0, 87 }, 88 }, 89 }, 90 }, 91 }, 92 }, 93 { 94 filename: "02.fav", 95 expected: &FavFile{ 96 Version: 3363, 97 Folder: &FavFolder{ 98 NAlloc: 12, 99 DataTail: 4, 100 NBoards: 3, 101 NLines: 0, 102 NFolders: 1, 103 LineID: 0, 104 FolderID: 1, 105 FavItems: []*FavItem{ 106 { 107 FavType: 2, 108 FavAttr: 1, 109 Item: &FavFolderItem{ 110 FolderID: 1, 111 Title: "新的目錄", 112 ThisFolder: &FavFolder{ 113 NAlloc: 11, 114 DataTail: 3, 115 NBoards: 0, 116 NLines: 2, 117 NFolders: 1, 118 LineID: 2, 119 FolderID: 1, 120 FavItems: []*FavItem{ 121 { 122 FavType: 3, 123 FavAttr: 1, 124 Item: &FavLineItem{LineID: 1}, 125 }, 126 { 127 FavType: 2, 128 FavAttr: 1, 129 Item: &FavFolderItem{ 130 FolderID: 1, 131 Title: "Folder02", 132 ThisFolder: &FavFolder{ 133 NAlloc: 9, 134 DataTail: 1, 135 NBoards: 0, 136 NLines: 0, 137 NFolders: 1, 138 LineID: 0, 139 FolderID: 1, 140 FavItems: []*FavItem{ 141 { 142 FavType: 2, 143 FavAttr: 1, 144 Item: &FavFolderItem{ 145 FolderID: 1, 146 Title: "Folder03", 147 ThisFolder: &FavFolder{ 148 NAlloc: 12, 149 DataTail: 4, 150 NBoards: 1, 151 NLines: 1, 152 NFolders: 2, 153 LineID: 1, 154 FolderID: 2, 155 FavItems: []*FavItem{ 156 { 157 FavType: 2, 158 FavAttr: 1, 159 Item: &FavFolderItem{ 160 FolderID: 1, 161 Title: "Folder04", 162 ThisFolder: &FavFolder{ 163 NAlloc: 9, 164 DataTail: 1, 165 NBoards: 1, 166 NLines: 0, 167 NFolders: 0, 168 LineID: 0, 169 FolderID: 0, 170 FavItems: []*FavItem{ 171 { 172 FavType: 1, 173 FavAttr: 1, 174 Item: &FavBoardItem{ 175 BoardID: 14, 176 LastVisit: time0, 177 Attr: 0, 178 }, 179 }, 180 }, 181 }, 182 }, 183 }, 184 { 185 FavType: 1, 186 FavAttr: 1, 187 Item: &FavBoardItem{ 188 BoardID: 14, 189 LastVisit: time0, 190 Attr: 0, 191 }, 192 }, 193 { 194 FavType: 3, 195 FavAttr: 1, 196 Item: &FavLineItem{LineID: 1}, 197 }, 198 { 199 FavType: 2, 200 FavAttr: 1, 201 Item: &FavFolderItem{ 202 FolderID: 2, 203 Title: "MAX Length:2345672234567890323456789042345678905", 204 ThisFolder: &FavFolder{ 205 NAlloc: 8, 206 DataTail: 0, 207 NBoards: 0, 208 NLines: 0, 209 NFolders: 0, 210 LineID: 0, 211 FolderID: 0, 212 FavItems: []*FavItem{}, 213 }, 214 }, 215 }, 216 }, 217 }, 218 }, 219 }, 220 }, 221 }, 222 }, 223 }, 224 { 225 FavType: 3, 226 FavAttr: 1, 227 Item: &FavLineItem{LineID: 2}, 228 }, 229 }, 230 }, 231 }, 232 }, 233 { 234 FavType: 1, 235 FavAttr: 1, 236 Item: &FavBoardItem{ 237 BoardID: 13, 238 LastVisit: time0, 239 Attr: 0, 240 }, 241 }, 242 { 243 FavType: 1, 244 FavAttr: 1, 245 Item: &FavBoardItem{ 246 BoardID: 14, 247 LastVisit: time0, 248 Attr: 0, 249 }, 250 }, 251 { 252 FavType: 1, 253 FavAttr: 1, 254 Item: &FavBoardItem{ 255 BoardID: 6, 256 LastVisit: time0, 257 Attr: 0, 258 }, 259 }, 260 }, 261 }, 262 }, 263 }, 264 } 265 266 for _, c := range testCases { 267 filePath := fmt.Sprintf("testcase/fav/%s", c.filename) 268 fav, err := OpenFavFile(filePath) 269 if err != nil { 270 t.Fatal(err) 271 } 272 favJSON, _ := json.Marshal(fav) 273 expectedJSON, _ := json.Marshal(c.expected) 274 if strings.Compare(string(favJSON), string(expectedJSON)) != 0 { 275 t.Errorf("Parsed result does not equal to expected. Expected json string: %s \nParsed json string: %s", string(expectedJSON), string(favJSON)) 276 } 277 for _, item := range fav.Folder.FavItems { 278 switch item.FavType { 279 case FavItemTypeBoard: 280 if target := item.GetBoard(); target == nil { 281 t.Error("Fav item type is Board should be able to GetBoard()") 282 } 283 case FavItemTypeFolder: 284 if target := item.GetFolder(); target == nil { 285 t.Error("Fav item type is Folder should be able to GetFolder()") 286 } 287 case FavItemTypeLine: 288 if target := item.GetLine(); target == nil { 289 t.Error("Fav item type is Line should be able to GetLine()") 290 } 291 default: 292 t.Error("invalid fav type") 293 } 294 } 295 296 expectedBytes, err := ioutil.ReadFile(filePath) 297 if err != nil { 298 t.Fatal(err) 299 } 300 encoded, err := fav.MarshalBinary() 301 if bytes.Compare(expectedBytes, encoded) != 0 { 302 t.Errorf("Encoded FavFile should be equal to the input. Expected: \n%v\nEncoded: \n%v\n ", expectedBytes, encoded) 303 } 304 } 305 306 }