Skip to content

Commit

Permalink
Merge pull request #101 from Matejkob/force-unwrapped-variable
Browse files Browse the repository at this point in the history
Support variable declaration with forced unwrapped type
  • Loading branch information
Matejkob authored Jun 26, 2024
2 parents fa32cbb + 823194a commit a925e03
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions Examples/Sources/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ protocol ServiceProtocol {
var name: String { get }
var anyProtocol: any Codable { get set }
var secondName: String? { get }
var address: String! { get }
var added: () -> Void { get set }
var removed: (() -> Void)? { get set }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ struct VariablesImplementationFactory {
// Since the count of `bindings` is exactly 1, it is safe to force unwrap it.
let binding = protocolVariableDeclaration.bindings.first!

if binding.typeAnnotation?.type.is(OptionalTypeSyntax.self) == true {
/*
var name: String?
var name: String!
*/
if binding.typeAnnotation?.type.is(OptionalTypeSyntax.self) == true ||
binding.typeAnnotation?.type.is(ImplicitlyUnwrappedOptionalTypeSyntax.self) == true {
accessorRemovalVisitor.visit(protocolVariableDeclaration)
/*
var name: String
*/
} else {
try protocolVariableDeclarationWithGetterAndSetter(binding: binding)

Expand Down
15 changes: 15 additions & 0 deletions Tests/SpyableMacroTests/Factories/UT_SpyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ final class UT_SpyFactory: XCTestCase {
)
}

func testDeclarationForcedUnwrappedVariable() throws {
try assertProtocol(
withDeclaration: """
protocol ServiceProtocol {
var data: String! { get set }
}
""",
expectingClassDeclaration: """
class ServiceProtocolSpy: ServiceProtocol {
var data: String!
}
"""
)
}

func testDeclarationExistentialVariable() throws {
try assertProtocol(
withDeclaration: """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ final class UT_VariablesImplementationFactory: XCTestCase {
)
}

func testVariablesDeclarationsForcedUnwrapped() throws {
try assertProtocolVariable(
withVariableDeclaration: "var foo: String! { get }",
expectingVariableDeclaration: "var foo: String!"
)
}

func testVariablesDeclarationsClosure() throws {
try assertProtocolVariable(
withVariableDeclaration: "var completion: () -> Void { get }",
Expand Down

0 comments on commit a925e03

Please sign in to comment.