-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Refactor FXIOS-7301 - Remove 1 closure_body_length violation from RustAutofillTests.swift #23006
base: main
Are you sure you want to change the base?
Refactor FXIOS-7301 - Remove 1 closure_body_length violation from RustAutofillTests.swift #23006
Conversation
@@ -195,12 +195,8 @@ class RustAutofillTests: XCTestCase { | |||
expectationGetCard.fulfill() | |||
|
|||
let expectedCcExpYear = Int64(2028) | |||
let updatedCreditCard = UnencryptedCreditCardFields(ccName: creditCard!.ccName, |
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.
Looks good to me, i would only refactor the force cast with a conditional cast, since there is already the plan to get this pattern removed. Let me know what you think. Thanks again 🎉
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.
This makes sense. I've made the updates and used guard let
for unwrapping, which works well here. I chose this approach because I found three variables in this test that were using force unwrap: creditCard
, card
, and updatedCardVal
.
I liked this approach because it avoids excessive indentation and keeps the code readable. I also noticed similar force unwrapping in other tests methods but haven't updated those yet.
What do you think of this adjustment?
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.
I like it with guards, but in the test area there is a better thing.
I would do:
let card = try XCTUnwrap(card)
So whit this approach you can remove all the guards and the XCTAssertNotNil
since they are incorporated in the XCTUnwrap
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.
Interesting! I noticed some examples in the project that use XCTUnwrap
, but they're applied in standard functions rather than closures. When I tried to apply it to the creditCard
variable here, I encountered this error:
Invalid conversion from throwing function of type '(CreditCard?, (any Error)?) throws -> Void' to non-throwing function type '(CreditCard?, (any Error)?) -> Void'
The same applies to the card
variable. To make this work, I would need to refactor the code further to ensure the closere is throwable, but I'm unsure if this is feasible. Another approach would be to use a do catch
block with XCTUnwrap
, though this would introduce additional indentation, similar to the if let
approach.
What do you think would be the best way to proceed?
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.
I think with the guard let we are loosing the information when a test is failing, i'd go with the do catch
so we can fail the test in the catch block by passing the error message, what do you think ?
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.
I've implemented it, but this increases the number of lines due to the three new do catch
blocks. To address this, I've extracted the final assertions into separated functions to keep the main closure under 30 lines and ensures the compliance with the closure_body_length
rule.
This might not be the best solution, but without extracting the final assertions, the main closure would still be around 40 lines.
Let me know your thoughts on this approach.
8fd0926
to
e6ed4a2
Compare
With the if let unwrap, this assertion is unecessary.
This reverts commit a6bab0b.
The assertion was beign performed with the name of the previously created variable - see XCTAssertNil(err).
e6ed4a2
to
b8fdc32
Compare
📜 Tickets
Jira ticket
Github issue
💡 Description
This PR removes 1
closure_body_length
violation from theRustAutofillTests.swift
file by extracting the creation of theUnencryptedCreditCardFields
object to a new function.This PR is part of a series of PRs described here #16442 (comment).
📝 Checklist
You have to check all boxes before merging
@Mergifyio backport release/v120
)