|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | + |
| 4 | +@testable import SyntaxKit |
| 5 | + |
| 6 | +@Suite internal struct ForLoopsExampleTests { |
| 7 | + @Test("Completed for loops DSL generates expected Swift code", .disabled()) |
| 8 | + internal func testCompletedForLoopsExample() throws { |
| 9 | + // Build DSL equivalent of Examples/Completed/for_loops/dsl.swift |
| 10 | + |
| 11 | + let program = Group { |
| 12 | + // MARK: - Basic For-in Loop |
| 13 | + Variable(.let, name: "names", equals: Literal.array([Literal.string("Alice"), Literal.string("Bob"), Literal.string("Charlie")])) |
| 14 | + .comment { |
| 15 | + Line("MARK: - Basic For-in Loop") |
| 16 | + Line("Simple for-in loop over an array") |
| 17 | + } |
| 18 | +// |
| 19 | +// For { |
| 20 | +// VariableExp("name") |
| 21 | +// } in: VariableExp("names") then: { |
| 22 | +// Call("print") { |
| 23 | +// ParameterExp(unlabeled: "\"Hello, \\(name)!\"") |
| 24 | +// } |
| 25 | +// } |
| 26 | +// |
| 27 | +// // MARK: - For-in with Enumerated |
| 28 | +// Call("print") { |
| 29 | +// ParameterExp(unlabeled: "\"\\n=== For-in with Enumerated ===\"") |
| 30 | +// } |
| 31 | +// .comment { |
| 32 | +// Line("MARK: - For-in with Enumerated") |
| 33 | +// Line("For-in loop with enumerated() to get index and value") |
| 34 | +// } |
| 35 | +// For { |
| 36 | +// Tuple.pattern([VariableExp("index"), VariableExp("name")]) |
| 37 | +// } in: VariableExp("names").call("enumerated") then: { |
| 38 | +// Call("print") { |
| 39 | +// ParameterExp(unlabeled: "\"Index: \\(index), Name: \\(name)\"") |
| 40 | +// } |
| 41 | +// } |
| 42 | +// |
| 43 | +// // MARK: - For-in with Where Clause |
| 44 | +// Call("print") { |
| 45 | +// ParameterExp(unlabeled: "\"\\n=== For-in with Where Clause ===\"") |
| 46 | +// } |
| 47 | +// .comment { |
| 48 | +// Line("MARK: - For-in with Where Clause") |
| 49 | +// Line("For-in loop with where clause") |
| 50 | +// } |
| 51 | +// Variable(.let, name: "numbers", equals: Literal.array([Literal.integer(1), Literal.integer(2), Literal.integer(3), Literal.integer(4), Literal.integer(5), Literal.integer(6), Literal.integer(7), Literal.integer(8), Literal.integer(9), Literal.integer(10)])) |
| 52 | +// |
| 53 | +// For { |
| 54 | +// VariableExp("number") |
| 55 | +// } in: VariableExp("numbers") where: { |
| 56 | +// Infix("%") { |
| 57 | +// VariableExp("number") |
| 58 | +// Literal.integer(2) |
| 59 | +// } |
| 60 | +// } then: { |
| 61 | +// Call("print") { |
| 62 | +// ParameterExp(unlabeled: "\"Even number: \\(number)\"") |
| 63 | +// } |
| 64 | +// } |
| 65 | +// |
| 66 | +// // MARK: - For-in with Dictionary |
| 67 | +// Call("print") { |
| 68 | +// ParameterExp(unlabeled: "\"\\n=== For-in with Dictionary ===\"") |
| 69 | +// } |
| 70 | +// .comment { |
| 71 | +// Line("MARK: - For-in with Dictionary") |
| 72 | +// Line("For-in loop over dictionary") |
| 73 | +// } |
| 74 | +// Variable(.let, name: "scores", equals: Literal.dictionary([(Literal.string("Alice"), Literal.integer(95)), (Literal.string("Bob"), Literal.integer(87)), (Literal.string("Charlie"), Literal.integer(92))])) |
| 75 | +// |
| 76 | +// For { |
| 77 | +// Tuple.pattern([VariableExp("name"), VariableExp("score")]) |
| 78 | +// } in: VariableExp("scores") then: { |
| 79 | +// Call("print") { |
| 80 | +// ParameterExp(unlabeled: "\"\\(name): \\(score)\"") |
| 81 | +// } |
| 82 | +// } |
| 83 | + } |
| 84 | + |
| 85 | + // Generate Swift from DSL |
| 86 | + var generated = program.generateCode() |
| 87 | + |
| 88 | + // Remove type annotations like ": Int =" for comparison to example code |
| 89 | + generated = generated.normalize() |
| 90 | + |
| 91 | + // Use the expected Swift code as a string literal |
| 92 | + let expected = """ |
| 93 | + // MARK: - Basic For-in Loop |
| 94 | + // Simple for-in loop over an array |
| 95 | + let names = ["Alice", "Bob", "Charlie"] |
| 96 | + for name in names { |
| 97 | + print("Hello, \\(name)!") |
| 98 | + } |
| 99 | +
|
| 100 | + // MARK: - For-in with Enumerated |
| 101 | + // For-in loop with enumerated() to get index and value |
| 102 | + print("\\n=== For-in with Enumerated ===") |
| 103 | + for (index, name) in names.enumerated() { |
| 104 | + print("Index: \\(index), Name: \\(name)") |
| 105 | + } |
| 106 | +
|
| 107 | + // MARK: - For-in with Where Clause |
| 108 | + // For-in loop with where clause |
| 109 | + print("\\n=== For-in with Where Clause ===") |
| 110 | + let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 111 | + for number in numbers where number % 2 == 0 { |
| 112 | + print("Even number: \\(number)") |
| 113 | + } |
| 114 | +
|
| 115 | + // MARK: - For-in with Dictionary |
| 116 | + // For-in loop over dictionary |
| 117 | + print("\\n=== For-in with Dictionary ===") |
| 118 | + let scores = ["Alice": 95, "Bob": 87, "Charlie": 92] |
| 119 | + for (name, score) in scores { |
| 120 | + print("\\(name): \\(score)") |
| 121 | + } |
| 122 | + """ |
| 123 | + .normalize() |
| 124 | + |
| 125 | + #expect(generated == expected) |
| 126 | + } |
| 127 | +} |
0 commit comments