OSDN Git Service

ColorSetを変更
[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 import Doutaku
15
16 class GuardEscapeTest: XCTestCase {
17
18     override func setUp() {
19         
20         super.setUp()
21         
22         let store = TemporaryDataStore.oneTimeEditor()
23         store.guardEscaped().forEach(store.delete)
24     }
25     
26     override func tearDown() {
27         
28         let store = TemporaryDataStore.oneTimeEditor()
29         store.guardEscaped().forEach(store.delete)
30         
31         super.tearDown()
32     }
33
34     func testShipEntity() {
35         
36         let shipId = 5225
37         let serverStore = ServerDataStore.default
38         
39         guard let ship = serverStore.ship(by: shipId) else {
40             
41             XCTFail("can not get Ship id \(shipId)")
42             
43             return
44         }
45         
46         XCTAssertFalse(ship.guardEscaped)
47         
48         let tempStore = TemporaryDataStore.oneTimeEditor()
49         guard let g = tempStore.createGuardEscaped() else {
50             
51             XCTFail("can not create GuardEscaped")
52             
53             return
54         }
55         
56         g.shipID = shipId
57         g.ensured = true
58         
59         tempStore.save()
60         
61         XCTAssertTrue(ship.guardEscaped)
62     }
63     
64     // 護衛退避
65     func testGuardShelter() {
66         
67         // 艦隊を設定
68         do {
69             
70             let store = ServerDataStore.oneTimeEditor()
71             
72             guard let deck1 = store.deck(by: 1) else {
73                 
74                 XCTFail("Can not get Deck.")
75                 
76                 return
77             }
78             (0...5).forEach { deck1.setShip(id: $0 + 1, for: $0) }
79             guard let deck2 = store.deck(by: 2) else {
80                 
81                 XCTFail("Can not get Deck.")
82                 
83                 return
84             }
85             (0...5).forEach { deck2.setShip(id: $0 + 1 + 6, for: $0) }
86         }
87         
88         // Battle生成
89         do {
90             
91             let store = TemporaryDataStore.oneTimeEditor()
92             store.guardEscaped().forEach(store.delete)
93             store.battles().forEach(store.delete)
94             guard let battle = store.createBattle() else {
95                 
96                 XCTFail("Can not create battle")
97                 
98                 return
99             }
100             
101             battle.deckId = 1
102         }
103         
104         do {
105             
106             let rawValue: [String: Any] = [
107                 "api_result": 1,
108                 "api_data": [
109                     "api_escape": [
110                         "api_escape_idx": [9, 12],
111                         "api_tow_idx": [10]
112                     ]
113                 ]
114             ]
115             
116             guard let json = JSON(rawValue: rawValue) else {
117                 
118                 XCTFail("json is nil")
119                 
120                 return
121             }
122             let param = Parameter(["Test": "Test"])
123             let resultApi = APIResponse(api: API(endpointPath: Endpoint.battleResult.rawValue), parameter: param, json: json)
124             GuardShelterCommand(apiResponse: resultApi).execute()
125             
126             let goBackApi = APIResponse(api: API(endpointPath: Endpoint.goback.rawValue), parameter: param, json: json)
127             GuardShelterCommand(apiResponse: goBackApi).execute()
128         }
129         
130         do {
131             
132             let store = ServerDataStore.default
133             let ships1 = store.ships(byDeckId: 1)
134             XCTAssertEqual(ships1.count, 6)
135             XCTAssertEqual(ships1[0].guardEscaped, false)
136             XCTAssertEqual(ships1[1].guardEscaped, false)
137             XCTAssertEqual(ships1[2].guardEscaped, false)
138             XCTAssertEqual(ships1[3].guardEscaped, false)
139             XCTAssertEqual(ships1[4].guardEscaped, false)
140             XCTAssertEqual(ships1[5].guardEscaped, false)
141             
142             let ships2 = store.ships(byDeckId: 2)
143             XCTAssertEqual(ships2.count, 6)
144             XCTAssertEqual(ships2[0].guardEscaped, false)
145             XCTAssertEqual(ships2[1].guardEscaped, false)
146             XCTAssertEqual(ships2[2].guardEscaped, true)
147             XCTAssertEqual(ships2[3].guardEscaped, true)
148             XCTAssertEqual(ships2[4].guardEscaped, false)
149             XCTAssertEqual(ships2[5].guardEscaped, false)
150         }
151         
152         do {
153             
154             let store = TemporaryDataStore.oneTimeEditor()
155             store.guardEscaped().forEach(store.delete)
156             store.battles().forEach(store.delete)
157         }
158     }
159     
160     // 単艦退避
161     func testShelter() {
162         
163         // 艦隊を設定
164         do {
165             
166             let store = ServerDataStore.oneTimeEditor()
167             
168             guard let deck1 = store.deck(by: 3) else {
169                 
170                 XCTFail("Can not get Deck.")
171                 
172                 return
173             }
174             (0...6).forEach { deck1.setShip(id: $0 + 5, for: $0) }
175         }
176         
177         // Battle生成
178         do {
179             
180             let store = TemporaryDataStore.oneTimeEditor()
181             store.guardEscaped().forEach(store.delete)
182             store.battles().forEach(store.delete)
183             guard let battle = store.createBattle() else {
184                 
185                 XCTFail("Can not create battle")
186                 
187                 return
188             }
189             
190             battle.deckId = 3
191         }
192         
193         do {
194             let rawValue: [String: Any] = [
195                 "api_result": 1,
196                 "api_data": [
197                     "api_escape": [
198                         "api_escape_idx": [3, 4]
199                     ]
200                 ]
201             ]
202             
203             guard let json = JSON(rawValue: rawValue) else {
204                 
205                 XCTFail("json is nil")
206                 
207                 return
208             }
209             let param = Parameter(["Test": "Test"])
210             let resultApi = APIResponse(api: API(endpointPath: Endpoint.battleResult.rawValue), parameter: param, json: json)
211             GuardShelterCommand(apiResponse: resultApi).execute()
212             
213             let goBackApi = APIResponse(api: API(endpointPath: Endpoint.gobakAlone.rawValue), parameter: param, json: json)
214             GuardShelterCommand(apiResponse: goBackApi).execute()
215         }
216         
217         do {
218             
219             let store = ServerDataStore.default
220             let ships1 = store.ships(byDeckId: 3)
221             XCTAssertEqual(ships1.count, 7)
222             XCTAssertEqual(ships1[0].guardEscaped, false)
223             XCTAssertEqual(ships1[1].guardEscaped, false)
224             XCTAssertEqual(ships1[2].guardEscaped, true)
225             XCTAssertEqual(ships1[3].guardEscaped, false)
226             XCTAssertEqual(ships1[4].guardEscaped, false)
227             XCTAssertEqual(ships1[5].guardEscaped, false)
228             XCTAssertEqual(ships1[6].guardEscaped, false)
229
230         }
231         
232         do {
233             
234             let store = TemporaryDataStore.oneTimeEditor()
235             store.guardEscaped().forEach(store.delete)
236             store.battles().forEach(store.delete)
237         }
238     }
239
240 }