Skip to content

Add Tests for PR#16 #17

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 3 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"))
}
}
57 changes: 23 additions & 34 deletions Tests/SyntaxKitTests/BasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading
Loading