Created
January 1, 2022 20:51
-
-
Save agiokas/7113de1f83db09fbaa5e906cc3728e42 to your computer and use it in GitHub Desktop.
Unit Testing 101 - Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public func addInt(_ a: Int, _ b: Int) throws -> Int { | |
| let result = a.addingReportingOverflow(b) | |
| if result.overflow { | |
| throw AddError.overflow | |
| } | |
| return result.partialValue | |
| } | |
| enum AddError: Error { | |
| case overflow | |
| } | |
| import XCTest | |
| @testable import UnitTesting101 | |
| final class FunctionsTests: XCTestCase { | |
| func testAddInt_basics() throws { | |
| XCTAssertEqual(try addInt(10, 20), 30) | |
| XCTAssertEqual(try addInt(-10, 20), 10) | |
| XCTAssertEqual(try addInt(-10, -20), -30) | |
| } | |
| func testAddInt_max() throws { | |
| XCTAssertThrowsError(try addInt(1, Int.max)) | |
| XCTAssertThrowsError(try addInt(Int.max, 1)) | |
| XCTAssertThrowsError(try addInt(Int.max, Int.max)) | |
| XCTAssertEqual(try addInt(Int.max, 0), Int.max) | |
| XCTAssertEqual(try addInt(0, Int.max), Int.max) | |
| XCTAssertEqual(try addInt(Int.max, -10), Int.max - 10) | |
| XCTAssertEqual(try addInt(-10, Int.max), Int.max - 10) | |
| XCTAssertThrowsError(try addInt(Int.max - 10, Int.max - 10)) | |
| } | |
| func testAddInt_min() throws { | |
| XCTAssertThrowsError(try addInt(-1, Int.min)) | |
| XCTAssertThrowsError(try addInt(Int.min, -1)) | |
| XCTAssertThrowsError(try addInt(Int.min, Int.min)) | |
| XCTAssertEqual(try addInt(Int.min, 0), Int.min) | |
| XCTAssertEqual(try addInt(0, Int.min), Int.min) | |
| XCTAssertEqual(try addInt(Int.min, 10), Int.min + 10) | |
| XCTAssertEqual(try addInt(10, Int.min), Int.min + 10) | |
| XCTAssertThrowsError(try addInt(Int.min + 10, Int.min + 10)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment