OSDN Git Service

swiftlintを元に戻した
[kcd/KCD.git] / KCDTests / GuardEscapeTest.swift
1 //
2 //  GuardEscapeTest.swift
3 //  KCDTests
4 //
5 //  Created by Hori,Masaki on 2017/12/03.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import XCTest
10
11 @testable import KCD
12
13 import SwiftyJSON
14
15 class GuardEscapeTest: XCTestCase {
16
17     override func setUp() {
18         super.setUp()
19         
20         let store = TemporaryDataStore.oneTimeEditor()
21         store.guardEscaped().forEach(store.delete)
22     }
23     
24     override func tearDown() {
25         
26         let store = TemporaryDataStore.oneTimeEditor()
27         store.guardEscaped().forEach(store.delete)
28         super.tearDown()
29     }
30
31     func testShipEntity() {
32         
33         let shipId = 5225
34         let serverStore = ServerDataStore.default
35         
36         guard let ship = serverStore.ship(by: shipId) else {
37             XCTFail("can not get Ship id \(shipId)")
38             return
39         }
40         
41         XCTAssertFalse(ship.guardEscaped)
42         
43         let tempStore = TemporaryDataStore.oneTimeEditor()
44         guard let g = tempStore.createGuardEscaped() else {
45             XCTFail("can not create GuardEscaped")
46             return
47         }
48         
49         g.shipID = shipId
50         g.ensured = true
51         
52         try? tempStore.save()
53         
54         XCTAssertTrue(ship.guardEscaped)
55     }
56     
57     // 護衛退避
58     func testGuardShelter() {
59         
60         // 艦隊を設定
61         do {
62             let store = ServerDataStore.oneTimeEditor()
63             
64             guard let deck1 = store.deck(by: 1) else { return XCTFail("Can not get Deck.") }
65             (0...5).forEach { deck1.setShip(id: $0 + 1, for: $0) }
66             guard let deck2 = store.deck(by: 2) else { return XCTFail("Can not get Deck.") }
67             (0...5).forEach { deck2.setShip(id: $0 + 1 + 6, for: $0) }
68         }
69         
70         // Battle生成
71         do {
72             
73             let store = TemporaryDataStore.oneTimeEditor()
74             store.guardEscaped().forEach(store.delete)
75             store.battles().forEach(store.delete)
76             guard let battle = store.createBattle() else {
77                 XCTFail("Can not create battle")
78                 return
79             }
80             
81             battle.deckId = 1
82         }
83         
84         do {
85             let rawValue: [String: Any] = [
86                 "api_result": 1,
87                 "api_data": [
88                     "api_escape": [
89                         "api_escape_idx": [9, 12],
90                         "api_tow_idx": [10]
91                     ]
92                 ]
93             ]
94             
95             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
96             let param = Parameter(["Test": "Test"])
97             let resultApi = APIResponse(api: API(endpointPath: Endpoint.battleResult.rawValue), parameter: param, json: json)
98             GuardShelterCommand(apiResponse: resultApi).execute()
99             
100             let goBackApi = APIResponse(api: API(endpointPath: Endpoint.goback.rawValue), parameter: param, json: json)
101             GuardShelterCommand(apiResponse: goBackApi).execute()
102         }
103         
104         do {
105             
106             let store = ServerDataStore.default
107             let ships1 = store.ships(byDeckId: 1)
108             XCTAssertEqual(ships1.count, 6)
109             XCTAssertEqual(ships1[0].guardEscaped, false)
110             XCTAssertEqual(ships1[1].guardEscaped, false)
111             XCTAssertEqual(ships1[2].guardEscaped, false)
112             XCTAssertEqual(ships1[3].guardEscaped, false)
113             XCTAssertEqual(ships1[4].guardEscaped, false)
114             XCTAssertEqual(ships1[5].guardEscaped, false)
115             
116             let ships2 = store.ships(byDeckId: 2)
117             XCTAssertEqual(ships2.count, 6)
118             XCTAssertEqual(ships2[0].guardEscaped, false)
119             XCTAssertEqual(ships2[1].guardEscaped, false)
120             XCTAssertEqual(ships2[2].guardEscaped, true)
121             XCTAssertEqual(ships2[3].guardEscaped, true)
122             XCTAssertEqual(ships2[4].guardEscaped, false)
123             XCTAssertEqual(ships2[5].guardEscaped, false)
124         }
125         
126         do {
127             let store = TemporaryDataStore.oneTimeEditor()
128             store.guardEscaped().forEach(store.delete)
129             store.battles().forEach(store.delete)
130         }
131     }
132     
133     // 単艦退避
134     func testShelter() {
135         
136         // 艦隊を設定
137         do {
138             let store = ServerDataStore.oneTimeEditor()
139             
140             guard let deck1 = store.deck(by: 3) else { return XCTFail("Can not get Deck.") }
141             (0...6).forEach { deck1.setShip(id: $0 + 5, for: $0) }
142         }
143         
144         // Battle生成
145         do {
146             
147             let store = TemporaryDataStore.oneTimeEditor()
148             store.guardEscaped().forEach(store.delete)
149             store.battles().forEach(store.delete)
150             guard let battle = store.createBattle() else {
151                 XCTFail("Can not create battle")
152                 return
153             }
154             
155             battle.deckId = 3
156         }
157         
158         do {
159             let rawValue: [String: Any] = [
160                 "api_result": 1,
161                 "api_data": [
162                     "api_escape": [
163                         "api_escape_idx": [3, 4]
164                     ]
165                 ]
166             ]
167             
168             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
169             let param = Parameter(["Test": "Test"])
170             let resultApi = APIResponse(api: API(endpointPath: Endpoint.battleResult.rawValue), parameter: param, json: json)
171             GuardShelterCommand(apiResponse: resultApi).execute()
172             
173             let goBackApi = APIResponse(api: API(endpointPath: Endpoint.gobakAlone.rawValue), parameter: param, json: json)
174             GuardShelterCommand(apiResponse: goBackApi).execute()
175         }
176         
177         do {
178             
179             let store = ServerDataStore.default
180             let ships1 = store.ships(byDeckId: 3)
181             XCTAssertEqual(ships1.count, 7)
182             XCTAssertEqual(ships1[0].guardEscaped, false)
183             XCTAssertEqual(ships1[1].guardEscaped, false)
184             XCTAssertEqual(ships1[2].guardEscaped, true)
185             XCTAssertEqual(ships1[3].guardEscaped, false)
186             XCTAssertEqual(ships1[4].guardEscaped, false)
187             XCTAssertEqual(ships1[5].guardEscaped, false)
188             XCTAssertEqual(ships1[6].guardEscaped, false)
189
190         }
191         
192         do {
193             let store = TemporaryDataStore.oneTimeEditor()
194             store.guardEscaped().forEach(store.delete)
195             store.battles().forEach(store.delete)
196         }
197     }
198
199 }