From 9cf764d326702bb8103f62f22bf2b35f20dd5911 Mon Sep 17 00:00:00 2001 From: "codecov-ai[bot]" <156709835+codecov-ai[bot]@users.noreply.github.com> Date: Wed, 18 Jun 2025 17:12:37 +0000 Subject: [PATCH 1/3] Add Tests for PR#69 --- .../ClassAndProtocolTests.swift | 298 +++++++++++++++++- 1 file changed, 297 insertions(+), 1 deletion(-) diff --git a/Tests/SyntaxKitTests/ClassAndProtocolTests.swift b/Tests/SyntaxKitTests/ClassAndProtocolTests.swift index 895b055..e3356eb 100644 --- a/Tests/SyntaxKitTests/ClassAndProtocolTests.swift +++ b/Tests/SyntaxKitTests/ClassAndProtocolTests.swift @@ -44,4 +44,300 @@ struct ClassAndProtocolTests { let normalizedExpected = expected.normalize() #expect(normalizedGenerated == normalizedExpected) } -} + + @Test func testEmptyProtocol() { + let emptyProtocol = Protocol("EmptyProtocol") {} + + let expected = """ + protocol EmptyProtocol { + } + """ + + let normalizedGenerated = emptyProtocol.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testProtocolWithInheritance() { + let protocolWithInheritance = Protocol("MyProtocol") { + PropertyRequirement("value", type: "String", access: .getSet) + }.inherits("Equatable", "Hashable") + + let expected = """ + protocol MyProtocol: Equatable, Hashable { + var value: String { get set } + } + """ + + let normalizedGenerated = protocolWithInheritance.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testFunctionRequirementWithParameters() { + let protocolWithFunction = Protocol("Calculator") { + FunctionRequirement("add", returns: "Int") { + Parameter(name: "a", type: "Int") + Parameter(name: "b", type: "Int") + } + } + + let expected = """ + protocol Calculator { + func add(a: Int, b: Int) -> Int + } + """ + + let normalizedGenerated = protocolWithFunction.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testStaticFunctionRequirement() { + let protocolWithStaticFunction = Protocol("Factory") { + FunctionRequirement("create", returns: "Self").static() + } + + let expected = """ + protocol Factory { + static func create() -> Self + } + """ + + let normalizedGenerated = protocolWithStaticFunction.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testMutatingFunctionRequirement() { + let protocolWithMutatingFunction = Protocol("Resettable") { + FunctionRequirement("reset").mutating() + } + + let expected = """ + protocol Resettable { + mutating func reset() + } + """ + + let normalizedGenerated = protocolWithMutatingFunction.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testEmptyClass() { + let emptyClass = Class("EmptyClass") {} + + let expected = """ + class EmptyClass { + } + """ + + let normalizedGenerated = emptyClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithGenerics() { + let genericClass = Class("Container", generics: ["T"]) { + Variable(.var, name: "value", type: "T") + } + + let expected = """ + class Container { + var value: T + } + """ + + let normalizedGenerated = genericClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithMultipleGenerics() { + let multiGenericClass = Class("Pair", generics: ["T", "U"]) { + Variable(.var, name: "first", type: "T") + Variable(.var, name: "second", type: "U") + } + + let expected = """ + class Pair { + var first: T + var second: U + } + """ + + let normalizedGenerated = multiGenericClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testFinalClass() { + let finalClass = Class("FinalClass") { + Variable(.var, name: "value", type: "String") + }.final() + + let expected = """ + final class FinalClass { + var value: String + } + """ + + let normalizedGenerated = finalClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithMultipleInheritance() { + let classWithMultipleInheritance = Class("AdvancedVehicle") { + Variable(.var, name: "speed", type: "Int") + }.inherits("Vehicle", "Codable", "Equatable") + + let expected = """ + class AdvancedVehicle: Vehicle, Codable, Equatable { + var speed: Int + } + """ + + let normalizedGenerated = classWithMultipleInheritance.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithGenericsAndInheritance() { + let genericClassWithInheritance = Class("GenericContainer", generics: ["T"]) { + Variable(.var, name: "items", type: "[T]") + }.inherits("Collection") + + let expected = """ + class GenericContainer: Collection { + var items: [T] + } + """ + + let normalizedGenerated = genericClassWithInheritance.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testFinalClassWithInheritanceAndGenerics() { + let finalGenericClass = Class("FinalGenericClass", generics: ["T"]) { + Variable(.var, name: "value", type: "T") + }.inherits("BaseClass").final() + + let expected = """ + final class FinalGenericClass: BaseClass { + var value: T + } + """ + + let normalizedGenerated = finalGenericClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithFunctions() { + let classWithFunctions = Class("Calculator") { + Function("add", returns: "Int") { + Parameter(name: "a", type: "Int") + Parameter(name: "b", type: "Int") + } _: { + Return { + VariableExp("a + b") + } + } + } + + let expected = """ + class Calculator { + func add(a: Int, b: Int) -> Int { + return a + b + } + } + """ + + let normalizedGenerated = classWithFunctions.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testPropertyRequirementGetOnly() { + let propertyReq = PropertyRequirement("readOnlyProperty", type: "String", access: .get) + let prtcl = Protocol("TestProtocol") { + propertyReq + } + + let expected = """ + protocol TestProtocol { + var readOnlyProperty: String { get } + } + """ + + let normalizedGenerated = prtcl.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testPropertyRequirementGetSet() { + let propertyReq = PropertyRequirement("readWriteProperty", type: "Int", access: .getSet) + let prtcl = Protocol("TestProtocol") { + propertyReq + } + + let expected = """ + protocol TestProtocol { + var readWriteProperty: Int { get set } + } + """ + + let normalizedGenerated = prtcl.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testFunctionRequirementWithDefaultParameters() { + let functionReq = FunctionRequirement("process", returns: "String") { + Parameter(name: "input", type: "String") + Parameter(name: "options", type: "ProcessingOptions", defaultValue: "ProcessingOptions()") + } + let prtcl = Protocol("TestProtocol") { + functionReq + } + + let expected = """ + protocol TestProtocol { + func process(input: String, options: ProcessingOptions = ProcessingOptions()) -> String + } + """ + + let normalizedGenerated = prtcl.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testComplexProtocolWithMixedRequirements() { + let complexProtocol = Protocol("ComplexProtocol") { + PropertyRequirement("id", type: "UUID", access: .get) + PropertyRequirement("name", type: "String", access: .getSet) + FunctionRequirement("initialize").mutating() + FunctionRequirement("process", returns: "Result") { + Parameter(name: "input", type: "Data") + } + FunctionRequirement("factory", returns: "Self").static() + }.inherits("Identifiable") + + let expected = """ + protocol ComplexProtocol: Identifiable { + var id: UUID { get } + var name: String { get set } + mutating func initialize() + func process(input: Data) -> Result + static func factory() -> Self + } + """ + + let normalizedGenerated = complexProtocol.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } +} \ No newline at end of file From 0ee2475b6a82a3382dfabefb5be3e5f4a3bf96c8 Mon Sep 17 00:00:00 2001 From: Leo Dion Date: Wed, 18 Jun 2025 13:20:28 -0400 Subject: [PATCH 2/3] fixup! Add Tests for PR#69 --- Tests/SyntaxKitTests/ClassAndProtocolTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SyntaxKitTests/ClassAndProtocolTests.swift b/Tests/SyntaxKitTests/ClassAndProtocolTests.swift index e3356eb..34ffe49 100644 --- a/Tests/SyntaxKitTests/ClassAndProtocolTests.swift +++ b/Tests/SyntaxKitTests/ClassAndProtocolTests.swift @@ -340,4 +340,4 @@ struct ClassAndProtocolTests { let normalizedExpected = expected.normalize() #expect(normalizedGenerated == normalizedExpected) } -} \ No newline at end of file +} From 6e6e5205a37d0ea1344afd49417d9ea5e1e017c2 Mon Sep 17 00:00:00 2001 From: leogdion Date: Wed, 18 Jun 2025 13:43:13 -0400 Subject: [PATCH 3/3] fixing long file --- Tests/SyntaxKitTests/ClassTests.swift | 159 ++++++++++++++++++ ...rotocolTests.swift => ProtocolTests.swift} | 156 +---------------- 2 files changed, 160 insertions(+), 155 deletions(-) create mode 100644 Tests/SyntaxKitTests/ClassTests.swift rename Tests/SyntaxKitTests/{ClassAndProtocolTests.swift => ProtocolTests.swift} (56%) diff --git a/Tests/SyntaxKitTests/ClassTests.swift b/Tests/SyntaxKitTests/ClassTests.swift new file mode 100644 index 0000000..f9c3836 --- /dev/null +++ b/Tests/SyntaxKitTests/ClassTests.swift @@ -0,0 +1,159 @@ +import Testing + +@testable import SyntaxKit + +struct ClassTests { + @Test func testClassWithInheritance() { + let carClass = Class("Car") { + Variable(.var, name: "brand", type: "String") + Variable(.var, name: "numberOfWheels", type: "Int") + }.inherits("Vehicle") + + let expected = """ + class Car: Vehicle { + var brand: String + var numberOfWheels: Int + } + """ + + let normalizedGenerated = carClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testEmptyClass() { + let emptyClass = Class("EmptyClass") {} + + let expected = """ + class EmptyClass { + } + """ + + let normalizedGenerated = emptyClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithGenerics() { + let genericClass = Class("Container", generics: ["T"]) { + Variable(.var, name: "value", type: "T") + } + + let expected = """ + class Container { + var value: T + } + """ + + let normalizedGenerated = genericClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithMultipleGenerics() { + let multiGenericClass = Class("Pair", generics: ["T", "U"]) { + Variable(.var, name: "first", type: "T") + Variable(.var, name: "second", type: "U") + } + + let expected = """ + class Pair { + var first: T + var second: U + } + """ + + let normalizedGenerated = multiGenericClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testFinalClass() { + let finalClass = Class("FinalClass") { + Variable(.var, name: "value", type: "String") + }.final() + + let expected = """ + final class FinalClass { + var value: String + } + """ + + let normalizedGenerated = finalClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithMultipleInheritance() { + let classWithMultipleInheritance = Class("AdvancedVehicle") { + Variable(.var, name: "speed", type: "Int") + }.inherits("Vehicle", "Codable", "Equatable") + + let expected = """ + class AdvancedVehicle: Vehicle, Codable, Equatable { + var speed: Int + } + """ + + let normalizedGenerated = classWithMultipleInheritance.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithGenericsAndInheritance() { + let genericClassWithInheritance = Class("GenericContainer", generics: ["T"]) { + Variable(.var, name: "items", type: "[T]") + }.inherits("Collection") + + let expected = """ + class GenericContainer: Collection { + var items: [T] + } + """ + + let normalizedGenerated = genericClassWithInheritance.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testFinalClassWithInheritanceAndGenerics() { + let finalGenericClass = Class("FinalGenericClass", generics: ["T"]) { + Variable(.var, name: "value", type: "T") + }.inherits("BaseClass").final() + + let expected = """ + final class FinalGenericClass: BaseClass { + var value: T + } + """ + + let normalizedGenerated = finalGenericClass.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testClassWithFunctions() { + let classWithFunctions = Class("Calculator") { + Function("add", returns: "Int") { + Parameter(name: "a", type: "Int") + Parameter(name: "b", type: "Int") + } _: { + Return { + VariableExp("a + b") + } + } + } + + let expected = """ + class Calculator { + func add(a: Int, b: Int) -> Int { + return a + b + } + } + """ + + let normalizedGenerated = classWithFunctions.generateCode().normalize() + let normalizedExpected = expected.normalize() + #expect(normalizedGenerated == normalizedExpected) + } +} diff --git a/Tests/SyntaxKitTests/ClassAndProtocolTests.swift b/Tests/SyntaxKitTests/ProtocolTests.swift similarity index 56% rename from Tests/SyntaxKitTests/ClassAndProtocolTests.swift rename to Tests/SyntaxKitTests/ProtocolTests.swift index 34ffe49..31a3c55 100644 --- a/Tests/SyntaxKitTests/ClassAndProtocolTests.swift +++ b/Tests/SyntaxKitTests/ProtocolTests.swift @@ -2,7 +2,7 @@ import Testing @testable import SyntaxKit -struct ClassAndProtocolTests { +struct ProtocolTests { @Test func testSimpleProtocol() { let vehicleProtocol = Protocol("Vehicle") { PropertyRequirement("numberOfWheels", type: "Int", access: .get) @@ -27,24 +27,6 @@ struct ClassAndProtocolTests { #expect(normalizedGenerated == normalizedExpected) } - @Test func testClassWithInheritance() { - let carClass = Class("Car") { - Variable(.var, name: "brand", type: "String") - Variable(.var, name: "numberOfWheels", type: "Int") - }.inherits("Vehicle") - - let expected = """ - class Car: Vehicle { - var brand: String - var numberOfWheels: Int - } - """ - - let normalizedGenerated = carClass.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - @Test func testEmptyProtocol() { let emptyProtocol = Protocol("EmptyProtocol") {} @@ -125,142 +107,6 @@ struct ClassAndProtocolTests { #expect(normalizedGenerated == normalizedExpected) } - @Test func testEmptyClass() { - let emptyClass = Class("EmptyClass") {} - - let expected = """ - class EmptyClass { - } - """ - - let normalizedGenerated = emptyClass.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testClassWithGenerics() { - let genericClass = Class("Container", generics: ["T"]) { - Variable(.var, name: "value", type: "T") - } - - let expected = """ - class Container { - var value: T - } - """ - - let normalizedGenerated = genericClass.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testClassWithMultipleGenerics() { - let multiGenericClass = Class("Pair", generics: ["T", "U"]) { - Variable(.var, name: "first", type: "T") - Variable(.var, name: "second", type: "U") - } - - let expected = """ - class Pair { - var first: T - var second: U - } - """ - - let normalizedGenerated = multiGenericClass.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testFinalClass() { - let finalClass = Class("FinalClass") { - Variable(.var, name: "value", type: "String") - }.final() - - let expected = """ - final class FinalClass { - var value: String - } - """ - - let normalizedGenerated = finalClass.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testClassWithMultipleInheritance() { - let classWithMultipleInheritance = Class("AdvancedVehicle") { - Variable(.var, name: "speed", type: "Int") - }.inherits("Vehicle", "Codable", "Equatable") - - let expected = """ - class AdvancedVehicle: Vehicle, Codable, Equatable { - var speed: Int - } - """ - - let normalizedGenerated = classWithMultipleInheritance.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testClassWithGenericsAndInheritance() { - let genericClassWithInheritance = Class("GenericContainer", generics: ["T"]) { - Variable(.var, name: "items", type: "[T]") - }.inherits("Collection") - - let expected = """ - class GenericContainer: Collection { - var items: [T] - } - """ - - let normalizedGenerated = genericClassWithInheritance.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testFinalClassWithInheritanceAndGenerics() { - let finalGenericClass = Class("FinalGenericClass", generics: ["T"]) { - Variable(.var, name: "value", type: "T") - }.inherits("BaseClass").final() - - let expected = """ - final class FinalGenericClass: BaseClass { - var value: T - } - """ - - let normalizedGenerated = finalGenericClass.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - - @Test func testClassWithFunctions() { - let classWithFunctions = Class("Calculator") { - Function("add", returns: "Int") { - Parameter(name: "a", type: "Int") - Parameter(name: "b", type: "Int") - } _: { - Return { - VariableExp("a + b") - } - } - } - - let expected = """ - class Calculator { - func add(a: Int, b: Int) -> Int { - return a + b - } - } - """ - - let normalizedGenerated = classWithFunctions.generateCode().normalize() - let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } - @Test func testPropertyRequirementGetOnly() { let propertyReq = PropertyRequirement("readOnlyProperty", type: "String", access: .get) let prtcl = Protocol("TestProtocol") {