-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Sources/SpyableMacro/Factories/VariablePrefixFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
/// The `VariablePrefixFactory` struct is responsible for creating a unique textual representation | ||
/// for a given function declaration. This representation can be used as a prefix when naming variables | ||
/// associated with that function. | ||
/// | ||
/// The factory constructs the representation by combining the function name with the first names of its parameters. | ||
/// | ||
/// For example, given the function declaration: | ||
/// ```swift | ||
/// func display(text: String, color: Color) | ||
/// ``` | ||
/// the `VariablePrefixFactory` generates the following text: | ||
/// ``` | ||
/// displayTextColor | ||
/// ``` | ||
/// It will capitalize the first letter of each parameter name and append it to the function name. | ||
/// Please note that if a parameter is underscored (anonymous), it's ignored. | ||
struct VariablePrefixFactory { | ||
func text(for functionDeclaration: FunctionDeclSyntax) -> String { | ||
var parts: [String] = [functionDeclaration.identifier.text] | ||
|
||
let parameterList = functionDeclaration.signature.input.parameterList | ||
|
||
let parameters = parameterList | ||
.map { $0.firstName.text } | ||
.filter { $0 != "_" } | ||
.map { $0.capitalizingFirstLetter() } | ||
|
||
parts.append(contentsOf: parameters) | ||
|
||
return parts.joined() | ||
} | ||
} | ||
|
||
extension String { | ||
fileprivate func capitalizingFirstLetter() -> String { | ||
return prefix(1).uppercased() + dropFirst() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Tests/SpyableMacroTests/Factories/UT_VariablePrefixFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import XCTest | ||
@testable import SpyableMacro | ||
import SwiftSyntax | ||
|
||
final class UT_VariablePrefixFactory: XCTestCase { | ||
func testTextFunctionWithoutArguments() throws { | ||
let declaration: DeclSyntax = "func foo() -> String" | ||
let functionDeclaration = try XCTUnwrap(FunctionDeclSyntax(declaration)) | ||
|
||
let result = VariablePrefixFactory().text(for: functionDeclaration) | ||
|
||
XCTAssertEqual(result, "foo") | ||
} | ||
|
||
func testTextFunctionWithSingleArgument() throws { | ||
let declaration: DeclSyntax = "func foo(text: String) -> String" | ||
let functionDeclaration = try XCTUnwrap(FunctionDeclSyntax(declaration)) | ||
|
||
let result = VariablePrefixFactory().text(for: functionDeclaration) | ||
|
||
XCTAssertEqual(result, "fooText") | ||
} | ||
|
||
func testTextFunctionWithSingleArgumentTwoNames() throws { | ||
let declaration: DeclSyntax = "func foo(generated text: String) -> String" | ||
let functionDeclaration = try XCTUnwrap(FunctionDeclSyntax(declaration)) | ||
|
||
let result = VariablePrefixFactory().text(for: functionDeclaration) | ||
|
||
XCTAssertEqual(result, "fooGenerated") | ||
} | ||
|
||
func testTextFunctionWithSingleArgumentOnlySecondName() throws { | ||
let declaration: DeclSyntax = "func foo(_ text: String) -> String" | ||
let functionDeclaration = try XCTUnwrap(FunctionDeclSyntax(declaration)) | ||
|
||
let result = VariablePrefixFactory().text(for: functionDeclaration) | ||
|
||
XCTAssertEqual(result, "foo") | ||
} | ||
|
||
func testTextFunctionWithMultiArguments() throws { | ||
let declaration: DeclSyntax = """ | ||
func foo(text1 text2: String, _ count2: Int, product1 product2: (name: String, price: Decimal)) -> String | ||
""" | ||
let functionDeclaration = try XCTUnwrap(FunctionDeclSyntax(declaration)) | ||
|
||
let result = VariablePrefixFactory().text(for: functionDeclaration) | ||
|
||
XCTAssertEqual(result, "fooText1Product1") | ||
} | ||
} |
Oops, something went wrong.