How to use testChain method of Error class

Best Nimble code snippet using Error.testChain

Run Nimble automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

AsynchronousTaskTests.swift

Source: AsynchronousTaskTests.swift Github

copy
1//
2//  AsynchronousTaskTests.swift
3//  LLVSTests
4//
5//  Created by Drew McCormack on 04/03/2019.
6//
7
8import XCTest
9import LLVS
10
11class AsynchronousTaskTests: XCTestCase {
12    
13    var task1: AsynchronousTask!
14    var task2: AsynchronousTask!
15    
16    override func setUp() {
17        super.setUp()
18        task1 = AsynchronousTask { finish in
19            finish(.success(()))
20        }
21        task2 = AsynchronousTask { finish in
22            finish(.success(()))
23        }
24    }
25    
26    func testNextTask() {
27        let expect = XCTestExpectation(description: "Completed basic")
28        task1.next = task2
29        task2.completionBlock = { result in
30            XCTAssert(result.isSuccess)
31            expect.fulfill()
32        }
33        task1.execute()
34        wait(for: [expect], timeout: 1.0)
35    }
36    
37    func testChain() {
38        let expect = XCTestExpectation(description: "Completed basic")
39        [task1, task2].chain()
40        task2.completionBlock = { result in
41            XCTAssert(result.isSuccess)
42            expect.fulfill()
43        }
44        task1.execute()
45        wait(for: [expect], timeout: 1.0)
46    }
47    
48    func testExecuteInOrder() {
49        let expect = XCTestExpectation(description: "Completed Execute in Order")
50        let task3 = AsynchronousTask { finish in
51            finish(.success(()))
52        }
53        let task4 = AsynchronousTask { finish in
54            finish(.success(()))
55        }
56        [task1, task2, task3, task4].executeInOrder { result in
57            XCTAssert(result.isSuccess)
58            expect.fulfill()
59        }
60        task1.execute()
61        wait(for: [expect], timeout: 1.0)
62    }
63    
64    func testWithAsyncDelay() {
65        let expect = XCTestExpectation(description: "Completed with Async Delay")
66        let task3 = AsynchronousTask { finish in
67            DispatchQueue.main.async {
68                finish(.success(()))
69            }
70        }
71        [task1, task2, task3].executeInOrder { result in
72            XCTAssert(result.isSuccess)
73            expect.fulfill()
74        }
75        task1.execute()
76        wait(for: [expect], timeout: 1.0)
77    }
78    
79    enum Error: Swift.Error {
80        case testError
81    }
82    
83    func testWithFailure() {
84        let expect = XCTestExpectation(description: "Test with failure")
85        let task3 = AsynchronousTask { finish in
86            finish(.failure(Error.testError))
87        }
88        var task4Executed = false
89        let task4 = AsynchronousTask { finish in
90            task4Executed = true
91            finish(.success(()))
92        }
93        [task1, task2, task3, task4].executeInOrder { result in
94            XCTAssertFalse(task4Executed)
95            XCTAssertFalse(result.isSuccess)
96            expect.fulfill()
97        }
98        task1.execute()
99        wait(for: [expect], timeout: 1.0)
100    }
101
102    static var allTests = [
103        ("testNextTask", testNextTask),
104        ("testChain", testChain),
105        ("testExecuteInOrder", testExecuteInOrder),
106        ("testWithFailure", testWithFailure),
107    ]
108}
109
Full Screen

SwiftAsyncTests.swift

Source: SwiftAsyncTests.swift Github

copy
1import XCTest
2@testable import Concurrency
3
4import Dispatch
5import Foundation
6
7class ConcurrencyTests: XCTestCase {
8    
9    enum Error: Swift.Error {
10        case er
11    }
12    
13    func testSend() {
14        let expectation = self.expectation(description: "expectation")
15        var value = 0
16        
17        let task = Task<Int>()
18            .done { value = $0 * 2 }
19            .always { _ in expectation.fulfill() }
20        
21        task.send(10)
22        
23        self.waitForExpectations(timeout: 5) { error in
24            XCTAssertNil(error)
25            XCTAssertEqual(20, value)
26        }
27    }
28    
29    func testThrow() {
30        let expectation = self.expectation(description: "expectation")
31        var taskError: Swift.Error?
32        
33        let task = Task<Int>()
34            .catch { taskError = $0 }
35            .always { _ in expectation.fulfill() }
36        
37        task.throw(Error.er)
38        
39        self.waitForExpectations(timeout: 5) { error in
40            XCTAssertNil(error)
41            XCTAssertNotNil(taskError)
42        }
43    }
44    
45    func testRecover() {
46        var expectation = self.expectation(description: "expectation")
47        var taskError: Swift.Error?
48        var value = 0
49        
50        var task = Task<Int>()
51        
52        
53        task.recover { _ in return 10 }
54            .then { $0 }
55            .done { value = $0 * 2 }
56            .catch { taskError = $0 }
57            .always { _ in expectation.fulfill() }
58        
59        task.throw(Error.er)
60        
61        self.waitForExpectations(timeout: 5) { error in
62            XCTAssertNil(error)
63            XCTAssertNil(taskError)
64            XCTAssertEqual(20, value)
65        }
66        
67        expectation = self.expectation(description: "expectation")
68        value = 0
69        
70        task = Task<Int>()
71        
72        
73        task.done { value = $0 * 2 }
74            .catch { taskError = $0 }
75            .recover { throw $0 }
76            .always { _ in expectation.fulfill() }
77        
78        task.throw(Error.er)
79        
80        self.waitForExpectations(timeout: 5) { error in
81            XCTAssertNil(error)
82            XCTAssertNotNil(taskError)
83            XCTAssertEqual(0, value)
84        }
85    }
86    
87    func testInitializerBuild() {
88        let expectation = self.expectation(description: "expectation")
89        var value = 0
90        
91        Task<Int> { task in
92            task.send(10)
93            }
94            .done { value = $0 * 2 }
95            .always { _ in expectation.fulfill() }
96        
97        self.waitForExpectations(timeout: 5) { error in
98            XCTAssertNil(error)
99            XCTAssertEqual(20, value)
100        }
101    }
102    
103    func testInitializerValue() {
104        let expectation = self.expectation(description: "expectation")
105        var value = 0
106        
107        Task<Int>(value: 10)
108            .done { value = $0 * 2 }
109            .always { _ in expectation.fulfill() }
110        
111        self.waitForExpectations(timeout: 5) { error in
112            XCTAssertNil(error)
113            XCTAssertEqual(20, value)
114        }
115    }
116    
117    func testInitializerState() {
118        let expectation = self.expectation(description: "expectation")
119        var value = 0
120        
121        Task<Int>(state: .success(10))
122            .done { value = $0 * 2 }
123            .always { _ in expectation.fulfill() }
124        
125        self.waitForExpectations(timeout: 5) { error in
126            XCTAssertNil(error)
127            XCTAssertEqual(20, value)
128        }
129    }
130    
131    func testChain() {
132        var expectation = self.expectation(description: "expectation")
133        var value = 0
134        
135        Task<Int>(value: 10)
136            .then { $0 + 5 }
137            .then { $0 * 2 }
138            .recover { _ in 40 }
139            .then { value = $0 }
140            .always { _ in expectation.fulfill() }
141        
142        self.waitForExpectations(timeout: 5) { error in
143            XCTAssertNil(error)
144            XCTAssertEqual(30, value)
145        }
146        
147        
148        expectation = self.expectation(description: "expectation")
149        value = 0
150        
151        Task<Int>(value: 10)
152            .then { $0 + 5 }
153            .then { $0 * 2 }
154            .then { _ in throw Error.er }
155            .recover { _ in 40 }
156            .then { value = $0 }
157            .always { _ in expectation.fulfill() }
158        
159        self.waitForExpectations(timeout: 5) { error in
160            XCTAssertNil(error)
161            XCTAssertEqual(40, value)
162        }
163    }
164    
165    func testQueue() {
166        var expectation = self.expectation(description: "expectation")
167        var value = 0
168        
169        Task<Int>(value: 10)
170            .then { $0 + 5 }
171            .then(in: .global()) { return $0 * 2 }
172            .then { value = $0 }
173            .always { _ in expectation.fulfill() }
174        
175        self.waitForExpectations(timeout: 5) { error in
176            XCTAssertNil(error)
177            XCTAssertEqual(30, value)
178        }
179        
180        expectation = self.expectation(description: "expectation")
181        value = 0
182        
183        Task<Int>(value: 10)
184            .then { $0 + 5 }
185            .then { $0 * 2 }
186            .then { _ in throw Error.er }
187            .recover { _ in 40 }
188            .then(in: .global()) { value = $0 }
189            .always { _ in expectation.fulfill() }
190        
191        self.waitForExpectations(timeout: 5) { error in
192            XCTAssertNil(error)
193            XCTAssertEqual(40, value)
194        }
195    }
196    
197    // TODO: hangs
198    func testDelay() {
199        let expectation = self.expectation(description: "expectation")
200        var value = 0
201        
202        //        let delay = { DispatchTime
203        Task<Int>(value: 10)
204            .then(delay: { .now() + 2 }) { $0 * 2 }
205            .done { print("delay \($0)"); value = $0 }
206            .always { _ in print("delay"); expectation.fulfill() }
207        
208        self.waitForExpectations(timeout: 5) { error in
209            XCTAssertNil(error)
210            XCTAssertEqual(20, value)
211        }
212    }
213    
214    func testUpdate() {
215        let expectation = self.expectation(description: "expectation")
216        var value = 0
217        
218        let task = Task<Int>(value: 10)
219            .then(delay: { .now() + 3 }) { $0 * 2 }
220        
221        DispatchQueue.global().async {
222            task.then { $0 * 2 }
223                .then { $0 + 5 }
224                .done { value = $0 }
225                .always { _ in expectation.fulfill() }
226        }
227        
228        self.waitForExpectations(timeout: 5) { error in
229            XCTAssertNil(error)
230            XCTAssertEqual(45, value)
231        }
232    }
233    
234    func testMultiple() {
235        let expectation = self.expectation(description: "expectation")
236        let e1 = self.expectation(description: "expectation1")
237        let e2 = self.expectation(description: "expectation2")
238        
239        
240        let task = Task<Int>(in: .global(), value: 10)
241            .then(in: .global()) { _ in expectation.fulfill() }
242        
243        task.then(in: .global()) { e1.fulfill() }
244        
245        task.then(in: .global()) { e2.fulfill() }
246        
247        self.waitForExpectations(timeout: 5) { error in
248            XCTAssertNil(error)
249        }
250    }
251    
252    func testWait() {
253        var value = try? Task<Int>(value: 10)
254            .then { $0 * 2 }
255            .then { $0 + 5 }
256            .wait()
257        
258        XCTAssertEqual(25, value)
259        
260        value = try? Task<Int>(value: 10)
261            .then { $0 * 2 }
262            .then(delay: { .now() + 10 }) { $0 + 5 }
263            .wait()
264        
265        XCTAssertEqual(25, value)
266    }
267    
268    func testWaitTimeout() {
269        var value = 0
270        do {
271            value = try Task<Int>(value: 10)
272                .then { $0 * 2 }
273                .then(in: .global(), delay: { .now() + .seconds(5) }) { $0 + 5 }
274                .wait(for: { .now() + 10 })
275//                try Task<Int>(value: 10)
276//                .then { $0 * 2 }
277//                .then(delay: { .now() + .seconds(5) }) { $0 + 5 }
278////                .wait()
279//                .wait(for: { .now() + .seconds(10) })
280        
281            XCTAssertEqual(25, value)
282        } catch {
283            XCTFail("error: \(error)")
284        }
285        
286//        value = 0
287//
288//        do {
289//            value = try Task<Int>(value: 10)
290//                .then { $0 * 2 }
291//                .then(delay: { .now() + 5 }) { $0 + 5 }
292//                .wait(for: { .now() + .seconds(1) })
293//
294//            XCTFail("should throw")
295//        } catch {
296//            switch error {
297//                //            case let error as TaskError where error == .timeout:
298//            //                XCTAssertTrue(true)
299//            default:
300//                XCTFail("wrong error \(error)")
301//            }
302//        }
303//
304//        XCTAssertEqual(0, value)
305    }
306    
307    func testCombine() {
308        var expectation = self.expectation(description: "e")
309        var intTask = Task<Int>()
310        var stringTask = Task<String>()
311        var boolTask = Task<Bool>()
312        var result = [Any]()
313        
314        [intTask.as(Any.self), stringTask.as(Any.self), boolTask.as(Any.self)]
315            .combine()
316            .done { result = $0 }
317            .always { _ in expectation.fulfill() }
318        
319        intTask.send(10)
320        stringTask.send("task string")
321        boolTask.send(true)
322        
323        self.waitForExpectations(timeout: 5) { error in
324            XCTAssertNil(error)
325            XCTAssertEqual(3, result.count)
326            guard result.count == 3 else {
327                XCTFail()
328                return
329            }
330            XCTAssertEqual(10, result[0] as? Int)
331            XCTAssertEqual("task string", result[1] as? String)
332            XCTAssertEqual(true, result[2] as? Bool)
333        }
334        
335        expectation = self.expectation(description: "e")
336        intTask = Task<Int>()
337        stringTask = Task<String>()
338        boolTask = Task<Bool>()
339        result = [Any]()
340        
341        [intTask.as(Any.self), stringTask.as(Any.self), boolTask.as(Any.self)]
342            .combine()
343            .done { result = $0 }
344            .always { _ in expectation.fulfill() }
345        
346        
347        intTask.send(10)
348        stringTask.throw(Error.er)
349        boolTask.send(true)
350        
351        self.waitForExpectations(timeout: 5) { error in
352            XCTAssertNil(error)
353            XCTAssertEqual(0, result.count)
354        }
355    }
356    
357    func testMapReduce() {
358        let task = Task<[Any]>(value: [10, 30, "ss", true, "aa"])
359        
360        let filterArray = try? task.filter { $0 is String }.wait()
361        XCTAssertEqual(["ss", "aa"], (filterArray ?? []).flatMap { $0 as? String })
362        
363        let mapArray = try? task.map { String.init(describing: $0) }.wait()
364        XCTAssertEqual(["10", "30", "ss", "true", "aa"], mapArray ?? [])
365        
366        let flatMapArray = try? task.flatMap { $0 as? String }.wait()
367        XCTAssertEqual(["ss", "aa"], (flatMapArray ?? []))
368        
369        let reduce = try? task.reduce(0) { $0 + (($1 as? Int) ?? 1) }.wait()
370        XCTAssertEqual(43, reduce)
371    }
372    
373    func testUnwrap() {
374        let e = self.expectation(description: "e")
375        var value = 0
376        
377        Task<Void>().finish()
378            .then { _ in return Task<Int>(value: 10) }
379            .then { $0.then { $0 * 2 } }
380            .unwrap()
381            .then { $0 + 5 }
382            .then { value = $0 }
383            .always(in: .global()) { _ in e.fulfill() }
384        
385        self.waitForExpectations(timeout: 5) { error in
386            XCTAssertNil(error)
387            XCTAssertEqual(25, value)
388        }
389    }
390    
391    func testThenOnState() {
392        
393        var e = self.expectation(description: "e")
394        var value = ""
395        func foo(_ string: String) {
396            value = string
397        }
398        
399        var task = Task<Int>()
400        
401        task.then(on: .success) { _ in foo("success") }
402            .always { _ in e.fulfill() }
403        
404        task.then(on: .failure) { _ in foo("fail") }
405            .always { _ in e.fulfill() }
406        
407        task.send(10)
408        
409        self.waitForExpectations(timeout: 5) { error in
410            XCTAssertNil(error)
411            XCTAssertEqual("success", value)
412        }
413        
414        e = self.expectation(description: "e")
415        task = Task<Int>()
416        
417        task.then(on: .success) { _ in foo("success") }
418            .always { _ in e.fulfill() }
419        
420        task.then(on: .failure) { $0 }
421            .catch { _ in foo("fail") }
422            .always { _ in e.fulfill() }
423        
424        task.throw(Error.er)
425        
426        self.waitForExpectations(timeout: 5) { error in
427            XCTAssertNil(error)
428            XCTAssertEqual("fail", value)
429        }
430    }
431    
432    static var allTests : [(String, (ConcurrencyTests) -> () throws -> Void)] {
433        return [
434            ("testSend", testSend),
435            ("testThrow", testThrow),
436            ("testRecover", testRecover),
437            ("testInitializerBuild", testInitializerBuild),
438            ("testInitializerValue", testInitializerValue),
439            ("testInitializerState", testInitializerState),
440            ("testChain", testChain),
441            ("testQueue", testQueue),
442            ("testDelay", testDelay),
443            ("testUpdate", testUpdate),
444            ("testMultiple", testMultiple),
445            ("testWait", testWait),
446            ("testWaitTimeout", testWaitTimeout),
447            ("testCombine", testCombine),
448            ("testUnwrap", testUnwrap),
449        ]
450    }
451}
452
Full Screen

E14Tests.swift

Source: E14Tests.swift Github

copy
1//
2//  E14Tests.swift
3//  E14Tests
4//
5//  Created by stephen weber on 5/4/22.
6//
7
8import XCTest
9@testable import E14
10
11class E14Tests: XCTestCase {
12    var collatz : Collatz!
13    override func setUpWithError() throws {
14        
15        collatz = Collatz()
16        // Put setup code here. This method is called before the invocation of each test method in the class.
17    }
18
19    override func tearDownWithError() throws {
20        // Put teardown code here. This method is called after the invocation of each test method in the class.
21    }
22
23    func testExample() throws {
24        // This is an example of a functional test case.
25        // Use XCTAssert and related functions to verify your tests produce the correct results.
26        // Any test you write for XCTest can be annotated as throws and async.
27        // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
28        // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
29    }
30
31    
32    func testarray() {
33        print(collatz.sieve.count)
34        print(collatz.possible.count)
35    }
36
37    func testCollatzodd() {
38        XCTAssertEqual(collatz.EvenReverseCollatz(14),7)
39        XCTAssertEqual(collatz.OddReverseCollatz(15),46)
40    }
41    
42    
43    func testChain() {
44         collatz.chain(8)
45        
46        XCTAssertEqual(collatz.theList[8],4)
47    }
48    
49    func testAllChain() {
50        for i in 1...100000000 {
51            collatz.chain(i)
52        }
53        print(collatz.maxCount)
54        print(collatz.maxInt)
55        /*
56         525
57         837799
58         */
59        //took 1.421 seconds without memorizing data
60        /*
61         686
62         8400511
63         14.075
64         */
65        /*
66         3GB
67         100000000
68         950
69         63728127
70         162.230 secs
71         without any dictionary 30mb total mem. 128 sec
72         */
73    }
74    
75    func testMEmeorizationSolution() {
76        ..not helpful
77        
78        for i in 1...100000000 {
79            collatz.chain(i)
80        }
81        print(collatz.maxCount)
82        print(collatz.maxInt)
83        /*
84         525
85         837799 at 1.44 sec
86         */
87        
88        /*
89         5000000
90         597
91         3732423 at 6.852 sec
92         */
93            /*
94             686
95             8400511
96             14.083
97             */
98        /*
99         100000000
100         950
101         63728127
102         161.637
103         3Gb
104         */
105//        collatz.theSolution(13)
106//        print(collatz.theList[13] ?? 999)
107//        print(collatz.maxCount)
108//        print(collatz.maxInt)
109       // XCTAssertEqual(collatz.theSolution(12),collatz.theList[12] )
110//        XCTAssertEqual(collatz.theSolution(2),2)
111//        XCTAssertEqual(collatz.theSolution(4),3)
112//    //    XCTAssertEqual(collatz.theSolution(1000000),837799)
113      //  XCTAssertEqual(collatz.theSolution(1000000),837799)
114    }
115    
116    func testRevatureSuits() {
117        XCTAssertEqual(collatz.revatureSuits(10,2),19)
118        XCTAssertEqual(collatz.revatureSuits(3,0),3)
119        
120    }
121}
122
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)