Skip to content

Commit 272243d

Browse files
author
codecov-ai[bot]
authored
Add Tests for PR#16
1 parent 04dc929 commit 272243d

4 files changed

Lines changed: 503 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import Testing
2+
3+
@testable import SyntaxKit
4+
5+
/// Tests specifically focused on assertion migration from XCTest to Swift Testing
6+
/// Ensures all assertion patterns from the original tests work correctly with #expect()
7+
struct AssertionMigrationTests {
8+
9+
// MARK: - XCTAssertEqual Migration Tests
10+
11+
@Test func testEqualityAssertionMigration() throws {
12+
// Test the most common migration: XCTAssertEqual -> #expect(a == b)
13+
let function = Function("test", returns: "String") {
14+
Return {
15+
Literal.string("hello")
16+
}
17+
}
18+
19+
let generated = function.syntax.description
20+
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
21+
.trimmingCharacters(in: .whitespacesAndNewlines)
22+
23+
let expected = "func test() -> String { return \"hello\" }"
24+
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
25+
.trimmingCharacters(in: .whitespacesAndNewlines)
26+
27+
// This replaces: XCTAssertEqual(generated, expected)
28+
#expect(generated == expected)
29+
}
30+
31+
// MARK: - XCTAssertFalse Migration Tests
32+
33+
@Test func testFalseAssertionMigration() {
34+
let syntax = Group {
35+
Variable(.let, name: "test", type: "String", equals: "\"value\"")
36+
}
37+
38+
let generated = syntax.generateCode().trimmingCharacters(in: .whitespacesAndNewlines)
39+
40+
// This replaces: XCTAssertFalse(generated.isEmpty)
41+
#expect(!generated.isEmpty)
42+
}
43+
44+
// MARK: - Complex Assertion Migration Tests
45+
46+
@Test func testNormalizedStringComparisonMigration() throws {
47+
let blackjackCard = Struct("Card") {
48+
Enum("Suit") {
49+
EnumCase("hearts").equals("")
50+
EnumCase("spades").equals("")
51+
}.inherits("Character")
52+
}
53+
54+
let expected = """
55+
struct Card {
56+
enum Suit: Character {
57+
case hearts = ""
58+
case spades = ""
59+
}
60+
}
61+
"""
62+
63+
// 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)
70+
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)
77+
78+
// This replaces: XCTAssertEqual(normalizedGenerated, normalizedExpected)
79+
#expect(normalizedGenerated == normalizedExpected)
80+
}
81+
82+
@Test func testMultipleAssertionsInSingleTest() {
83+
let generated = "struct Test { var value: Int }"
84+
85+
// Test multiple assertions in one test method
86+
#expect(!generated.isEmpty)
87+
#expect(generated.contains("struct Test"))
88+
#expect(generated.contains("var value: Int"))
89+
}
90+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import Testing
2+
3+
@testable import SyntaxKit
4+
5+
/// Tests for code style and API simplification changes introduced during Swift Testing migration
6+
/// Validates the simplified Swift APIs and formatting changes
7+
struct CodeStyleMigrationTests {
8+
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+
}
40+
41+
// MARK: - CharacterSet Simplification Tests
42+
43+
@Test func testCharacterSetSimplification() {
44+
// Test that .whitespacesAndNewlines works instead of CharacterSet.whitespacesAndNewlines
45+
let testString = "\n test content \n\t"
46+
47+
// Old style: CharacterSet.whitespacesAndNewlines
48+
// New style: .whitespacesAndNewlines
49+
let trimmed = testString.trimmingCharacters(in: .whitespacesAndNewlines)
50+
51+
#expect(trimmed == "test content")
52+
}
53+
54+
// MARK: - Indentation and Formatting Tests
55+
56+
@Test func testConsistentIndentationInMigratedCode() throws {
57+
// Test that the indentation changes in the migrated code work correctly
58+
let syntax = Struct("IndentationTest") {
59+
Variable(.let, name: "property1", type: "String")
60+
Variable(.let, name: "property2", type: "Int")
61+
62+
Function("method") {
63+
Parameter(name: "param", type: "String")
64+
} _: {
65+
VariableDecl(.let, name: "local", equals: "\"value\"")
66+
Return {
67+
VariableExp("local")
68+
}
69+
}
70+
}
71+
72+
let generated = syntax.generateCode()
73+
74+
// 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)"))
79+
}
80+
81+
// MARK: - Multiline String Formatting Tests
82+
83+
@Test func testMultilineStringFormatting() {
84+
let expected = """
85+
struct TestStruct {
86+
let value: String
87+
var count: Int
88+
}
89+
"""
90+
91+
let syntax = Struct("TestStruct") {
92+
Variable(.let, name: "value", type: "String")
93+
Variable(.var, name: "count", type: "Int")
94+
}
95+
96+
let normalized = syntax.generateCode()
97+
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
98+
.trimmingCharacters(in: .whitespacesAndNewlines)
99+
100+
let expectedNormalized = expected
101+
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
102+
.trimmingCharacters(in: .whitespacesAndNewlines)
103+
104+
#expect(normalized == expectedNormalized)
105+
}
106+
107+
@Test func testMigrationPreservesCodeGeneration() {
108+
// Ensure that the style changes don't break core functionality
109+
let group = Group {
110+
Return {
111+
Literal.string("migrated")
112+
}
113+
}
114+
115+
let generated = group.generateCode().trimmingCharacters(in: .whitespacesAndNewlines)
116+
#expect(generated == "return \"migrated\"")
117+
}
118+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import Testing
2+
3+
@testable import SyntaxKit
4+
5+
/// Tests to ensure compatibility and feature parity between XCTest and Swift Testing
6+
/// Validates that the migration maintains all testing capabilities
7+
struct FrameworkCompatibilityTests {
8+
9+
// MARK: - Test Organization Migration Tests
10+
11+
@Test func testStructBasedOrganization() {
12+
// Test that struct-based test organization works
13+
// This replaces: final class TestClass: XCTestCase
14+
let testExecuted = true
15+
#expect(testExecuted)
16+
}
17+
18+
@Test func testMethodAnnotationMigration() throws {
19+
// Test that @Test annotation works with throws
20+
// This replaces: func testMethod() throws
21+
let syntax = Enum("TestEnum") {
22+
EnumCase("first")
23+
EnumCase("second")
24+
}
25+
26+
let generated = syntax.syntax.description
27+
#expect(!generated.isEmpty)
28+
#expect(generated.contains("enum TestEnum"))
29+
}
30+
31+
// MARK: - Error Handling Compatibility Tests
32+
33+
@Test func testThrowingTestCompatibility() throws {
34+
// Ensure throws declaration works properly with @Test
35+
let function = Function("throwingFunction", returns: "String") {
36+
Parameter(name: "input", type: "String")
37+
} _: {
38+
Return {
39+
VariableExp("input.uppercased()")
40+
}
41+
}
42+
43+
let generated = try function.syntax.description
44+
#expect(generated.contains("func throwingFunction"))
45+
}
46+
47+
// MARK: - Complex DSL Compatibility Tests
48+
49+
@Test func testFullBlackjackCompatibility() throws {
50+
// Test complex DSL patterns work with new framework
51+
let syntax = Struct("BlackjackCard") {
52+
Enum("Suit") {
53+
EnumCase("spades").equals("")
54+
EnumCase("hearts").equals("")
55+
EnumCase("diamonds").equals("")
56+
EnumCase("clubs").equals("")
57+
}.inherits("Character")
58+
59+
Enum("Rank") {
60+
EnumCase("ace").equals(1)
61+
EnumCase("two").equals(2)
62+
EnumCase("jack").equals(11)
63+
EnumCase("queen").equals(12)
64+
EnumCase("king").equals(13)
65+
}.inherits("Int")
66+
67+
Variable(.let, name: "rank", type: "Rank")
68+
Variable(.let, name: "suit", type: "Suit")
69+
}
70+
71+
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)
78+
79+
// Validate all components are present
80+
#expect(normalized.contains("struct BlackjackCard"))
81+
#expect(normalized.contains("enum Suit: Character"))
82+
#expect(normalized.contains("enum Rank: Int"))
83+
#expect(normalized.contains("let rank: Rank"))
84+
#expect(normalized.contains("let suit: Suit"))
85+
}
86+
87+
// MARK: - Function Generation Compatibility Tests
88+
89+
@Test func testFunctionGenerationCompatibility() throws {
90+
let function = Function("calculateValue", returns: "Int") {
91+
Parameter(name: "multiplier", type: "Int")
92+
Parameter(name: "base", type: "Int", defaultValue: "10")
93+
} _: {
94+
Return {
95+
VariableExp("multiplier * base")
96+
}
97+
}
98+
99+
let generated = function.syntax.description
100+
let normalized = generated
101+
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
102+
.trimmingCharacters(in: .whitespacesAndNewlines)
103+
104+
#expect(normalized.contains("func calculateValue(multiplier: Int, base: Int = 10) -> Int"))
105+
#expect(normalized.contains("return multiplier * base"))
106+
}
107+
108+
// MARK: - Comment Injection Compatibility Tests
109+
110+
@Test func testCommentInjectionCompatibility() {
111+
let syntax = Struct("DocumentedStruct") {
112+
Variable(.let, name: "value", type: "String")
113+
.comment {
114+
Line(.doc, "The main value of the struct")
115+
}
116+
}.comment {
117+
Line("MARK: - Data Models")
118+
Line(.doc, "A documented struct for testing")
119+
}
120+
121+
let generated = syntax.generateCode()
122+
123+
#expect(!generated.isEmpty)
124+
#expect(generated.contains("struct DocumentedStruct"))
125+
#expect(generated.contains("let value: String"))
126+
}
127+
128+
// MARK: - Migration Regression Tests
129+
130+
@Test func testNoRegressionInCodeGeneration() {
131+
// Ensure migration doesn't introduce regressions
132+
let simpleStruct = Struct("Point") {
133+
Variable(.var, name: "x", type: "Double", equals: "0.0")
134+
Variable(.var, name: "y", type: "Double", equals: "0.0")
135+
}
136+
137+
let generated = simpleStruct.generateCode()
138+
139+
#expect(generated.contains("struct Point"))
140+
#expect(generated.contains("var x: Double = 0.0"))
141+
#expect(generated.contains("var y: Double = 0.0"))
142+
}
143+
144+
@Test func testLiteralGeneration() {
145+
let group = Group {
146+
Return {
147+
Literal.integer(100)
148+
}
149+
}
150+
151+
let generated = group.generateCode().trimmingCharacters(in: .whitespacesAndNewlines)
152+
#expect(generated == "return 100")
153+
}
154+
}

0 commit comments

Comments
 (0)