Skip to content

Commit 56c4436

Browse files
committed
fixing tests
1 parent 272243d commit 56c4436

9 files changed

Lines changed: 53 additions & 189 deletions

Tests/SyntaxKitTests/AssertionMigrationTests.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,9 @@ struct AssertionMigrationTests {
6161
"""
6262

6363
// Test the complete normalization pipeline that was used in XCTest
64-
let normalizedGenerated = blackjackCard.syntax.description
65-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
66-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
67-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
68-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
69-
.trimmingCharacters(in: .whitespacesAndNewlines)
64+
let normalizedGenerated = blackjackCard.syntax.description.normalize()
7065

71-
let normalizedExpected = expected
72-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
73-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
74-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
75-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
76-
.trimmingCharacters(in: .whitespacesAndNewlines)
66+
let normalizedExpected = expected.normalize()
7767

7868
// This replaces: XCTAssertEqual(normalizedGenerated, normalizedExpected)
7969
#expect(normalizedGenerated == normalizedExpected)

Tests/SyntaxKitTests/BasicTests.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,9 @@ struct BasicTests {
2525
"""
2626

2727
// Normalize whitespace, remove comments and modifiers, and normalize colon spacing
28-
let normalizedGenerated = blackjackCard.syntax.description
29-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
30-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
31-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
32-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
33-
.trimmingCharacters(in: .whitespacesAndNewlines)
28+
let normalizedGenerated = blackjackCard.syntax.description.normalize()
3429

35-
let normalizedExpected =
36-
expected
37-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
38-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
39-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
40-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
41-
.trimmingCharacters(in: .whitespacesAndNewlines)
30+
let normalizedExpected = expected.normalize()
4231

4332
#expect(normalizedGenerated == normalizedExpected)
4433
}

Tests/SyntaxKitTests/BlackjackTests.swift

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,9 @@ struct BlackjackTests {
6363
"""
6464

6565
// Normalize whitespace, remove comments and modifiers, and normalize colon spacing
66-
let normalizedGenerated = syntax.syntax.description
67-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
68-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
69-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
70-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
71-
.trimmingCharacters(in: .whitespacesAndNewlines)
72-
73-
let normalizedExpected =
74-
expected
75-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
76-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
77-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
78-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
79-
.trimmingCharacters(in: .whitespacesAndNewlines)
66+
let normalizedGenerated = syntax.syntax.description.normalize()
67+
68+
let normalizedExpected = expected.normalize()
8069

8170
#expect(normalizedGenerated == normalizedExpected)
8271
}
@@ -214,28 +203,9 @@ struct BlackjackTests {
214203
"""
215204

216205
// Normalize whitespace, remove comments and modifiers, and normalize colon spacing
217-
let normalizedGenerated = syntax.syntax.description
218-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
219-
.replacingOccurrences(
220-
of: "public\\s+", with: "", options: .regularExpression
221-
)
222-
.replacingOccurrences(
223-
of: "\\s*:\\s*", with: ": ", options: .regularExpression
224-
)
225-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
226-
.trimmingCharacters(in: .whitespacesAndNewlines)
227-
228-
let normalizedExpected =
229-
expected
230-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
231-
.replacingOccurrences(
232-
of: "public\\s+", with: "", options: .regularExpression
233-
)
234-
.replacingOccurrences(
235-
of: "\\s*:\\s*", with: ": ", options: .regularExpression
236-
)
237-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
238-
.trimmingCharacters(in: .whitespacesAndNewlines)
206+
let normalizedGenerated = syntax.syntax.description.normalize()
207+
208+
let normalizedExpected = expected.normalize()
239209

240210
#expect(normalizedGenerated == normalizedExpected)
241211
}

Tests/SyntaxKitTests/CodeStyleMigrationTests.swift

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,6 @@ import Testing
66
/// Validates the simplified Swift APIs and formatting changes
77
struct CodeStyleMigrationTests {
88

9-
// MARK: - String.CompareOptions Simplification Tests
10-
11-
@Test func testRegularExpressionOptionSimplification() {
12-
// Test that .regularExpression works instead of String.CompareOptions.regularExpression
13-
let testCode = "public func test() { // comment }"
14-
15-
// Old style: String.CompareOptions.regularExpression
16-
// New style: .regularExpression
17-
let withoutComments = testCode.replacingOccurrences(
18-
of: "//.*$", with: "", options: .regularExpression
19-
)
20-
let withoutPublic = withoutComments.replacingOccurrences(
21-
of: "public\\s+", with: "", options: .regularExpression
22-
)
23-
24-
#expect(withoutPublic.trimmingCharacters(in: .whitespacesAndNewlines) == "func test() { }")
25-
}
26-
27-
@Test func testAllStringOptionsSimplifications() {
28-
let testString = "public struct Test: Protocol { // docs }"
29-
30-
// Test the complete pipeline of string replacements used in the migrated tests
31-
let normalized = testString
32-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
33-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
34-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
35-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
36-
.trimmingCharacters(in: .whitespacesAndNewlines)
37-
38-
#expect(normalized == "struct Test: Protocol { }")
39-
}
409

4110
// MARK: - CharacterSet Simplification Tests
4211

@@ -69,13 +38,10 @@ struct CodeStyleMigrationTests {
6938
}
7039
}
7140

72-
let generated = syntax.generateCode()
41+
let generated = syntax.generateCode().normalize()
7342

7443
// Verify proper indentation is maintained
75-
#expect(generated.contains("struct IndentationTest"))
76-
#expect(generated.contains(" let property1: String"))
77-
#expect(generated.contains(" let property2: Int"))
78-
#expect(generated.contains(" func method(param: String)"))
44+
#expect(generated == "struct IndentationTest { let property1: String let property2: Int func method(param: String) { let local = \"value\" return local } }")
7945
}
8046

8147
// MARK: - Multiline String Formatting Tests
@@ -93,13 +59,9 @@ struct CodeStyleMigrationTests {
9359
Variable(.var, name: "count", type: "Int")
9460
}
9561

96-
let normalized = syntax.generateCode()
97-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
98-
.trimmingCharacters(in: .whitespacesAndNewlines)
62+
let normalized = syntax.generateCode().normalize()
9963

100-
let expectedNormalized = expected
101-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
102-
.trimmingCharacters(in: .whitespacesAndNewlines)
64+
let expectedNormalized = expected.normalize()
10365

10466
#expect(normalized == expectedNormalized)
10567
}

Tests/SyntaxKitTests/FrameworkCompatibilityTests.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ struct FrameworkCompatibilityTests {
6969
}
7070

7171
let generated = syntax.syntax.description
72-
let normalized = generated
73-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
74-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
75-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
76-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
77-
.trimmingCharacters(in: .whitespacesAndNewlines)
72+
let normalized = generated.normalize()
7873

7974
// Validate all components are present
8075
#expect(normalized.contains("struct BlackjackCard"))
@@ -98,8 +93,7 @@ struct FrameworkCompatibilityTests {
9893

9994
let generated = function.syntax.description
10095
let normalized = generated
101-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
102-
.trimmingCharacters(in: .whitespacesAndNewlines)
96+
.normalize()
10397

10498
#expect(normalized.contains("func calculateValue(multiplier: Int, base: Int = 10) -> Int"))
10599
#expect(normalized.contains("return multiplier * base"))
@@ -122,7 +116,7 @@ struct FrameworkCompatibilityTests {
122116

123117
#expect(!generated.isEmpty)
124118
#expect(generated.contains("struct DocumentedStruct"))
125-
#expect(generated.contains("let value: String"))
119+
#expect(generated.normalize().contains("let value: String".normalize()))
126120
}
127121

128122
// MARK: - Migration Regression Tests
@@ -134,11 +128,11 @@ struct FrameworkCompatibilityTests {
134128
Variable(.var, name: "y", type: "Double", equals: "0.0")
135129
}
136130

137-
let generated = simpleStruct.generateCode()
131+
let generated = simpleStruct.generateCode().normalize()
138132

139133
#expect(generated.contains("struct Point"))
140-
#expect(generated.contains("var x: Double = 0.0"))
141-
#expect(generated.contains("var y: Double = 0.0"))
134+
#expect(generated.contains("var x: Double = 0.0".normalize()))
135+
#expect(generated.contains("var y: Double = 0.0".normalize()))
142136
}
143137

144138
@Test func testLiteralGeneration() {

Tests/SyntaxKitTests/FunctionTests.swift

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,9 @@ struct FunctionTests {
2020
"""
2121

2222
// Normalize whitespace, remove comments and modifiers, and normalize colon spacing
23-
let normalizedGenerated = function.syntax.description
24-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
25-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
26-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
27-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
28-
.trimmingCharacters(in: .whitespacesAndNewlines)
29-
30-
let normalizedExpected =
31-
expected
32-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
33-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
34-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
35-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
36-
.trimmingCharacters(in: .whitespacesAndNewlines)
23+
let normalizedGenerated = function.syntax.description.normalize()
24+
25+
let normalizedExpected = expected.normalize()
3726

3827
#expect(normalizedGenerated == normalizedExpected)
3928
}
@@ -59,20 +48,9 @@ struct FunctionTests {
5948
"""
6049

6150
// Normalize whitespace, remove comments and modifiers, and normalize colon spacing
62-
let normalizedGenerated = function.syntax.description
63-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
64-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
65-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
66-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
67-
.trimmingCharacters(in: .whitespacesAndNewlines)
68-
69-
let normalizedExpected =
70-
expected
71-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
72-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
73-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
74-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
75-
.trimmingCharacters(in: .whitespacesAndNewlines)
51+
let normalizedGenerated = function.syntax.description.normalize()
52+
53+
let normalizedExpected = expected.normalize()
7654

7755
#expect(normalizedGenerated == normalizedExpected)
7856
}
@@ -94,20 +72,9 @@ struct FunctionTests {
9472
"""
9573

9674
// Normalize whitespace, remove comments and modifiers, and normalize colon spacing
97-
let normalizedGenerated = function.syntax.description
98-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
99-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
100-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
101-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
102-
.trimmingCharacters(in: .whitespacesAndNewlines)
103-
104-
let normalizedExpected =
105-
expected
106-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
107-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
108-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
109-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
110-
.trimmingCharacters(in: .whitespacesAndNewlines)
75+
let normalizedGenerated = function.syntax.description.normalize()
76+
77+
let normalizedExpected = expected.normalize()
11178

11279
#expect(normalizedGenerated == normalizedExpected)
11380
}

Tests/SyntaxKitTests/MigrationTests.swift

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,9 @@ struct MigrationTests {
7070
"""
7171

7272
// Use the same normalization approach as existing tests
73-
let normalizedGenerated = blackjackCard.syntax.description
74-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
75-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
76-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
77-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
78-
.trimmingCharacters(in: .whitespacesAndNewlines)
73+
let normalizedGenerated = blackjackCard.syntax.description.normalize()
7974

80-
let normalizedExpected = expected
81-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
82-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression)
83-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
84-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
85-
.trimmingCharacters(in: .whitespacesAndNewlines)
75+
let normalizedExpected = expected.normalize()
8676

8777
#expect(normalizedGenerated == normalizedExpected)
8878
}
@@ -119,13 +109,13 @@ struct MigrationTests {
119109
}
120110
}
121111

122-
let generated = syntax.syntax.description
112+
let generated = syntax.syntax.description.normalize()
123113

124114
// Verify generated code contains expected elements
125-
#expect(generated.contains("struct TestCard"))
126-
#expect(generated.contains("let rank: String"))
127-
#expect(generated.contains("let suit: String"))
128-
#expect(generated.contains("func description() -> String"))
115+
#expect(generated.contains("struct TestCard".normalize()))
116+
#expect(generated.contains("let rank: String".normalize()))
117+
#expect(generated.contains("let suit: String".normalize()))
118+
#expect(generated.contains("func description() -> String".normalize()))
129119
}
130120

131121
@Test func testMigrationBackwardCompatibility() {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
extension String {
4+
func normalize() -> String {
5+
self
6+
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression)
7+
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression)
8+
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
9+
.trimmingCharacters(in: .whitespacesAndNewlines)
10+
}
11+
}

Tests/SyntaxKitTests/StructTests.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@ import Testing
33
@testable import SyntaxKit
44

55
struct StructTests {
6-
func normalize(_ code: String) -> String {
7-
code
8-
.replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments
9-
.replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier
10-
.replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing
11-
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace
12-
.trimmingCharacters(in: .whitespacesAndNewlines)
13-
}
14-
156
@Test func testGenericStruct() {
167
let stackStruct = Struct("Stack", generic: "Element") {
178
Variable(.var, name: "items", type: "[Element]", equals: "[]")
@@ -67,8 +58,8 @@ struct StructTests {
6758
}
6859
"""
6960

70-
let normalizedGenerated = normalize(stackStruct.generateCode())
71-
let normalizedExpected = normalize(expectedCode)
61+
let normalizedGenerated = stackStruct.generateCode().normalize()
62+
let normalizedExpected = expectedCode.normalize()
7263
#expect(normalizedGenerated == normalizedExpected)
7364
}
7465

@@ -83,8 +74,8 @@ struct StructTests {
8374
}
8475
"""
8576

86-
let normalizedGenerated = normalize(containerStruct.generateCode())
87-
let normalizedExpected = normalize(expectedCode)
77+
let normalizedGenerated = containerStruct.generateCode().normalize()
78+
let normalizedExpected = expectedCode.normalize()
8879
#expect(normalizedGenerated == normalizedExpected)
8980
}
9081

@@ -101,8 +92,8 @@ struct StructTests {
10192
}
10293
"""
10394

104-
let normalizedGenerated = normalize(simpleStruct.generateCode())
105-
let normalizedExpected = normalize(expectedCode)
95+
let normalizedGenerated = simpleStruct.generateCode().normalize()
96+
let normalizedExpected = expectedCode.normalize()
10697
#expect(normalizedGenerated == normalizedExpected)
10798
}
10899
}

0 commit comments

Comments
 (0)