-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Sendable
compliant protocols and classes
#254
Changes from 1 commit
2a79ad9
7bee97d
425498c
30cd46c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,18 +23,20 @@ import Foundation | |
/// @param protocolMap Used to look up the current entity and its inheritance types | ||
/// @param inheritanceMap Used to look up inherited types if not contained in protocolMap | ||
/// @returns a list of models representing sub-entities of the current entity, a list of models processed in dependent mock files if exists, | ||
/// cumulated attributes, and a map of filepaths and file contents (used for import lines lookup later). | ||
/// cumulated attributes, cumulated inherited types, and a map of filepaths and file contents (used for import lines lookup later). | ||
func lookupEntities(key: String, | ||
declType: DeclType, | ||
protocolMap: [String: Entity], | ||
inheritanceMap: [String: Entity]) -> ([Model], [Model], [String], [String], [(String, Data, Int64)]) { | ||
inheritanceMap: [String: Entity]) -> ([Model], [Model], [String], Set<String>, [String], [(String, Data, Int64)]) { | ||
|
||
// Used to keep track of types to be mocked | ||
var models = [Model]() | ||
// Used to keep track of types that were already mocked | ||
var processedModels = [Model]() | ||
// Gather attributes declared in current or parent protocols | ||
var attributes = [String]() | ||
// Gather inherited types declared in current or parent protocols | ||
var inheritedTypes = Set<String>() | ||
// Gather filepaths and contents used for imports | ||
var pathToContents = [(String, Data, Int64)]() | ||
// Gather filepaths used for imports | ||
|
@@ -47,6 +49,7 @@ func lookupEntities(key: String, | |
if !current.isProcessed { | ||
attributes.append(contentsOf: sub.attributes) | ||
} | ||
inheritedTypes = inheritedTypes.union(current.entityNode.inheritedTypes) | ||
if let data = current.data { | ||
pathToContents.append((current.filepath, data, current.entityNode.offset)) | ||
} | ||
|
@@ -57,10 +60,11 @@ func lookupEntities(key: String, | |
// If the protocol inherits other protocols, look up their entities as well. | ||
for parent in current.entityNode.inheritedTypes { | ||
if parent != .class, parent != .anyType, parent != .anyObject { | ||
let (parentModels, parentProcessedModels, parentAttributes, parentPaths, parentPathToContents) = lookupEntities(key: parent, declType: declType, protocolMap: protocolMap, inheritanceMap: inheritanceMap) | ||
let (parentModels, parentProcessedModels, parentAttributes, parentInheritedTypes, parentPaths, parentPathToContents) = lookupEntities(key: parent, declType: declType, protocolMap: protocolMap, inheritanceMap: inheritanceMap) | ||
models.append(contentsOf: parentModels) | ||
processedModels.append(contentsOf: parentProcessedModels) | ||
attributes.append(contentsOf: parentAttributes) | ||
inheritedTypes = inheritedTypes.union(parentInheritedTypes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review! |
||
paths.append(contentsOf: parentPaths) | ||
pathToContents.append(contentsOf:parentPathToContents) | ||
} | ||
|
@@ -79,7 +83,7 @@ func lookupEntities(key: String, | |
paths.append(parentMock.filepath) | ||
} | ||
|
||
return (models, processedModels, attributes, paths, pathToContents) | ||
return (models, processedModels, attributes, inheritedTypes, paths, pathToContents) | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import MockoloFramework | ||
|
||
let sendableProtocol = """ | ||
import Foundation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused import. |
||
|
||
/// \(String.mockAnnotation) | ||
public protocol SendableProtocol: Sendable { | ||
func update(arg: Int) -> String | ||
} | ||
""" | ||
|
||
let sendableProtocolMock = """ | ||
|
||
import Foundation | ||
|
||
public class SendableProtocolMock: SendableProtocol, @unchecked Sendable { | ||
public init() { } | ||
|
||
|
||
public private(set) var updateCallCount = 0 | ||
public var updateHandler: ((Int) -> (String))? | ||
public func update(arg: Int) -> String { | ||
updateCallCount += 1 | ||
if let updateHandler = updateHandler { | ||
return updateHandler(arg) | ||
} | ||
return "" | ||
} | ||
} | ||
|
||
""" | ||
|
||
let uncheckedSendableClass = """ | ||
import Foundation | ||
|
||
/// \(String.mockAnnotation) | ||
public class UncheckedSendableClass: @unchecked Sendable { | ||
func update(arg: Int) -> String | ||
} | ||
""" | ||
|
||
let uncheckedSendableClassMock = """ | ||
|
||
import Foundation | ||
|
||
public class UncheckedSendableClassMock: UncheckedSendableClass, @unchecked Sendable { | ||
public init() { } | ||
|
||
|
||
private(set) var updateCallCount = 0 | ||
var updateHandler: ((Int) -> (String))? | ||
override func update(arg: Int) -> String { | ||
updateCallCount += 1 | ||
if let updateHandler = updateHandler { | ||
return updateHandler(arg) | ||
} | ||
return "" | ||
} | ||
} | ||
|
||
""" | ||
|
||
let confirmedSendableProtocol = """ | ||
import Foundation | ||
|
||
public protocol SendableSendable: Sendable { | ||
func update(arg: Int) -> String | ||
} | ||
|
||
/// \(String.mockAnnotation) | ||
public protocol ConfirmedSendableProtocol: SendableSendable { | ||
} | ||
""" | ||
|
||
let confirmedSendableProtocolMock = """ | ||
|
||
import Foundation | ||
|
||
public class ConfirmedSendableProtocolMock: ConfirmedSendableProtocol, @unchecked Sendable { | ||
public init() { } | ||
|
||
|
||
public private(set) var updateCallCount = 0 | ||
public var updateHandler: ((Int) -> (String))? | ||
public func update(arg: Int) -> String { | ||
updateCallCount += 1 | ||
if let updateHandler = updateHandler { | ||
return updateHandler(arg) | ||
} | ||
return "" | ||
} | ||
} | ||
|
||
""" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused import. |
||
|
||
|
||
class SendableTests: MockoloTestCase { | ||
func testSendableProtocol() { | ||
verify(srcContent: sendableProtocol, | ||
dstContent: sendableProtocolMock) | ||
} | ||
|
||
func testUncheckedSendableClass() { | ||
verify(srcContent: uncheckedSendableClass, | ||
dstContent: uncheckedSendableClassMock, | ||
declType: .classType) | ||
} | ||
|
||
func testConfirmingSendableProtocol() { | ||
verify(srcContent: confirmedSendableProtocol, | ||
dstContent: confirmedSendableProtocolMock) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this can be more beautify like this: