Skip to content

Commit 1519d53

Browse files
authored
Refactor MacroArgumentValue, reorganize files, and add unit tests (#111)
1 parent ed08a0c commit 1519d53

18 files changed

+496
-87
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

Sources/MockingMacros/Macros/MockedMacro/MockedMacro+MacroArguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension MockedMacro {
3939
let argument = arguments.first(where: { argument in
4040
argument.label?.text == name
4141
}),
42-
let value = ArgumentValue(argument: argument)
42+
let value = try? ArgumentValue(argument: argument)
4343
else {
4444
return `default`
4545
}

Sources/MockingMacros/Models/MacroArguments/MacroArgumentValue.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ protocol MacroArgumentValue {
1414
///
1515
/// - Parameter argument: The argument syntax from which to parse the
1616
/// macro argument value.
17-
init?(argument: LabeledExprSyntax)
17+
/// - Throws: An error if unable to parse the macro argument value.
18+
init(argument: LabeledExprSyntax) throws
1819
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// MockCompilationCondition+ParsingError.swift
3+
//
4+
// Copyright © 2025 Fetch.
5+
//
6+
7+
import Foundation
8+
9+
extension MockCompilationCondition {
10+
11+
/// A parsing error generated by ``MockCompilationCondition``.
12+
enum ParsingError: CaseIterable, CustomStringConvertible, Error {
13+
14+
// MARK: Cases
15+
16+
/// An error indicating that ``MockCompilationCondition`` was unable to
17+
/// parse a valid instance from the provided macro argument.
18+
case unableToParseCompilationCondition
19+
20+
// MARK: Properties
21+
22+
/// The description of the error.
23+
var description: String {
24+
switch self {
25+
case .unableToParseCompilationCondition:
26+
"Unable to parse compilation condition."
27+
}
28+
}
29+
}
30+
}

Sources/MockingMacros/Models/MacroArguments/MockCompilationCondition.swift renamed to Sources/MockingMacros/Models/MacroArguments/MockCompilationCondition/MockCompilationCondition.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ enum MockCompilationCondition: RawRepresentable, Equatable, MacroArgumentValue {
6565
///
6666
/// - Parameter argument: The argument syntax from which to parse a
6767
/// compilation condition.
68-
init?(argument: LabeledExprSyntax) {
68+
/// - Throws: An error if a valid compilation condition cannot be parsed
69+
/// from the provided `argument`.
70+
init(argument: LabeledExprSyntax) throws {
6971
let (
7072
memberAccessExpression,
7173
arguments
@@ -103,7 +105,7 @@ enum MockCompilationCondition: RawRepresentable, Equatable, MacroArgumentValue {
103105
}
104106

105107
guard let memberAccessExpression else {
106-
return nil
108+
throw ParsingError.unableToParseCompilationCondition
107109
}
108110

109111
let declarationNameTokenKind = memberAccessExpression.declName.baseName.tokenKind
@@ -125,7 +127,7 @@ enum MockCompilationCondition: RawRepresentable, Equatable, MacroArgumentValue {
125127
{
126128
self = .custom(condition)
127129
} else {
128-
return nil
130+
throw ParsingError.unableToParseCompilationCondition
129131
}
130132
}
131133
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// MockSendableConformance+ParsingError.swift
3+
//
4+
// Copyright © 2025 Fetch.
5+
//
6+
7+
import Foundation
8+
9+
extension MockSendableConformance {
10+
11+
/// A parsing error generated by ``MockSendableConformance``.
12+
enum ParsingError: CaseIterable, CustomStringConvertible, Error {
13+
14+
// MARK: Cases
15+
16+
/// An error indicating that ``MockSendableConformance`` was unable to
17+
/// parse a valid instance from the provided macro argument.
18+
case unableToParseSendableConformance
19+
20+
// MARK: Properties
21+
22+
/// The description of the error.
23+
var description: String {
24+
switch self {
25+
case .unableToParseSendableConformance:
26+
"Unable to parse Sendable conformance."
27+
}
28+
}
29+
}
30+
}

Sources/MockingMacros/Models/MacroArguments/MockSendableConformance.swift renamed to Sources/MockingMacros/Models/MacroArguments/MockSendableConformance/MockSendableConformance.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ enum MockSendableConformance: String, MacroArgumentValue {
2121
///
2222
/// - Parameter argument: The argument syntax from which to parse a
2323
/// `Sendable` conformance.
24-
init?(argument: LabeledExprSyntax) {
24+
init(argument: LabeledExprSyntax) throws {
2525
guard
2626
let memberAccessExpression = argument.expression.as(
2727
MemberAccessExprSyntax.self
2828
),
29-
let identifier = memberAccessExpression.declName.baseName.identifier
29+
let identifier = memberAccessExpression.declName.baseName.identifier,
30+
let sendableConformance = MockSendableConformance(rawValue: identifier.name)
3031
else {
31-
return nil
32+
throw ParsingError.unableToParseSendableConformance
3233
}
3334

34-
self.init(rawValue: identifier.name)
35+
self = sendableConformance
3536
}
3637
}
File renamed without changes.

0 commit comments

Comments
 (0)