Skip to content

Commit 209bffb

Browse files
author
codecov-ai[bot]
authored
Add Tests for PR#69 (#71)
1 parent 51dcb41 commit 209bffb

File tree

3 files changed

+348
-47
lines changed

3 files changed

+348
-47
lines changed

Tests/SyntaxKitTests/ClassAndProtocolTests.swift

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import Testing
2+
3+
@testable import SyntaxKit
4+
5+
struct ClassTests {
6+
@Test func testClassWithInheritance() {
7+
let carClass = Class("Car") {
8+
Variable(.var, name: "brand", type: "String")
9+
Variable(.var, name: "numberOfWheels", type: "Int")
10+
}.inherits("Vehicle")
11+
12+
let expected = """
13+
class Car: Vehicle {
14+
var brand: String
15+
var numberOfWheels: Int
16+
}
17+
"""
18+
19+
let normalizedGenerated = carClass.generateCode().normalize()
20+
let normalizedExpected = expected.normalize()
21+
#expect(normalizedGenerated == normalizedExpected)
22+
}
23+
24+
@Test func testEmptyClass() {
25+
let emptyClass = Class("EmptyClass") {}
26+
27+
let expected = """
28+
class EmptyClass {
29+
}
30+
"""
31+
32+
let normalizedGenerated = emptyClass.generateCode().normalize()
33+
let normalizedExpected = expected.normalize()
34+
#expect(normalizedGenerated == normalizedExpected)
35+
}
36+
37+
@Test func testClassWithGenerics() {
38+
let genericClass = Class("Container", generics: ["T"]) {
39+
Variable(.var, name: "value", type: "T")
40+
}
41+
42+
let expected = """
43+
class Container<T> {
44+
var value: T
45+
}
46+
"""
47+
48+
let normalizedGenerated = genericClass.generateCode().normalize()
49+
let normalizedExpected = expected.normalize()
50+
#expect(normalizedGenerated == normalizedExpected)
51+
}
52+
53+
@Test func testClassWithMultipleGenerics() {
54+
let multiGenericClass = Class("Pair", generics: ["T", "U"]) {
55+
Variable(.var, name: "first", type: "T")
56+
Variable(.var, name: "second", type: "U")
57+
}
58+
59+
let expected = """
60+
class Pair<T, U> {
61+
var first: T
62+
var second: U
63+
}
64+
"""
65+
66+
let normalizedGenerated = multiGenericClass.generateCode().normalize()
67+
let normalizedExpected = expected.normalize()
68+
#expect(normalizedGenerated == normalizedExpected)
69+
}
70+
71+
@Test func testFinalClass() {
72+
let finalClass = Class("FinalClass") {
73+
Variable(.var, name: "value", type: "String")
74+
}.final()
75+
76+
let expected = """
77+
final class FinalClass {
78+
var value: String
79+
}
80+
"""
81+
82+
let normalizedGenerated = finalClass.generateCode().normalize()
83+
let normalizedExpected = expected.normalize()
84+
#expect(normalizedGenerated == normalizedExpected)
85+
}
86+
87+
@Test func testClassWithMultipleInheritance() {
88+
let classWithMultipleInheritance = Class("AdvancedVehicle") {
89+
Variable(.var, name: "speed", type: "Int")
90+
}.inherits("Vehicle", "Codable", "Equatable")
91+
92+
let expected = """
93+
class AdvancedVehicle: Vehicle, Codable, Equatable {
94+
var speed: Int
95+
}
96+
"""
97+
98+
let normalizedGenerated = classWithMultipleInheritance.generateCode().normalize()
99+
let normalizedExpected = expected.normalize()
100+
#expect(normalizedGenerated == normalizedExpected)
101+
}
102+
103+
@Test func testClassWithGenericsAndInheritance() {
104+
let genericClassWithInheritance = Class("GenericContainer", generics: ["T"]) {
105+
Variable(.var, name: "items", type: "[T]")
106+
}.inherits("Collection")
107+
108+
let expected = """
109+
class GenericContainer<T>: Collection {
110+
var items: [T]
111+
}
112+
"""
113+
114+
let normalizedGenerated = genericClassWithInheritance.generateCode().normalize()
115+
let normalizedExpected = expected.normalize()
116+
#expect(normalizedGenerated == normalizedExpected)
117+
}
118+
119+
@Test func testFinalClassWithInheritanceAndGenerics() {
120+
let finalGenericClass = Class("FinalGenericClass", generics: ["T"]) {
121+
Variable(.var, name: "value", type: "T")
122+
}.inherits("BaseClass").final()
123+
124+
let expected = """
125+
final class FinalGenericClass<T>: BaseClass {
126+
var value: T
127+
}
128+
"""
129+
130+
let normalizedGenerated = finalGenericClass.generateCode().normalize()
131+
let normalizedExpected = expected.normalize()
132+
#expect(normalizedGenerated == normalizedExpected)
133+
}
134+
135+
@Test func testClassWithFunctions() {
136+
let classWithFunctions = Class("Calculator") {
137+
Function("add", returns: "Int") {
138+
Parameter(name: "a", type: "Int")
139+
Parameter(name: "b", type: "Int")
140+
} _: {
141+
Return {
142+
VariableExp("a + b")
143+
}
144+
}
145+
}
146+
147+
let expected = """
148+
class Calculator {
149+
func add(a: Int, b: Int) -> Int {
150+
return a + b
151+
}
152+
}
153+
"""
154+
155+
let normalizedGenerated = classWithFunctions.generateCode().normalize()
156+
let normalizedExpected = expected.normalize()
157+
#expect(normalizedGenerated == normalizedExpected)
158+
}
159+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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

Comments
 (0)