How to use testBeCloseToWithinOperatorWithDate method of BeCloseToTest class

Best Nimble code snippet using BeCloseToTest.testBeCloseToWithinOperatorWithDate

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
5final class BeCloseToTest: XCTestCase, XCTestCaseProvider {
6    static var allTests: [(String, (BeCloseToTest) -> () throws -> Void)] {
7        return [
8            ("testBeCloseTo", testBeCloseTo),
9            ("testBeCloseToWithin", testBeCloseToWithin),
10            ("testBeCloseToWithNSNumber", testBeCloseToWithNSNumber),
11            ("testBeCloseToWithDate", testBeCloseToWithDate),
12            ("testBeCloseToWithNSDate", testBeCloseToWithNSDate),
13            ("testBeCloseToOperator", testBeCloseToOperator),
14            ("testBeCloseToWithinOperator", testBeCloseToWithinOperator),
15            ("testPlusMinusOperator", testPlusMinusOperator),
16            ("testBeCloseToOperatorWithDate", testBeCloseToOperatorWithDate),
17            ("testBeCloseToWithinOperatorWithDate", testBeCloseToWithinOperatorWithDate),
18            ("testPlusMinusOperatorWithDate", testPlusMinusOperatorWithDate),
19            ("testBeCloseToArray", testBeCloseToArray),
20            ("testBeCloseToWithCGFloat", testBeCloseToWithCGFloat),
21        ]
22    }
23
24    func testBeCloseTo() {
25        expect(1.2).to(beCloseTo(1.2001))
26        expect(1.2 as CDouble).to(beCloseTo(1.2001))
27        expect(1.2 as Float).to(beCloseTo(1.2001))
28
29        failsWithErrorMessage("expected to not be close to <1.2001> (within 0.0001), got <1.2>") {
30            expect(1.2).toNot(beCloseTo(1.2001))
31        }
32    }
33
34    func testBeCloseToWithin() {
35        expect(1.2).to(beCloseTo(9.300, within: 10))
36
37        failsWithErrorMessage("expected to not be close to <1.2001> (within 1), got <1.2>") {
38            expect(1.2).toNot(beCloseTo(1.2001, within: 1.0))
39        }
40    }
41
42    func testBeCloseToWithNSNumber() {
43        expect(NSNumber(value:1.2)).to(beCloseTo(9.300, within: 10))
44        expect(NSNumber(value:1.2)).to(beCloseTo(NSNumber(value:9.300), within: 10))
45        expect(1.2).to(beCloseTo(NSNumber(value:9.300), within: 10))
46
47        failsWithErrorMessage("expected to not be close to <1.2001> (within 1), got <1.2>") {
48            expect(NSNumber(value:1.2)).toNot(beCloseTo(1.2001, within: 1.0))
49        }
50    }
51
52    func testBeCloseToWithCGFloat() {
53        expect(CGFloat(1.2)).to(beCloseTo(1.2001))
54        expect(CGFloat(1.2)).to(beCloseTo(CGFloat(1.2001)))
55
56        failsWithErrorMessage("expected to not be close to <1.2001> (within 1), got <1.2>") {
57            expect(CGFloat(1.2)).toNot(beCloseTo(1.2001, within: 1.0))
58        }
59    }
60
61    func testBeCloseToWithDate() {
62        expect(Date(dateTimeString: "2015-08-26 11:43:00")).to(beCloseTo(Date(dateTimeString: "2015-08-26 11:43:05"), within: 10))
63
64        failsWithErrorMessage("expected to not be close to <2015-08-26 11:43:00.0050> (within 0.006), got <2015-08-26 11:43:00.0000>") {
65            let expectedDate = Date(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
66            expect(Date(dateTimeString: "2015-08-26 11:43:00")).toNot(beCloseTo(expectedDate, within: 0.006))
67        }
68    }
69
70    func testBeCloseToWithNSDate() {
71        expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).to(beCloseTo(NSDate(dateTimeString: "2015-08-26 11:43:05"), within: 10))
72
73        failsWithErrorMessage("expected to not be close to <2015-08-26 11:43:00.0050> (within 0.006), got <2015-08-26 11:43:00.0000>") {
74            let expectedDate = NSDate(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
75            expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).toNot(beCloseTo(expectedDate, within: 0.006))
76        }
77    }
78
79    func testBeCloseToOperator() {
80        expect(1.2) ≈ 1.2001
81        expect(1.2 as CDouble) ≈ 1.2001
82
83        failsWithErrorMessage("expected to be close to <1.2002> (within 0.0001), got <1.2>") {
84            expect(1.2) ≈ 1.2002
85        }
86    }
87
88    func testBeCloseToWithinOperator() {
89        expect(1.2) ≈ (9.300, 10)
90        expect(1.2) == (9.300, 10)
91
92        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
93            expect(1.2) ≈ (1.0, 0.1)
94        }
95        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
96            expect(1.2) == (1.0, 0.1)
97        }
98    }
99
100    func testPlusMinusOperator() {
101        expect(1.2) ≈ 9.300 ± 10
102        expect(1.2) == 9.300 ± 10
103
104        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
105            expect(1.2) ≈ 1.0 ± 0.1
106        }
107        failsWithErrorMessage("expected to be close to <1> (within 0.1), got <1.2>") {
108            expect(1.2) == 1.0 ± 0.1
109        }
110    }
111
112    func testBeCloseToOperatorWithDate() {
113        expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ Date(dateTimeString: "2015-08-26 11:43:00")
114
115        failsWithErrorMessage("expected to be close to <2015-08-26 11:43:00.0050> (within 0.0001), got <2015-08-26 11:43:00.0000>") {
116            let expectedDate = Date(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
117            expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ expectedDate
118        }
119    }
120
121    func testBeCloseToWithinOperatorWithDate() {
122        expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ (Date(dateTimeString: "2015-08-26 11:43:05"), 10)
123        expect(Date(dateTimeString: "2015-08-26 11:43:00")) == (Date(dateTimeString: "2015-08-26 11:43:05"), 10)
124
125        failsWithErrorMessage("expected to be close to <2015-08-26 11:43:00.0050> (within 0.004), got <2015-08-26 11:43:00.0000>") {
126            let expectedDate = Date(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
127            expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ (expectedDate, 0.004)
128        }
129        failsWithErrorMessage("expected to be close to <2015-08-26 11:43:00.0050> (within 0.004), got <2015-08-26 11:43:00.0000>") {
130            let expectedDate = Date(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
131            expect(Date(dateTimeString: "2015-08-26 11:43:00")) == (expectedDate, 0.004)
132        }
133    }
134
135    func testPlusMinusOperatorWithDate() {
136        expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ Date(dateTimeString: "2015-08-26 11:43:05") ± 10
137        expect(Date(dateTimeString: "2015-08-26 11:43:00")) == Date(dateTimeString: "2015-08-26 11:43:05") ± 10
138
139        failsWithErrorMessage("expected to be close to <2015-08-26 11:43:00.0050> (within 0.004), got <2015-08-26 11:43:00.0000>") {
140            let expectedDate = Date(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
141            expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ expectedDate ± 0.004
142        }
143        failsWithErrorMessage("expected to be close to <2015-08-26 11:43:00.0050> (within 0.004), got <2015-08-26 11:43:00.0000>") {
144            let expectedDate = Date(dateTimeString: "2015-08-26 11:43:00").addingTimeInterval(0.005)
145            expect(Date(dateTimeString: "2015-08-26 11:43:00")) == expectedDate ± 0.004
146        }
147    }
148
149    func testBeCloseToArray() {
150        expect([0.0, 1.1, 2.2]) ≈ [0.0001, 1.1001, 2.2001]
151        expect([0.0, 1.1, 2.2]).to(beCloseTo([0.1, 1.2, 2.3], within: 0.1))
152
153        failsWithErrorMessage("expected to be close to <[0, 1]> (each within 0.0001), got <[0, 1.1]>") {
154            expect([0.0, 1.1]) ≈ [0.0, 1.0]
155        }
156        failsWithErrorMessage("expected to be close to <[0.2, 1.2]> (each within 0.1), got <[0, 1.1]>") {
157            expect([0.0, 1.1]).to(beCloseTo([0.2, 1.2], within: 0.1))
158        }
159    }
160}
161
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 testBeCloseToWithinOperatorWithDate code on LambdaTest Cloud Grid

Execute automation tests with testBeCloseToWithinOperatorWithDate 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)