// // QuizAppUnitTests.swift // QuizAppUnitTests // // Created by Paul on 08.09.25. // import XCTest @testable import QuizApp final class QuizAppUnitTests: XCTestCase { private func makeMC( question: String, answers: [String] = ["A", "B", "C"], correct: Int ) -> QuizQuestion { struct Payload: Encodable { let question: String let answers: [String] let correctAnswer: Int } let payload = Payload( question: question, answers: answers, correctAnswer: correct ) let data = try! JSONEncoder().encode(payload) return try! JSONDecoder().decode(QuizQuestion.self, from: data) } private func makeEst( question: String, correct: Double, min: Double = 0, max: Double = 100, unit: String = "" ) -> QuizQuestion { struct Payload: Encodable { let type = "estimation" let question: String let minValue: Double let maxValue: Double let correctValue: Double let unit: String } let payload = Payload( question: question, minValue: min, maxValue: max, correctValue: correct, unit: unit ) let data = try! JSONEncoder().encode(payload) return try! JSONDecoder().decode(QuizQuestion.self, from: data) } func testIncrementScore_CorrectAnswer() { let vm = QuizViewModel() vm.questions = [makeMC(question: "Q1", correct: 2)] vm.currentQuestionIndex = 0 XCTAssertEqual(vm.score, 0) vm.incrementScore(selectedIndex: 2) XCTAssertEqual(vm.score, 1) } func testIncrementScore_WrongAnswer() { let vm = QuizViewModel() vm.questions = [makeMC(question: "Q1", correct: 2)] vm.currentQuestionIndex = 0 vm.incrementScore(selectedIndex: 1) XCTAssertEqual(vm.score, 0) } func testLoadNextQuestion_NotFinished() { let vm = QuizViewModel() vm.questions = [ makeMC(question: "Q1", correct: 0), makeMC(question: "Q2", correct: 0), ] vm.currentQuestionIndex = 0 vm.loadNextQuestion() XCTAssertFalse(vm.isQuizFinished) } func testLoadNextQuestion_Finished() { let vm = QuizViewModel() vm.questions = [makeMC(question: "Q1", correct: 0)] vm.currentQuestionIndex = 0 vm.loadNextQuestion() XCTAssertTrue(vm.isQuizFinished) } func testPointsForEstimation_onePoint() { let vm = QuizViewModel() let p = vm.pointsForEstimation(guess: 109, correct: 100, maxPoints: 1) XCTAssertEqual(p, 1) } func testPointsForEstimation_ZeroPoints() { let vm = QuizViewModel() let p = vm.pointsForEstimation(guess: 111, correct: 100, maxPoints: 1) XCTAssertEqual(p, 0) } }