FormatTests.swift (2562B)
1 // 2 // FormatTests.swift 3 // damusTests 4 // 5 // Created by William Casarin on 2023-01-17. 6 // 7 8 import XCTest 9 @testable import damus 10 11 final class FormatTests: XCTestCase { 12 13 override func setUpWithError() throws { 14 // Put setup code here. This method is called before the invocation of each test method in the class. 15 } 16 17 override func tearDownWithError() throws { 18 // Put teardown code here. This method is called after the invocation of each test method in the class. 19 } 20 21 func testAbbrevSatsFormat() throws { 22 XCTAssertEqual(format_msats_abbrev(1_000_000 * 1000), "1m") 23 XCTAssertEqual(format_msats_abbrev(1_100_000 * 1000), "1.1m") 24 XCTAssertEqual(format_msats_abbrev(100_000_000 * 1000), "100m") 25 XCTAssertEqual(format_msats_abbrev(1000 * 1000), "1k") 26 XCTAssertEqual(format_msats_abbrev(1500 * 1000), "1.5k") 27 XCTAssertEqual(format_msats_abbrev(1595 * 1000), "1.5k") 28 XCTAssertEqual(format_msats_abbrev(100 * 1000), "100") 29 XCTAssertEqual(format_msats_abbrev(0), "0") 30 XCTAssertEqual(format_msats_abbrev(100_000_000 * 1000), "100m") 31 XCTAssertEqual(format_msats_abbrev(999 * 1000), "999") 32 XCTAssertEqual(format_msats_abbrev(999), "0.999") 33 XCTAssertEqual(format_msats_abbrev(1), "0.001") 34 XCTAssertEqual(format_msats_abbrev(1000), "1") 35 } 36 37 func testFormatMsats() throws { 38 let enUsLocale = Locale(identifier: "en-US") 39 XCTAssertEqual(format_msats(0, locale: enUsLocale), "0 sats") 40 XCTAssertEqual(format_msats(1, locale: enUsLocale), "0.001 sats") 41 XCTAssertEqual(format_msats(1000, locale: enUsLocale), "1 sat") 42 XCTAssertEqual(format_msats(1001, locale: enUsLocale), "1.001 sats") 43 XCTAssertEqual(format_msats(2000, locale: enUsLocale), "2 sats") 44 XCTAssertEqual(format_msats(123456789, locale: enUsLocale), "123,456.789 sats") 45 // Sanity check that function call does not throw in any supported locale as the string format accepts arguments, and a mismatched format in any one of the locales could break the app. 46 Bundle.main.localizations.map { Locale(identifier: $0) }.forEach { 47 XCTAssertNoThrow(format_msats(0, locale: $0)) 48 XCTAssertNoThrow(format_msats(1, locale: $0)) 49 XCTAssertNoThrow(format_msats(1000, locale: $0)) 50 XCTAssertNoThrow(format_msats(1001, locale: $0)) 51 XCTAssertNoThrow(format_msats(2000, locale: $0)) 52 XCTAssertNoThrow(format_msats(123456789, locale: $0)) 53 } 54 } 55 56 }