|
| 1 | +import Testing |
| 2 | + |
| 3 | +@testable import SyntaxKit |
| 4 | + |
| 5 | +struct ProtocolTests { |
| 6 | + @Test func testSimpleProtocol() { |
| 7 | + let vehicleProtocol = Protocol("Vehicle") { |
| 8 | + PropertyRequirement("numberOfWheels", type: "Int", access: .get) |
| 9 | + PropertyRequirement("brand", type: "String", access: .getSet) |
| 10 | + FunctionRequirement("start") |
| 11 | + FunctionRequirement("stop") |
| 12 | + FunctionRequirement("speed", returns: "Int") |
| 13 | + } |
| 14 | + |
| 15 | + let expected = """ |
| 16 | + protocol Vehicle { |
| 17 | + var numberOfWheels: Int { get } |
| 18 | + var brand: String { get set } |
| 19 | + func start() |
| 20 | + func stop() |
| 21 | + func speed() -> Int |
| 22 | + } |
| 23 | + """ |
| 24 | + |
| 25 | + let normalizedGenerated = vehicleProtocol.generateCode().normalize() |
| 26 | + let normalizedExpected = expected.normalize() |
| 27 | + #expect(normalizedGenerated == normalizedExpected) |
| 28 | + } |
| 29 | + |
| 30 | + @Test func testEmptyProtocol() { |
| 31 | + let emptyProtocol = Protocol("EmptyProtocol") {} |
| 32 | + |
| 33 | + let expected = """ |
| 34 | + protocol EmptyProtocol { |
| 35 | + } |
| 36 | + """ |
| 37 | + |
| 38 | + let normalizedGenerated = emptyProtocol.generateCode().normalize() |
| 39 | + let normalizedExpected = expected.normalize() |
| 40 | + #expect(normalizedGenerated == normalizedExpected) |
| 41 | + } |
| 42 | + |
| 43 | + @Test func testProtocolWithInheritance() { |
| 44 | + let protocolWithInheritance = Protocol("MyProtocol") { |
| 45 | + PropertyRequirement("value", type: "String", access: .getSet) |
| 46 | + }.inherits("Equatable", "Hashable") |
| 47 | + |
| 48 | + let expected = """ |
| 49 | + protocol MyProtocol: Equatable, Hashable { |
| 50 | + var value: String { get set } |
| 51 | + } |
| 52 | + """ |
| 53 | + |
| 54 | + let normalizedGenerated = protocolWithInheritance.generateCode().normalize() |
| 55 | + let normalizedExpected = expected.normalize() |
| 56 | + #expect(normalizedGenerated == normalizedExpected) |
| 57 | + } |
| 58 | + |
| 59 | + @Test func testFunctionRequirementWithParameters() { |
| 60 | + let protocolWithFunction = Protocol("Calculator") { |
| 61 | + FunctionRequirement("add", returns: "Int") { |
| 62 | + Parameter(name: "a", type: "Int") |
| 63 | + Parameter(name: "b", type: "Int") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + let expected = """ |
| 68 | + protocol Calculator { |
| 69 | + func add(a: Int, b: Int) -> Int |
| 70 | + } |
| 71 | + """ |
| 72 | + |
| 73 | + let normalizedGenerated = protocolWithFunction.generateCode().normalize() |
| 74 | + let normalizedExpected = expected.normalize() |
| 75 | + #expect(normalizedGenerated == normalizedExpected) |
| 76 | + } |
| 77 | + |
| 78 | + @Test func testStaticFunctionRequirement() { |
| 79 | + let protocolWithStaticFunction = Protocol("Factory") { |
| 80 | + FunctionRequirement("create", returns: "Self").static() |
| 81 | + } |
| 82 | + |
| 83 | + let expected = """ |
| 84 | + protocol Factory { |
| 85 | + static func create() -> Self |
| 86 | + } |
| 87 | + """ |
| 88 | + |
| 89 | + let normalizedGenerated = protocolWithStaticFunction.generateCode().normalize() |
| 90 | + let normalizedExpected = expected.normalize() |
| 91 | + #expect(normalizedGenerated == normalizedExpected) |
| 92 | + } |
| 93 | + |
| 94 | + @Test func testMutatingFunctionRequirement() { |
| 95 | + let protocolWithMutatingFunction = Protocol("Resettable") { |
| 96 | + FunctionRequirement("reset").mutating() |
| 97 | + } |
| 98 | + |
| 99 | + let expected = """ |
| 100 | + protocol Resettable { |
| 101 | + mutating func reset() |
| 102 | + } |
| 103 | + """ |
| 104 | + |
| 105 | + let normalizedGenerated = protocolWithMutatingFunction.generateCode().normalize() |
| 106 | + let normalizedExpected = expected.normalize() |
| 107 | + #expect(normalizedGenerated == normalizedExpected) |
| 108 | + } |
| 109 | + |
| 110 | + @Test func testPropertyRequirementGetOnly() { |
| 111 | + let propertyReq = PropertyRequirement("readOnlyProperty", type: "String", access: .get) |
| 112 | + let prtcl = Protocol("TestProtocol") { |
| 113 | + propertyReq |
| 114 | + } |
| 115 | + |
| 116 | + let expected = """ |
| 117 | + protocol TestProtocol { |
| 118 | + var readOnlyProperty: String { get } |
| 119 | + } |
| 120 | + """ |
| 121 | + |
| 122 | + let normalizedGenerated = prtcl.generateCode().normalize() |
| 123 | + let normalizedExpected = expected.normalize() |
| 124 | + #expect(normalizedGenerated == normalizedExpected) |
| 125 | + } |
| 126 | + |
| 127 | + @Test func testPropertyRequirementGetSet() { |
| 128 | + let propertyReq = PropertyRequirement("readWriteProperty", type: "Int", access: .getSet) |
| 129 | + let prtcl = Protocol("TestProtocol") { |
| 130 | + propertyReq |
| 131 | + } |
| 132 | + |
| 133 | + let expected = """ |
| 134 | + protocol TestProtocol { |
| 135 | + var readWriteProperty: Int { get set } |
| 136 | + } |
| 137 | + """ |
| 138 | + |
| 139 | + let normalizedGenerated = prtcl.generateCode().normalize() |
| 140 | + let normalizedExpected = expected.normalize() |
| 141 | + #expect(normalizedGenerated == normalizedExpected) |
| 142 | + } |
| 143 | + |
| 144 | + @Test func testFunctionRequirementWithDefaultParameters() { |
| 145 | + let functionReq = FunctionRequirement("process", returns: "String") { |
| 146 | + Parameter(name: "input", type: "String") |
| 147 | + Parameter(name: "options", type: "ProcessingOptions", defaultValue: "ProcessingOptions()") |
| 148 | + } |
| 149 | + let prtcl = Protocol("TestProtocol") { |
| 150 | + functionReq |
| 151 | + } |
| 152 | + |
| 153 | + let expected = """ |
| 154 | + protocol TestProtocol { |
| 155 | + func process(input: String, options: ProcessingOptions = ProcessingOptions()) -> String |
| 156 | + } |
| 157 | + """ |
| 158 | + |
| 159 | + let normalizedGenerated = prtcl.generateCode().normalize() |
| 160 | + let normalizedExpected = expected.normalize() |
| 161 | + #expect(normalizedGenerated == normalizedExpected) |
| 162 | + } |
| 163 | + |
| 164 | + @Test func testComplexProtocolWithMixedRequirements() { |
| 165 | + let complexProtocol = Protocol("ComplexProtocol") { |
| 166 | + PropertyRequirement("id", type: "UUID", access: .get) |
| 167 | + PropertyRequirement("name", type: "String", access: .getSet) |
| 168 | + FunctionRequirement("initialize").mutating() |
| 169 | + FunctionRequirement("process", returns: "Result") { |
| 170 | + Parameter(name: "input", type: "Data") |
| 171 | + } |
| 172 | + FunctionRequirement("factory", returns: "Self").static() |
| 173 | + }.inherits("Identifiable") |
| 174 | + |
| 175 | + let expected = """ |
| 176 | + protocol ComplexProtocol: Identifiable { |
| 177 | + var id: UUID { get } |
| 178 | + var name: String { get set } |
| 179 | + mutating func initialize() |
| 180 | + func process(input: Data) -> Result |
| 181 | + static func factory() -> Self |
| 182 | + } |
| 183 | + """ |
| 184 | + |
| 185 | + let normalizedGenerated = complexProtocol.generateCode().normalize() |
| 186 | + let normalizedExpected = expected.normalize() |
| 187 | + #expect(normalizedGenerated == normalizedExpected) |
| 188 | + } |
| 189 | +} |
0 commit comments