-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for macro conditions to named enums
- Loading branch information
1 parent
cf86daa
commit 78088be
Showing
8 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Sources/KnitCodeGen/SourceGen/NamedRegistrationGroup+SourceCode.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Copyright © Block, Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftSyntax | ||
|
||
extension NamedRegistrationGroup { | ||
func enumSourceCode(assemblyName: String) throws -> DeclSyntaxProtocol { | ||
let modifier = accessLevel == .public ? "public " : "" | ||
let enumSyntax = try EnumDeclSyntax("\(raw: modifier)enum \(raw: enumName): String, CaseIterable") { | ||
for reg in registrations { | ||
("case \(raw: reg.name!)" as DeclSyntax).maybeWithCondition( | ||
ifConfigCondition: ifConfigCondition == nil ? reg.ifConfigCondition : nil | ||
) | ||
} | ||
} | ||
return enumSyntax.maybeWithCondition(ifConfigCondition: ifConfigCondition) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Tests/KnitCodeGenTests/NamedRegistrationGroupSourceTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Copyright © Block, Inc. All rights reserved. | ||
// | ||
|
||
@testable import KnitCodeGen | ||
import XCTest | ||
import SwiftSyntax | ||
|
||
final class NamedRegistrationGroupSourceTests: XCTestCase { | ||
|
||
func testResolutionKeyWithInternalMacro() throws { | ||
let registration1 = Registration(service: "ServiceB", name: "name", ifConfigCondition: ExprSyntax("DEBUG")) | ||
let registration2 = Registration(service: "ServiceB", name: "name2") | ||
let registration3 = Registration(service: "ServiceB", name: "name3", ifConfigCondition: ExprSyntax("DEBUG")) | ||
let group = NamedRegistrationGroup.make(from: [registration1, registration2, registration3])[0] | ||
let result = try group.enumSourceCode(assemblyName: "Assembly") | ||
|
||
let expected = """ | ||
enum ServiceB_ResolutionKey: String, CaseIterable { | ||
#if DEBUG | ||
case name | ||
#endif | ||
case name2 | ||
#if DEBUG | ||
case name3 | ||
#endif | ||
} | ||
""" | ||
|
||
XCTAssertEqual(expected, result.formatted().description) | ||
|
||
} | ||
|
||
func testResolutionKeyWithExternalMacro() throws { | ||
let registration1 = Registration(service: "ServiceB", name: "name2", ifConfigCondition: ExprSyntax("DEBUG")) | ||
let group = NamedRegistrationGroup.make(from: [registration1])[0] | ||
let result = try group.enumSourceCode(assemblyName: "Assembly") | ||
|
||
let expected = """ | ||
#if DEBUG | ||
enum ServiceB_ResolutionKey: String, CaseIterable { | ||
case name2 | ||
} | ||
#endif | ||
""" | ||
|
||
XCTAssertEqual(expected, result.formatted().description) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters