Files
t3code/apps/swift-ios/Tests/FeatureTests/UserInputAnswerTests.swift
T

65 lines
2.3 KiB
Swift

import Foundation
import Testing
@testable import T3Code
@Suite("User input answers")
struct UserInputAnswerTests {
@Test
func testCodableShapeMatchesProviderWireValues() throws {
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let textData = try encoder.encode(FeatureInputAnswer.text("Deploy"))
let selectionsData = try encoder.encode(
FeatureInputAnswer.selections(["Server", "Web"])
)
#expect(try decoder.decode(JSONValue.self, from: textData) == .string("Deploy"))
#expect(
try decoder.decode(JSONValue.self, from: selectionsData)
== .array([.string("Server"), .string("Web")])
)
#expect(try decoder.decode(FeatureInputAnswer.self, from: textData) == .text("Deploy"))
#expect(
try decoder.decode(FeatureInputAnswer.self, from: selectionsData)
== .selections(["Server", "Web"])
)
}
@Test
func testNativeJSONMappingPreservesStringAndArrayTypes() {
#expect(FeatureInputAnswer.text("Deploy").jsonValue == .string("Deploy"))
#expect(
FeatureInputAnswer.selections(["Server", "Web"]).jsonValue
== .array([.string("Server"), .string("Web")])
)
}
@Test
func testMultiSelectTogglesWithoutFlatteningSelections() {
let first = FeatureInputAnswer.selections([])
.togglingOption("Server", allowsMultiple: true)
let second = first.togglingOption("Web", allowsMultiple: true)
let deselected = second.togglingOption("Server", allowsMultiple: true)
#expect(first == .selections(["Server"]))
#expect(second == .selections(["Server", "Web"]))
#expect(deselected == .selections(["Web"]))
#expect(
second.togglingOption("CLI", allowsMultiple: false)
== .text("CLI")
)
}
@Test
func testAnswersNormalizeBeforeSubmission() {
#expect(FeatureInputAnswer.text(" ship it ").normalized == .text("ship it"))
#expect(
FeatureInputAnswer.selections([" Server ", "", "Server", "Web"]).normalized
== .selections(["Server", "Web"])
)
#expect(FeatureInputAnswer.text(" ").normalized == nil)
#expect(FeatureInputAnswer.selections([]).normalized == nil)
}
}