|
| 1 | +// |
| 2 | +// RoundTripTests.swift |
| 3 | +// EmailAddress Tests |
| 4 | +// |
| 5 | +// Tests for round-trip conversions between RFC formats |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +@testable import EmailAddress |
| 10 | +import RFC_5321 |
| 11 | +import RFC_5322 |
| 12 | +import RFC_6531 |
| 13 | +import Testing |
| 14 | + |
| 15 | +@Suite("Round-Trip Conversion Tests") |
| 16 | +struct RoundTripTests { |
| 17 | + |
| 18 | + @Test("RFC 5321 -> EmailAddress -> RFC 5321") |
| 19 | + func rfc5321RoundTrip() throws { |
| 20 | + let original = try RFC_5321.EmailAddress("[email protected]") |
| 21 | + let emailAddress = try EmailAddress(rfc5321: original) |
| 22 | + |
| 23 | + guard let converted = emailAddress.rfc5321 else { |
| 24 | + throw EmailAddress.Error.conversionFailure |
| 25 | + } |
| 26 | + |
| 27 | + #expect(converted.addressValue == original.addressValue) |
| 28 | + #expect(converted.displayName == original.displayName) |
| 29 | + #expect(converted.localPart.description == original.localPart.description) |
| 30 | + #expect(converted.domain.name == original.domain.name) |
| 31 | + } |
| 32 | + |
| 33 | + @Test("RFC 5322 -> EmailAddress -> RFC 5322") |
| 34 | + func rfc5322RoundTrip() throws { |
| 35 | + let original = try RFC_5322.EmailAddress("John Doe <[email protected]>") |
| 36 | + let emailAddress = try EmailAddress(rfc5322: original) |
| 37 | + |
| 38 | + guard let converted = emailAddress.rfc5322 else { |
| 39 | + throw EmailAddress.Error.conversionFailure |
| 40 | + } |
| 41 | + |
| 42 | + #expect(converted.addressValue == original.addressValue) |
| 43 | + #expect(converted.displayName == original.displayName) |
| 44 | + } |
| 45 | + |
| 46 | + @Test("RFC 6531 -> EmailAddress -> RFC 6531") |
| 47 | + func rfc6531RoundTrip() throws { |
| 48 | + let original = try RFC_6531.EmailAddress("用户@example.com") |
| 49 | + let emailAddress = EmailAddress(rfc6531: original) |
| 50 | + let converted = emailAddress.rfc6531 |
| 51 | + |
| 52 | + #expect(converted.addressValue == original.addressValue) |
| 53 | + #expect(converted.displayName == original.displayName) |
| 54 | + } |
| 55 | + |
| 56 | + @Test("ASCII email has all RFC format representations") |
| 57 | + func asciiEmailAllFormats() throws { |
| 58 | + // Initialize from string - using init with components to avoid ambiguity |
| 59 | + let emailAddress = try EmailAddress(localPart: "test", domain: "example.com") |
| 60 | + |
| 61 | + #expect(emailAddress.isASCII == true) |
| 62 | + #expect(emailAddress.rfc5321 != nil) |
| 63 | + #expect(emailAddress.rfc5322 != nil) |
| 64 | + |
| 65 | + // Verify all formats produce the same address value |
| 66 | + let rfc5321Value = emailAddress.rfc5321?.addressValue |
| 67 | + let rfc5322Value = emailAddress.rfc5322?.addressValue |
| 68 | + let rfc6531Value = emailAddress.rfc6531.addressValue |
| 69 | + |
| 70 | + #expect (rfc5321Value == "[email protected]") |
| 71 | + #expect (rfc5322Value == "[email protected]") |
| 72 | + #expect (rfc6531Value == "[email protected]") |
| 73 | + } |
| 74 | + |
| 75 | + @Test("Internationalized email only has RFC 6531 format") |
| 76 | + func internationalizedEmailFormat() throws { |
| 77 | + // Create via RFC 6531 directly to avoid ambiguity |
| 78 | + let rfc6531 = try RFC_6531.EmailAddress("用户@example.com") |
| 79 | + let emailAddress = EmailAddress(rfc6531: rfc6531) |
| 80 | + |
| 81 | + #expect(emailAddress.isInternationalized == true) |
| 82 | + #expect(emailAddress.rfc5321 == nil) |
| 83 | + #expect(emailAddress.rfc5322 == nil) |
| 84 | + #expect(emailAddress.rfc6531.addressValue == "用户@example.com") |
| 85 | + } |
| 86 | +} |
0 commit comments