Skip to content

Commit

Permalink
Public stub adjustment (#15)
Browse files Browse the repository at this point in the history
* Remove modifier and change it to bool literal instead

* Separate Macro to two different class
  • Loading branch information
hainayanda authored Apr 21, 2024
1 parent 98ccd25 commit e02cec4
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 60 deletions.
17 changes: 6 additions & 11 deletions Sources/SwiftEnvironment/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public macro EnvironmentValue() = #externalMacro(
)

@attached(member, names: arbitrary)
public macro Stubbed(_ modifier: Modifier = .internalStub) = #externalMacro(
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
public macro Stubbed(public: Bool = false) = #externalMacro(
module: "SwiftEnvironmentMacro", type: "StubFromTypeGeneratorMacro"
)

@attached(member, names: arbitrary)
public macro Stubbed(_ modifier: Modifier = .internalStub, _ values: DefaultType...) = #externalMacro(
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
public macro Stubbed(public: Bool = false, _ values: DefaultType...) = #externalMacro(
module: "SwiftEnvironmentMacro", type: "StubFromTypeGeneratorMacro"
)

@attached(peer, names: suffixed(Stub))
public macro Stubbed(type: StubType) = #externalMacro(
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
module: "SwiftEnvironmentMacro", type: "StubFromProtocolGeneratorMacro"
)

@attached(peer, names: suffixed(Stub))
public macro Stubbed(type: StubType, _ values: DefaultType...) = #externalMacro(
module: "SwiftEnvironmentMacro", type: "StubGeneratorMacro"
module: "SwiftEnvironmentMacro", type: "StubFromProtocolGeneratorMacro"
)

public enum StubType {
Expand All @@ -40,11 +40,6 @@ public enum StubType {
case openSubclass(AnyClass.Type)
}

public enum Modifier {
case publicStub
case internalStub
}

public struct DefaultType {
public static func value<T>(for: T.Type, _: T) -> DefaultType {
DefaultType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import SwiftSyntaxMacros
@main
struct EnvironmentValueMacroPlugin: CompilerPlugin {
let providingMacros: [Macro.Type] = [
EnvironmentValueMacro.self, StubGeneratorMacro.self
EnvironmentValueMacro.self, StubFromProtocolGeneratorMacro.self, StubFromTypeGeneratorMacro.self
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Nayanda Haberty on 21/4/24.
//
Expand All @@ -9,26 +9,20 @@ import Foundation
import SwiftSyntax

extension AttributeSyntax {
var hasModifiers: Bool {
guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?
.first?.as(LabeledExprSyntax.self) else { return false }
return firstArgument.trimmedDescription == ".publicStub"
|| firstArgument.trimmedDescription == ".internalStub"
}

var isPublicStub: Bool {
guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?
.first?.as(LabeledExprSyntax.self) else { return false }
return firstArgument.trimmedDescription == ".publicStub"
guard let firstArgument = arguments?.as(LabeledExprListSyntax.self)?.first,
firstArgument.label?.trimmedDescription == "public" else {
return false
}
return firstArgument.expression.trimmedDescription == "true"
}

var defaultValueArguments: [String: String] {
get throws {
let arguments = arguments?.as(LabeledExprListSyntax.self)?
.filter { $0.label == nil }
.compactMap { $0.as(LabeledExprSyntax.self) }
guard var arguments else { return [:] }
arguments = hasModifiers ? Array(arguments.suffix(from: 1)) : arguments
guard let arguments else { return [:] }
return try arguments
.map { try $0.extractDefaultValueArguments() }
.mapToTypeDefaultValueArguments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntax

extension StubGeneratorMacro: PeerMacro {
struct StubFromProtocolGeneratorMacro: PeerMacro {
public static func expansion(of node: AttributeSyntax, providingPeersOf declaration: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
guard let protocolSyntax = declaration.as(ProtocolDeclSyntax.self) else {
return []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntax

extension StubGeneratorMacro: MemberMacro {
public struct StubFromTypeGeneratorMacro: MemberMacro {
public static func expansion(of node: AttributeSyntax, providingMembersOf declaration: some DeclGroupSyntax, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
guard let name = declaration.className ?? declaration.structName else {
return []
Expand Down
10 changes: 0 additions & 10 deletions Sources/SwiftEnvironmentMacro/Stubbed/StubGeneratorMacro.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,79 @@ final class StubFromProtocolGeneratorMacroTests: XCTestCase {
assertMacroExpansion(
simpleProtocol,
expandedSource: simpleProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenOptionalProtocol_shouldGenerateDefault() {
assertMacroExpansion(
optionalProtocol,
expandedSource: optionalProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenArrayProtocol_shouldGenerateDefault() {
assertMacroExpansion(
arrayProtocol,
expandedSource: arrayProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenDictionaryProtocol_shouldGenerateDefault() {
assertMacroExpansion(
dictionaryProtocol,
expandedSource: dictionaryProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenClassProtocol_shouldGenerateDefault() {
assertMacroExpansion(
classProtocol,
expandedSource: classProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenSuperClassProtocol_shouldGenerateDefault() {
assertMacroExpansion(
superClassProtocol,
expandedSource: superClassProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenInjectValueProtocol_shouldGenerateDefault() {
assertMacroExpansion(
injectValueProtocol,
expandedSource: injectValueProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenStructProtocol_shouldGenerateDefault() {
assertMacroExpansion(
structProtocol,
expandedSource: structProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenAllArgsProtocol_shouldGenerateDefault() {
assertMacroExpansion(
allArgsProtocol,
expandedSource: allArgsProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}

func test_givenAnyObjectProtocol_shouldGenerateDefault() {
assertMacroExpansion(
anyObjectProtocol,
expandedSource: anyObjectProtocolExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromProtocolGeneratorMacro.self]
)
}
}
Expand Down
26 changes: 13 additions & 13 deletions Tests/SwiftEnvironmentTests/StubFromTypeGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,71 @@ final class StubFromClassAndStructGeneratorMacroTests: XCTestCase {
assertMacroExpansion(
simpleStruct,
expandedSource: simpleStructExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleClass_shouldGenerateDefault() {
assertMacroExpansion(
simpleClass,
expandedSource: simpleClassExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleClassWithInit_shouldGenerateDefault() {
assertMacroExpansion(
simpleClassWithInit,
expandedSource: simpleClassWithInitExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleClassWithRandomInit_shouldGenerateDefault() {
assertMacroExpansion(
simpleClassWithRandomInit,
expandedSource: simpleClassWithRandomInitExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleClassWithDifferentInit_shouldGenerateDefault() {
assertMacroExpansion(
simpleClassWithDifferentInit,
expandedSource: simpleClassWithDifferentInitExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleStructWithClosure_shouldGenerateDefault() {
assertMacroExpansion(
simpleStructWithClosure,
expandedSource: simpleStructWithClosureExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleClasstWithClosure_shouldGenerateDefault() {
assertMacroExpansion(
simpleClassWithClosure,
expandedSource: simpleClassWithClosureExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleStructWithOptional_shouldGenerateDefault() {
assertMacroExpansion(
simpleStructWithOptional,
expandedSource: simpleStructWithOptionalExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}

func test_givenSimpleStructWithTypeAlias_shouldGenerateDefault() {
assertMacroExpansion(
typeAliasStruct,
expandedSource: typeAliasStructExpansion,
macros: ["Stubbed": StubGeneratorMacro.self]
macros: ["Stubbed": StubFromTypeGeneratorMacro.self]
)
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ struct Some {
"""

private let simpleClassWithClosure: String = """
@Stubbed(.publicStub)
@Stubbed(public: true)
class Some {
let voidClosure: () -> Void
let argVoidClosure: (Int) -> Void
Expand Down Expand Up @@ -193,7 +193,7 @@ struct Some {
"""

private let simpleClassWithDifferentInit: String = """
@Stubbed(.publicStub)
@Stubbed(public: true)
class Some {
let int: Int
var double: Double
Expand Down Expand Up @@ -260,7 +260,7 @@ class Some {
"""

private let simpleClassWithInit: String = """
@Stubbed(.publicStub)
@Stubbed(public: true)
class Some {
let int: Int
var double: Double
Expand Down Expand Up @@ -317,7 +317,7 @@ class Some {
"""

private let simpleStruct: String = """
@Stubbed(.publicStub)
@Stubbed(public: true)
struct Some {
let int: Int
var double: Double
Expand Down

0 comments on commit e02cec4

Please sign in to comment.