Skip to content

Commit 6252b8c

Browse files
committed
Adds testing infrastructure
1 parent b9fcfff commit 6252b8c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ jobs:
1717
uses: actions/checkout@v3
1818
- name: Build
1919
run: swift build -c ${{ matrix.build-config }}
20+
- name: Test
21+
if: ${{ matrix.build-config == 'debug' }}
22+
run: swift test

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,10 @@ let package = Package(
7676
dependencies: [
7777
.byName(name: "TecoCodeGeneratorCommons"),
7878
]),
79+
.target(
80+
name: "TecoCodeGeneratorTestHelpers",
81+
dependencies: [
82+
.byName(name: "TecoCodeGeneratorCommons"),
83+
]),
7984
]
8085
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import SwiftSyntax
2+
@_implementationOnly import TecoCodeGeneratorCommons
3+
import XCTest
4+
5+
private func buildSyntax(_ buildable: some SyntaxProtocol) -> [String] {
6+
// Format the code.
7+
let source = buildable.formatted(using: CodeGenerationFormat())
8+
9+
// Work around styling issues regarding blank lines.
10+
let code = source.description.trimmingCharacters(in: .whitespacesAndNewlines)
11+
12+
// Work around styling issues regarding trailing whitespaces.
13+
return code.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline).map {
14+
var line = $0
15+
while line.last?.isWhitespace == true {
16+
line.removeLast()
17+
}
18+
return String(line)
19+
}
20+
}
21+
22+
public func AssertBuilder(_ buildable: some SyntaxProtocol, _ result: String) {
23+
// Format the code.
24+
let code = buildSyntax(buildable).joined(separator: "\n")
25+
26+
// Assert build result
27+
XCTAssertEqual(code, result)
28+
}
29+
30+
public func AssertBuilder(_ buildable: some SyntaxProtocol, contains lines: [String]) {
31+
// Format the code.
32+
let code = buildSyntax(buildable)
33+
34+
// Assert build result
35+
for line in lines {
36+
XCTAssertTrue(code.contains(line))
37+
}
38+
}

0 commit comments

Comments
 (0)