@@ -111,7 +111,7 @@ final class NoForceUnwrapInTestsTests: XCTestCase {
111111 class TestCase: XCTestCase {
112112 func test_functionCall() throws {
113113 someFunction(try XCTUnwrap(myOptional), try XCTUnwrap(anotherOptional))
114- XCTAssertEqual(try XCTUnwrap( result?.property) , " expected " )
114+ XCTAssertEqual(result?.property, " expected " )
115115 }
116116 }
117117 """
@@ -140,7 +140,7 @@ final class NoForceUnwrapInTestsTests: XCTestCase {
140140 func test_ifStatement() throws {
141141 if
142142 try XCTUnwrap(foo?.bar()),
143- try XCTUnwrap( myOptional?.value) == someValue
143+ myOptional?.value == someValue
144144 {
145145 // do something
146146 }
@@ -198,7 +198,7 @@ final class NoForceUnwrapInTestsTests: XCTestCase {
198198 func test_guardStatement() throws {
199199 guard
200200 try XCTUnwrap(foo?.bar()),
201- try XCTUnwrap( myOptional?.value) == someValue
201+ myOptional?.value == someValue
202202 else {
203203 return
204204 }
@@ -420,7 +420,7 @@ final class NoForceUnwrapInTestsTests: XCTestCase {
420420 class TestCase: XCTestCase {
421421 func test_complexExpression() throws {
422422 XCTAssertEqual(
423- try XCTUnwrap( myDictionary[ " key " ]?.processedValue(with: try XCTUnwrap(parameter) )),
423+ myDictionary[ " key " ]?.processedValue(with: try XCTUnwrap(parameter)),
424424 expectedResult
425425 )
426426 }
@@ -506,7 +506,7 @@ final class NoForceUnwrapInTestsTests: XCTestCase {
506506
507507 class TestCase: XCTestCase {
508508 func test_forceCasts() throws {
509- XCTAssertEqual(try XCTUnwrap( route.query as? [String: String]) , [ " a " : " b " ])
509+ XCTAssertEqual(route.query as? [String: String], [ " a " : " b " ])
510510 XCTAssert(try XCTUnwrap((foo as? Bar)?.baaz))
511511 XCTAssert(try XCTUnwrap((foo as? Bar)?.baaz))
512512 }
@@ -659,4 +659,124 @@ final class NoForceUnwrapInTestsTests: XCTestCase {
659659 """
660660 testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
661661 }
662+
663+ func testXCTAssertEqual_KeepsForceUnwrapsAsOptionalChaining( ) throws {
664+ let input = """
665+ import XCTest
666+
667+ class TestCase: XCTestCase {
668+ func test_something() {
669+ XCTAssertEqual(foo!.bar, baaz!.quux)
670+ }
671+ }
672+ """
673+ let output = """
674+ import XCTest
675+
676+ class TestCase: XCTestCase {
677+ func test_something() {
678+ XCTAssertEqual(foo?.bar, baaz?.quux)
679+ }
680+ }
681+ """
682+ testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
683+ }
684+
685+ func testXCTAssertNil_KeepsForceUnwrapsAsOptionalChaining( ) throws {
686+ let input = """
687+ import XCTest
688+
689+ class TestCase: XCTestCase {
690+ func test_something() {
691+ XCTAssertNil(foo!.bar)
692+ }
693+ }
694+ """
695+ let output = """
696+ import XCTest
697+
698+ class TestCase: XCTestCase {
699+ func test_something() {
700+ XCTAssertNil(foo?.bar)
701+ }
702+ }
703+ """
704+ testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
705+ }
706+
707+ func testEqualityComparison_KeepsForceUnwrapsAsOptionalChaining( ) throws {
708+ let input = """
709+ import Testing
710+
711+ @Test func something() {
712+ #expect(foo!.bar == baaz)
713+ }
714+ """
715+ let output = """
716+ import Testing
717+
718+ @Test func something() {
719+ #expect(foo?.bar == baaz)
720+ }
721+ """
722+ testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
723+ }
724+
725+ func testEqualityComparisonWithNil_KeepsForceUnwrapsAsOptionalChaining( ) throws {
726+ let input = """
727+ import Testing
728+
729+ @Test func something() {
730+ #expect(foo!.bar == nil)
731+ }
732+ """
733+ let output = """
734+ import Testing
735+
736+ @Test func something() {
737+ #expect(foo?.bar == nil)
738+ }
739+ """
740+ testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
741+ }
742+
743+ func testXCTAssertEqualWithAccuracy_RequiresXCTUnwrap( ) throws {
744+ let input = """
745+ import XCTest
746+
747+ class TestCase: XCTestCase {
748+ func test_something() {
749+ XCTAssertEqual(foo!.value, 3.14, accuracy: 0.01)
750+ }
751+ }
752+ """
753+ let output = """
754+ import XCTest
755+
756+ class TestCase: XCTestCase {
757+ func test_something() throws {
758+ XCTAssertEqual(try XCTUnwrap(foo?.value), 3.14, accuracy: 0.01)
759+ }
760+ }
761+ """
762+ testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
763+ }
764+
765+ func testForceUnwrapWithOperatorFollowing_RequiresXCTUnwrap( ) throws {
766+ let input = """
767+ import Testing
768+
769+ @Test func something() {
770+ #expect(foo!.bar + 2 == 3)
771+ }
772+ """
773+ let output = """
774+ import Testing
775+
776+ @Test func something() throws {
777+ #expect(try #require(foo?.bar) + 2 == 3)
778+ }
779+ """
780+ testFormatting ( for: input, output, rule: . noForceUnwrapInTests)
781+ }
662782}
0 commit comments