Skip to content
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 conformed protocol #133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/SpyableMacro/Factories/SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ struct SpyFactory {
genericParameterClause: genericParameterClause,
inheritanceClause: InheritanceClauseSyntax {
InheritedTypeSyntax(
type: IdentifierTypeSyntax(name: protocolDeclaration.name)
type: TypeSyntax(stringLiteral: protocolDeclaration.name.text)
)
InheritedTypeSyntax(
type: TypeSyntax(stringLiteral: "@unchecked Sendable")
)
},
memberBlockBuilder: {
Expand Down
94 changes: 72 additions & 22 deletions Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class UT_SpyFactory: XCTestCase {
try assertProtocol(
withDeclaration: "protocol Foo {}",
expectingClassDeclaration: """
class FooSpy: Foo {
class FooSpy: Foo, @unchecked Sendable {
init() {
}
}
Expand All @@ -25,7 +25,57 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceSpy: Service {
class ServiceSpy: Service, @unchecked Sendable {
init() {
}
var fetchCallsCount = 0
var fetchCalled: Bool {
return fetchCallsCount > 0
}
var fetchClosure: (() -> Void)?
func fetch() {
fetchCallsCount += 1
fetchClosure?()
}
}
"""
)
}

func testDeclarationWithSendable() throws {
try assertProtocol(
withDeclaration: """
protocol Service: Sendable {
func fetch()
}
""",
expectingClassDeclaration: """
class ServiceSpy: Service, @unchecked Sendable {
init() {
}
var fetchCallsCount = 0
var fetchCalled: Bool {
return fetchCallsCount > 0
}
var fetchClosure: (() -> Void)?
func fetch() {
fetchCallsCount += 1
fetchClosure?()
}
}
"""
)
}

func testDeclarationWithUncheckedSendable() throws {
try assertProtocol(
withDeclaration: """
protocol Service: @unchecked Sendable {
func fetch()
}
""",
expectingClassDeclaration: """
class ServiceSpy: Service, @unchecked Sendable {
init() {
}
var fetchCallsCount = 0
Expand All @@ -50,7 +100,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooTextCountCallsCount = 0
Expand Down Expand Up @@ -79,7 +129,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooModelCallsCount = 0
Expand Down Expand Up @@ -108,7 +158,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooModelCallsCount = 0
Expand Down Expand Up @@ -137,7 +187,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooModelCallsCount = 0
Expand Down Expand Up @@ -166,7 +216,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooModelCallsCount = 0
Expand Down Expand Up @@ -202,7 +252,7 @@ final class UT_SpyFactory: XCTestCase {
assertBuildResult(
result,
"""
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooTextValueCallsCount = 0
Expand Down Expand Up @@ -236,7 +286,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooActionCallsCount = 0
Expand Down Expand Up @@ -265,7 +315,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ViewModelProtocolSpy: ViewModelProtocol {
class ViewModelProtocolSpy: ViewModelProtocol, @unchecked Sendable {
init() {
}
var fooActionCallsCount = 0
Expand All @@ -290,7 +340,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class BarSpy: Bar {
class BarSpy: Bar, @unchecked Sendable {
init() {
}
var printCallsCount = 0
Expand Down Expand Up @@ -320,7 +370,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var fooTextCountCallsCount = 0
Expand Down Expand Up @@ -354,7 +404,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var fooCallsCount = 0
Expand Down Expand Up @@ -392,7 +442,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var fooCallsCount = 0
Expand Down Expand Up @@ -422,7 +472,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var fooCallsCount = 0
Expand Down Expand Up @@ -452,7 +502,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var data: Data {
Expand All @@ -477,7 +527,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var data: Data?
Expand All @@ -494,7 +544,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var data: String!
Expand All @@ -511,7 +561,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var data: any Codable {
Expand All @@ -536,7 +586,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
init() {
}
var completion: () -> Void {
Expand All @@ -563,7 +613,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class FooSpy<Key: Hashable>: Foo {
class FooSpy<Key: Hashable>: Foo, @unchecked Sendable {
init() {
}
}
Expand All @@ -580,7 +630,7 @@ final class UT_SpyFactory: XCTestCase {
}
""",
expectingClassDeclaration: """
class FooSpy<Key: Hashable, Value>: Foo {
class FooSpy<Key: Hashable, Value>: Foo, @unchecked Sendable {
init() {
}
}
Expand Down
22 changes: 11 additions & 11 deletions Tests/SpyableMacroTests/Macro/UT_SpyableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDeclaration)

public class ServiceProtocolSpy: ServiceProtocol {
public class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
public init() {
}
public var name: String {
Expand Down Expand Up @@ -223,7 +223,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDeclaration)

class MyProtocolSpy: MyProtocol {
class MyProtocolSpy: MyProtocol, @unchecked Sendable {
init() {
}
}
Expand All @@ -244,7 +244,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDeclaration)

class MyProtocolSpy: MyProtocol {
class MyProtocolSpy: MyProtocol, @unchecked Sendable {
init() {
}
}
Expand All @@ -266,7 +266,7 @@ final class UT_SpyableMacro: XCTestCase {
\(protocolDeclaration)

#if CUSTOM
class MyProtocolSpy: MyProtocol {
class MyProtocolSpy: MyProtocol, @unchecked Sendable {
init() {
}
}
Expand All @@ -293,7 +293,7 @@ final class UT_SpyableMacro: XCTestCase {
\(protocolDeclaration)

#if CUSTOM
class MyProtocolSpy: MyProtocol {
class MyProtocolSpy: MyProtocol, @unchecked Sendable {
init() {
}
}
Expand All @@ -315,7 +315,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDeclaration)

class MyProtocolSpy: MyProtocol {
class MyProtocolSpy: MyProtocol, @unchecked Sendable {
init() {
}
}
Expand Down Expand Up @@ -353,7 +353,7 @@ final class UT_SpyableMacro: XCTestCase {
let myCustomFlag = "DEBUG"
\(protocolDeclaration)

class MyProtocolSpy: MyProtocol {
class MyProtocolSpy: MyProtocol, @unchecked Sendable {
init() {
}
}
Expand Down Expand Up @@ -404,7 +404,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDefinition)

\(mapping.spyClassAccessLevel) class ServiceProtocolSpy: ServiceProtocol {
\(mapping.spyClassAccessLevel) class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
\(mapping.spyClassAccessLevel) init() {
}
\(mapping.spyClassAccessLevel)
Expand Down Expand Up @@ -458,7 +458,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDefinition)

\(mapping.spyClassAccessLevel) class ServiceProtocolSpy: ServiceProtocol {
\(mapping.spyClassAccessLevel) class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
\(mapping.spyClassAccessLevel) init() {
}
\(mapping.spyClassAccessLevel)
Expand Down Expand Up @@ -503,7 +503,7 @@ final class UT_SpyableMacro: XCTestCase {

\(protocolDeclaration)

fileprivate class ServiceProtocolSpy: ServiceProtocol {
fileprivate class ServiceProtocolSpy: ServiceProtocol, @unchecked Sendable {
fileprivate init() {
}
fileprivate
Expand Down Expand Up @@ -546,7 +546,7 @@ final class UT_SpyableMacro: XCTestCase {
\(protocolDeclaration)

#if CUSTOM_FLAG
package class MyProtocolSpy: MyProtocol {
package class MyProtocolSpy: MyProtocol, @unchecked Sendable {
package init() {
}
}
Expand Down
Loading