|
12 | 12 | private init(){ |
13 | 13 | } |
14 | 14 |
|
15 | | - // MARK: - Tests |
| 15 | + // MARK: - XCTest |
16 | 16 |
|
17 | 17 | {% for method in protocol.allMethods|!definedInExtension %} |
18 | | - |
| 18 | + |
19 | 19 | public static func XCTCallsCount( |
20 | 20 | _ mock: {% call mockName protocol %}, |
21 | 21 | {% call swiftifyMethodNameV2 method %}NumberOfCalls numberOfCalls: Int |
|
26 | 26 | "Wrong {{ method.name }} number of called on {{ protocol.name }}" |
27 | 27 | ) |
28 | 28 | } |
29 | | - |
| 29 | + |
30 | 30 | public static func XCTCalled( |
31 | 31 | _ mock: {% call mockName protocol %}, |
32 | 32 | {% call swiftifyMethodNameV2 method %}Called isCalled: Bool |
|
136 | 136 | } |
137 | 137 |
|
138 | 138 | {% endfor %} |
| 139 | + |
| 140 | + // MARK: - Swift Testing |
| 141 | + |
| 142 | + {% for method in protocol.allMethods|!definedInExtension %} |
| 143 | + |
| 144 | + public static func expectCallsCount( |
| 145 | + _ mock: {% call mockName protocol %}, |
| 146 | + {% call swiftifyMethodNameV2 method %}NumberOfCalls numberOfCalls: Int, |
| 147 | + sourceLocation: Testing.SourceLocation = #_sourceLocation |
| 148 | + ) { |
| 149 | + #expect( |
| 150 | + mock.{% call swiftifyMethodNameV2 method %}CallsCount == numberOfCalls, |
| 151 | + "Wrong {{ method.name }} number of called on {{ protocol.name }}. Expected \(numberOfCalls), got \(mock.{% call swiftifyMethodNameV2 method %}CallsCount)", |
| 152 | + sourceLocation: sourceLocation |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + public static func expectCalled( |
| 157 | + _ mock: {% call mockName protocol %}, |
| 158 | + {% call swiftifyMethodNameV2 method %}Called isCalled: Bool, |
| 159 | + sourceLocation: Testing.SourceLocation = #_sourceLocation |
| 160 | + ) { |
| 161 | + #expect( |
| 162 | + mock.{% call swiftifyMethodNameV2 method %}Called == isCalled, |
| 163 | + "Wrong {{ method.name }} isCalled on {{ protocol.name }}. Expected \(isCalled), got \(mock.{% call swiftifyMethodNameV2 method %}Called)", |
| 164 | + sourceLocation: sourceLocation |
| 165 | + ) |
| 166 | + } |
| 167 | + |
| 168 | + public static func expect{% for key, value in method.annotations where value == "Identical" %}{% if forloop.first %}< |
| 169 | + {% for param in method.parameters where method.annotations[param.name] == "Identical" %} |
| 170 | + {% call dynamicTypeName param.name %}: AnyObject{% if not forloop.last or method.annotations["return"] == "Identical" %},{% endif %} |
| 171 | + {% endfor %} |
| 172 | + {% if method.annotations["return"] == "Identical" %} |
| 173 | + {% call dynamicTypeName "return" %}: AnyObject |
| 174 | + {% endif %} |
| 175 | + >{% endif %}{% endfor %}( |
| 176 | + _ mock: {% call mockName protocol %}, |
| 177 | + expectedNumberOfCalls: Int, |
| 178 | + {% for param in method.parameters %} |
| 179 | + {% call givenParameter param %}: {% if method.annotations[param.name] == "Identical" %}{% call dynamicTypeName param.name %}{% elif protocol.associatedTypes[param.typeName] != nil %}{% call mockName protocol %}.{{ protocol.associatedTypes[param.typeName].name }}Mock{% else %}{{ '(' if param.isClosure or param.typeName|contains:"any" }}{{ param.typeName | replace:"?","" }}{{ ')' if param.isClosure or param.typeName|contains:"any" }}{% endif %}? = nil{% if not forloop.last or not method.returnTypeName.isVoid %}, {% endif %} |
| 180 | + {% endfor %} |
| 181 | + {% if not method.returnTypeName.isVoid %}expectedReturnValue: {% if method.annotations["return"] == "Identical" %}{% call dynamicTypeName "return" %}{% elif protocol.associatedTypes["Return"] != nil %}{% call mockName protocol %}.ReturnMock{% if method.isOptionalReturnType %}?{% endif %}{% else %}{{ method.returnTypeName }}{% endif %},{% endif %} |
| 182 | + sourceLocation: Testing.SourceLocation = #_sourceLocation |
| 183 | + ) { |
| 184 | + // Count |
| 185 | + #expect( |
| 186 | + mock.{% call swiftifyMethodNameV2 method %}CallsCount == expectedNumberOfCalls, |
| 187 | + "Wrong {{ method.name }} number of called on {{ protocol.name }}. Expected \(expectedNumberOfCalls), got \(mock.{% call swiftifyMethodNameV2 method %}CallsCount)", |
| 188 | + sourceLocation: sourceLocation |
| 189 | + ) |
| 190 | + |
| 191 | + // Parameters |
| 192 | + if expectedNumberOfCalls > 0 { |
| 193 | + {% if method.parameters.count > 1 %} |
| 194 | + let args = mock.{% call swiftifyMethodNameV2 method %}ReceivedArguments |
| 195 | + |
| 196 | + {% endif %} |
| 197 | + {% for param in method.parameters %} |
| 198 | + // {{ param.name|upperFirstLetter }} |
| 199 | + if let {% call givenParameter param %} { |
| 200 | + |
| 201 | + {% if method.annotations[param.name] == "Identical" %} |
| 202 | + {% if method.parameters.count == 1 %} |
| 203 | + guard let receivedValue = mock.{% call swiftifyMethodNameV2 method %}Received{{ param.name|upperFirstLetter }} as? _{{ param.name|upperFirstLetter }} else { |
| 204 | + Issue.record("Wrong {{ method.name }} {{ param.name }} parameter type on {{ protocol.name }}", sourceLocation: sourceLocation) |
| 205 | + return |
| 206 | + } |
| 207 | + #expect( |
| 208 | + receivedValue === given{{ param.name | snakeToCamelCase }}, |
| 209 | + "Wrong {{ method.name }} {{ param.name }} parameter on {{ protocol.name }}", |
| 210 | + sourceLocation: sourceLocation |
| 211 | + ) |
| 212 | + {% else %} |
| 213 | + guard let receivedValue = args?.{{ param.name }} as? _{{ param.name|upperFirstLetter }} else { |
| 214 | + Issue.record("Wrong {{ method.name }} {{ param.name }} parameter type on {{ protocol.name }}", sourceLocation: sourceLocation) |
| 215 | + return |
| 216 | + } |
| 217 | + #expect( |
| 218 | + receivedValue === given{{ param.name | snakeToCamelCase }}, |
| 219 | + "Wrong {{ method.name }} {{ param.name }} parameter on {{ protocol.name }}", |
| 220 | + sourceLocation: sourceLocation |
| 221 | + ) |
| 222 | + {% endif %} |
| 223 | + {% else %} |
| 224 | + {% if method.parameters.count == 1 %} |
| 225 | + #expect( |
| 226 | + mock.{% call swiftifyMethodNameV2 method %}Received{{ param.name|upperFirstLetter }} == given{{ param.name | snakeToCamelCase }}, |
| 227 | + "Wrong {{ method.name }} {{ param.name }} parameter on {{ protocol.name }}. Expected \(String(describing: given{{ param.name | snakeToCamelCase }})), got \(String(describing: mock.{% call swiftifyMethodNameV2 method %}Received{{ param.name|upperFirstLetter }}))", |
| 228 | + sourceLocation: sourceLocation |
| 229 | + ) |
| 230 | + {% else %} |
| 231 | + #expect( |
| 232 | + args?.{{ param.name }} == given{{ param.name | snakeToCamelCase }}, |
| 233 | + "Wrong {{ method.name }} {{ param.name }} parameter on {{ protocol.name }}. Expected \(String(describing: given{{ param.name | snakeToCamelCase }})), got \(String(describing: args?.{{ param.name }}))", |
| 234 | + sourceLocation: sourceLocation |
| 235 | + ) |
| 236 | + {% endif %} |
| 237 | + {% endif %} |
| 238 | + } else { |
| 239 | + #expect( |
| 240 | + {% if method.parameters.count == 1 %}mock.{% call swiftifyMethodNameV2 method %}Received{{ param.name|upperFirstLetter }}{% else %}args?.{{ param.name }}{% endif %} == nil, |
| 241 | + "Wrong {{ method.name }} {{ param.name }} parameter value on {{ protocol.name }}. Should be nil but got \(String(describing: {% if method.parameters.count == 1 %}mock.{% call swiftifyMethodNameV2 method %}Received{{ param.name|upperFirstLetter }}{% else %}args?.{{ param.name }}{% endif %}))", |
| 242 | + sourceLocation: sourceLocation |
| 243 | + ) |
| 244 | + } |
| 245 | + |
| 246 | + {% endfor %} |
| 247 | + } |
| 248 | + |
| 249 | + {% if not method.returnTypeName.isVoid %} |
| 250 | + // Return |
| 251 | + {% if method.isOptionalReturnType %}if let expectedReturnValue { {% endif %} |
| 252 | + {% if method.annotations["return"] == "Identical" %} |
| 253 | + guard let returnValue = mock.{% call swiftifyMethodNameV2 method %}ReturnValue as? _Return else { |
| 254 | + Issue.record("Wrong {{ method.name }} return value type on {{ protocol.name }}", sourceLocation: sourceLocation) |
| 255 | + return |
| 256 | + } |
| 257 | + #expect( |
| 258 | + returnValue === expectedReturnValue, |
| 259 | + "Wrong {{ method.name }} return value on {{ protocol.name }}", |
| 260 | + sourceLocation: sourceLocation |
| 261 | + ) |
| 262 | + {% else %} |
| 263 | + #expect( |
| 264 | + mock.{% call swiftifyMethodNameV2 method %}ReturnValue == expectedReturnValue, |
| 265 | + "Wrong {{ method.name }} return value on {{ protocol.name }}. Expected \(String(describing: expectedReturnValue)), got \(String(describing: mock.{% call swiftifyMethodNameV2 method %}ReturnValue))", |
| 266 | + sourceLocation: sourceLocation |
| 267 | + ) |
| 268 | + {% endif %} |
| 269 | + {% endif %} |
| 270 | + {% if method.isOptionalReturnType %} } else { |
| 271 | + #expect( |
| 272 | + mock.{% call swiftifyMethodNameV2 method %}ReturnValue == nil, |
| 273 | + "Wrong {{ method.name }} return value on {{ protocol.name }}. Should be nil but got \(String(describing: mock.{% call swiftifyMethodNameV2 method %}ReturnValue))", |
| 274 | + sourceLocation: sourceLocation |
| 275 | + ) |
| 276 | + } |
| 277 | + {% endif %} |
| 278 | + } |
| 279 | + |
| 280 | + {% endfor %} |
139 | 281 | } |
140 | 282 |
|
141 | 283 | {% endif %}{% endfor %} |
0 commit comments