Skip to content

Commit

Permalink
Skip to add typeMap when the type may have globalActor
Browse files Browse the repository at this point in the history
  • Loading branch information
sidepelican committed Oct 28, 2024
1 parent 02f1109 commit 21d65e9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions Sources/MockoloFramework/Models/ParsedEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct ResolvedEntityContainer {

protocol EntityNode {
var nameText: String { get }
var mayHaveGlobalActor: Bool { get }
var accessLevel: String { get }
var attributesDescription: String { get }
var declType: DeclType { get }
Expand Down
9 changes: 6 additions & 3 deletions Sources/MockoloFramework/Operations/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ public func generate(sourceDirs: [String],
log("Took", t2-t1, level: .verbose)

let typeKeyList = [
parentMocks.compactMap {
$0.key.components(separatedBy: "Mock").first
parentMocks.compactMap { (key, value) -> String? in
if value.entityNode.mayHaveGlobalActor {
return nil
}
return key.components(separatedBy: "Mock").first
},
annotatedProtocolMap.map(\.key)
annotatedProtocolMap.filter { !$0.value.entityNode.mayHaveGlobalActor }.map(\.key)
]
.flatMap { $0 }
.map { typeName in
Expand Down
27 changes: 27 additions & 0 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ extension ProtocolDeclSyntax: EntityNode {
return name.text
}

var mayHaveGlobalActor: Bool {
return attributes.mayHaveGlobalActor
}

var accessLevel: String {
return self.modifiers.acl
}
Expand Down Expand Up @@ -306,6 +310,10 @@ extension ClassDeclSyntax: EntityNode {
return name.text
}

var mayHaveGlobalActor: Bool {
return attributes.mayHaveGlobalActor
}

var accessLevel: String {
return self.modifiers.acl
}
Expand Down Expand Up @@ -351,6 +359,25 @@ extension ClassDeclSyntax: EntityNode {
}
}

extension AttributeListSyntax {
fileprivate var mayHaveGlobalActor: Bool {
let wellKnownGlobalActor: Set<String> = [.mainActor]
return self.contains { element in
switch element {
case .attribute(let attribute):
return wellKnownGlobalActor.contains(attribute.attributeName.trimmedDescription)
case .ifConfigDecl(let ifConfig):
return ifConfig.clauses.contains { clause in
if case .attributes(let attributes) = clause.elements {
return attributes.mayHaveGlobalActor
}
return false
}
}
}
}
}

extension VariableDeclSyntax {
func models(with acl: String, declType: DeclType, metadata: AnnotationMetadata?, processed: Bool) -> [Model] {
// Detect whether it's static
Expand Down
1 change: 1 addition & 0 deletions Sources/MockoloFramework/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extension String {
static let name = "name"
static let sendable = "Sendable"
static let uncheckedSendable = "@unchecked Sendable"
static let mainActor = "MainActor"
static public let mockAnnotation = "@mockable"
static public let mockObservable = "@MockObservable"
static public let poundIf = "#if "
Expand Down
8 changes: 2 additions & 6 deletions Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ public final class SwiftType {
/// if "non-nil", type is non-optional
/// if "", type is String, with an empty string value
func defaultVal(with overrides: [String: String]? = nil, overrideKey: String = "", isInitParam: Bool = false) -> String? {

if let val = cachedDefaultVal {
return val
}
Expand Down Expand Up @@ -677,6 +676,7 @@ public final class SwiftType {
"TimeInterval": "0.0",
"NSTimeInterval": "0.0",
"PublishSubject": "PublishSubject()",
"Data": "Data()",
"Date": "Date()",
"NSDate": "NSDate()",
"CGRect": ".zero",
Expand All @@ -686,14 +686,10 @@ public final class SwiftType {
"UIColor": ".black",
"UIFont": ".systemFont(ofSize: 12)",
"UIImage": "UIImage()",
"UIView": "UIView(frame: .zero)",
"UIViewController": "UIViewController()",
"UICollectionView": "UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewLayout())",
"UICollectionViewLayout": "UICollectionViewLayout()",
"UIScrollView": "UIScrollView()",
"UIScrollViewKeyboardDismissMode": ".interactive",
"UIAccessibilityTraits": ".none",
"Void": "Void",
"()": "()",
"URL": "URL(fileURLWithPath: \"\")",
"NSURL": "NSURL(fileURLWithPath: \"\")",
"UUID": "UUID()",
Expand Down
5 changes: 5 additions & 0 deletions Tests/TestActor/ActorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ final class ActorTests: MockoloTestCase {
verify(srcContent: parentProtocolInheritsActor,
dstContent: parentProtocolInheritsActorMock)
}

func testGlobalActorProtocol() {
verify(srcContent: globalActorProtocol,
dstContent: globalActorProtocolMock)
}
}
45 changes: 45 additions & 0 deletions Tests/TestActor/FixtureGlobalActor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import MockoloFramework

let globalActorProtocol = """
/// \(String.mockAnnotation)
@MainActor protocol RootController: AnyObject {
var viewController: UIViewController { get }
}
/// \(String.mockAnnotation)
protocol RootBuildable {
func build() -> RootController
}
"""

let globalActorProtocolMock = """
class RootControllerMock: RootController {
init() { }
init(viewController: UIViewController) {
self._viewController = viewController
}
private var _viewController: UIViewController!
var viewController: UIViewController {
get { return _viewController }
set { _viewController = newValue }
}
}
class RootBuildableMock: RootBuildable {
init() { }
private(set) var buildCallCount = 0
var buildHandler: (() -> (RootController))?
func build() -> RootController {
buildCallCount += 1
if let buildHandler = buildHandler {
return buildHandler()
}
fatalError("buildHandler returns can't have a default value thus its handler must be set")
}
}
"""

0 comments on commit 21d65e9

Please sign in to comment.