/ BalanceKitTests / WorkoutTrackerTests.swift
WorkoutTrackerTests.swift
1 // 2 // WorkoutTrackerTests.swift 3 // BalanceKitTests 4 // 5 // Created by Alexander Kunau on 13.12.25. 6 // 7 8 import XCTest 9 @testable import BalanceKit 10 11 final class WorkoutTrackerTests: XCTestCase { 12 var sut: WorkoutManager! 13 14 override func setUp() { 15 super.setUp() 16 sut = WorkoutManager() 17 sut.workouts = [] 18 } 19 20 override func tearDown() { 21 sut = nil 22 super.tearDown() 23 } 24 25 func testCalculateCaloriesRunning() { 26 let calories = Workout.calculateCalories(type: .running, durationInMinutes: 30, weightInKg: 70) 27 let expected = Int((600.0 / 60.0 * 30.0) * (70.0 / 70.0)) 28 XCTAssertEqual(calories, expected) 29 } 30 31 func testCalculateCaloriesWithDifferentWeight() { 32 let calories = Workout.calculateCalories(type: .running, durationInMinutes: 30, weightInKg: 80) 33 let expected = Int((600.0 / 60.0 * 30.0) * (80.0 / 70.0)) 34 XCTAssertEqual(calories, expected) 35 } 36 37 func testAddWorkout() { 38 sut.addWorkout(type: .running, durationInMinutes: 30, date: Date(), userWeight: 70) 39 40 XCTAssertEqual(sut.workouts.count, 1) 41 XCTAssertEqual(sut.workouts.first?.type, .running) 42 XCTAssertEqual(sut.workouts.first?.duration, 30 * 60) 43 } 44 45 func testTotalCaloriesBurnedForDay() { 46 let today = Date() 47 sut.addWorkout(type: .running, durationInMinutes: 30, date: today, userWeight: 70) 48 sut.addWorkout(type: .cycling, durationInMinutes: 45, date: today, userWeight: 70) 49 50 let total = sut.totalCaloriesBurnedForDay(today) 51 52 let runningCal = Workout.calculateCalories(type: .running, durationInMinutes: 30, weightInKg: 70) 53 let cyclingCal = Workout.calculateCalories(type: .cycling, durationInMinutes: 45, weightInKg: 70) 54 55 XCTAssertEqual(total, runningCal + cyclingCal) 56 } 57 58 func testWorkoutsForDay() { 59 let today = Date() 60 let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)! 61 62 sut.addWorkout(type: .running, durationInMinutes: 30, date: today, userWeight: 70) 63 sut.addWorkout(type: .cycling, durationInMinutes: 45, date: yesterday, userWeight: 70) 64 sut.addWorkout(type: .yoga, durationInMinutes: 60, date: today, userWeight: 70) 65 66 let todayWorkouts = sut.workoutsForDay(today) 67 68 XCTAssertEqual(todayWorkouts.count, 2) 69 XCTAssertTrue(todayWorkouts.contains(where: { $0.type == .running })) 70 XCTAssertTrue(todayWorkouts.contains(where: { $0.type == .yoga })) 71 } 72 73 func testDeleteWorkout() { 74 sut.addWorkout(type: .running, durationInMinutes: 30, date: Date(), userWeight: 70) 75 sut.addWorkout(type: .cycling, durationInMinutes: 45, date: Date(), userWeight: 70) 76 77 XCTAssertEqual(sut.workouts.count, 2) 78 79 sut.deleteWorkout(at: IndexSet(integer: 0)) 80 81 XCTAssertEqual(sut.workouts.count, 1) 82 } 83 84 // MARK: - Edge Case Tests 85 86 func testZeroDurationWorkout() { 87 let calories = Workout.calculateCalories(type: .running, durationInMinutes: 0, weightInKg: 70) 88 XCTAssertEqual(calories, 0) 89 } 90 91 func testNegativeWeightHandling() { 92 // Should handle gracefully 93 let calories = Workout.calculateCalories(type: .running, durationInMinutes: 30, weightInKg: -70) 94 XCTAssertGreaterThanOrEqual(calories, 0) 95 } 96 97 func testEmptyWorkoutsList() { 98 let today = Date() 99 let total = sut.totalCaloriesBurnedForDay(today) 100 101 XCTAssertEqual(total, 0) 102 } 103 104 func testTotalCaloriesForWeekWithNoWorkouts() { 105 let total = sut.totalCaloriesBurnedForWeek(startingFrom: Date()) 106 XCTAssertEqual(total, 0) 107 } 108 109 func testWorkoutsForDayReturnsEmptyForInvalidDate() { 110 let futureDate = Calendar.current.date(byAdding: .day, value: 365, to: Date())! 111 let workouts = sut.workoutsForDay(futureDate) 112 113 XCTAssertEqual(workouts.count, 0) 114 } 115 }