Skip to content

Instantly share code, notes, and snippets.

@spicyjack
Last active March 1, 2023 01:33
Show Gist options
  • Save spicyjack/b65fe3209c09917a671ae9f9c8b26665 to your computer and use it in GitHub Desktop.
Save spicyjack/b65fe3209c09917a671ae9f9c8b26665 to your computer and use it in GitHub Desktop.
Tests for an extension to 'Int' with a built in NumberFormatter()
//
// CurrencyFormattingDemoTests.swift
// CurrencyFormattingDemoTests
//
// Created by Brian Manning on 2/28/23.
//
import XCTest
@testable import CurrencyFormattingDemo
final class CurrencyFormattingDemoTests: XCTestCase {
// fails to compile: "'nil' cannot initialize specified type 'Int'"
// func test_toCurrencyFormattedString_withNil_returnsZero() throws {
// let testInt: Int = nil
// let testString = testInt.toCurrencyFormattedString()
// XCTAssertEqual("0.00", testString)
// }
func test_toCurrencyFormattedString_withZero_returnsZero() throws {
let testInt = 0
let testString = testInt.toCurrencyFormattedString()
XCTAssertEqual("0.00", testString)
}
func test_toCurrencyFormattedString_withNegativeOne_returnsNegativePointZeroOne() {
let testInt = -1
let testString = testInt.toCurrencyFormattedString()
XCTAssertEqual("-0.01", testString)
}
func test_toCurrencyFormattedString_withBigInt_returnsNegativePointZeroOne() {
let testInt = 2_147_483_648
let testString = testInt.toCurrencyFormattedString()
XCTAssertEqual("21,474,836.48", testString)
}
func test_toCurrencyFormattedString_withBigNegativeInt_returnsNegativePointZeroOne() {
let testInt: Int = -2_147_483_648
let testString = testInt.toCurrencyFormattedString()
XCTAssertEqual("-21,474,836.48", testString)
}
func test_toCurrencyFormattedString_withMaxInt_returnsNegativePointZeroOne() {
let testInt = Int(Int64.max)
let testString = testInt.toCurrencyFormattedString()
XCTAssertEqual("92,233,720,368,547,760.00", testString)
}
func test_toCurrencyFormattedString_withMinInt_returnsNegativePointZeroOne() {
let testInt = Int(Int64.min)
let testString = testInt.toCurrencyFormattedString()
XCTAssertEqual("-92,233,720,368,547,760.00", testString)
}
// fails to compile; "arithmetic overflow"
// func test_toCurrencyFormattedString_withMaxIntPlusOne_failsToCompile() {
// let testInt = Int(Int64.max) + 1
// let testString = testInt.toCurrencyFormattedString()
// XCTAssertEqual("92,233,720,368,547,760.00", testString)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment