Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassFormsTests/GlassTextFieldsTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43import SPGlassCore44@testable import SPGlassForms4546@Suite("GlassTextField, GlassSecureField and GlassTextArea")47struct GlassTextFieldsTests {48 @Test func noRulesMeansAlwaysValid() {49 #expect(GlassFieldValidation.none.isValid(""))50 #expect(GlassFieldValidation.none.message(for: "anything") == nil)51 #expect(!GlassFieldValidation.none.isRequired)52 }5354 @Test func requiredRejectsEmptyAndBlankValues() {55 let validation = GlassFieldValidation(.required())56 #expect(validation.isRequired)57 #expect(validation.message(for: "") == GlassL10n.string(.fieldRequired))58 #expect(validation.message(for: " \n\t ") == GlassL10n.string(.fieldRequired))59 #expect(validation.isValid(" a "))60 }6162 @Test func callerMessagesReplaceTheDefaultOnes() {63 let validation = GlassFieldValidation(.required(message: "Name needed"), .minLength(3, message: "Too short"), .maxLength(5, message: "Too long"))64 #expect(validation.message(for: "") == "Name needed")65 #expect(validation.message(for: "ab") == "Too short")66 #expect(validation.message(for: "abcdef") == "Too long")67 #expect(validation.message(for: "abc") == nil)68 }6970 @Test func lengthsCountUserPerceivedCharacters() {71 let validation = GlassFieldValidation(.minLength(2), .maxLength(3))72 #expect(validation.isValid("👨👩👧👦🇨🇦"))73 #expect(validation.isValid("e\u{301}a\u{300}u\u{308}"))74 #expect(!validation.isValid("👨👩👧👦"))75 #expect(!validation.isValid("abcd"))76 #expect(validation.message(for: "a") == GlassL10n.string(.fieldTooShort, 2))77 #expect(validation.message(for: "abcd") == GlassL10n.string(.fieldTooLong, 3))78 }7980 @Test func lengthAndCustomRulesLeaveEmptinessToRequired() {81 let optional = GlassFieldValidation(.minLength(3), .custom(message: "Needs @") { $0.contains("@") })82 #expect(optional.isValid(""))83 #expect(optional.message(for: "abc") == "Needs @")84 #expect(optional.isValid("a@b"))85 }8687 @Test func theFirstBrokenRuleWins() {88 let validation = GlassFieldValidation(rules: [.custom(message: "first") { _ in false }, .custom(message: "second") { _ in false }])89 #expect(validation.message(for: "x") == "first")90 }9192 @Test func defaultMessagesExistInBothLanguages() {93 for key in [GlassL10n.Key.fieldRequired, .fieldTooShort, .fieldTooLong, .currentLocation] {94 let english = GlassL10n.string(key, localization: "en")95 let french = GlassL10n.string(key, localization: "fr")96 #expect(english != key.rawValue && french != key.rawValue && english != french)97 }98 }99100 @Test func messagesAppearAccordingToTheTrigger() {101 let validation = GlassFieldValidation(.required(message: "Required"))102 #expect(validation.visibleMessage(for: "", trigger: .always, hasFinishedEditing: false) == "Required")103 #expect(validation.visibleMessage(for: "", trigger: .afterEditing, hasFinishedEditing: false) == nil)104 #expect(validation.visibleMessage(for: "", trigger: .afterEditing, hasFinishedEditing: true) == "Required")105 #expect(validation.visibleMessage(for: "", trigger: .never, hasFinishedEditing: true) == nil)106 #expect(validation.visibleMessage(for: "ok", trigger: .always, hasFinishedEditing: true) == nil)107 }108109 @Test func anExternalErrorAlwaysWins() {110 let validation = GlassFieldValidation(.required(message: "Required"))111 #expect(validation.visibleMessage(for: "ok", trigger: .never, hasFinishedEditing: false, externalError: "Taken") == "Taken")112 #expect(validation.visibleMessage(for: "", trigger: .always, hasFinishedEditing: true, externalError: "Taken") == "Taken")113 #expect(validation.visibleMessage(for: "", trigger: .always, hasFinishedEditing: true, externalError: "") == "Required")114 }115116 @Test func inputPresetsDisableWhatWouldCorruptTheValue() {117 #expect(GlassTextInputConfiguration.standard == GlassTextInputConfiguration())118 #expect(!GlassTextInputConfiguration.standard.autocorrectionDisabled)119 #expect(GlassTextInputConfiguration.email.autocorrectionDisabled)120 #expect(GlassTextInputConfiguration.email.capitalization == .never)121 #expect(GlassTextInputConfiguration.email.keyboard == .emailAddress)122 #expect(GlassTextInputConfiguration.email.contentKind == .emailAddress)123 #expect(GlassTextInputConfiguration.username.contentKind == .username)124 #expect(GlassTextInputConfiguration.name.capitalization == .words)125 #expect(GlassTextInputConfiguration.number.keyboard == .numberPad)126 }127128 @Test func revealControlNamesTheActionItPerforms() {129 #expect(GlassSecureFieldReveal.labelKey(isRevealed: false) == .showPassword)130 #expect(GlassSecureFieldReveal.labelKey(isRevealed: true) == .hidePassword)131 #expect(GlassSecureFieldReveal.systemImage(isRevealed: false) != GlassSecureFieldReveal.systemImage(isRevealed: true))132 }133134 @Test func clearTextNeedsPermissionARequestAndAnEnabledField() {135 #expect(GlassSecureFieldReveal.showsClearText(allowsReveal: true, isRevealed: true, isEnabled: true))136 #expect(!GlassSecureFieldReveal.showsClearText(allowsReveal: false, isRevealed: true, isEnabled: true))137 #expect(!GlassSecureFieldReveal.showsClearText(allowsReveal: true, isRevealed: false, isEnabled: true))138 #expect(!GlassSecureFieldReveal.showsClearText(allowsReveal: true, isRevealed: true, isEnabled: false))139 }140141 @Test func secureContentMapsToPasswordContentTypes() {142 #expect(GlassSecureContent.password.contentKind == .password)143 #expect(GlassSecureContent.newPassword.contentKind == .newPassword)144 }145146 @Test func clampNeverSplitsAComposedCharacter() {147 let text = "a👨👩👧👦e\u{301}🇨🇦z"148 #expect(GlassTextLimit.count(text) == 5)149 #expect(GlassTextLimit.clamp(text, to: 2) == "a👨👩👧👦")150 #expect(GlassTextLimit.clamp(text, to: 3) == "a👨👩👧👦e\u{301}")151 #expect(GlassTextLimit.clamp(text, to: 4) == "a👨👩👧👦e\u{301}🇨🇦")152 #expect(GlassTextLimit.clamp(text, to: 5) == text)153 #expect(GlassTextLimit.clamp(text, to: 99) == text)154 }155156 @Test func clampHandlesMissingZeroAndNegativeLimits() {157 #expect(GlassTextLimit.clamp("abc", to: nil) == "abc")158 #expect(GlassTextLimit.clamp("abc", to: 0).isEmpty)159 #expect(GlassTextLimit.clamp("abc", to: -5).isEmpty)160 #expect(GlassTextLimit.clamp("", to: 3).isEmpty)161 }162163 @Test func remainingAndCounters() {164 let locale = Locale(identifier: "en_US")165 #expect(GlassTextLimit.remaining("abc", limit: 10) == 7)166 #expect(GlassTextLimit.remaining("abc", limit: 3) == 0)167 #expect(GlassTextLimit.remaining("abc", limit: nil) == nil)168 #expect(GlassTextLimit.counter("abc", limit: 280, locale: locale) == "3/280")169 #expect(GlassTextLimit.counter("abc", limit: nil, locale: locale) == "3")170 #expect(GlassTextLimit.counter("", limit: 1500, locale: locale) == "0/1,500")171 #expect(GlassTextLimit.accessibilityCounter("abc", limit: 280) == GlassL10n.string(.charactersOfLimit, 3, 280))172 #expect(GlassTextLimit.accessibilityCounter("abc", limit: nil) == GlassL10n.string(.charactersCount, 3))173 }174}175