-
Notifications
You must be signed in to change notification settings - Fork 85
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
Generate mock method with Typed Throw #262
base: master
Are you sure you want to change the base?
Changes from 4 commits
199c2ef
aa0e097
2e7259b
725c67d
7f7028a
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 |
---|---|---|
|
@@ -108,10 +108,10 @@ extension String { | |
/// | ||
""" | ||
|
||
|
||
var hasThrowsOrRethrows: Bool { | ||
return components(separatedBy: .whitespaces).contains { component in | ||
return component == .throws || component == .rethrows | ||
let hasTypedThrow = hasPrefix(String.throws.withLeftParen) && hasSuffix(")") | ||
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. We can have trailing trivia to func f() throws (any Error) {
} (However, it will work because the 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. It may be the time to extract this from the |
||
return component == .throws || hasTypedThrow || component == .rethrows | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -510,6 +510,25 @@ public final class `Type` { | |
return mutableArg | ||
} | ||
|
||
/// Parse throws clause from suffix and return typed-throw's type. | ||
/// | ||
/// - Returns: if typed-throw is used, returns its concrete type, unless returns nil. | ||
static func extractTypedThrow( | ||
suffix: String | ||
) -> String? { | ||
return suffix | ||
.components(separatedBy: .whitespaces) | ||
.compactMap { clause in | ||
guard let prefixRange = clause.range(of: "\(String.throws)(") else { | ||
return nil | ||
} | ||
let endIndex = clause.dropLast().endIndex | ||
return String( | ||
clause[prefixRange.upperBound..<endIndex] | ||
) | ||
} | ||
.first | ||
} | ||
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. I am not sure if splitting by As far as I investigated,
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.
The Swift's syntax has become more than complicated enough to look these up from 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 your information.
I did not know
Absolutely. I felt like |
||
|
||
static func toClosureType(with params: [Type], typeParams: [String], suffix: String, returnType: Type, encloser: String) -> Type { | ||
|
||
|
@@ -558,9 +577,18 @@ public final class `Type` { | |
displayableReturnType = "(\(displayableReturnType))" | ||
} | ||
|
||
let hasThrowsOrRethrows = suffix.hasThrowsOrRethrows | ||
let typedThrowTypeName = extractTypedThrow(suffix: suffix) | ||
|
||
let thrownSuffix: String = if let typedThrowTypeName { | ||
"\(String.throws)(\(typedThrowTypeName))" | ||
} else { | ||
String.throws | ||
} | ||
|
||
let suffixStr = [ | ||
suffix.hasAsync ? String.async + " " : nil, | ||
suffix.hasThrowsOrRethrows ? String.throws + " " : nil, | ||
hasThrowsOrRethrows ? thrownSuffix + " " : nil, | ||
].compactMap { $0 }.joined() | ||
|
||
let typeStr = "((\(displayableParamStr)) \(suffixStr)-> \(displayableReturnType))?" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,15 @@ | ||
import MockoloFramework | ||
|
||
|
||
let funcThrow2 = """ | ||
import Foundation | ||
|
||
/// \(String.mockAnnotation) | ||
protocol FuncThrow { | ||
func update<T>(arg1: T, arg2: () throws -> T) rethrows -> T | ||
func update<T, U>(arg1: T, arg2: @escaping (U) throws -> ()) throws -> ((T) -> (U)) | ||
} | ||
""" | ||
|
||
Comment on lines
-4
to
-13
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. FYI This test-fixture is not used and is covered by |
||
let funcThrow = """ | ||
import Foundation | ||
|
||
/// \(String.mockAnnotation) | ||
protocol FuncThrow { | ||
func f1(arg: Int) throws -> String | ||
func f2(arg: Int) throws | ||
func f3(arg: Int) throws(SomeError) | ||
func f4(arg: Int) throws(SomeError) -> String | ||
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. Could you add other possible patterns?
|
||
func g1(arg: (Int) throws -> ()) | ||
throws -> String | ||
func g2(arg: (Int) throws -> ()) throws | ||
|
@@ -57,6 +49,26 @@ class FuncThrowMock: FuncThrow { | |
|
||
} | ||
|
||
private(set) var f3CallCount = 0 | ||
var f3Handler: ((Int) throws(SomeError) -> ())? | ||
func f3(arg: Int) throws(SomeError) { | ||
f3CallCount += 1 | ||
if let f3Handler = f3Handler { | ||
try f3Handler(arg) | ||
} | ||
|
||
} | ||
|
||
private(set) var f4CallCount = 0 | ||
var f4Handler: ((Int) throws(SomeError) -> (String))? | ||
func f4(arg: Int) throws(SomeError) -> String { | ||
f4CallCount += 1 | ||
if let f4Handler = f4Handler { | ||
return try f4Handler(arg) | ||
} | ||
return "" | ||
} | ||
|
||
private(set) var g1CallCount = 0 | ||
var g1Handler: (((Int) throws -> ()) throws -> (String))? | ||
func g1(arg: (Int) throws -> ()) throws -> String { | ||
|
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.
to use
ThrowsClauseSyntax
, I updated swift-syntax.