github.com/kyoukaya/hoxy@v0.0.0-20191001035635-40cb85a94340/proxy/json/unmarshaller_test.go (about) 1 package json_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "go/importer" 7 "github.com/kyoukaya/hoxy/defs" 8 . "github.com/kyoukaya/hoxy/proxy/json" 9 "io/ioutil" 10 "os" 11 "strings" 12 "testing" 13 "unicode" 14 15 "github.com/sergi/go-diff/diffmatchpatch" 16 ) 17 18 func compareOutput(orig, marshalled []byte) error { 19 if !bytes.Equal(marshalled, orig) { 20 dmp := diffmatchpatch.New() 21 diffs := dmp.DiffMain(string(marshalled), string(orig), false) 22 23 diff := dmp.PatchToText(dmp.PatchMake(diffs)) 24 return fmt.Errorf("marshalled JSON is not the same as original\n"+ 25 "Output: %s\nOrig: %s\n%s", marshalled, orig, diff) 26 } 27 return nil 28 } 29 30 func nameToOpString(typeName string) string { 31 upCount := 0 32 nameBuf := bytes.Buffer{} 33 for _, v := range typeName { 34 if unicode.IsUpper(v) { 35 upCount++ 36 if upCount == 3 { 37 nameBuf.WriteString("/") 38 v = unicode.ToLower(v) 39 } 40 } 41 nameBuf.WriteRune(v) 42 } 43 return nameBuf.String() 44 } 45 46 func testMarshalAndCompare(t *testing.T, marFunc MarshalFunc, data interface{}, orig []byte) { 47 mar, err := marFunc(data) 48 if err != nil { 49 t.Error(err) 50 } 51 if !bytes.Equal(mar, orig) { 52 t.Errorf("Marshalled output different from original\n"+ 53 "Original: %s\n"+ 54 "Output: %s", orig, mar) 55 } 56 } 57 58 // Test that all defs declared in hoxy/defs are initialized in proxy.DefMap. 59 // Requires source. 60 func TestDefExists(t *testing.T) { 61 pkg, err := importer.For("source", nil).Import("github.com/kyoukaya/hoxy/defs") 62 if err != nil { 63 t.Errorf("error: %s\n", err.Error()) 64 return 65 } 66 exportedNames := make(map[string]bool) 67 for _, name := range pkg.Scope().Names() { 68 exportedNames[strings.ToLower(nameToOpString(name))] = true 69 } 70 // All exported structs should be in DefMap 71 for name, _ := range exportedNames { 72 _, ok := DefMap[name] 73 if !ok { 74 t.Errorf("%s in defs but not in DefMap", name) 75 } 76 } 77 // All structs in DefMap should be exported by defs 78 for name, _ := range DefMap { 79 if !exportedNames[name] { 80 t.Errorf("%s in DefMap but not in defs", name) 81 } 82 } 83 } 84 85 func loadFile(t *testing.T, fileName string) []byte { 86 f, err := os.Open("testdata/" + fileName + ".json") 87 if err != nil { 88 t.Error(err) 89 } 90 b, err := ioutil.ReadAll(f) 91 if err != nil { 92 t.Error(err) 93 } 94 return b 95 } 96 97 func TestGetUidEnMicaQueue(t *testing.T) { 98 op := "SIndex/getUidEnMicaQueue" 99 orig := loadFile(t, "SMicaQueue") 100 ret, marFunc, err := UnMarshal(op, orig) 101 casted, ok := ret.(*defs.SIndexGetUidEnMicaQueue) 102 if !ok { 103 t.Error("Failed to cast.") 104 } 105 if err != nil { 106 testMarshalAndCompare(t, marFunc, casted, orig) 107 } 108 } 109 110 func TestSIndexIndex(t *testing.T) { 111 op := "SIndex/index" 112 orig := loadFile(t, "SIndexIndex") 113 ret, marFunc, err := UnMarshal(op, orig) 114 if err != nil { 115 t.Error(err) 116 } 117 casted, ok := ret.(*defs.SIndexIndex) 118 if !ok { 119 t.Error("Failed to cast") 120 } 121 if err != nil { 122 testMarshalAndCompare(t, marFunc, casted, orig) 123 } 124 } 125 126 func TestSIndexDownloadSuccess(t *testing.T) { 127 op := "SIndex/downloadSuccess" 128 orig := []byte(`1`) 129 ret, marFunc, err := UnMarshal(op, orig) 130 casted, ok := ret.(*defs.SIndexDownloadSuccess) 131 if !ok { 132 t.Error("Failed to cast") 133 } 134 if err != nil { 135 testMarshalAndCompare(t, marFunc, casted, orig) 136 } 137 } 138 139 func TestSIndexGetMailList(t *testing.T) { 140 op := "SIndex/getMailList" 141 orig := []byte(`[]`) 142 ret, marFunc, err := UnMarshal(op, orig) 143 casted, ok := ret.(*defs.SIndexGetMailList) 144 if !ok { 145 t.Error("Failed to cast") 146 } 147 if err != nil { 148 testMarshalAndCompare(t, marFunc, casted, orig) 149 } 150 } 151 152 func TestSIndexHome(t *testing.T) { 153 op := "SIndex/home" 154 orig := loadFile(t, "SIndexHome") 155 ret, marFunc, err := UnMarshal(op, orig) 156 casted, ok := ret.(*defs.SIndexHome) 157 if !ok { 158 t.Error("Failed to cast") 159 } 160 if err != nil { 161 testMarshalAndCompare(t, marFunc, casted, orig) 162 } 163 } 164 func TestCMissionBattleFinish(t *testing.T) { 165 op := "CMission/battleFinish" 166 orig := loadFile(t, "CMissionBattleFinish") 167 ret, marFunc, err := UnMarshal(op, orig) 168 casted, ok := ret.(*defs.CMissionBattleFinish) 169 if !ok { 170 t.Error("Failed to cast.") 171 } 172 if err != nil { 173 testMarshalAndCompare(t, marFunc, casted, orig) 174 } 175 } 176 177 func TestSMissionBattleFinish(t *testing.T) { 178 op := "SMission/battleFinish" 179 orig := loadFile(t, "SMissionBattleFinish") 180 ret, marFunc, err := UnMarshal(op, orig) 181 casted, ok := ret.(*defs.SMissionBattleFinish) 182 if !ok { 183 t.Error("Failed to cast.") 184 } 185 if err != nil { 186 testMarshalAndCompare(t, marFunc, casted, orig) 187 } 188 orig = loadFile(t, "SMissionBattleFinish.1") 189 ret, marFunc, err = UnMarshal(op, orig) 190 casted, ok = ret.(*defs.SMissionBattleFinish) 191 if !ok { 192 t.Error("Failed to cast.") 193 } 194 if err != nil { 195 testMarshalAndCompare(t, marFunc, casted, orig) 196 } 197 } 198 199 func TestSMissionDrawEvent(t *testing.T) { 200 op := "SMission/drawEvent" 201 orig := loadFile(t, "SMissionDrawEvent") 202 ret, marFunc, err := UnMarshal(op, orig) 203 casted, ok := ret.(*defs.SMissionDrawEvent) 204 if !ok { 205 t.Error("Failed to cast.") 206 } 207 if err != nil { 208 testMarshalAndCompare(t, marFunc, casted, orig) 209 } 210 } 211 212 func TestSIndexAttendance(t *testing.T) { 213 op := "SIndex/attendance" 214 orig := loadFile(t, "SIndexAttendance") 215 ret, marFunc, err := UnMarshal(op, orig) 216 casted, ok := ret.(*defs.SIndexAttendance) 217 if !ok { 218 t.Error("Failed to cast.") 219 } 220 if err != nil { 221 testMarshalAndCompare(t, marFunc, casted, orig) 222 } 223 } 224 225 func TestSEquipAdjust(t *testing.T) { 226 op := "SEquip/adjust" 227 orig := []byte(`{"pow":10000,"hit":7500}`) 228 ret, marFunc, err := UnMarshal(op, orig) 229 casted, ok := ret.(*defs.SEquipAdjust) 230 if !ok { 231 t.Error("Failed to cast.") 232 } 233 if err != nil { 234 testMarshalAndCompare(t, marFunc, casted, orig) 235 } 236 orig = []byte(`[]`) 237 ret, marFunc, err = UnMarshal(op, orig) 238 casted, ok = ret.(*defs.SEquipAdjust) 239 if !ok { 240 t.Error("Failed to cast.") 241 } 242 if err != nil { 243 testMarshalAndCompare(t, marFunc, casted, orig) 244 } 245 } 246 247 func TestSMissionEndTurn(t *testing.T) { 248 op := "SMission/endTurn" 249 orig := loadFile(t, "SMissionEndTurn.1") 250 ret, marFunc, err := UnMarshal(op, orig) 251 casted, ok := ret.(*defs.SMissionEndTurn) 252 if !ok { 253 t.Error("Failed to cast.") 254 } 255 if err != nil { 256 testMarshalAndCompare(t, marFunc, casted, orig) 257 } 258 orig = loadFile(t, "SMissionEndTurn.2") 259 ret, marFunc, err = UnMarshal(op, orig) 260 casted, ok = ret.(*defs.SMissionEndTurn) 261 if !ok { 262 t.Error("Failed to cast.") 263 } 264 if err != nil { 265 testMarshalAndCompare(t, marFunc, casted, orig) 266 } 267 orig = loadFile(t, "SMissionEndTurn.3") 268 ret, marFunc, err = UnMarshal(op, orig) 269 casted, ok = ret.(*defs.SMissionEndTurn) 270 if !ok { 271 t.Error("Failed to cast.") 272 } 273 if err != nil { 274 testMarshalAndCompare(t, marFunc, casted, orig) 275 } 276 } 277 278 func TestCGunRetireGun(t *testing.T) { 279 op := "CGun/retireGun" 280 orig := []byte(`[1,2,3,4]`) 281 ret, marFunc, err := UnMarshal(op, orig) 282 casted, ok := ret.(*defs.CGunRetireGun) 283 if !ok { 284 t.Error("Failed to cast.") 285 } 286 if err != nil { 287 t.Error(err) 288 testMarshalAndCompare(t, marFunc, casted, orig) 289 } 290 } 291 292 func TestSOuthouseEstablishBuild(t *testing.T) { 293 op := "SOuthouse/establish_build" 294 orig := loadFile(t, "SOuthouseEstablish_build") 295 ret, marFunc, err := UnMarshal(op, orig) 296 casted, ok := ret.(*defs.SOuthouseEstablish_build) 297 if !ok { 298 t.Error("Failed to cast.") 299 } 300 if err != nil { 301 t.Error(err) 302 testMarshalAndCompare(t, marFunc, casted, orig) 303 } 304 } 305 306 func TestSFriendList(t *testing.T) { 307 op := "SFriend/list" 308 orig := loadFile(t, "SFriendList") 309 ret, marFunc, err := UnMarshal(op, orig) 310 casted, ok := ret.(*defs.SFriendList) 311 if !ok { 312 t.Error("Failed to cast.") 313 } 314 if err != nil { 315 t.Error(err) 316 testMarshalAndCompare(t, marFunc, casted, orig) 317 } 318 } 319 320 func Benchmark(b *testing.B) { 321 op := "SFriend/teamGuns" 322 orig := loadFile(nil, "SFriendTeamGuns") 323 UnMarshal(op, orig) 324 } 325 326 func TestSFriendDormInfo(t *testing.T) { 327 op := "SFriend/dormInfo" 328 orig := loadFile(t, "SFriendDormInfo") 329 ret, marFunc, err := UnMarshal(op, orig) 330 casted, ok := ret.(*defs.SFriendDormInfo) 331 if !ok { 332 t.Error(casted, ok) 333 } 334 if err != nil { 335 testMarshalAndCompare(t, marFunc, casted, orig) 336 } 337 orig = loadFile(t, "SFriendDormInfo.1") 338 ret, marFunc, err = UnMarshal(op, orig) 339 casted, ok = ret.(*defs.SFriendDormInfo) 340 if !ok { 341 t.Error(casted, ok) 342 } 343 if err != nil { 344 t.Error(err) 345 testMarshalAndCompare(t, marFunc, casted, orig) 346 } 347 } 348 349 func TestSStartMission(t *testing.T) { 350 op := "SMission/startMission" 351 orig := loadFile(t, "SMissionStartMission") 352 ret, marFunc, err := UnMarshal(op, orig) 353 casted, ok := ret.(*defs.SMissionStartMission) 354 if !ok { 355 t.Error(casted, ok) 356 } 357 if err != nil { 358 testMarshalAndCompare(t, marFunc, casted, orig) 359 } 360 } 361 362 func TestSIndexQuest(t *testing.T) { 363 op := "SIndex/Quest" 364 orig := loadFile(t, "SIndexQuest") 365 ret, marFunc, err := UnMarshal(op, orig) 366 casted, ok := ret.(*defs.SIndexQuest) 367 if !ok { 368 t.Error(casted, ok) 369 } 370 if err != nil { 371 testMarshalAndCompare(t, marFunc, casted, orig) 372 } 373 } 374 375 func TestSGunDevelopCollectList(t *testing.T) { 376 op := "SGun/developCollectList" 377 orig := loadFile(t, "SGunDevelopCollectList") 378 ret, marFunc, err := UnMarshal(op, orig) 379 casted, ok := ret.(*defs.SGunDevelopCollectList) 380 if !ok { 381 t.Error(casted, ok) 382 } 383 if err != nil { 384 testMarshalAndCompare(t, marFunc, casted, orig) 385 } 386 } 387 388 func TestSGunDevelopLog(t *testing.T) { 389 op := "SGun/developLog" 390 orig := loadFile(t, "SGunDevelopLog") 391 ret, marFunc, err := UnMarshal(op, orig) 392 casted, ok := ret.(*defs.SGunDevelopLog) 393 if !ok { 394 t.Error(casted, ok) 395 } 396 if err != nil { 397 testMarshalAndCompare(t, marFunc, casted, orig) 398 } 399 } 400 401 func TestSMissionReinforceFriendTeam(t *testing.T) { 402 op := "SMission/reinforceFriendTeam" 403 orig := loadFile(t, "SMissionReinforceFriendTeam") 404 ret, marFunc, err := UnMarshal(op, orig) 405 casted, ok := ret.(*defs.SMissionReinforceFriendTeam) 406 if !ok { 407 t.Error(casted, ok) 408 } 409 if err != nil { 410 testMarshalAndCompare(t, marFunc, casted, orig) 411 } 412 }