How to use testPlusMinusOperator method of BeCloseToTest class

Best Nimble code snippet using BeCloseToTest.testPlusMinusOperator

Run Nimble automation tests on LambdaTest cloud grid

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

BeCloseToTest.swift

Source: BeCloseToTest.swift Github

copy
1import Foundation
2import XCTest
3import Nimble
4
5class BeCloseToTest: XCTestCase, XCTestCaseProvider {
6    var allTests: [(String, () throws -> Void)] {
7        return [
8            ("testBeCloseTo", testBeCloseTo),
9            ("testBeCloseToWithin", testBeCloseToWithin),
10            ("testBeCloseToWithNSNumber", testBeCloseToWithNSNumber),
11            ("testBeCloseToWithNSDate", testBeCloseToWithNSDate),
12            ("testBeCloseToOperator", testBeCloseToOperator),
13            ("testBeCloseToWithinOperator", testBeCloseToWithinOperator),
14            ("testPlusMinusOperator", testPlusMinusOperator),
15            ("testBeCloseToArray", testBeCloseToArray),
16        ]
17    }
18
19    func testBeCloseTo() {
20        expect(1.2).to(beCloseTo(1.2001))
21        expect(1.2 as CDouble).to(beCloseTo(1.2001))
22        expect(1.2 as Float).to(beCloseTo(1.2001))
23
24        failsWithErrorMessage("expected to not be close to <1.2001> (within 0.0001), got <1.2>") {
25            expect(1.2).toNot(beCloseTo(1.2001))
26        }
27    }
28
29    func testBeCloseToWithin() {
30        expect(1.2).to(beCloseTo(9.300, within: 10))
31
32        failsWithErrorMessage("expected to not be close to <1.2001> (within 1), got <1.2>") {
33            expect(1.2).toNot(beCloseTo(1.2001, within: 1.0))
34        }
35    }
36
37    func testBeCloseToWithNSNumber() {
38        expect(NSNumber(double:1.2)).to(beCloseTo(9.300, within: 10))
39        expect(NSNumber(double:1.2)).to(beCloseTo(NSNumber(double:9.300), within: 10))
40        expect(1.2).to(beCloseTo(NSNumber(double:9.300), within: 10))
41        
42        failsWithErrorMessage("expected to not be close to <1.2001> (within 1), got <1.2>") {
43            expect(NSNumber(double:1.2)).toNot(beCloseTo(1.2001, within: 1.0))
44        }
45    }
46    
47    func testBeCloseToWithNSDate() {
48#if _runtime(_ObjC) // NSDateFormatter isn't functional in swift-corelibs-foundation yet.
49        expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).to(beCloseTo(NSDate(dateTimeString: "2015-08-26 11:43:05"), within: 10))
50        
51        failsWithErrorMessage("expected to not be close to <2015-08-26 11:43:00.0050> (within 0.004), got <2015-08-26 11:43:00.0000>") {
52
53            let expectedDate = NSDate(dateTimeString: "2015-08-26 11:43:00").dateByAddingTimeInterval(0.005)
54            expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).toNot(beCloseTo(expectedDate, within: 0.004))
55        }
56#endif
57    }
58    
59    func testBeCloseToOperator() {
60        expect(1.2) ≈ 1.2001
61        expect(1.2 as CDouble) ≈ 1.2001
62        
63        failsWithErrorMessage("expected to be close to <1.2002> (within 0.0001), got <1.2>") {
64            expect(1.2) ≈ 1.2002
65        }
66    }
67
68    func testBeCloseToWithinOperator() {
69        expect(1.2) ≈ (9.300, 10)
70        expect(1.2) == (9.300, 10)
71        
72        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
73            expect(1.2) ≈ (1.0, 0.1)
74        }
75        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
76            expect(1.2) == (1.0, 0.1)
77        }
78    }
79    
80    func testPlusMinusOperator() {
81        expect(1.2) ≈ 9.300 ± 10
82        expect(1.2) == 9.300 ± 10
83        
84        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
85            expect(1.2) ≈ 1.0 ± 0.1
86        }
87        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
88            expect(1.2) == 1.0 ± 0.1
89        }
90    }
91
92    func testBeCloseToArray() {
93        expect([0.0, 1.1, 2.2]) ≈ [0.0001, 1.1001, 2.2001]
94        expect([0.0, 1.1, 2.2]).to(beCloseTo([0.1, 1.2, 2.3], within: 0.1))
95        
96        failsWithErrorMessage("expected to be close to <[0, 1]> (each within 0.0001), got <[0, 1.1]>") {
97            expect([0.0, 1.1]) ≈ [0.0, 1.0]
98        }
99        failsWithErrorMessage("expected to be close to <[0.2, 1.2]> (each within 0.1), got <[0, 1.1]>") {
100            expect([0.0, 1.1]).to(beCloseTo([0.2, 1.2], within: 0.1))
101        }
102    }
103}
104
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

Trigger testPlusMinusOperator code on LambdaTest Cloud Grid

Execute automation tests with testPlusMinusOperator on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)