ios-projects/AIStudyApp/AIStudyAppTests/QuizQuestionTypeTests.swift

137 lines
6.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// QuizQuestionTypeTests.swift M-QUIZ-01-CLEANUP-02
//
import XCTest
@testable import AIStudyApp
final class QuizQuestionTypeTests: XCTestCase {
//
//
//
func testDecodeChoice() {
XCTAssertEqual(QuizQuestionType.from(rawValue: "choice"), .choice)
}
func testDecodeFill() {
XCTAssertEqual(QuizQuestionType.from(rawValue: "fill"), .fill)
}
func testDecodeJudge() {
XCTAssertEqual(QuizQuestionType.from(rawValue: "judge"), .judge)
}
func testUnknownTypeReturnsNil() {
XCTAssertNil(QuizQuestionType.from(rawValue: "essay"))
XCTAssertNil(QuizQuestionType.from(rawValue: "multi_choice"))
XCTAssertNil(QuizQuestionType.from(rawValue: ""))
XCTAssertNil(QuizQuestionType.from(rawValue: nil))
}
//
// displayAnswer
//
func testChoiceDisplayAnswerIndexToText() {
let type = QuizQuestionType.choice
XCTAssertEqual(type.displayAnswer(raw: "0", options: ["氧气", "氮气"]), "氧气")
XCTAssertEqual(type.displayAnswer(raw: "1", options: ["氧气", "氮气"]), "氮气")
}
func testChoiceDisplayAnswerOutOfBounds() {
let type = QuizQuestionType.choice
//
XCTAssertEqual(type.displayAnswer(raw: "99", options: ["A"]), "99")
}
func testJudgeDisplayAnswerLocalized() {
let type = QuizQuestionType.judge
XCTAssertEqual(type.displayAnswer(raw: "true"), "正确")
XCTAssertEqual(type.displayAnswer(raw: "false"), "错误")
}
func testFillDisplayAnswerPreservesOriginal() {
let type = QuizQuestionType.fill
//
XCTAssertEqual(type.displayAnswer(raw: " 光能 "), " 光能 ")
XCTAssertEqual(type.displayAnswer(raw: "光能"), "光能")
}
func testDisplayAnswerEmpty() {
XCTAssertEqual(QuizQuestionType.choice.displayAnswer(raw: "", options: ["A"]), "")
XCTAssertEqual(QuizQuestionType.fill.displayAnswer(raw: ""), "")
XCTAssertEqual(QuizQuestionType.judge.displayAnswer(raw: ""), "")
}
//
// displayName
//
func testDisplayName() {
XCTAssertEqual(QuizQuestionType.choice.displayName, "选择题")
XCTAssertEqual(QuizQuestionType.fill.displayName, "填空题")
XCTAssertEqual(QuizQuestionType.judge.displayName, "判断题")
}
//
// QuizQuestion.questionType
//
func testQuizQuestionTypeProperty() {
let q = QuizQuestion(id: "1", quizId: nil, stem: nil, questionText: nil, questionType: "choice", options: nil, answer: nil, correctAnswer: nil, explanation: nil, orderIndex: nil)
XCTAssertEqual(q.questionTypeEnum, .choice)
}
func testQuizQuestionUnknownTypeProperty() {
let q = QuizQuestion(id: "1", quizId: nil, stem: nil, questionText: nil, questionType: "essay", options: nil, answer: nil, correctAnswer: nil, explanation: nil, orderIndex: nil)
XCTAssertNil(q.questionTypeEnum)
}
func testQuizQuestionNilTypeProperty() {
let q = QuizQuestion(id: "1", quizId: nil, stem: nil, questionText: nil, questionType: nil, options: nil, answer: nil, correctAnswer: nil, explanation: nil, orderIndex: nil)
XCTAssertNil(q.questionTypeEnum)
}
//
//
//
func testChoiceSubmitValueIsIndexString() {
// choice "0", "1"
let answer = "0"
XCTAssertEqual(answer, "0") //
}
func testJudgeSubmitValueIsTrueOrFalse() {
// judge "true" "false"
XCTAssertTrue(["true", "false"].contains("true"))
XCTAssertTrue(["true", "false"].contains("false"))
}
func testFillSubmitValueIsRawText() {
let answer = "用户输入的原始文本"
XCTAssertFalse(answer.isEmpty)
}
//
// QuizResultView.answerDisplayText
//
func testAnswerDisplayTextChoice() {
let result = QuizResultView.answerDisplayText(raw: "0", type: .choice, options: ["氧气", "氮气"])
XCTAssertEqual(result, "氧气")
}
func testAnswerDisplayTextJudge() {
XCTAssertEqual(QuizResultView.answerDisplayText(raw: "true", type: .judge, options: []), "正确")
XCTAssertEqual(QuizResultView.answerDisplayText(raw: "false", type: .judge, options: []), "错误")
}
func testAnswerDisplayTextUnknownType() {
//
XCTAssertEqual(QuizResultView.answerDisplayText(raw: "essay answer", type: nil, options: []), "essay answer")
}
}