Skip to content

Commit

Permalink
add code generation test
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Oct 28, 2024
1 parent 7b7079d commit bacf5c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ jobs:
- swift:6.0-noble
- swiftlang/swift:nightly-main-jammy
config:
- args: "--enable-code-coverage"
- mode: "debug"
args: "--enable-code-coverage"
with_codecov: true
- args: "-c release -Xswiftc -enable-testing"
- mode: "release"
args: "-c release -Xswiftc -enable-testing"
with_codecov: false

runs-on: ubuntu-latest
Expand All @@ -37,8 +39,8 @@ jobs:
with:
path: .build
# e.g. 'enumerator-macro-debug-build-swiftlang/swift:nightly-6.0-jammy-Linux-c7008df8062ac4d5887ead9e59aa05829e'
key: "${{ github.event.repository.name }}-debug-build-${{ matrix.swift-image }}-${{ runner.os }}-${{ hashFiles('./Package.resolved') }}"
restore-keys: "${{ github.event.repository.name }}-debug-build-${{ matrix.swift-image }}-${{ runner.os }}-"
key: "${{ github.event.repository.name }}-${{ matrix.config.mode }}-build-${{ matrix.swift-image }}-${{ runner.os }}-${{ hashFiles('./Package.resolved') }}"
restore-keys: "${{ github.event.repository.name }}-${{ matrix.config.mode }}-build-${{ matrix.swift-image }}-${{ runner.os }}-"

- name: Build ${{ github.event.repository.name }}
run: swift build --build-tests
Expand All @@ -48,7 +50,7 @@ jobs:
uses: actions/cache/save@v4
with:
path: .build
key: "${{ github.event.repository.name }}-debug-build-${{ matrix.swift-image }}-${{ runner.os }}-${{ hashFiles('./Package.resolved') }}"
key: "${{ github.event.repository.name }}-${{ matrix.config.mode }}-build-${{ matrix.swift-image }}-${{ runner.os }}-${{ hashFiles('./Package.resolved') }}"

- name: Run tests
run: swift test ${{ matrix.config.args }} --parallel
Expand Down
22 changes: 18 additions & 4 deletions Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ final class EnumeratorMacroTests: XCTestCase {
enum TestEnum {
case aBcD
case eFgH
var caseCode: Int {
switch self {
case .aBcD:
Expand Down Expand Up @@ -108,7 +108,7 @@ final class EnumeratorMacroTests: XCTestCase {
enum TestEnum {
case aBcD
case eFgH
var caseCode: Int {
switch self {
case .aBcD:
Expand Down Expand Up @@ -273,7 +273,7 @@ final class EnumeratorMacroTests: XCTestCase {
assertMacroExpansion(
#"""
@Enumerator("""
{{#cases}}
{{#cases}}
{{^isEmpty(parameters)}}
func get{{capitalized(name)}}() -> ({{joined(tupleValue(parameters))}})? {
switch self {
Expand All @@ -283,7 +283,7 @@ final class EnumeratorMacroTests: XCTestCase {
return nil
}
}
{{/isEmpty(parameters)}}
{{/isEmpty(parameters)}}
{{/cases}}
""")
enum TestEnum {
Expand Down Expand Up @@ -1230,6 +1230,10 @@ final class EnumeratorMacroTests: XCTestCase {
)
}

func testRealEnumGeneratesCode() throws {
XCTAssertEqual(TestEnum.b.caseName, "b")
}

/// FixItApplier not available in older versions of SwiftSyntax.
#if canImport(SwiftSyntax600)
/// Test name is referenced in the README.
Expand Down Expand Up @@ -1285,6 +1289,16 @@ var isTestCase2: Bool {
}
}
""")
@Enumerator("""
var caseName: Bool {
switch self {
{{#cases}}
case .{{name}}:
return "{{name}}"
{{/cases}}
}
}
""")
enum TestEnum {
case a(val1: String, val2: Int)
case b
Expand Down

0 comments on commit bacf5c6

Please sign in to comment.