Skip to content

Commit

Permalink
Add open class and subclass to control stub modifier output
Browse files Browse the repository at this point in the history
  • Loading branch information
hainayanda committed Mar 17, 2024
1 parent de6f846 commit 2b888d0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Sources/SwiftEnvironment/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public macro Stubbed(name: String? = nil, type: StubType = .struct, _ values: De
public enum StubType {
case `struct`
case `class`
case openClass
case subclass(AnyClass.Type)
case openSubclass(AnyClass.Type)
}

public struct DefaultType {
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftEnvironmentMacro/Model/StubDeclaration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ struct StubDeclaration: CustomStringConvertible {
extension StubDeclaration {

enum InstanceType {
case `class`(`protocol`: String, superClass: String? = nil)
case `class`(`protocol`: String, superClass: String? = nil, final: Bool = true)
case `struct`(`protocol`: String)

func declarationString(for name: String) -> String {
switch self {
case .class(let protocolName, let superClass):
return "final class \(name): \(superClassClause(for: superClass))\(protocolName)"
case .class(let protocolName, let superClass, let final):
return "\(final ? "final ": "")class \(name): \(superClassClause(for: superClass))\(protocolName)"
case .struct(let protocolName):
return "struct \(name): \(protocolName)"
}
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftEnvironmentMacro/StubGeneratorMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,25 @@ private extension AttributeSyntax {
}
return .struct(protocol: protocolName)
} else if argument.trimmedDescription.match(#"^(StubType)?\.`?class`?$"#) {
return .class(protocol: protocolName, superClass: nil)
return .class(protocol: protocolName, superClass: nil, final: true)
} else if argument.trimmedDescription.match(#"^(StubType)?\.openClass$"#) {
return .class(protocol: protocolName, superClass: nil, final: false)
} else {
throw StubGeneratorMacroError.cannotDetermineDefaultValue(argument.trimmedDescription)
}
}

func argsTypeArgument(for protocolName: String, argument: FunctionCallExprSyntax) throws -> StubDeclaration.InstanceType {
guard argument.calledExpression.trimmedDescription.match(#"^(StubType)?\.`?subclass`?$"#),
let isSubclass: Bool = argument.calledExpression.trimmedDescription.match(#"^(StubType)?\.subclass$"#)
let isOpenSubclass: Bool = argument.calledExpression.trimmedDescription.match(#"^(StubType)?\.openSubclass$"#)
guard isSubclass || isOpenSubclass,
let argumentExpression = argument.arguments.first?.expression,
argumentExpression.trimmedDescription.match(#"^\S+\.self$"#),
let memberAccess = argumentExpression.as(MemberAccessExprSyntax.self),
let superClass = memberAccess.base?.trimmedDescription else {
throw StubGeneratorMacroError.cannotDetermineDefaultValue(argument.trimmedDescription)
}
return .class(protocol: protocolName, superClass: superClass)
return .class(protocol: protocolName, superClass: superClass, final: isSubclass)
}
}

Expand Down

0 comments on commit 2b888d0

Please sign in to comment.