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 c8bb006..bfc9433 100644 --- a/Tests/SyntaxKitTests/BasicTests.swift +++ b/Tests/SyntaxKitTests/BasicTests.swift @@ -1,9 +1,9 @@ -import XCTest +import Testing @testable import SyntaxKit -final class BasicTests: XCTestCase { - func testBlackjackCardExample() throws { +struct BasicTests { + @Test func testBlackjackCardExample() throws { let blackjackCard = Struct("BlackjackCard") { Enum("Suit") { EnumCase("spades").equals("♠") @@ -25,21 +25,10 @@ final class BasicTests: XCTestCase { """ // 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) + let normalizedGenerated = blackjackCard.syntax.description.normalize() - 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.normalize() - XCTAssertEqual(normalizedGenerated, normalizedExpected) + #expect(normalizedGenerated == normalizedExpected) } } diff --git a/Tests/SyntaxKitTests/BlackjackTests.swift b/Tests/SyntaxKitTests/BlackjackTests.swift index 6e5eee1..16ab0d0 100644 --- a/Tests/SyntaxKitTests/BlackjackTests.swift +++ b/Tests/SyntaxKitTests/BlackjackTests.swift @@ -1,9 +1,9 @@ -import XCTest +import Testing @testable import SyntaxKit -final class BlackjackTests: XCTestCase { - func testBlackjackCardExample() throws { +struct BlackjackTests { + @Test func testBlackjackCardExample() throws { let syntax = Struct("BlackjackCard") { Enum("Suit") { EnumCase("spades").equals("♠") @@ -63,34 +63,15 @@ 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) + let normalizedGenerated = syntax.syntax.description.normalize() + + let normalizedExpected = expected.normalize() + + #expect(normalizedGenerated == normalizedExpected) } // swiftlint:disable:next function_body_length - func testFullBlackjackCardExample() throws { + @Test func testFullBlackjackCardExample() throws { // swiftlint:disable:next closure_body_length let syntax = Struct("BlackjackCard") { Enum("Suit") { @@ -159,7 +140,8 @@ final class BlackjackTests: XCTestCase { Let("second", "rank.values.second"), then: { PlusAssign("output", "\" or \\(second)\"") - }) + } + ) Return { VariableExp("output") } @@ -221,29 +203,10 @@ 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) + 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/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/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 0fee02a..797002e 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") @@ -20,25 +20,14 @@ final class FunctionTests: XCTestCase { """ // 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) - - XCTAssertEqual(normalizedGenerated, normalizedExpected) + let normalizedGenerated = function.syntax.description.normalize() + + let normalizedExpected = expected.normalize() + + #expect(normalizedGenerated == normalizedExpected) } - func testStaticFunction() throws { + @Test func testStaticFunction() throws { let function = Function( "createInstance", returns: "MyType", { @@ -59,25 +48,14 @@ final class FunctionTests: XCTestCase { """ // 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) - - XCTAssertEqual(normalizedGenerated, normalizedExpected) + let normalizedGenerated = function.syntax.description.normalize() + + let normalizedExpected = expected.normalize() + + #expect(normalizedGenerated == normalizedExpected) } - func testMutatingFunction() throws { + @Test func testMutatingFunction() throws { let function = Function( "updateValue", { @@ -94,21 +72,10 @@ final class FunctionTests: XCTestCase { """ // 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) - - XCTAssertEqual(normalizedGenerated, normalizedExpected) + 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 666402c..255675a 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() { +struct LiteralTests { + @Test func testGroupWithLiterals() { let group = Group { Return { Literal.integer(1) } } let generated = group.generateCode() - XCTAssertEqual(generated.trimmingCharacters(in: .whitespacesAndNewlines), "return 1") + #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 de6aba4..e578664 100644 --- a/Tests/SyntaxKitTests/StructTests.swift +++ b/Tests/SyntaxKitTests/StructTests.swift @@ -1,18 +1,9 @@ -import XCTest +import Testing @testable import SyntaxKit -final class StructTests: XCTestCase { - 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) - } - - func testGenericStruct() { +struct StructTests { + @Test func testGenericStruct() { let stackStruct = Struct("Stack", generic: "Element") { Variable(.var, name: "items", type: "[Element]", equals: "[]") @@ -67,12 +58,12 @@ final class StructTests: XCTestCase { } """ - let normalizedGenerated = normalize(stackStruct.generateCode()) - let normalizedExpected = normalize(expectedCode) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + let normalizedGenerated = stackStruct.generateCode().normalize() + let normalizedExpected = expectedCode.normalize() + #expect(normalizedGenerated == normalizedExpected) } - func testGenericStructWithInheritance() { + @Test func testGenericStructWithInheritance() { let containerStruct = Struct("Container", generic: "T") { Variable(.var, name: "value", type: "T") }.inherits("Equatable") @@ -83,12 +74,12 @@ final class StructTests: XCTestCase { } """ - let normalizedGenerated = normalize(containerStruct.generateCode()) - let normalizedExpected = normalize(expectedCode) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + let normalizedGenerated = containerStruct.generateCode().normalize() + let normalizedExpected = expectedCode.normalize() + #expect(normalizedGenerated == normalizedExpected) } - func testNonGenericStruct() { + @Test func testNonGenericStruct() { let simpleStruct = Struct("Point") { Variable(.var, name: "x", type: "Double") Variable(.var, name: "y", type: "Double") @@ -101,8 +92,8 @@ final class StructTests: XCTestCase { } """ - let normalizedGenerated = normalize(simpleStruct.generateCode()) - let normalizedExpected = normalize(expectedCode) - XCTAssertEqual(normalizedGenerated, normalizedExpected) + let normalizedGenerated = simpleStruct.generateCode().normalize() + let normalizedExpected = expectedCode.normalize() + #expect(normalizedGenerated == normalizedExpected) } }