diff --git a/app/metro-now/metro-now-tests/formatTimeTests.swift b/app/metro-now/metro-now-tests/formatTimeTests.swift new file mode 100644 index 00000000..d41589de --- /dev/null +++ b/app/metro-now/metro-now-tests/formatTimeTests.swift @@ -0,0 +1,369 @@ +// +// formatTimeTests.swift +// metro-now-tests +// +// Created by Kryštof Krátký on 15.05.2024. +// + +@testable import metro_now +import XCTest + +final class formatTimeTests: XCTestCase { + // MARK: - outputs with seconds + + /// should show positive and negative or zero seconds without any issues + + func testFormatTimePositiveSeconds() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = 40 + result = formatTime(seconds: seconds) + expectedResult = "40s" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = 1 + result = formatTime(seconds: seconds) + expectedResult = "1s" + + XCTAssertEqual(result, expectedResult) + + /// test 3 + seconds = 59 + result = formatTime(seconds: seconds) + expectedResult = "59s" + + XCTAssertEqual(result, expectedResult) + } + + func testFormatTimeNegativeSeconds() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = -40 + result = formatTime(seconds: seconds) + expectedResult = "-40s" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = -1 + result = formatTime(seconds: seconds) + expectedResult = "-1s" + + XCTAssertEqual(result, expectedResult) + + /// test 3 + seconds = -59 + result = formatTime(seconds: seconds) + expectedResult = "-59s" + + XCTAssertEqual(result, expectedResult) + + /// test 4 + seconds = -47 + result = formatTime(seconds: seconds) + expectedResult = "-47s" + + XCTAssertEqual(result, expectedResult) + } + + func testFormatTimeZeroSeconds() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = 0 + result = formatTime(seconds: seconds) + expectedResult = "0s" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = -0 + result = formatTime(seconds: seconds) + expectedResult = "0s" + + XCTAssertEqual(result, expectedResult) + } + + // MARK: - outputs with minutes + + /// should show minutes and seconds (if seconds is > 0) + + func testFormatTimePositiveMinutes() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = 60 + result = formatTime(seconds: seconds) + expectedResult = "1m" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = 61 + result = formatTime(seconds: seconds) + expectedResult = "1m 1s" + + XCTAssertEqual(result, expectedResult) + + /// test 3 + seconds = 119 + result = formatTime(seconds: seconds) + expectedResult = "1m 59s" + + XCTAssertEqual(result, expectedResult) + + /// test 4 + seconds = 3540 + result = formatTime(seconds: seconds) + expectedResult = "59m" + + XCTAssertEqual(result, expectedResult) + + /// test 5 + seconds = 3599 + result = formatTime(seconds: seconds) + expectedResult = "59m 59s" + + XCTAssertEqual(result, expectedResult) + } + + func testFormatTimeNegativeMinutes() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = -60 + result = formatTime(seconds: seconds) + expectedResult = "-1m" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = -61 + result = formatTime(seconds: seconds) + expectedResult = "-1m 1s" + + XCTAssertEqual(result, expectedResult) + + /// test 3 + seconds = -119 + result = formatTime(seconds: seconds) + expectedResult = "-1m 59s" + + XCTAssertEqual(result, expectedResult) + + /// test 4 + seconds = -3540 + result = formatTime(seconds: seconds) + expectedResult = "-59m" + + XCTAssertEqual(result, expectedResult) + + /// test 5 + seconds = -3599 + result = formatTime(seconds: seconds) + expectedResult = "-59m 59s" + + XCTAssertEqual(result, expectedResult) + } + + // MARK: - outputs with hours + + /// shouldn't return seconds if value > 1hr + + func testFormatTimePositiveHours() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = 3600 + result = formatTime(seconds: seconds) + expectedResult = "1h" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = 3601 + result = formatTime(seconds: seconds) + expectedResult = "1h" + + XCTAssertEqual(result, expectedResult) + + /// test 3 + seconds = 3659 + result = formatTime(seconds: seconds) + expectedResult = "1h" + + XCTAssertEqual(result, expectedResult) + + /// test 4 + seconds = 3660 + result = formatTime(seconds: seconds) + expectedResult = "1h 1m" + + XCTAssertEqual(result, expectedResult) + + /// test 5 + seconds = 3661 + result = formatTime(seconds: seconds) + expectedResult = "1h 1m" + + XCTAssertEqual(result, expectedResult) + + /// test 6 + seconds = 7199 + result = formatTime(seconds: seconds) + expectedResult = "1h 59m" + + XCTAssertEqual(result, expectedResult) + + /// test 7 + seconds = 7200 + result = formatTime(seconds: seconds) + expectedResult = "2h" + + /// test 8 + seconds = 7201 + result = formatTime(seconds: seconds) + expectedResult = "2h" + + XCTAssertEqual(result, expectedResult) + + /// test 10 + seconds = 7260 + result = formatTime(seconds: seconds) + expectedResult = "2h 1m" + + XCTAssertEqual(result, expectedResult) + + /// test 11 + seconds = 43200 + result = formatTime(seconds: seconds) + expectedResult = "12h" + + XCTAssertEqual(result, expectedResult) + + /// test 11 + seconds = 43201 + result = formatTime(seconds: seconds) + expectedResult = ">12h" + + XCTAssertEqual(result, expectedResult) + + /// test 13 + seconds = Int.max + result = formatTime(seconds: seconds) + expectedResult = ">12h" + + XCTAssertEqual(result, expectedResult) + } + + func testFormatTimeNegativeHours() { + /// init variables for testing + var seconds: Int + var result: String + var expectedResult: String + + /// test 1 + seconds = -3600 + result = formatTime(seconds: seconds) + expectedResult = "-1h" + + XCTAssertEqual(result, expectedResult) + + /// test 2 + seconds = -3601 + result = formatTime(seconds: seconds) + expectedResult = "-1h" + + XCTAssertEqual(result, expectedResult) + + /// test 3 + seconds = -3659 + result = formatTime(seconds: seconds) + expectedResult = "-1h" + + XCTAssertEqual(result, expectedResult) + + /// test 4 + seconds = -3660 + result = formatTime(seconds: seconds) + expectedResult = "-1h 1m" + + XCTAssertEqual(result, expectedResult) + + /// test 5 + seconds = -3661 + result = formatTime(seconds: seconds) + expectedResult = "-1h 1m" + + XCTAssertEqual(result, expectedResult) + + /// test 6 + seconds = -7199 + result = formatTime(seconds: seconds) + expectedResult = "-1h 59m" + + XCTAssertEqual(result, expectedResult) + + /// test 7 + seconds = -7200 + result = formatTime(seconds: seconds) + expectedResult = "-2h" + + /// test 8 + seconds = -7201 + result = formatTime(seconds: seconds) + expectedResult = "-2h" + + XCTAssertEqual(result, expectedResult) + + /// test 10 + seconds = -7260 + result = formatTime(seconds: seconds) + expectedResult = "-2h 1m" + + XCTAssertEqual(result, expectedResult) + + /// test 11 + seconds = -43200 + result = formatTime(seconds: seconds) + expectedResult = "-12h" + + XCTAssertEqual(result, expectedResult) + + /// test 11 + seconds = -43201 + result = formatTime(seconds: seconds) + expectedResult = "- >12h" + + XCTAssertEqual(result, expectedResult) + + /// test 13 + seconds = Int.min + result = formatTime(seconds: seconds) + expectedResult = "- >12h" + + XCTAssertEqual(result, expectedResult) + } +} diff --git a/app/metro-now/metro-now-tests/metro_now_tests.swift b/app/metro-now/metro-now-tests/metro_now_tests.swift new file mode 100644 index 00000000..d455efe3 --- /dev/null +++ b/app/metro-now/metro-now-tests/metro_now_tests.swift @@ -0,0 +1,33 @@ +// +// metro_now_tests.swift +// metro-now-tests +// +// Created by Kryštof Krátký on 15.05.2024. +// + +import XCTest + +final class metro_now_tests: XCTestCase { + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testExample() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct results. + // Any test you write for XCTest can be annotated as throws and async. + // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. + // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. + } + + func testPerformanceExample() throws { + // This is an example of a performance test case. + measure { + // Put the code you want to measure the time of here. + } + } +} diff --git a/app/metro-now/metro-now.xcodeproj/project.pbxproj b/app/metro-now/metro-now.xcodeproj/project.pbxproj index 35b89bdb..fae19e5a 100644 --- a/app/metro-now/metro-now.xcodeproj/project.pbxproj +++ b/app/metro-now/metro-now.xcodeproj/project.pbxproj @@ -22,8 +22,20 @@ 2DC63A0F2BF4D13200A72C7F /* stationsJSONManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A0E2BF4D13200A72C7F /* stationsJSONManager.swift */; }; 2DC63A112BF4D3F800A72C7F /* stations.json in Resources */ = {isa = PBXBuildFile; fileRef = 2DC63A102BF4D3F800A72C7F /* stations.json */; }; 2DC63A132BF4D98F00A72C7F /* getMetroLineColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A122BF4D98F00A72C7F /* getMetroLineColor.swift */; }; + 2DC63A1B2BF50E8F00A72C7F /* metro_now_tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A1A2BF50E8F00A72C7F /* metro_now_tests.swift */; }; + 2DC63A222BF50EDD00A72C7F /* formatTimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC63A212BF50EDD00A72C7F /* formatTimeTests.swift */; }; /* End PBXBuildFile section */ +/* Begin PBXContainerItemProxy section */ + 2DC63A1C2BF50E8F00A72C7F /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 2DC639D02BF3CCBA00A72C7F /* Project object */; + proxyType = 1; + remoteGlobalIDString = 2DC639D72BF3CCBA00A72C7F; + remoteInfo = "metro-now"; + }; +/* End PBXContainerItemProxy section */ + /* Begin PBXFileReference section */ 2DC639D82BF3CCBA00A72C7F /* metro-now.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "metro-now.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 2DC639DB2BF3CCBA00A72C7F /* metro_nowApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metro_nowApp.swift; sourceTree = ""; }; @@ -41,6 +53,9 @@ 2DC63A0E2BF4D13200A72C7F /* stationsJSONManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = stationsJSONManager.swift; sourceTree = ""; }; 2DC63A102BF4D3F800A72C7F /* stations.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; name = stations.json; path = ../../../../data/stations.json; sourceTree = ""; }; 2DC63A122BF4D98F00A72C7F /* getMetroLineColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = getMetroLineColor.swift; sourceTree = ""; }; + 2DC63A182BF50E8F00A72C7F /* metro-now-tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "metro-now-tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DC63A1A2BF50E8F00A72C7F /* metro_now_tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metro_now_tests.swift; sourceTree = ""; }; + 2DC63A212BF50EDD00A72C7F /* formatTimeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = formatTimeTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -51,6 +66,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2DC63A152BF50E8F00A72C7F /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -58,6 +80,7 @@ isa = PBXGroup; children = ( 2DC639DA2BF3CCBA00A72C7F /* metro-now */, + 2DC63A192BF50E8F00A72C7F /* metro-now-tests */, 2DC639D92BF3CCBA00A72C7F /* Products */, ); sourceTree = ""; @@ -66,6 +89,7 @@ isa = PBXGroup; children = ( 2DC639D82BF3CCBA00A72C7F /* metro-now.app */, + 2DC63A182BF50E8F00A72C7F /* metro-now-tests.xctest */, ); name = Products; sourceTree = ""; @@ -156,6 +180,15 @@ path = Models; sourceTree = ""; }; + 2DC63A192BF50E8F00A72C7F /* metro-now-tests */ = { + isa = PBXGroup; + children = ( + 2DC63A1A2BF50E8F00A72C7F /* metro_now_tests.swift */, + 2DC63A212BF50EDD00A72C7F /* formatTimeTests.swift */, + ); + path = "metro-now-tests"; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -176,6 +209,24 @@ productReference = 2DC639D82BF3CCBA00A72C7F /* metro-now.app */; productType = "com.apple.product-type.application"; }; + 2DC63A172BF50E8F00A72C7F /* metro-now-tests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2DC63A1E2BF50E8F00A72C7F /* Build configuration list for PBXNativeTarget "metro-now-tests" */; + buildPhases = ( + 2DC63A142BF50E8F00A72C7F /* Sources */, + 2DC63A152BF50E8F00A72C7F /* Frameworks */, + 2DC63A162BF50E8F00A72C7F /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 2DC63A1D2BF50E8F00A72C7F /* PBXTargetDependency */, + ); + name = "metro-now-tests"; + productName = "metro-now-tests"; + productReference = 2DC63A182BF50E8F00A72C7F /* metro-now-tests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -189,6 +240,10 @@ 2DC639D72BF3CCBA00A72C7F = { CreatedOnToolsVersion = 15.4; }; + 2DC63A172BF50E8F00A72C7F = { + CreatedOnToolsVersion = 15.4; + TestTargetID = 2DC639D72BF3CCBA00A72C7F; + }; }; }; buildConfigurationList = 2DC639D32BF3CCBA00A72C7F /* Build configuration list for PBXProject "metro-now" */; @@ -205,6 +260,7 @@ projectRoot = ""; targets = ( 2DC639D72BF3CCBA00A72C7F /* metro-now */, + 2DC63A172BF50E8F00A72C7F /* metro-now-tests */, ); }; /* End PBXProject section */ @@ -220,6 +276,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2DC63A162BF50E8F00A72C7F /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -242,8 +305,25 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2DC63A142BF50E8F00A72C7F /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2DC63A1B2BF50E8F00A72C7F /* metro_now_tests.swift in Sources */, + 2DC63A222BF50EDD00A72C7F /* formatTimeTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ +/* Begin PBXTargetDependency section */ + 2DC63A1D2BF50E8F00A72C7F /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 2DC639D72BF3CCBA00A72C7F /* metro-now */; + targetProxy = 2DC63A1C2BF50E8F00A72C7F /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + /* Begin XCBuildConfiguration section */ 2DC639E42BF3CCBC00A72C7F /* Debug */ = { isa = XCBuildConfiguration; @@ -424,6 +504,42 @@ }; name = Release; }; + 2DC63A1F2BF50E8F00A72C7F /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = R6WU5ABNG2; + GENERATE_INFOPLIST_FILE = YES; + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.krystof.metro-now-tests"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = NO; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/metro-now.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/metro-now"; + }; + name = Debug; + }; + 2DC63A202BF50E8F00A72C7F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = R6WU5ABNG2; + GENERATE_INFOPLIST_FILE = YES; + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.krystof.metro-now-tests"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = NO; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/metro-now.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/metro-now"; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -445,6 +561,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 2DC63A1E2BF50E8F00A72C7F /* Build configuration list for PBXNativeTarget "metro-now-tests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2DC63A1F2BF50E8F00A72C7F /* Debug */, + 2DC63A202BF50E8F00A72C7F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = 2DC639D02BF3CCBA00A72C7F /* Project object */; diff --git a/app/metro-now/metro-now/Utils/formatTime.swift b/app/metro-now/metro-now/Utils/formatTime.swift index 468de33e..78faea21 100644 --- a/app/metro-now/metro-now/Utils/formatTime.swift +++ b/app/metro-now/metro-now/Utils/formatTime.swift @@ -7,16 +7,39 @@ import Foundation +let twelveHoursSeconds = 12 * 60 * 60 + func formatTime(seconds: Int) -> String { - let hours = seconds / 3600 - let minutes = (seconds % 3600) / 60 - let remainingSeconds = seconds % 60 + let isNegative = seconds < 0 + + if isNegative, seconds < -twelveHoursSeconds { + return "- >12h" + } else if !isNegative, seconds > twelveHoursSeconds { + return ">12h" + } + let positiveSeconds = isNegative ? -seconds : seconds + + let hours = positiveSeconds / 3600 + let minutes = (positiveSeconds % 3600) / 60 + let remainingSeconds = positiveSeconds % 60 + + var output: String = isNegative ? "-" : "" if hours > 0 { - return String(format: "%dh %dm", hours, minutes) + if minutes == 0 { + output += String(format: "%dh", hours) + } else { + output += String(format: "%dh %dm", hours, minutes) + } } else if minutes > 0 { - return String(format: "%dm %ds", minutes, remainingSeconds) + if remainingSeconds == 0 { + output += String(format: "%dm", minutes) + } else { + output += String(format: "%dm %ds", minutes, remainingSeconds) + } } else { - return String(format: "%ds", remainingSeconds) + output += String(format: "%ds", remainingSeconds) } + + return output }