|
20 | 20 | // Internet Access iOS Client. If not, see <https://www.gnu.org/licenses/>. |
21 | 21 | // |
22 | 22 |
|
23 | | -import XCTest |
| 23 | +import Testing |
24 | 24 | @testable import PIALibrary |
25 | 25 |
|
26 | | -class ValidatorTests: XCTestCase { |
27 | | - |
28 | | - func testValidatorGiftCardIsValid() { |
29 | | - let giftCode = "1234567812345678" |
30 | | - XCTAssertTrue(Validator.validate(giftCode: giftCode)) |
| 26 | +@Suite("Email Validation Tests") |
| 27 | +struct ValidatorTests { |
| 28 | + |
| 29 | + // MARK: - Valid Email Tests |
| 30 | + |
| 31 | + @Test("Valid email addresses should pass validation") |
| 32 | + func validEmails() throws { |
| 33 | + let validEmails = [ |
| 34 | + "user@example.com", |
| 35 | + "test.user@example.com", |
| 36 | + "test+tag@example.co.uk", |
| 37 | + "user123@test-domain.com", |
| 38 | + "first.last@subdomain.example.org", |
| 39 | + "a@example.com", |
| 40 | + "test_user@example.com" |
| 41 | + ] |
| 42 | + |
| 43 | + for email in validEmails { |
| 44 | + try Validator.validate(email: email) |
| 45 | + } |
31 | 46 | } |
32 | | - |
33 | | - func testValidatorGiftCardIsInvalid() { |
34 | | - let giftCode = "12345678678" |
35 | | - XCTAssertFalse(Validator.validate(giftCode: giftCode)) |
| 47 | + |
| 48 | + // MARK: - Empty Email Tests |
| 49 | + |
| 50 | + @Test("Nil email should throw emailIsEmpty error") |
| 51 | + func nilEmail() { |
| 52 | + #expect(throws: Validator.EmailValidationError.emailIsEmpty) { |
| 53 | + try Validator.validate(email: nil) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test("Empty string should throw emailIsEmpty error") |
| 58 | + func emptyEmail() { |
| 59 | + #expect(throws: Validator.EmailValidationError.emailIsEmpty) { |
| 60 | + try Validator.validate(email: "") |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test("Whitespace-only email should throw emailIsInvalid error") |
| 65 | + func whitespaceOnlyEmail() { |
| 66 | + #expect(throws: Validator.EmailValidationError.emailIsInvalid) { |
| 67 | + try Validator.validate(email: " ") |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // MARK: - Invalid Email Tests |
| 72 | + |
| 73 | + @Test("Invalid email formats should throw emailIsInvalid error", arguments: [ |
| 74 | + "plaintext", |
| 75 | + "@example.com", |
| 76 | + "user@", |
| 77 | + "user @example.com", |
| 78 | + "user@example .com", |
| 79 | + "user@.com", |
| 80 | + "user@example.", |
| 81 | + "user..name@example.com", |
| 82 | + "user@example..com", |
| 83 | + "user@-example.com", |
| 84 | + "user@example-.com", |
| 85 | + "user@example", |
| 86 | + "user name@example.com", |
| 87 | + "user@exam ple.com", |
| 88 | + ".user@example.com", |
| 89 | + "user.@example.com", |
| 90 | + "user@@example.com", |
| 91 | + "user@example@com", |
| 92 | + ]) |
| 93 | + func invalidEmails(email: String) { |
| 94 | + #expect(throws: Validator.EmailValidationError.emailIsInvalid) { |
| 95 | + try Validator.validate(email: email) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // MARK: - Edge Cases |
| 100 | + |
| 101 | + @Test("Email with leading/trailing spaces should be handled by caller") |
| 102 | + func emailWithSpaces() { |
| 103 | + // Note: The validator expects trimmed input |
| 104 | + // The UI layer should trim before validation |
| 105 | + #expect(throws: Validator.EmailValidationError.emailIsInvalid) { |
| 106 | + try Validator.validate(email: " user@example.com ") |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test("Very long valid email should pass") |
| 111 | + func longValidEmail() throws { |
| 112 | + let longEmail = "very.long.email.address.with.many.parts@very.long.domain.name.example.com" |
| 113 | + try Validator.validate(email: longEmail) |
36 | 114 | } |
37 | | - |
38 | 115 | } |
0 commit comments