Skip to content

Commit

Permalink
add: class inherithance on automockable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-T-Dev committed Jan 3, 2025
1 parent 2890a2e commit 2765523
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.2.6] - 2025-01-03

### Added

- Automackable handle inherited classes

# [0.2.5] - 2024-11-15

### Fixed
Expand Down
5 changes: 4 additions & 1 deletion Sources/SageSwiftKit/AutoMockableMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
// All Rights Reserved.

@attached(peer, names: suffixed(Mock))
public macro AutoMockable(accessLevel: String? = nil) = #externalMacro(module: "SageSwiftKitMacros", type: "AutoMockable")
public macro AutoMockable(
accessLevel: String? = nil,
classInheritance: Bool = false
) = #externalMacro(module: "SageSwiftKitMacros", type: "AutoMockable")
16 changes: 14 additions & 2 deletions Sources/SageSwiftKitClient/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ struct PlayingObject {
}


@AutoMockable()
public protocol DateSelectorViewModel {
//@NodePrinter
public class TmpClass {
var a: String
var b: String

init(a: String, b: String) {
self.a = a
self.b = b
}
}

@AutoMockable(classInheritance: true)
public protocol DateSelectorViewModel: TmpClass {
var onTap: (String, Int?) -> Void { get set }
var onString: String { get set }
var onDouble: Double? { get set }
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public enum AutoMockable: PeerMacro {
.adapter
.expression(cast: StringLiteralExprSyntax.self)?.representedLiteralValue ?? "internal"

let classInheritance = node
.adapter
.findArgument(id: "classInheritance")?
.adapter
.expression(cast: BooleanLiteralExprSyntax.self)?.literal.text ?? "false"

let procotolName = protocolSyntax.name.text

guard let members = declaration.as(ProtocolDeclSyntax.self)?.memberBlock.members else {
Expand All @@ -51,39 +57,49 @@ public enum AutoMockable: PeerMacro {
name: .identifier("\(procotolName)Mock"),
inheritanceClause: .init(
inheritedTypes: .init(itemsBuilder: {
if classInheritance == "true" {
if let inherited = protocolSyntax.inheritanceClause {
inherited.inheritedTypes
}
}
InheritedTypeSyntax(
type: IdentifierTypeSyntax(
name: .identifier(procotolName)
)
)

if let inherited = protocolSyntax.inheritanceClause {
inherited.inheritedTypes
if classInheritance == "false" {
if let inherited = protocolSyntax.inheritanceClause {
inherited.inheritedTypes
}
}
})
),
memberBlock: MemberBlockSyntax(
members: try MemberBlockItemListSyntax(itemsBuilder: {
// Init
InitializerDeclSyntax(
modifiers: .init(itemsBuilder: {
DeclModifierSyntax(name: accessLevel.tokenSyntax)
}),
signature: .init(
parameterClause: .init(
parameters: .init(
itemsBuilder: {}

if classInheritance == "false" {
InitializerDeclSyntax(
modifiers: .init(itemsBuilder: {
DeclModifierSyntax(name: accessLevel.tokenSyntax)
}),
signature: .init(
parameterClause: .init(
parameters: .init(
itemsBuilder: {}
)
)
),
body: .init(
statements: .init(
itemsBuilder: {

}
)
)
),
body: .init(
statements: .init(
itemsBuilder: {

}
)
)
)
}

// Classes that has mock data for each function
for funcData in functionsToMock {
Expand Down

0 comments on commit 2765523

Please sign in to comment.