From 04dc9299efa942f86c939c85f7dd9fcebd8fa559 Mon Sep 17 00:00:00 2001 From: leogdion Date: Tue, 17 Jun 2025 08:58:59 -0400 Subject: [PATCH 1/2] Migrated from XCTest to Swift Testing --- Tests/SyntaxKitTests/BasicTests.swift | 72 ++--- Tests/SyntaxKitTests/BlackjackTests.swift | 305 +++++++++++----------- Tests/SyntaxKitTests/CommentTests.swift | 17 +- Tests/SyntaxKitTests/FunctionTests.swift | 16 +- Tests/SyntaxKitTests/LiteralTests.swift | 20 +- Tests/SyntaxKitTests/StructTests.swift | 16 +- 6 files changed, 219 insertions(+), 227 deletions(-) diff --git a/Tests/SyntaxKitTests/BasicTests.swift b/Tests/SyntaxKitTests/BasicTests.swift index c8bb006..f002b5d 100644 --- a/Tests/SyntaxKitTests/BasicTests.swift +++ b/Tests/SyntaxKitTests/BasicTests.swift @@ -1,45 +1,45 @@ -import XCTest +import Testing @testable import SyntaxKit -final class BasicTests: XCTestCase { - func testBlackjackCardExample() throws { - let blackjackCard = Struct("BlackjackCard") { - Enum("Suit") { - EnumCase("spades").equals("♠") - EnumCase("hearts").equals("♡") - EnumCase("diamonds").equals("♢") - EnumCase("clubs").equals("♣") - }.inherits("Character") - } +struct BasicTests { + @Test func testBlackjackCardExample() throws { + let blackjackCard = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + }.inherits("Character") + } - let expected = """ - struct BlackjackCard { - enum Suit: Character { - case spades = "♠" - case hearts = "♡" - case diamonds = "♢" - case clubs = "♣" + let expected = """ + struct BlackjackCard { + enum Suit: Character { + case spades = "♠" + case hearts = "♡" + case diamonds = "♢" + case clubs = "♣" + } } - } - """ + """ - // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = blackjackCard.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) + // Normalize whitespace, remove comments and modifiers, and normalize colon spacing + let normalizedGenerated = blackjackCard.syntax.description + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) + .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) + let normalizedExpected = + expected + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) + .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) - XCTAssertEqual(normalizedGenerated, normalizedExpected) - } + #expect(normalizedGenerated == normalizedExpected) + } } diff --git a/Tests/SyntaxKitTests/BlackjackTests.swift b/Tests/SyntaxKitTests/BlackjackTests.swift index 6e5eee1..511f3cc 100644 --- a/Tests/SyntaxKitTests/BlackjackTests.swift +++ b/Tests/SyntaxKitTests/BlackjackTests.swift @@ -1,40 +1,40 @@ -import XCTest +import Testing @testable import SyntaxKit -final class BlackjackTests: XCTestCase { - func testBlackjackCardExample() throws { - let syntax = Struct("BlackjackCard") { - Enum("Suit") { - EnumCase("spades").equals("♠") - EnumCase("hearts").equals("♡") - EnumCase("diamonds").equals("♢") - EnumCase("clubs").equals("♣") - } - .inherits("Character") - - Enum("Rank") { - EnumCase("two").equals(2) - EnumCase("three") - EnumCase("four") - EnumCase("five") - EnumCase("six") - EnumCase("seven") - EnumCase("eight") - EnumCase("nine") - EnumCase("ten") - EnumCase("jack") - EnumCase("queen") - EnumCase("king") - EnumCase("ace") - } - .inherits("Int") +struct BlackjackTests { + @Test func testBlackjackCardExample() throws { + let syntax = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + } + .inherits("Character") + + Enum("Rank") { + EnumCase("two").equals(2) + EnumCase("three") + EnumCase("four") + EnumCase("five") + EnumCase("six") + EnumCase("seven") + EnumCase("eight") + EnumCase("nine") + EnumCase("ten") + EnumCase("jack") + EnumCase("queen") + EnumCase("king") + EnumCase("ace") + } + .inherits("Int") - Variable(.let, name: "rank", type: "Rank") - Variable(.let, name: "suit", type: "Suit") - } + Variable(.let, name: "rank", type: "Rank") + Variable(.let, name: "suit", type: "Suit") + } - let expected = """ + let expected = """ struct BlackjackCard { enum Suit: Character { case spades = "♠" @@ -62,111 +62,104 @@ final class BlackjackTests: XCTestCase { } """ - // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = syntax.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: String.CompareOptions.regularExpression) - .replacingOccurrences( - of: "public\\s+", with: "", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences( - of: "\\s*:\\s*", with: ": ", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences(of: "\\s+", with: " ", options: String.CompareOptions.regularExpression) - .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: String.CompareOptions.regularExpression) - .replacingOccurrences( - of: "public\\s+", with: "", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences( - of: "\\s*:\\s*", with: ": ", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences(of: "\\s+", with: " ", options: String.CompareOptions.regularExpression) - .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) - - XCTAssertEqual(normalizedGenerated, normalizedExpected) - } - - // swiftlint:disable:next function_body_length - func testFullBlackjackCardExample() throws { - // swiftlint:disable:next closure_body_length - let syntax = Struct("BlackjackCard") { - Enum("Suit") { - EnumCase("spades").equals("♠") - EnumCase("hearts").equals("♡") - EnumCase("diamonds").equals("♢") - EnumCase("clubs").equals("♣") - } - .inherits("Character") - - Enum("Rank") { - EnumCase("two").equals(2) - EnumCase("three") - EnumCase("four") - EnumCase("five") - EnumCase("six") - EnumCase("seven") - EnumCase("eight") - EnumCase("nine") - EnumCase("ten") - EnumCase("jack") - EnumCase("queen") - EnumCase("king") - EnumCase("ace") - Struct("Values") { - Variable(.let, name: "first", type: "Int") - Variable(.let, name: "second", type: "Int?") - } - ComputedProperty("values", type: "Values") { - Switch("self") { - SwitchCase(".ace") { - Return { - Init("Values") { - Parameter(name: "first", type: "", defaultValue: "1") - Parameter(name: "second", type: "", defaultValue: "11") - } - } + // Normalize whitespace, remove comments and modifiers, and normalize colon spacing + let normalizedGenerated = syntax.syntax.description + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) + .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + + let normalizedExpected = + expected + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) + .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + + #expect(normalizedGenerated == normalizedExpected) + } + + // swiftlint:disable:next function_body_length + @Test func testFullBlackjackCardExample() throws { + // swiftlint:disable:next closure_body_length + let syntax = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") } - SwitchCase(".jack", ".queen", ".king") { - Return { - Init("Values") { - Parameter(name: "first", type: "", defaultValue: "10") - Parameter(name: "second", type: "", defaultValue: "nil") + .inherits("Character") + + Enum("Rank") { + EnumCase("two").equals(2) + EnumCase("three") + EnumCase("four") + EnumCase("five") + EnumCase("six") + EnumCase("seven") + EnumCase("eight") + EnumCase("nine") + EnumCase("ten") + EnumCase("jack") + EnumCase("queen") + EnumCase("king") + EnumCase("ace") + Struct("Values") { + Variable(.let, name: "first", type: "Int") + Variable(.let, name: "second", type: "Int?") + } + ComputedProperty("values", type: "Values") { + Switch("self") { + SwitchCase(".ace") { + Return { + Init("Values") { + Parameter(name: "first", type: "", defaultValue: "1") + Parameter(name: "second", type: "", defaultValue: "11") + } + } + } + SwitchCase(".jack", ".queen", ".king") { + Return { + Init("Values") { + Parameter(name: "first", type: "", defaultValue: "10") + Parameter(name: "second", type: "", defaultValue: "nil") + } + } + } + Default { + Return { + Init("Values") { + Parameter(name: "first", type: "", defaultValue: "self.rawValue") + Parameter(name: "second", type: "", defaultValue: "nil") + } + } + } + } } - } } - Default { - Return { - Init("Values") { - Parameter(name: "first", type: "", defaultValue: "self.rawValue") - Parameter(name: "second", type: "", defaultValue: "nil") + .inherits("Int") + + Variable(.let, name: "rank", type: "Rank") + Variable(.let, name: "suit", type: "Suit") + ComputedProperty("description", type: "String") { + VariableDecl(.var, name: "output", equals: "\"suit is \\(suit.rawValue),\"") + PlusAssign("output", "\" value is \\(rank.values.first)\"") + If( + Let("second", "rank.values.second"), + then: { + PlusAssign("output", "\" or \\(second)\"") + } + ) + Return { + VariableExp("output") } - } } - } } - } - .inherits("Int") - - Variable(.let, name: "rank", type: "Rank") - Variable(.let, name: "suit", type: "Suit") - ComputedProperty("description", type: "String") { - VariableDecl(.var, name: "output", equals: "\"suit is \\(suit.rawValue),\"") - PlusAssign("output", "\" value is \\(rank.values.first)\"") - If( - Let("second", "rank.values.second"), - then: { - PlusAssign("output", "\" or \\(second)\"") - }) - Return { - VariableExp("output") - } - } - } - let expected = """ + let expected = """ struct BlackjackCard { enum Suit: Character { case spades = \"♠\" @@ -220,30 +213,30 @@ final class BlackjackTests: XCTestCase { } """ - // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = syntax.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: String.CompareOptions.regularExpression) - .replacingOccurrences( - of: "public\\s+", with: "", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences( - of: "\\s*:\\s*", with: ": ", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences(of: "\\s+", with: " ", options: String.CompareOptions.regularExpression) - .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: String.CompareOptions.regularExpression) - .replacingOccurrences( - of: "public\\s+", with: "", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences( - of: "\\s*:\\s*", with: ": ", options: String.CompareOptions.regularExpression - ) - .replacingOccurrences(of: "\\s+", with: " ", options: String.CompareOptions.regularExpression) - .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) - - XCTAssertEqual(normalizedGenerated, normalizedExpected) - } + // Normalize whitespace, remove comments and modifiers, and normalize colon spacing + let normalizedGenerated = syntax.syntax.description + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences( + of: "public\\s+", with: "", options: .regularExpression + ) + .replacingOccurrences( + of: "\\s*:\\s*", with: ": ", options: .regularExpression + ) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + + let normalizedExpected = + expected + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences( + of: "public\\s+", with: "", options: .regularExpression + ) + .replacingOccurrences( + of: "\\s*:\\s*", with: ": ", options: .regularExpression + ) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + + #expect(normalizedGenerated == normalizedExpected) + } } diff --git a/Tests/SyntaxKitTests/CommentTests.swift b/Tests/SyntaxKitTests/CommentTests.swift index 488c583..331852e 100644 --- a/Tests/SyntaxKitTests/CommentTests.swift +++ b/Tests/SyntaxKitTests/CommentTests.swift @@ -1,10 +1,10 @@ -import XCTest +import Testing @testable import SyntaxKit -final class CommentTests: XCTestCase { +struct CommentTests { // swiftlint:disable:next function_body_length - func testCommentInjection() { + @Test func testCommentInjection() { // swiftlint:disable:next closure_body_length let syntax = Group { Struct("Card") { @@ -100,14 +100,13 @@ final class CommentTests: XCTestCase { } let generated = syntax.generateCode().trimmingCharacters(in: .whitespacesAndNewlines) - print("Generated:\n", generated) - XCTAssertFalse(generated.isEmpty) + #expect(!generated.isEmpty) // - // XCTAssertTrue(generated.contains("MARK: - Models"), "MARK line should be present in generated code") - // XCTAssertTrue(generated.contains("Foo struct docs"), "Doc comment line should be present in generated code") + // #expect(generated.contains("MARK: - Models"), "MARK line should be present in generated code") + // #expect(generated.contains("Foo struct docs"), "Doc comment line should be present in generated code") // // Ensure the struct declaration itself is still correct - // XCTAssertTrue(generated.contains("struct Foo")) - // XCTAssertTrue(generated.contains("bar"), "Variable declaration should be present") + // #expect(generated.contains("struct Foo")) + // #expect(generated.contains("bar"), "Variable declaration should be present") } } diff --git a/Tests/SyntaxKitTests/FunctionTests.swift b/Tests/SyntaxKitTests/FunctionTests.swift index 0fee02a..b2d5312 100644 --- a/Tests/SyntaxKitTests/FunctionTests.swift +++ b/Tests/SyntaxKitTests/FunctionTests.swift @@ -1,9 +1,9 @@ -import XCTest +import Testing @testable import SyntaxKit -final class FunctionTests: XCTestCase { - func testBasicFunction() throws { +struct FunctionTests { + @Test func testBasicFunction() throws { let function = Function("calculateSum", returns: "Int") { Parameter(name: "a", type: "Int") Parameter(name: "b", type: "Int") @@ -35,10 +35,10 @@ final class FunctionTests: XCTestCase { .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace .trimmingCharacters(in: .whitespacesAndNewlines) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } - func testStaticFunction() throws { + @Test func testStaticFunction() throws { let function = Function( "createInstance", returns: "MyType", { @@ -74,10 +74,10 @@ final class FunctionTests: XCTestCase { .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace .trimmingCharacters(in: .whitespacesAndNewlines) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } - func testMutatingFunction() throws { + @Test func testMutatingFunction() throws { let function = Function( "updateValue", { @@ -109,6 +109,6 @@ final class FunctionTests: XCTestCase { .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace .trimmingCharacters(in: .whitespacesAndNewlines) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } } diff --git a/Tests/SyntaxKitTests/LiteralTests.swift b/Tests/SyntaxKitTests/LiteralTests.swift index 666402c..1c220ea 100644 --- a/Tests/SyntaxKitTests/LiteralTests.swift +++ b/Tests/SyntaxKitTests/LiteralTests.swift @@ -1,15 +1,15 @@ -import XCTest +import Testing @testable import SyntaxKit -final class LiteralTests: XCTestCase { - func testGroupWithLiterals() { - let group = Group { - Return { - Literal.integer(1) - } +struct LiteralTests { + @Test func testGroupWithLiterals() { + let group = Group { + Return { + Literal.integer(1) + } + } + let generated = group.generateCode() + #expect(generated.trimmingCharacters(in: .whitespacesAndNewlines) == "return 1") } - let generated = group.generateCode() - XCTAssertEqual(generated.trimmingCharacters(in: .whitespacesAndNewlines), "return 1") - } } diff --git a/Tests/SyntaxKitTests/StructTests.swift b/Tests/SyntaxKitTests/StructTests.swift index de6aba4..fdeb48e 100644 --- a/Tests/SyntaxKitTests/StructTests.swift +++ b/Tests/SyntaxKitTests/StructTests.swift @@ -1,8 +1,8 @@ -import XCTest +import Testing @testable import SyntaxKit -final class StructTests: XCTestCase { +struct StructTests { func normalize(_ code: String) -> String { code .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments @@ -12,7 +12,7 @@ final class StructTests: XCTestCase { .trimmingCharacters(in: .whitespacesAndNewlines) } - func testGenericStruct() { + @Test func testGenericStruct() { let stackStruct = Struct("Stack", generic: "Element") { Variable(.var, name: "items", type: "[Element]", equals: "[]") @@ -69,10 +69,10 @@ final class StructTests: XCTestCase { let normalizedGenerated = normalize(stackStruct.generateCode()) let normalizedExpected = normalize(expectedCode) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } - func testGenericStructWithInheritance() { + @Test func testGenericStructWithInheritance() { let containerStruct = Struct("Container", generic: "T") { Variable(.var, name: "value", type: "T") }.inherits("Equatable") @@ -85,10 +85,10 @@ final class StructTests: XCTestCase { let normalizedGenerated = normalize(containerStruct.generateCode()) let normalizedExpected = normalize(expectedCode) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } - func testNonGenericStruct() { + @Test func testNonGenericStruct() { let simpleStruct = Struct("Point") { Variable(.var, name: "x", type: "Double") Variable(.var, name: "y", type: "Double") @@ -103,6 +103,6 @@ final class StructTests: XCTestCase { let normalizedGenerated = normalize(simpleStruct.generateCode()) let normalizedExpected = normalize(expectedCode) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } } From ffe9a621052f97bf5c6b4099b28f03886b81d512 Mon Sep 17 00:00:00 2001 From: "codecov-ai[bot]" <156709835+codecov-ai[bot]@users.noreply.github.com> Date: Tue, 17 Jun 2025 10:21:50 -0400 Subject: [PATCH 2/2] Add Tests for PR#16 (#17) --- .../AssertionMigrationTests.swift | 79 ++++++ Tests/SyntaxKitTests/BasicTests.swift | 57 ++-- Tests/SyntaxKitTests/BlackjackTests.swift | 262 ++++++++---------- .../CodeStyleMigrationTests.swift | 81 ++++++ .../FrameworkCompatibilityTests.swift | 148 ++++++++++ Tests/SyntaxKitTests/FunctionTests.swift | 51 +--- Tests/SyntaxKitTests/LiteralTests.swift | 16 +- Tests/SyntaxKitTests/MigrationTests.swift | 131 +++++++++ Tests/SyntaxKitTests/String+Normalize.swift | 11 + Tests/SyntaxKitTests/StructTests.swift | 21 +- 10 files changed, 612 insertions(+), 245 deletions(-) create mode 100644 Tests/SyntaxKitTests/AssertionMigrationTests.swift create mode 100644 Tests/SyntaxKitTests/CodeStyleMigrationTests.swift create mode 100644 Tests/SyntaxKitTests/FrameworkCompatibilityTests.swift create mode 100644 Tests/SyntaxKitTests/MigrationTests.swift create mode 100644 Tests/SyntaxKitTests/String+Normalize.swift diff --git a/Tests/SyntaxKitTests/AssertionMigrationTests.swift b/Tests/SyntaxKitTests/AssertionMigrationTests.swift new file mode 100644 index 0000000..a6d03f2 --- /dev/null +++ b/Tests/SyntaxKitTests/AssertionMigrationTests.swift @@ -0,0 +1,79 @@ +import Testing + +@testable import SyntaxKit + +/// Tests specifically focused on assertion migration from XCTest to Swift Testing +/// Ensures all assertion patterns from the original tests work correctly with #expect() +struct AssertionMigrationTests { + // MARK: - XCTAssertEqual Migration Tests + + @Test func testEqualityAssertionMigration() throws { + // Test the most common migration: XCTAssertEqual -> #expect(a == b) + let function = Function("test", returns: "String") { + Return { + Literal.string("hello") + } + } + + let generated = function.syntax.description + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + + let expected = "func test() -> String { return \"hello\" }" + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + + // This replaces: XCTAssertEqual(generated, expected) + #expect(generated == expected) + } + + // MARK: - XCTAssertFalse Migration Tests + + @Test func testFalseAssertionMigration() { + let syntax = Group { + Variable(.let, name: "test", type: "String", equals: "\"value\"") + } + + let generated = syntax.generateCode().trimmingCharacters(in: .whitespacesAndNewlines) + + // This replaces: XCTAssertFalse(generated.isEmpty) + #expect(!generated.isEmpty) + } + + // MARK: - Complex Assertion Migration Tests + + @Test func testNormalizedStringComparisonMigration() throws { + let blackjackCard = Struct("Card") { + Enum("Suit") { + EnumCase("hearts").equals("♡") + EnumCase("spades").equals("♠") + }.inherits("Character") + } + + let expected = """ + struct Card { + enum Suit: Character { + case hearts = "♡" + case spades = "♠" + } + } + """ + + // Test the complete normalization pipeline that was used in XCTest + let normalizedGenerated = blackjackCard.syntax.description.normalize() + + let normalizedExpected = expected.normalize() + + // This replaces: XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) + } + + @Test func testMultipleAssertionsInSingleTest() { + let generated = "struct Test { var value: Int }" + + // Test multiple assertions in one test method + #expect(!generated.isEmpty) + #expect(generated.contains("struct Test")) + #expect(generated.contains("var value: Int")) + } +} diff --git a/Tests/SyntaxKitTests/BasicTests.swift b/Tests/SyntaxKitTests/BasicTests.swift index f002b5d..bfc9433 100644 --- a/Tests/SyntaxKitTests/BasicTests.swift +++ b/Tests/SyntaxKitTests/BasicTests.swift @@ -3,43 +3,32 @@ import Testing @testable import SyntaxKit struct BasicTests { - @Test func testBlackjackCardExample() throws { - let blackjackCard = Struct("BlackjackCard") { - Enum("Suit") { - EnumCase("spades").equals("♠") - EnumCase("hearts").equals("♡") - EnumCase("diamonds").equals("♢") - EnumCase("clubs").equals("♣") - }.inherits("Character") - } + @Test func testBlackjackCardExample() throws { + let blackjackCard = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + }.inherits("Character") + } - let expected = """ - struct BlackjackCard { - enum Suit: Character { - case spades = "♠" - case hearts = "♡" - case diamonds = "♢" - case clubs = "♣" - } + let expected = """ + struct BlackjackCard { + enum Suit: Character { + case spades = "♠" + case hearts = "♡" + case diamonds = "♢" + case clubs = "♣" } - """ + } + """ - // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = blackjackCard.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) - .trimmingCharacters(in: .whitespacesAndNewlines) + // Normalize whitespace, remove comments and modifiers, and normalize colon spacing + let normalizedGenerated = blackjackCard.syntax.description.normalize() - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) - .trimmingCharacters(in: .whitespacesAndNewlines) + let normalizedExpected = expected.normalize() - #expect(normalizedGenerated == normalizedExpected) - } + #expect(normalizedGenerated == normalizedExpected) + } } diff --git a/Tests/SyntaxKitTests/BlackjackTests.swift b/Tests/SyntaxKitTests/BlackjackTests.swift index 511f3cc..16ab0d0 100644 --- a/Tests/SyntaxKitTests/BlackjackTests.swift +++ b/Tests/SyntaxKitTests/BlackjackTests.swift @@ -3,38 +3,38 @@ import Testing @testable import SyntaxKit struct BlackjackTests { - @Test func testBlackjackCardExample() throws { - let syntax = Struct("BlackjackCard") { - Enum("Suit") { - EnumCase("spades").equals("♠") - EnumCase("hearts").equals("♡") - EnumCase("diamonds").equals("♢") - EnumCase("clubs").equals("♣") - } - .inherits("Character") - - Enum("Rank") { - EnumCase("two").equals(2) - EnumCase("three") - EnumCase("four") - EnumCase("five") - EnumCase("six") - EnumCase("seven") - EnumCase("eight") - EnumCase("nine") - EnumCase("ten") - EnumCase("jack") - EnumCase("queen") - EnumCase("king") - EnumCase("ace") - } - .inherits("Int") + @Test func testBlackjackCardExample() throws { + let syntax = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + } + .inherits("Character") + + Enum("Rank") { + EnumCase("two").equals(2) + EnumCase("three") + EnumCase("four") + EnumCase("five") + EnumCase("six") + EnumCase("seven") + EnumCase("eight") + EnumCase("nine") + EnumCase("ten") + EnumCase("jack") + EnumCase("queen") + EnumCase("king") + EnumCase("ace") + } + .inherits("Int") - Variable(.let, name: "rank", type: "Rank") - Variable(.let, name: "suit", type: "Suit") - } + Variable(.let, name: "rank", type: "Rank") + Variable(.let, name: "suit", type: "Suit") + } - let expected = """ + let expected = """ struct BlackjackCard { enum Suit: Character { case spades = "♠" @@ -62,104 +62,93 @@ struct BlackjackTests { } """ - // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = syntax.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) - .trimmingCharacters(in: .whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) - .trimmingCharacters(in: .whitespacesAndNewlines) - - #expect(normalizedGenerated == normalizedExpected) - } + // Normalize whitespace, remove comments and modifiers, and normalize colon spacing + let normalizedGenerated = syntax.syntax.description.normalize() - // swiftlint:disable:next function_body_length - @Test func testFullBlackjackCardExample() throws { - // swiftlint:disable:next closure_body_length - let syntax = Struct("BlackjackCard") { - Enum("Suit") { - EnumCase("spades").equals("♠") - EnumCase("hearts").equals("♡") - EnumCase("diamonds").equals("♢") - EnumCase("clubs").equals("♣") - } - .inherits("Character") - - Enum("Rank") { - EnumCase("two").equals(2) - EnumCase("three") - EnumCase("four") - EnumCase("five") - EnumCase("six") - EnumCase("seven") - EnumCase("eight") - EnumCase("nine") - EnumCase("ten") - EnumCase("jack") - EnumCase("queen") - EnumCase("king") - EnumCase("ace") - Struct("Values") { - Variable(.let, name: "first", type: "Int") - Variable(.let, name: "second", type: "Int?") + let normalizedExpected = expected.normalize() + + #expect(normalizedGenerated == normalizedExpected) + } + + // swiftlint:disable:next function_body_length + @Test func testFullBlackjackCardExample() throws { + // swiftlint:disable:next closure_body_length + let syntax = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + } + .inherits("Character") + + Enum("Rank") { + EnumCase("two").equals(2) + EnumCase("three") + EnumCase("four") + EnumCase("five") + EnumCase("six") + EnumCase("seven") + EnumCase("eight") + EnumCase("nine") + EnumCase("ten") + EnumCase("jack") + EnumCase("queen") + EnumCase("king") + EnumCase("ace") + Struct("Values") { + Variable(.let, name: "first", type: "Int") + Variable(.let, name: "second", type: "Int?") + } + ComputedProperty("values", type: "Values") { + Switch("self") { + SwitchCase(".ace") { + Return { + Init("Values") { + Parameter(name: "first", type: "", defaultValue: "1") + Parameter(name: "second", type: "", defaultValue: "11") } - ComputedProperty("values", type: "Values") { - Switch("self") { - SwitchCase(".ace") { - Return { - Init("Values") { - Parameter(name: "first", type: "", defaultValue: "1") - Parameter(name: "second", type: "", defaultValue: "11") - } - } - } - SwitchCase(".jack", ".queen", ".king") { - Return { - Init("Values") { - Parameter(name: "first", type: "", defaultValue: "10") - Parameter(name: "second", type: "", defaultValue: "nil") - } - } - } - Default { - Return { - Init("Values") { - Parameter(name: "first", type: "", defaultValue: "self.rawValue") - Parameter(name: "second", type: "", defaultValue: "nil") - } - } - } - } + } + } + SwitchCase(".jack", ".queen", ".king") { + Return { + Init("Values") { + Parameter(name: "first", type: "", defaultValue: "10") + Parameter(name: "second", type: "", defaultValue: "nil") } + } } - .inherits("Int") - - Variable(.let, name: "rank", type: "Rank") - Variable(.let, name: "suit", type: "Suit") - ComputedProperty("description", type: "String") { - VariableDecl(.var, name: "output", equals: "\"suit is \\(suit.rawValue),\"") - PlusAssign("output", "\" value is \\(rank.values.first)\"") - If( - Let("second", "rank.values.second"), - then: { - PlusAssign("output", "\" or \\(second)\"") - } - ) - Return { - VariableExp("output") + Default { + Return { + Init("Values") { + Parameter(name: "first", type: "", defaultValue: "self.rawValue") + Parameter(name: "second", type: "", defaultValue: "nil") } + } } + } + } + } + .inherits("Int") + + Variable(.let, name: "rank", type: "Rank") + Variable(.let, name: "suit", type: "Suit") + ComputedProperty("description", type: "String") { + VariableDecl(.var, name: "output", equals: "\"suit is \\(suit.rawValue),\"") + PlusAssign("output", "\" value is \\(rank.values.first)\"") + If( + Let("second", "rank.values.second"), + then: { + PlusAssign("output", "\" or \\(second)\"") + } + ) + Return { + VariableExp("output") } + } + } - let expected = """ + let expected = """ struct BlackjackCard { enum Suit: Character { case spades = \"♠\" @@ -213,30 +202,11 @@ struct BlackjackTests { } """ - // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = syntax.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) - .replacingOccurrences( - of: "public\\s+", with: "", options: .regularExpression - ) - .replacingOccurrences( - of: "\\s*:\\s*", with: ": ", options: .regularExpression - ) - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) - .trimmingCharacters(in: .whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) - .replacingOccurrences( - of: "public\\s+", with: "", options: .regularExpression - ) - .replacingOccurrences( - of: "\\s*:\\s*", with: ": ", options: .regularExpression - ) - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) - .trimmingCharacters(in: .whitespacesAndNewlines) - - #expect(normalizedGenerated == normalizedExpected) - } + // Normalize whitespace, remove comments and modifiers, and normalize colon spacing + let normalizedGenerated = syntax.syntax.description.normalize() + + let normalizedExpected = expected.normalize() + + #expect(normalizedGenerated == normalizedExpected) + } } diff --git a/Tests/SyntaxKitTests/CodeStyleMigrationTests.swift b/Tests/SyntaxKitTests/CodeStyleMigrationTests.swift new file mode 100644 index 0000000..08aea89 --- /dev/null +++ b/Tests/SyntaxKitTests/CodeStyleMigrationTests.swift @@ -0,0 +1,81 @@ +import Testing + +@testable import SyntaxKit + +/// Tests for code style and API simplification changes introduced during Swift Testing migration +/// Validates the simplified Swift APIs and formatting changes +struct CodeStyleMigrationTests { + // MARK: - CharacterSet Simplification Tests + + @Test func testCharacterSetSimplification() { + // Test that .whitespacesAndNewlines works instead of CharacterSet.whitespacesAndNewlines + let testString = "\n test content \n\t" + + // Old style: CharacterSet.whitespacesAndNewlines + // New style: .whitespacesAndNewlines + let trimmed = testString.trimmingCharacters(in: .whitespacesAndNewlines) + + #expect(trimmed == "test content") + } + + // MARK: - Indentation and Formatting Tests + + @Test func testConsistentIndentationInMigratedCode() throws { + // Test that the indentation changes in the migrated code work correctly + let syntax = Struct("IndentationTest") { + Variable(.let, name: "property1", type: "String") + Variable(.let, name: "property2", type: "Int") + + Function("method") { + Parameter(name: "param", type: "String") + } _: { + VariableDecl(.let, name: "local", equals: "\"value\"") + Return { + VariableExp("local") + } + } + } + + let generated = syntax.generateCode().normalize() + + // Verify proper indentation is maintained + #expect( + generated + == "struct IndentationTest { let property1: String let property2: Int func method(param: String) { let local = \"value\" return local } }" + ) + } + + // MARK: - Multiline String Formatting Tests + + @Test func testMultilineStringFormatting() { + let expected = """ + struct TestStruct { + let value: String + var count: Int + } + """ + + let syntax = Struct("TestStruct") { + Variable(.let, name: "value", type: "String") + Variable(.var, name: "count", type: "Int") + } + + let normalized = syntax.generateCode().normalize() + + let expectedNormalized = expected.normalize() + + #expect(normalized == expectedNormalized) + } + + @Test func testMigrationPreservesCodeGeneration() { + // Ensure that the style changes don't break core functionality + let group = Group { + Return { + Literal.string("migrated") + } + } + + let generated = group.generateCode().trimmingCharacters(in: .whitespacesAndNewlines) + #expect(generated == "return \"migrated\"") + } +} diff --git a/Tests/SyntaxKitTests/FrameworkCompatibilityTests.swift b/Tests/SyntaxKitTests/FrameworkCompatibilityTests.swift new file mode 100644 index 0000000..d16bdc0 --- /dev/null +++ b/Tests/SyntaxKitTests/FrameworkCompatibilityTests.swift @@ -0,0 +1,148 @@ +import Testing + +@testable import SyntaxKit + +/// Tests to ensure compatibility and feature parity between XCTest and Swift Testing +/// Validates that the migration maintains all testing capabilities +struct FrameworkCompatibilityTests { + // MARK: - Test Organization Migration Tests + + @Test func testStructBasedOrganization() { + // Test that struct-based test organization works + // This replaces: final class TestClass: XCTestCase + let testExecuted = true + #expect(testExecuted) + } + + @Test func testMethodAnnotationMigration() throws { + // Test that @Test annotation works with throws + // This replaces: func testMethod() throws + let syntax = Enum("TestEnum") { + EnumCase("first") + EnumCase("second") + } + + let generated = syntax.syntax.description + #expect(!generated.isEmpty) + #expect(generated.contains("enum TestEnum")) + } + + // MARK: - Error Handling Compatibility Tests + + @Test func testThrowingTestCompatibility() throws { + // Ensure throws declaration works properly with @Test + let function = Function("throwingFunction", returns: "String") { + Parameter(name: "input", type: "String") + } _: { + Return { + VariableExp("input.uppercased()") + } + } + + let generated = try function.syntax.description + #expect(generated.contains("func throwingFunction")) + } + + // MARK: - Complex DSL Compatibility Tests + + @Test func testFullBlackjackCompatibility() throws { + // Test complex DSL patterns work with new framework + let syntax = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + }.inherits("Character") + + Enum("Rank") { + EnumCase("ace").equals(1) + EnumCase("two").equals(2) + EnumCase("jack").equals(11) + EnumCase("queen").equals(12) + EnumCase("king").equals(13) + }.inherits("Int") + + Variable(.let, name: "rank", type: "Rank") + Variable(.let, name: "suit", type: "Suit") + } + + let generated = syntax.syntax.description + let normalized = generated.normalize() + + // Validate all components are present + #expect(normalized.contains("struct BlackjackCard")) + #expect(normalized.contains("enum Suit: Character")) + #expect(normalized.contains("enum Rank: Int")) + #expect(normalized.contains("let rank: Rank")) + #expect(normalized.contains("let suit: Suit")) + } + + // MARK: - Function Generation Compatibility Tests + + @Test func testFunctionGenerationCompatibility() throws { + let function = Function("calculateValue", returns: "Int") { + Parameter(name: "multiplier", type: "Int") + Parameter(name: "base", type: "Int", defaultValue: "10") + } _: { + Return { + VariableExp("multiplier * base") + } + } + + let generated = function.syntax.description + let normalized = + generated + .normalize() + + #expect(normalized.contains("func calculateValue(multiplier: Int, base: Int = 10) -> Int")) + #expect(normalized.contains("return multiplier * base")) + } + + // MARK: - Comment Injection Compatibility Tests + + @Test func testCommentInjectionCompatibility() { + let syntax = Struct("DocumentedStruct") { + Variable(.let, name: "value", type: "String") + .comment { + Line(.doc, "The main value of the struct") + } + }.comment { + Line("MARK: - Data Models") + Line(.doc, "A documented struct for testing") + } + + let generated = syntax.generateCode() + + #expect(!generated.isEmpty) + #expect(generated.contains("struct DocumentedStruct")) + #expect(generated.normalize().contains("let value: String".normalize())) + } + + // MARK: - Migration Regression Tests + + @Test func testNoRegressionInCodeGeneration() { + // Ensure migration doesn't introduce regressions + let simpleStruct = Struct("Point") { + Variable(.var, name: "x", type: "Double", equals: "0.0") + Variable(.var, name: "y", type: "Double", equals: "0.0") + } + + let generated = simpleStruct.generateCode().normalize() + + #expect(generated.contains("struct Point")) + #expect(generated.contains("var x: Double = 0.0".normalize())) + #expect(generated.contains("var y: Double = 0.0".normalize())) + } + + @Test func testLiteralGeneration() { + let group = Group { + Return { + Literal.integer(100) + } + } + + let generated = group.generateCode().trimmingCharacters(in: .whitespacesAndNewlines) + #expect(generated == "return 100") + } +} diff --git a/Tests/SyntaxKitTests/FunctionTests.swift b/Tests/SyntaxKitTests/FunctionTests.swift index b2d5312..797002e 100644 --- a/Tests/SyntaxKitTests/FunctionTests.swift +++ b/Tests/SyntaxKitTests/FunctionTests.swift @@ -20,20 +20,9 @@ struct FunctionTests { """ // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = function.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) + let normalizedGenerated = function.syntax.description.normalize() + + let normalizedExpected = expected.normalize() #expect(normalizedGenerated == normalizedExpected) } @@ -59,20 +48,9 @@ struct FunctionTests { """ // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = function.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) + let normalizedGenerated = function.syntax.description.normalize() + + let normalizedExpected = expected.normalize() #expect(normalizedGenerated == normalizedExpected) } @@ -94,20 +72,9 @@ struct FunctionTests { """ // Normalize whitespace, remove comments and modifiers, and normalize colon spacing - let normalizedGenerated = function.syntax.description - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) - - let normalizedExpected = - expected - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) + let normalizedGenerated = function.syntax.description.normalize() + + let normalizedExpected = expected.normalize() #expect(normalizedGenerated == normalizedExpected) } diff --git a/Tests/SyntaxKitTests/LiteralTests.swift b/Tests/SyntaxKitTests/LiteralTests.swift index 1c220ea..255675a 100644 --- a/Tests/SyntaxKitTests/LiteralTests.swift +++ b/Tests/SyntaxKitTests/LiteralTests.swift @@ -3,13 +3,13 @@ import Testing @testable import SyntaxKit struct LiteralTests { - @Test func testGroupWithLiterals() { - let group = Group { - Return { - Literal.integer(1) - } - } - let generated = group.generateCode() - #expect(generated.trimmingCharacters(in: .whitespacesAndNewlines) == "return 1") + @Test func testGroupWithLiterals() { + let group = Group { + Return { + Literal.integer(1) + } } + let generated = group.generateCode() + #expect(generated.trimmingCharacters(in: .whitespacesAndNewlines) == "return 1") + } } diff --git a/Tests/SyntaxKitTests/MigrationTests.swift b/Tests/SyntaxKitTests/MigrationTests.swift new file mode 100644 index 0000000..ddcc5a7 --- /dev/null +++ b/Tests/SyntaxKitTests/MigrationTests.swift @@ -0,0 +1,131 @@ +import Testing + +@testable import SyntaxKit + +/// Tests specifically for verifying the Swift Testing framework migration +/// These tests ensure that the migration from XCTest to Swift Testing works correctly +struct MigrationTests { + // MARK: - Basic Test Structure Migration Tests + + @Test func testStructBasedTestExecution() { + // Test that struct-based tests execute properly + let result = true + #expect(result == true) + } + + @Test func testThrowingTestMethod() throws { + // Test that @Test works with throws declaration + let syntax = Struct("TestStruct") { + Variable(.let, name: "value", type: "String") + } + + let generated = syntax.syntax.description + #expect(!generated.isEmpty) + } + + // MARK: - Assertion Migration Tests + + @Test func testExpectEqualityAssertion() { + // Test #expect() replacement for XCTAssertEqual + let actual = "test" + let expected = "test" + #expect(actual == expected) + } + + @Test func testExpectBooleanAssertion() { + // Test #expect() replacement for XCTAssertTrue/XCTAssertFalse + let condition = true + #expect(condition) + #expect(!false) + } + + @Test func testExpectEmptyStringAssertion() { + // Test #expect() replacement for XCTAssertFalse(string.isEmpty) + let generated = "non-empty string" + #expect(!generated.isEmpty) + } + + // MARK: - Code Generation Testing with New Framework + + @Test func testBasicCodeGenerationWithNewFramework() throws { + let blackjackCard = Struct("BlackjackCard") { + Enum("Suit") { + EnumCase("spades").equals("♠") + EnumCase("hearts").equals("♡") + EnumCase("diamonds").equals("♢") + EnumCase("clubs").equals("♣") + }.inherits("Character") + } + + let expected = """ + struct BlackjackCard { + enum Suit: Character { + case spades = "♠" + case hearts = "♡" + case diamonds = "♢" + case clubs = "♣" + } + } + """ + + // Use the same normalization approach as existing tests + let normalizedGenerated = blackjackCard.syntax.description.normalize() + + let normalizedExpected = expected.normalize() + + #expect(normalizedGenerated == normalizedExpected) + } + + // MARK: - String Options Migration Tests + + @Test func testStringCompareOptionsSimplification() { + // Test that .regularExpression works instead of String.CompareOptions.regularExpression + let testString = "public func test() { }" + let result = testString.replacingOccurrences( + of: "public\\s+", with: "", options: .regularExpression) + let expected = "func test() { }" + #expect(result == expected) + } + + @Test func testCharacterSetSimplification() { + // Test that .whitespacesAndNewlines works instead of CharacterSet.whitespacesAndNewlines + let testString = " test \n" + let result = testString.trimmingCharacters(in: .whitespacesAndNewlines) + let expected = "test" + #expect(result == expected) + } + + // MARK: - Complex Code Generation Tests + + @Test func testComplexStructGeneration() throws { + let syntax = Struct("TestCard") { + Variable(.let, name: "rank", type: "String") + Variable(.let, name: "suit", type: "String") + + Function("description", returns: "String") { + Return { + VariableExp("\"\\(rank) of \\(suit)\"") + } + } + } + + let generated = syntax.syntax.description.normalize() + + // Verify generated code contains expected elements + #expect(generated.contains("struct TestCard".normalize())) + #expect(generated.contains("let rank: String".normalize())) + #expect(generated.contains("let suit: String".normalize())) + #expect(generated.contains("func description() -> String".normalize())) + } + + @Test func testMigrationBackwardCompatibility() { + // Ensure that the migrated tests maintain the same functionality + let group = Group { + Return { + Literal.integer(42) + } + } + let generated = group.generateCode() + #expect(generated.trimmingCharacters(in: .whitespacesAndNewlines) == "return 42") + } +} diff --git a/Tests/SyntaxKitTests/String+Normalize.swift b/Tests/SyntaxKitTests/String+Normalize.swift new file mode 100644 index 0000000..5343b37 --- /dev/null +++ b/Tests/SyntaxKitTests/String+Normalize.swift @@ -0,0 +1,11 @@ +import Foundation + +extension String { + func normalize() -> String { + self + .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) + .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) + .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + } +} diff --git a/Tests/SyntaxKitTests/StructTests.swift b/Tests/SyntaxKitTests/StructTests.swift index fdeb48e..e578664 100644 --- a/Tests/SyntaxKitTests/StructTests.swift +++ b/Tests/SyntaxKitTests/StructTests.swift @@ -3,15 +3,6 @@ import Testing @testable import SyntaxKit struct StructTests { - func normalize(_ code: String) -> String { - code - .replacingOccurrences(of: "//.*$", with: "", options: .regularExpression) // Remove comments - .replacingOccurrences(of: "public\\s+", with: "", options: .regularExpression) // Remove public modifier - .replacingOccurrences(of: "\\s*:\\s*", with: ": ", options: .regularExpression) // Normalize colon spacing - .replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression) // Normalize whitespace - .trimmingCharacters(in: .whitespacesAndNewlines) - } - @Test func testGenericStruct() { let stackStruct = Struct("Stack", generic: "Element") { Variable(.var, name: "items", type: "[Element]", equals: "[]") @@ -67,8 +58,8 @@ struct StructTests { } """ - let normalizedGenerated = normalize(stackStruct.generateCode()) - let normalizedExpected = normalize(expectedCode) + let normalizedGenerated = stackStruct.generateCode().normalize() + let normalizedExpected = expectedCode.normalize() #expect(normalizedGenerated == normalizedExpected) } @@ -83,8 +74,8 @@ struct StructTests { } """ - let normalizedGenerated = normalize(containerStruct.generateCode()) - let normalizedExpected = normalize(expectedCode) + let normalizedGenerated = containerStruct.generateCode().normalize() + let normalizedExpected = expectedCode.normalize() #expect(normalizedGenerated == normalizedExpected) } @@ -101,8 +92,8 @@ struct StructTests { } """ - let normalizedGenerated = normalize(simpleStruct.generateCode()) - let normalizedExpected = normalize(expectedCode) + let normalizedGenerated = simpleStruct.generateCode().normalize() + let normalizedExpected = expectedCode.normalize() #expect(normalizedGenerated == normalizedExpected) } }