Skip to content

Migrated from XCTest to Swift Testing #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Tests/SyntaxKitTests/AssertionMigrationTests.swift
Original file line number Diff line number Diff line change
@@ -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"))
}
}
23 changes: 6 additions & 17 deletions Tests/SyntaxKitTests/BasicTests.swift
Original file line number Diff line number Diff line change
@@ -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("♠")
Expand All @@ -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)
}
}
69 changes: 16 additions & 53 deletions Tests/SyntaxKitTests/BlackjackTests.swift
Original file line number Diff line number Diff line change
@@ -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("♠")
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -159,7 +140,8 @@ final class BlackjackTests: XCTestCase {
Let("second", "rank.values.second"),
then: {
PlusAssign("output", "\" or \\(second)\"")
})
}
)
Return {
VariableExp("output")
}
Expand Down Expand Up @@ -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)
}
}
81 changes: 81 additions & 0 deletions Tests/SyntaxKitTests/CodeStyleMigrationTests.swift
Original file line number Diff line number Diff line change
@@ -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\"")
}
}
17 changes: 8 additions & 9 deletions Tests/SyntaxKitTests/CommentTests.swift
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down Expand Up @@ -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")
}
}
Loading
Loading