Best Quick code snippet using that
ResponseSerializationTests.swift
Source:ResponseSerializationTests.swift
1//2// ResponseSerializationTests.swift3//4// Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)5//6// Permission is hereby granted, free of charge, to any person obtaining a copy7// of this software and associated documentation files (the "Software"), to deal8// in the Software without restriction, including without limitation the rights9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10// copies of the Software, and to permit persons to whom the Software is11// furnished to do so, subject to the following conditions:12//13// The above copyright notice and this permission notice shall be included in14// all copies or substantial portions of the Software.15//16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22// THE SOFTWARE.23//24import Alamofire25import Foundation26import XCTest27final class DataResponseSerializationTestCase: BaseTestCase {28 // MARK: Properties29 private let error = AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)30 // MARK: DataResponseSerializer31 func testThatDataResponseSerializerSucceedsWhenDataIsNotNil() {32 // Given33 let serializer = DataResponseSerializer()34 let data = Data("data".utf8)35 // When36 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }37 // Then38 XCTAssertTrue(result.isSuccess)39 XCTAssertNotNil(result.value)40 XCTAssertNil(result.error)41 }42 func testThatDataResponseSerializerFailsWhenDataIsNil() {43 // Given44 let serializer = DataResponseSerializer()45 // When46 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: nil) }47 // Then48 XCTAssertTrue(result.isFailure)49 XCTAssertNil(result.value)50 XCTAssertNotNil(result.error)51 if let error = result.error?.asAFError {52 XCTAssertTrue(error.isInputDataNilOrZeroLength)53 } else {54 XCTFail("error should not be nil")55 }56 }57 func testThatDataResponseSerializerFailsWhenErrorIsNotNil() {58 // Given59 let serializer = DataResponseSerializer()60 // When61 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: error) }62 // Then63 XCTAssertTrue(result.isFailure)64 XCTAssertNil(result.value)65 XCTAssertNotNil(result.error)66 if let error = result.error?.asAFError {67 XCTAssertTrue(error.isInputDataNilOrZeroLength)68 } else {69 XCTFail("error should not be nil")70 }71 }72 func testThatDataResponseSerializerFailsWhenDataIsNilWithNonEmptyResponseStatusCode() {73 // Given74 let serializer = DataResponseSerializer()75 let response = HTTPURLResponse(statusCode: 200)76 // When77 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }78 // Then79 XCTAssertTrue(result.isFailure, "result is failure should be true")80 XCTAssertNil(result.value, "result value should be nil")81 XCTAssertNotNil(result.error, "result error should not be nil")82 if let error = result.error?.asAFError {83 XCTAssertTrue(error.isInputDataNilOrZeroLength)84 } else {85 XCTFail("error should not be nil")86 }87 }88 func testThatDataResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {89 // Given90 let serializer = DataResponseSerializer()91 let response = HTTPURLResponse(statusCode: 204)92 // When93 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }94 // Then95 XCTAssertTrue(result.isSuccess)96 XCTAssertNotNil(result.value)97 XCTAssertNil(result.error)98 if let data = result.value {99 XCTAssertEqual(data.count, 0)100 }101 }102 // MARK: StringResponseSerializer103 func testThatStringResponseSerializerFailsWhenDataIsNil() {104 // Given105 let serializer = DataResponseSerializer()106 // When107 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: nil) }108 // Then109 XCTAssertTrue(result.isFailure)110 XCTAssertNil(result.value)111 XCTAssertNotNil(result.error)112 if let error = result.error?.asAFError {113 XCTAssertTrue(error.isInputDataNilOrZeroLength)114 } else {115 XCTFail("error should not be nil")116 }117 }118 func testThatStringResponseSerializerFailsWhenDataIsEmpty() {119 // Given120 let serializer = StringResponseSerializer()121 // When122 let result = Result { try serializer.serialize(request: nil, response: nil, data: Data(), error: nil) }123 // Then124 XCTAssertTrue(result.isFailure)125 XCTAssertNil(result.value)126 XCTAssertNotNil(result.error)127 if let error = result.error?.asAFError {128 XCTAssertTrue(error.isInputDataNilOrZeroLength)129 } else {130 XCTFail("error should not be nil")131 }132 }133 func testThatStringResponseSerializerSucceedsWithUTF8DataAndNoProvidedEncoding() {134 let serializer = StringResponseSerializer()135 let data = Data("data".utf8)136 // When137 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }138 // Then139 XCTAssertTrue(result.isSuccess)140 XCTAssertNotNil(result.value)141 XCTAssertNil(result.error)142 }143 func testThatStringResponseSerializerSucceedsWithUTF8DataAndUTF8ProvidedEncoding() {144 let serializer = StringResponseSerializer(encoding: .utf8)145 let data = Data("data".utf8)146 // When147 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }148 // Then149 XCTAssertTrue(result.isSuccess)150 XCTAssertNotNil(result.value)151 XCTAssertNil(result.error)152 }153 func testThatStringResponseSerializerSucceedsWithUTF8DataUsingResponseTextEncodingName() {154 let serializer = StringResponseSerializer()155 let data = Data("data".utf8)156 let response = HTTPURLResponse(statusCode: 200, headers: ["Content-Type": "image/jpeg; charset=utf-8"])157 // When158 let result = Result { try serializer.serialize(request: nil, response: response, data: data, error: nil) }159 // Then160 XCTAssertTrue(result.isSuccess)161 XCTAssertNotNil(result.value)162 XCTAssertNil(result.error)163 }164 func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ProvidedEncoding() {165 // Given166 let serializer = StringResponseSerializer(encoding: .utf8)167 let data = "random data".data(using: .utf32)!168 // When169 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }170 // Then171 XCTAssertTrue(result.isFailure)172 XCTAssertNil(result.value)173 XCTAssertNotNil(result.error)174 if let error = result.error?.asAFError, let failedEncoding = error.failedStringEncoding {175 XCTAssertTrue(error.isStringSerializationFailed)176 XCTAssertEqual(failedEncoding, .utf8)177 } else {178 XCTFail("error should not be nil")179 }180 }181 func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ResponseEncoding() {182 // Given183 let serializer = StringResponseSerializer()184 let data = "random data".data(using: .utf32)!185 let response = HTTPURLResponse(statusCode: 200, headers: ["Content-Type": "image/jpeg; charset=utf-8"])186 // When187 let result = Result { try serializer.serialize(request: nil, response: response, data: data, error: nil) }188 // Then189 XCTAssertTrue(result.isFailure)190 XCTAssertNil(result.value)191 XCTAssertNotNil(result.error)192 if let error = result.error?.asAFError, let failedEncoding = error.failedStringEncoding {193 XCTAssertTrue(error.isStringSerializationFailed)194 XCTAssertEqual(failedEncoding, .utf8)195 } else {196 XCTFail("error should not be nil")197 }198 }199 func testThatStringResponseSerializerFailsWhenErrorIsNotNil() {200 // Given201 let serializer = StringResponseSerializer()202 // When203 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: error) }204 // Then205 XCTAssertTrue(result.isFailure)206 XCTAssertNil(result.value)207 XCTAssertNotNil(result.error)208 if let error = result.error?.asAFError {209 XCTAssertTrue(error.isInputDataNilOrZeroLength)210 } else {211 XCTFail("error should not be nil")212 }213 }214 func testThatStringResponseSerializerFailsWhenDataIsNilWithNonEmptyResponseStatusCode() {215 // Given216 let serializer = StringResponseSerializer()217 let response = HTTPURLResponse(statusCode: 200)218 // When219 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }220 // Then221 XCTAssertTrue(result.isFailure)222 XCTAssertNil(result.value)223 XCTAssertNotNil(result.error)224 if let error = result.error?.asAFError {225 XCTAssertTrue(error.isInputDataNilOrZeroLength)226 } else {227 XCTFail("error should not be nil")228 }229 }230 func testThatStringResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {231 // Given232 let serializer = StringResponseSerializer()233 let response = HTTPURLResponse(statusCode: 205)234 // When235 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }236 // Then237 XCTAssertTrue(result.isSuccess)238 XCTAssertNotNil(result.value)239 XCTAssertNil(result.error)240 if let string = result.value {241 XCTAssertEqual(string, "")242 }243 }244 // MARK: JSONResponseSerializer245 func testThatJSONResponseSerializerFailsWhenDataIsNil() {246 // Given247 let serializer = JSONResponseSerializer()248 // When249 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: nil) }250 // Then251 XCTAssertTrue(result.isFailure)252 XCTAssertNil(result.value)253 XCTAssertNotNil(result.error)254 if let error = result.error?.asAFError {255 XCTAssertTrue(error.isInputDataNilOrZeroLength)256 } else {257 XCTFail("error should not be nil")258 }259 }260 func testThatJSONResponseSerializerFailsWhenDataIsEmpty() {261 // Given262 let serializer = JSONResponseSerializer()263 // When264 let result = Result { try serializer.serialize(request: nil, response: nil, data: Data(), error: nil) }265 // Then266 XCTAssertTrue(result.isFailure)267 XCTAssertNil(result.value)268 XCTAssertNotNil(result.error)269 if let error = result.error?.asAFError {270 XCTAssertTrue(error.isInputDataNilOrZeroLength)271 } else {272 XCTFail("error should not be nil")273 }274 }275 func testThatJSONResponseSerializerSucceedsWhenDataIsValidJSON() {276 // Given277 let serializer = JSONResponseSerializer()278 let data = Data("{\"json\": true}".utf8)279 // When280 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }281 // Then282 XCTAssertTrue(result.isSuccess)283 XCTAssertNotNil(result.value)284 XCTAssertNil(result.error)285 }286 func testThatJSONResponseSerializerFailsWhenDataIsInvalidJSON() {287 // Given288 let serializer = JSONResponseSerializer()289 let data = Data("definitely not valid json".utf8)290 // When291 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }292 // Then293 XCTAssertTrue(result.isFailure)294 XCTAssertNil(result.value)295 XCTAssertNotNil(result.error)296 if let error = result.error?.asAFError, let underlyingError = error.underlyingError as? CocoaError {297 XCTAssertTrue(error.isJSONSerializationFailed)298 XCTAssertEqual(underlyingError.errorCode, 3840)299 } else {300 XCTFail("error should not be nil")301 }302 }303 func testThatJSONResponseSerializerFailsWhenErrorIsNotNil() {304 // Given305 let serializer = JSONResponseSerializer()306 // When307 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: error) }308 // Then309 XCTAssertTrue(result.isFailure)310 XCTAssertNil(result.value)311 XCTAssertNotNil(result.error)312 if let error = result.error?.asAFError {313 XCTAssertTrue(error.isInputDataNilOrZeroLength)314 } else {315 XCTFail("error should not be nil")316 }317 }318 func testThatJSONResponseSerializerFailsWhenDataIsNilWithNonEmptyResponseStatusCode() {319 // Given320 let serializer = JSONResponseSerializer()321 let response = HTTPURLResponse(statusCode: 200)322 // When323 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }324 // Then325 XCTAssertTrue(result.isFailure)326 XCTAssertNil(result.value)327 XCTAssertNotNil(result.error)328 if let error = result.error?.asAFError {329 XCTAssertTrue(error.isInputDataNilOrZeroLength)330 } else {331 XCTFail("error should not be nil")332 }333 }334 func testThatJSONResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {335 // Given336 let serializer = JSONResponseSerializer()337 let response = HTTPURLResponse(statusCode: 204)338 // When339 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }340 // Then341 XCTAssertTrue(result.isSuccess)342 XCTAssertNotNil(result.value)343 XCTAssertNil(result.error)344 if let json = result.value as? NSNull {345 XCTAssertEqual(json, NSNull())346 } else {347 XCTFail("json should not be nil")348 }349 }350}351// MARK: -352final class DecodableResponseSerializerTests: BaseTestCase {353 private let error = AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)354 struct DecodableValue: Decodable {355 let string: String356 }357 func testThatDecodableResponseSerializerFailsWhenDataIsNil() {358 // Given359 let serializer = DecodableResponseSerializer<DecodableValue>()360 // When361 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: nil) }362 // Then363 XCTAssertTrue(result.isFailure)364 XCTAssertNil(result.value)365 XCTAssertNotNil(result.error)366 if let error = result.error?.asAFError {367 XCTAssertTrue(error.isInputDataNilOrZeroLength)368 } else {369 XCTFail("error should not be nil")370 }371 }372 func testThatDecodableResponseSerializerFailsWhenDataIsEmpty() {373 // Given374 let serializer = DecodableResponseSerializer<DecodableValue>()375 // When376 let result = Result { try serializer.serialize(request: nil, response: nil, data: Data(), error: nil) }377 // Then378 XCTAssertTrue(result.isFailure)379 XCTAssertNil(result.value)380 XCTAssertNotNil(result.error)381 if let error = result.error?.asAFError {382 XCTAssertTrue(error.isInputDataNilOrZeroLength)383 } else {384 XCTFail("error should not be nil")385 }386 }387 func testThatDecodableResponseSerializerSucceedsWhenDataIsValidJSON() {388 // Given389 let data = Data("{\"string\":\"string\"}".utf8)390 let serializer = DecodableResponseSerializer<DecodableValue>()391 // When392 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }393 // Then394 XCTAssertTrue(result.isSuccess)395 XCTAssertNotNil(result.value)396 XCTAssertEqual(result.value?.string, "string")397 XCTAssertNil(result.error)398 }399 func testThatDecodableResponseSerializerFailsWhenDataIsInvalidRepresentation() {400 // Given401 let serializer = DecodableResponseSerializer<DecodableValue>()402 let data = Data("definitely not valid".utf8)403 // When404 let result = Result { try serializer.serialize(request: nil, response: nil, data: data, error: nil) }405 // Then406 XCTAssertTrue(result.isFailure)407 XCTAssertNil(result.value)408 XCTAssertNotNil(result.error)409 }410 func testThatDecodableResponseSerializerFailsWhenErrorIsNotNil() {411 // Given412 let serializer = DecodableResponseSerializer<DecodableValue>()413 // When414 let result = Result { try serializer.serialize(request: nil, response: nil, data: nil, error: error) }415 // Then416 XCTAssertTrue(result.isFailure)417 XCTAssertNil(result.value)418 XCTAssertNotNil(result.error)419 if let error = result.error?.asAFError {420 XCTAssertTrue(error.isInputDataNilOrZeroLength)421 } else {422 XCTFail("error should not be nil")423 }424 }425 func testThatDecodableResponseSerializerFailsWhenDataIsNilWithNonEmptyResponseStatusCode() {426 // Given427 let serializer = DecodableResponseSerializer<DecodableValue>()428 let response = HTTPURLResponse(statusCode: 200)429 // When430 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }431 // Then432 XCTAssertTrue(result.isFailure)433 XCTAssertNil(result.value)434 XCTAssertNotNil(result.error)435 if let error = result.error?.asAFError {436 XCTAssertTrue(error.isInputDataNilOrZeroLength)437 } else {438 XCTFail("error should not be nil")439 }440 }441 func testThatDecodableResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {442 // Given443 let serializer = DecodableResponseSerializer<Empty>()444 let response = HTTPURLResponse(statusCode: 204)445 // When446 let result = Result { try serializer.serialize(request: nil, response: response, data: nil, error: nil) }447 // Then448 XCTAssertTrue(result.isSuccess)449 XCTAssertNotNil(result.value)450 XCTAssertNil(result.error)451 }452}453// MARK: -454final class DownloadResponseSerializationTestCase: BaseTestCase {455 // MARK: Properties456 private let error = AFError.responseSerializationFailed(reason: .inputFileNil)457 private var jsonEmptyDataFileURL: URL { return url(forResource: "empty_data", withExtension: "json") }458 private var jsonValidDataFileURL: URL { return url(forResource: "valid_data", withExtension: "json") }459 private var jsonInvalidDataFileURL: URL { return url(forResource: "invalid_data", withExtension: "json") }460 private var plistEmptyDataFileURL: URL { return url(forResource: "empty", withExtension: "data") }461 private var plistValidDataFileURL: URL { return url(forResource: "valid", withExtension: "data") }462 private var plistInvalidDataFileURL: URL { return url(forResource: "invalid", withExtension: "data") }463 private var stringEmptyDataFileURL: URL { return url(forResource: "empty_string", withExtension: "txt") }464 private var stringUTF8DataFileURL: URL { return url(forResource: "utf8_string", withExtension: "txt") }465 private var stringUTF32DataFileURL: URL { return url(forResource: "utf32_string", withExtension: "txt") }466 private var invalidFileURL: URL { return URL(fileURLWithPath: "/this/file/does/not/exist.txt") }467 // MARK: Tests - Data Response Serializer468 func testThatDataResponseSerializerSucceedsWhenFileDataIsNotNil() {469 // Given470 let serializer = DataResponseSerializer()471 // When472 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: jsonValidDataFileURL, error: nil) }473 // Then474 XCTAssertTrue(result.isSuccess)475 XCTAssertNotNil(result.value)476 XCTAssertNil(result.error)477 }478 func testThatDataResponseSerializerFailsWhenFileDataIsEmpty() {479 // Given480 let serializer = DataResponseSerializer()481 // When482 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: jsonEmptyDataFileURL, error: nil) }483 // Then484 XCTAssertTrue(result.isFailure)485 XCTAssertNil(result.value)486 XCTAssertNotNil(result.error)487 if let error = result.error?.asAFError {488 XCTAssertTrue(error.isInputDataNilOrZeroLength)489 } else {490 XCTFail("error should not be nil")491 }492 }493 func testThatDataResponseSerializerFailsWhenFileURLIsNil() {494 // Given495 let serializer = DataResponseSerializer()496 // When497 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: nil, error: nil) }498 // Then499 XCTAssertTrue(result.isFailure)500 XCTAssertNil(result.value)501 XCTAssertNotNil(result.error)502 if let error = result.error?.asAFError {503 XCTAssertTrue(error.isInputFileNil)504 } else {505 XCTFail("error should not be nil")506 }507 }508 func testThatDataResponseSerializerFailsWhenFileURLIsInvalid() {509 // Given510 let serializer = DataResponseSerializer()511 // When512 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: invalidFileURL, error: nil) }513 // Then514 XCTAssertTrue(result.isFailure)515 XCTAssertNil(result.value)516 XCTAssertNotNil(result.error)517 if let error = result.error?.asAFError {518 XCTAssertTrue(error.isInputFileReadFailed)519 } else {520 XCTFail("error should not be nil")521 }522 }523 func testThatDataResponseSerializerFailsWhenErrorIsNotNil() {524 // Given525 let serializer = DataResponseSerializer()526 // When527 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: nil, error: error) }528 // Then529 XCTAssertTrue(result.isFailure)530 XCTAssertNil(result.value)531 XCTAssertNotNil(result.error)532 if let error = result.error?.asAFError {533 XCTAssertTrue(error.isInputFileNil)534 } else {535 XCTFail("error should not be nil")536 }537 }538 func testThatDataResponseSerializerFailsWhenFileURLIsNilWithNonEmptyResponseStatusCode() {539 // Given540 let serializer = DataResponseSerializer()541 let response = HTTPURLResponse(statusCode: 200)542 // When543 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: nil, error: nil) }544 // Then545 XCTAssertTrue(result.isFailure)546 XCTAssertNil(result.value)547 XCTAssertNotNil(result.error)548 if let error = result.error?.asAFError {549 XCTAssertTrue(error.isInputFileNil)550 } else {551 XCTFail("error should not be nil")552 }553 }554 func testThatDataResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {555 // Given556 let serializer = DataResponseSerializer()557 let response = HTTPURLResponse(statusCode: 205)558 // When559 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: jsonEmptyDataFileURL, error: nil) }560 // Then561 XCTAssertTrue(result.isSuccess)562 XCTAssertNotNil(result.value)563 XCTAssertNil(result.error)564 if let data = result.value {565 XCTAssertEqual(data.count, 0)566 } else {567 XCTFail("data should not be nil")568 }569 }570 // MARK: Tests - String Response Serializer571 func testThatStringResponseSerializerFailsWhenFileURLIsNil() {572 // Given573 let serializer = StringResponseSerializer()574 // When575 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: nil, error: nil) }576 // Then577 XCTAssertTrue(result.isFailure)578 XCTAssertNil(result.value)579 XCTAssertNotNil(result.error)580 if let error = result.error?.asAFError {581 XCTAssertTrue(error.isInputFileNil)582 } else {583 XCTFail("error should not be nil")584 }585 }586 func testThatStringResponseSerializerFailsWhenFileURLIsInvalid() {587 // Given588 let serializer = StringResponseSerializer()589 // When590 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: invalidFileURL, error: nil) }591 // Then592 XCTAssertEqual(result.isSuccess, false)593 XCTAssertNil(result.value)594 XCTAssertNotNil(result.error)595 if let error = result.error?.asAFError {596 XCTAssertTrue(error.isInputFileReadFailed)597 } else {598 XCTFail("error should not be nil")599 }600 }601 func testThatStringResponseSerializerFailsWhenFileDataIsEmpty() {602 // Given603 let serializer = StringResponseSerializer()604 // When605 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: stringEmptyDataFileURL, error: nil) }606 // Then607 XCTAssertTrue(result.isFailure)608 XCTAssertNil(result.value)609 XCTAssertNotNil(result.error)610 if let error = result.error?.asAFError {611 XCTAssertTrue(error.isInputDataNilOrZeroLength)612 } else {613 XCTFail("error should not be nil")614 }615 }616 func testThatStringResponseSerializerSucceedsWithUTF8DataAndNoProvidedEncoding() {617 // Given618 let serializer = StringResponseSerializer()619 // When620 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: stringUTF8DataFileURL, error: nil) }621 // Then622 XCTAssertTrue(result.isSuccess)623 XCTAssertNotNil(result.value)624 XCTAssertNil(result.error)625 }626 func testThatStringResponseSerializerSucceedsWithUTF8DataAndUTF8ProvidedEncoding() {627 // Given628 let serializer = StringResponseSerializer(encoding: .utf8)629 // When630 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: stringUTF8DataFileURL, error: nil) }631 // Then632 XCTAssertTrue(result.isSuccess)633 XCTAssertNotNil(result.value)634 XCTAssertNil(result.error)635 }636 func testThatStringResponseSerializerSucceedsWithUTF8DataUsingResponseTextEncodingName() {637 // Given638 let serializer = StringResponseSerializer()639 let response = HTTPURLResponse(statusCode: 200, headers: ["Content-Type": "image/jpeg; charset=utf-8"])640 // When641 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: stringUTF8DataFileURL, error: nil) }642 // Then643 XCTAssertTrue(result.isSuccess)644 XCTAssertNotNil(result.value)645 XCTAssertNil(result.error)646 }647 func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ProvidedEncoding() {648 // Given649 let serializer = StringResponseSerializer(encoding: .utf8)650 // When651 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: stringUTF32DataFileURL, error: nil) }652 // Then653 XCTAssertTrue(result.isFailure)654 XCTAssertNil(result.value)655 XCTAssertNotNil(result.error)656 if let error = result.error?.asAFError, let failedEncoding = error.failedStringEncoding {657 XCTAssertTrue(error.isStringSerializationFailed)658 XCTAssertEqual(failedEncoding, .utf8)659 } else {660 XCTFail("error should not be nil")661 }662 }663 func testThatStringResponseSerializerFailsWithUTF32DataAndUTF8ResponseEncoding() {664 // Given665 let serializer = StringResponseSerializer()666 let response = HTTPURLResponse(statusCode: 200, headers: ["Content-Type": "image/jpeg; charset=utf-8"])667 // When668 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: stringUTF32DataFileURL, error: nil) }669 // Then670 XCTAssertTrue(result.isFailure)671 XCTAssertNil(result.value)672 XCTAssertNotNil(result.error)673 if let error = result.error?.asAFError, let failedEncoding = error.failedStringEncoding {674 XCTAssertTrue(error.isStringSerializationFailed)675 XCTAssertEqual(failedEncoding, .utf8)676 } else {677 XCTFail("error should not be nil")678 }679 }680 func testThatStringResponseSerializerFailsWhenErrorIsNotNil() {681 // Given682 let serializer = StringResponseSerializer()683 // When684 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: nil, error: error) }685 // Then686 XCTAssertTrue(result.isFailure)687 XCTAssertNil(result.value)688 XCTAssertNotNil(result.error)689 if let error = result.error?.asAFError {690 XCTAssertTrue(error.isInputFileNil)691 } else {692 XCTFail("error should not be nil")693 }694 }695 func testThatStringResponseSerializerFailsWhenDataIsNilWithNonEmptyResponseStatusCode() {696 // Given697 let serializer = StringResponseSerializer()698 let response = HTTPURLResponse(statusCode: 200)699 // When700 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: nil, error: nil) }701 // Then702 XCTAssertTrue(result.isFailure)703 XCTAssertNil(result.value)704 XCTAssertNotNil(result.error)705 if let error = result.error?.asAFError {706 XCTAssertTrue(error.isInputFileNil)707 } else {708 XCTFail("error should not be nil")709 }710 }711 func testThatStringResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {712 // Given713 let serializer = StringResponseSerializer()714 let response = HTTPURLResponse(statusCode: 204)715 // When716 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: stringEmptyDataFileURL, error: nil) }717 // Then718 XCTAssertTrue(result.isSuccess)719 XCTAssertNotNil(result.value)720 XCTAssertNil(result.error)721 if let string = result.value {722 XCTAssertEqual(string, "")723 }724 }725 // MARK: Tests - JSON Response Serializer726 func testThatJSONResponseSerializerFailsWhenFileURLIsNil() {727 // Given728 let serializer = JSONResponseSerializer()729 // When730 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: nil, error: nil) }731 // Then732 XCTAssertTrue(result.isFailure)733 XCTAssertNil(result.value)734 XCTAssertNotNil(result.error)735 if let error = result.error?.asAFError {736 XCTAssertTrue(error.isInputFileNil)737 } else {738 XCTFail("error should not be nil")739 }740 }741 func testThatJSONResponseSerializerFailsWhenFileURLIsInvalid() {742 // Given743 let serializer = JSONResponseSerializer()744 // When745 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: invalidFileURL, error: nil) }746 // Then747 XCTAssertTrue(result.isFailure)748 XCTAssertNil(result.value)749 XCTAssertNotNil(result.error)750 if let error = result.error?.asAFError {751 XCTAssertTrue(error.isInputFileReadFailed)752 } else {753 XCTFail("error should not be nil")754 }755 }756 func testThatJSONResponseSerializerFailsWhenFileDataIsEmpty() {757 // Given758 let serializer = JSONResponseSerializer()759 // When760 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: jsonEmptyDataFileURL, error: nil) }761 // Then762 XCTAssertTrue(result.isFailure)763 XCTAssertNil(result.value)764 XCTAssertNotNil(result.error)765 if let error = result.error?.asAFError {766 XCTAssertTrue(error.isInputDataNilOrZeroLength)767 } else {768 XCTFail("error should not be nil")769 }770 }771 func testThatJSONResponseSerializerSucceedsWhenDataIsValidJSON() {772 // Given773 let serializer = JSONResponseSerializer()774 // When775 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: jsonValidDataFileURL, error: nil) }776 // Then777 XCTAssertTrue(result.isSuccess)778 XCTAssertNotNil(result.value)779 XCTAssertNil(result.error)780 }781 func testThatJSONResponseSerializerFailsWhenDataIsInvalidJSON() {782 // Given783 let serializer = JSONResponseSerializer()784 // When785 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: jsonInvalidDataFileURL, error: nil) }786 // Then787 XCTAssertTrue(result.isFailure)788 XCTAssertNil(result.value)789 XCTAssertNotNil(result.error)790 if let error = result.error?.asAFError, let underlyingError = error.underlyingError as? CocoaError {791 XCTAssertTrue(error.isJSONSerializationFailed)792 XCTAssertEqual(underlyingError.errorCode, 3840)793 } else {794 XCTFail("error should not be nil")795 }796 }797 func testThatJSONResponseSerializerFailsWhenErrorIsNotNil() {798 // Given799 let serializer = JSONResponseSerializer()800 // When801 let result = Result { try serializer.serializeDownload(request: nil, response: nil, fileURL: nil, error: error) }802 // Then803 XCTAssertTrue(result.isFailure)804 XCTAssertNil(result.value)805 XCTAssertNotNil(result.error)806 if let error = result.error?.asAFError {807 XCTAssertTrue(error.isInputFileNil)808 } else {809 XCTFail("error should not be nil")810 }811 }812 func testThatJSONResponseSerializerFailsWhenDataIsNilWithNonEmptyResponseStatusCode() {813 // Given814 let serializer = JSONResponseSerializer()815 let response = HTTPURLResponse(statusCode: 200)816 // When817 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: nil, error: nil) }818 // Then819 XCTAssertTrue(result.isFailure)820 XCTAssertNil(result.value)821 XCTAssertNotNil(result.error)822 if let error = result.error?.asAFError {823 XCTAssertTrue(error.isInputFileNil)824 } else {825 XCTFail("error should not be nil")826 }827 }828 func testThatJSONResponseSerializerSucceedsWhenDataIsNilWithEmptyResponseStatusCode() {829 // Given830 let serializer = JSONResponseSerializer()831 let response = HTTPURLResponse(statusCode: 205)832 // When833 let result = Result { try serializer.serializeDownload(request: nil, response: response, fileURL: jsonEmptyDataFileURL, error: nil) }834 // Then835 XCTAssertTrue(result.isSuccess)836 XCTAssertNotNil(result.value)837 XCTAssertNil(result.error)838 if let json = result.value as? NSNull {839 XCTAssertEqual(json, NSNull())840 }841 }842}843final class CustomResponseSerializerTestCases: BaseTestCase {844 func testThatCustomResponseSerializersCanBeWrittenWithoutCompilerIssues() {845 // Given846 final class UselessResponseSerializer: ResponseSerializer {847 func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Data? {848 return data849 }850 }851 let serializer = UselessResponseSerializer()852 let expectation = self.expectation(description: "request should finish")853 var data: Data?854 // When855 AF.request(URLRequest.makeHTTPBinRequest()).response(responseSerializer: serializer) { (response) in856 data = response.data857 expectation.fulfill()858 }859 waitForExpectations(timeout: timeout, handler: nil)860 // Then861 XCTAssertNotNil(data)862 }863}864extension HTTPURLResponse {865 convenience init(statusCode: Int, headers: HTTPHeaders? = nil) {866 let url = URL(string: "https://httpbin.org/get")!867 self.init(url: url, statusCode: statusCode, httpVersion: "HTTP/1.1", headerFields: headers?.dictionary)!868 }869}...
AudioObjectProperty.swift
Source:AudioObjectProperty.swift
...5// Created by Tomohiro Kumagai on 2021/11/17.6//7import CoreAudio8final class AudioObjectProperty : AudioProperty {9 /// An AudioClassID that identifies the class from which the class of the10 /// AudioObject is derived. This value must always be one of the standard classes.11 static let baseClass = AudioObjectProperty(selector: kAudioObjectPropertyBaseClass)12 13 /// An AudioClassID that identifies the class of the AudioObject.14 static let `class` = AudioObjectProperty(selector: kAudioObjectPropertyClass)15 /// An AudioObjectID that identifies the the AudioObject that owns the given16 /// AudioObject. Note that all AudioObjects are owned by some other AudioObject.17 /// The only exception is the AudioSystemObject,18 /// for which the value of this property is kAudioObjectUnknown.19 static let owner = AudioObjectProperty(selector: kAudioObjectPropertyOwner)20 /// A CFString that contains the human readable name of the object.21 /// The caller is responsible for releasing the returned CFObject.22 static let name = AudioObjectProperty(selector: kAudioObjectPropertyName)23 /// A CFString that contains the human readable model name of the object.24 /// The model name differs from kAudioObjectPropertyName in that two objects of the25 /// same model will have the same value for this property but may have different26 /// values for kAudioObjectPropertyName.27 static let modelName = AudioObjectProperty(selector: kAudioObjectPropertyModelName)28 /// A CFString that contains the human readable name of the manufacturer of the29 /// hardware the AudioObject is a part of. The caller is responsible for30 /// releasing the returned CFObject.31 static let manufacturer = AudioObjectProperty(selector: kAudioObjectPropertyManufacturer)32 /// A CFString that contains a human readable name for the given element in the33 /// given scope. The caller is responsible for releasing the returned CFObject.34 static let elementName = AudioObjectProperty(selector: kAudioObjectPropertyElementName)35 /// A CFString that contains a human readable name for the category of the given36 /// element in the given scope. The caller is responsible for releasing the returned CFObject.37 static let categoryName = AudioObjectProperty(selector: kAudioObjectPropertyElementCategoryName)38 /// A CFString that contains a human readable name for the number of the given39 /// element in the given scope. The caller is responsible for releasing the40 /// returned CFObject.41 static let numberName = AudioObjectProperty(selector: kAudioObjectPropertyElementNumberName)42 /// An array of AudioObjectIDs that represent all the AudioObjects owned by the43 /// given object. The qualifier is an array of AudioClassIDs. If it is44 /// non-empty, the returned array of AudioObjectIDs will only refer to objects45 /// whose class is in the qualifier array or whose is a subclass of one in the46 /// qualifier array.47 static let ownedObjects = AudioObjectProperty(selector: kAudioObjectPropertyOwnedObjects)48 /// A UInt32 where a value of one indicates that the object's hardware is49 /// drawing attention to itself, typically by flashing or lighting up its front50 /// panel display. A value of 0 indicates that this function is turned off. This51 /// makes it easy for a user to associate the physical hardware with its52 /// representation in an application. Typically, this property is only supported53 /// by AudioDevices and AudioBoxes.54 static let identify = AudioObjectProperty(selector: kAudioObjectPropertyIdentify)55 /// A CFString that contains the human readable serial number for the object.56 /// This property will typically be implemented by AudioBox and AudioDevice57 /// objects. Note that the serial number is not defined to be unique in the same58 /// way that an AudioBox's or AudioDevice's UID property are defined. This is59 /// purely an informational value. The caller is responsible for releasing the60 /// returned CFObject.61 static let serialNumber = AudioObjectProperty(selector: kAudioObjectPropertySerialNumber)62 /// A CFString that contains the human readable firmware version for the object.63 /// This property will typically be implemented by AudioBox and AudioDevice64 /// objects. Note that this is purely an informational value. The caller is65 /// responsible for releasing the returned CFObject.66 static let firmwareVersion = AudioObjectProperty(selector: kAudioObjectPropertyFirmwareVersion)67}...
Contents.swift
Source:Contents.swift
...9 */10import RxSwift11/*:12# Error Handling Operators13Operators that help to recover from error notifications from an Observable.14## `catchErrorJustReturn`15Recovers from an Error event by returning an `Observable` sequence that emits a single element and then terminates. [More info](http://reactivex.io/documentation/operators/catch.html)16![](https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/MarbleDiagrams/png/catch.png)17*/18example("catchErrorJustReturn") {19 let disposeBag = DisposeBag()20 21 let sequenceThatFails = PublishSubject<String>()22 23 sequenceThatFails24 .catchErrorJustReturn("ð")25 .subscribe { print($0) }26 .disposed(by: disposeBag)27 28 sequenceThatFails.onNext("ð¬")29 sequenceThatFails.onNext("ð¨")...
that
Using AI Code Generation
1import Foundation2var quickSort = QuickSort()3quickSort.quickSort(array: &array, low: 0, high: array.count - 1)4print(array)5import Foundation6var quickSort = QuickSort()7quickSort.quickSort(array: &array, low: 0, high: array.count - 1)8print(array)9import Foundation10var quickSort = QuickSort()11quickSort.quickSort(array: &array, low: 0, high: array.count - 1)12print(array)13import Foundation14var quickSort = QuickSort()15quickSort.quickSort(array: &array, low: 0, high: array.count - 1)16print(array)17import Foundation18var quickSort = QuickSort()19quickSort.quickSort(array: &array, low: 0, high: array.count - 1)20print(array)21import Foundation22var quickSort = QuickSort()23quickSort.quickSort(array: &array, low: 0, high: array.count - 1)24print(array)25import Foundation26var quickSort = QuickSort()27quickSort.quickSort(array: &
that
Using AI Code Generation
1QuickSort().quickSort(arr: arr)2QuickSort().quickSort(arr: arr)3QuickSort().quickSort(arr: arr)4QuickSort().quickSort(arr: arr)5QuickSort().quickSort(arr: arr)6QuickSort().quickSort(arr: arr)7QuickSort().quickSort(arr: arr)8QuickSort().quickSort(arr: arr)
that
Using AI Code Generation
1let quickSort = QuickSort()2print(quickSort.sort(arr: arr))3let quickSort = QuickSort()4print(quickSort.sort(arr: arr))5"Multiple commands produce '/Users/username/Library/Developer/Xcode/DerivedData/ProjectName-xxxxxxxxxxxxxxxxxxxxxxx/Build/Products/Debug-iphonesimulator/ProjectName.app/QuickSort.swift': 1) Target 'ProjectName' (project 'ProjectName') has compile command with input '/Users/username/Documents/ProjectName/QuickSort.swift' 2) That command depends on command in Target 'ProjectName' (project 'ProjectName'): script phase “[CP] Check Pods Manifest.lock”"6class CustomCell: UITableViewCell {7}8override func viewDidLoad() {9 super.viewDidLoad()10}11func textFieldShouldReturn(_ textField: UITextField) -> Bool {12 textField.resignFirstResponder()13}
that
Using AI Code Generation
1var q = QuickSort()2q.quickSort(array: &list, low: 0, high: list.count-1)3print(list)4var m = MergeSort()5m.mergeSort(array: &list, low: 0, high: list.count-1)6print(list)7var h = HeapSort()8h.heapSort(array: &list)9print(list)10var b = BubbleSort()11b.bubbleSort(array: &list)12print(list)13var s = SelectionSort()14s.selectionSort(array: &list)15print(list)16var i = InsertionSort()17i.insertionSort(array: &list)18print(list)19var c = CountingSort()20c.countingSort(array: &list, size: list.count)21print(list)22var r = RadixSort()23r.radixSort(array: &list, size: list.count)24print(list)25var bu = BucketSort()26bu.bucketSort(array: &list, size: list.count)27print(list)
that
Using AI Code Generation
1import Foundation2QuickSort().sort(&arr)3print(arr)4import Foundation5QuickSort().sort(&arr)6print(arr)7import Foundation8QuickSort().sort(&arr)9print(arr)10import Foundation11QuickSort().sort(&arr)12print(arr)13import Foundation14QuickSort().sort(&arr)15print(arr)16import Foundation
that
Using AI Code Generation
1var quickSort = QuickSort()2quickSort.sort(&array)3print(array)4var quickSort = QuickSort()5quickSort.sort(&array)6print(array)7Recommended Posts: Quick Sort Algorithm | Set 2 (Randomized Quick Sort)8Quick Sort Algorithm | Set 1 (Partitioning)9Quick Sort Algorithm | Set 3 (Dutch National Flag)10Quick Sort Algorithm | Set 4 (Hoare’s Partition Scheme)11Quick Sort Algorithm | Set 5 (Randomized Quick Sort)12Quick Sort Algorithm | Set 6 (Iterative Quick Sort)13Quick Sort Algorithm | Set 7 (Using Lomuto Partition Scheme)14Quick Sort Algorithm | Set 8 (Using Hoare’s Partition Scheme)15Quick Sort Algorithm | Set 9 (Using 3-Way Partition Scheme)16Quick Sort Algorithm | Set 10 (Using Randomized Quick Sort)17Quick Sort Algorithm | Set 11 (Using Iterative Quick Sort)18Quick Sort Algorithm | Set 12 (Using Randomized Quick Sort)19Quick Sort Algorithm | Set 13 (Using Lomuto Partition Scheme)20Quick Sort Algorithm | Set 14 (Using Hoare’s Partition Scheme)21Quick Sort Algorithm | Set 15 (Using 3-Way Partition Scheme)22Quick Sort Algorithm | Set 16 (Using Iterative Quick Sort)23Quick Sort Algorithm | Set 17 (Using Lomuto Partition Scheme)24Quick Sort Algorithm | Set 18 (Using Hoare’s Partition Scheme)25Quick Sort Algorithm | Set 19 (Using 3-Way Partition Scheme)26Quick Sort Algorithm | Set 20 (Using Iterative Quick Sort)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!