|
| 1 | +#if SWIFT_SYNTAX_EXTENSION_MACRO_FIXED |
| 2 | +import XCTest |
| 3 | + |
| 4 | +@testable import CodableMacroPlugin |
| 5 | + |
| 6 | +final class AccessModifierTests: XCTestCase { |
| 7 | + |
| 8 | + func testPublic() throws { |
| 9 | + assertMacroExpansion( |
| 10 | + """ |
| 11 | + @Codable |
| 12 | + @MemberInit |
| 13 | + public struct SomeCodable { |
| 14 | + let value: String |
| 15 | + } |
| 16 | + """, |
| 17 | + expandedSource: |
| 18 | + """ |
| 19 | + public struct SomeCodable { |
| 20 | + let value: String |
| 21 | +
|
| 22 | + public init(value: String) { |
| 23 | + self.value = value |
| 24 | + } |
| 25 | + } |
| 26 | +
|
| 27 | + extension SomeCodable: Decodable { |
| 28 | + public init(from decoder: any Decoder) throws { |
| 29 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 30 | + self.value = try container.decode(String.self, forKey: CodingKeys.value) |
| 31 | + } |
| 32 | + } |
| 33 | +
|
| 34 | + extension SomeCodable: Encodable { |
| 35 | + public func encode(to encoder: any Encoder) throws { |
| 36 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 37 | + try container.encode(self.value, forKey: CodingKeys.value) |
| 38 | + } |
| 39 | + } |
| 40 | +
|
| 41 | + extension SomeCodable { |
| 42 | + enum CodingKeys: String, CodingKey { |
| 43 | + case value = "value" |
| 44 | + } |
| 45 | + } |
| 46 | + """ |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + func testPackage() throws { |
| 51 | + assertMacroExpansion( |
| 52 | + """ |
| 53 | + @Codable |
| 54 | + @MemberInit |
| 55 | + package struct SomeCodable { |
| 56 | + let value: String |
| 57 | + } |
| 58 | + """, |
| 59 | + expandedSource: |
| 60 | + """ |
| 61 | + package struct SomeCodable { |
| 62 | + let value: String |
| 63 | +
|
| 64 | + package init(value: String) { |
| 65 | + self.value = value |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + extension SomeCodable: Decodable { |
| 70 | + package init(from decoder: any Decoder) throws { |
| 71 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 72 | + self.value = try container.decode(String.self, forKey: CodingKeys.value) |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + extension SomeCodable: Encodable { |
| 77 | + package func encode(to encoder: any Encoder) throws { |
| 78 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 79 | + try container.encode(self.value, forKey: CodingKeys.value) |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + extension SomeCodable { |
| 84 | + enum CodingKeys: String, CodingKey { |
| 85 | + case value = "value" |
| 86 | + } |
| 87 | + } |
| 88 | + """ |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + func testOthers() throws { |
| 93 | + for modifier in ["internal", "fileprivate", "private", ""] { |
| 94 | + let prefix = modifier.isEmpty ? "" : "\(modifier) " |
| 95 | + assertMacroExpansion( |
| 96 | + """ |
| 97 | + @Codable |
| 98 | + @MemberInit |
| 99 | + \(prefix)struct SomeCodable { |
| 100 | + let value: String |
| 101 | + } |
| 102 | + """, |
| 103 | + expandedSource: |
| 104 | + """ |
| 105 | + \(prefix)struct SomeCodable { |
| 106 | + let value: String |
| 107 | +
|
| 108 | + init(value: String) { |
| 109 | + self.value = value |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + extension SomeCodable: Decodable { |
| 114 | + init(from decoder: any Decoder) throws { |
| 115 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 116 | + self.value = try container.decode(String.self, forKey: CodingKeys.value) |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + extension SomeCodable: Encodable { |
| 121 | + func encode(to encoder: any Encoder) throws { |
| 122 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 123 | + try container.encode(self.value, forKey: CodingKeys.value) |
| 124 | + } |
| 125 | + } |
| 126 | +
|
| 127 | + extension SomeCodable { |
| 128 | + enum CodingKeys: String, CodingKey { |
| 129 | + case value = "value" |
| 130 | + } |
| 131 | + } |
| 132 | + """ |
| 133 | + ) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +#endif |
0 commit comments