|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftRefactor |
| 14 | +import SwiftSyntax |
| 15 | +import SwiftSyntaxBuilder |
| 16 | + |
| 17 | +import XCTest |
| 18 | +import _SwiftSyntaxTestSupport |
| 19 | + |
| 20 | +final class MigrateToNewIfLetSyntaxTest: XCTestCase { |
| 21 | + func testRefactoring() throws { |
| 22 | + let baselineSyntax: StmtSyntax = """ |
| 23 | + if let x = x {} |
| 24 | + """ |
| 25 | + |
| 26 | + let expectedSyntax: StmtSyntax = """ |
| 27 | + if let x {} |
| 28 | + """ |
| 29 | + |
| 30 | + let baseline = try XCTUnwrap(baselineSyntax.as(IfStmtSyntax.self)) |
| 31 | + let expected = try XCTUnwrap(expectedSyntax.as(IfStmtSyntax.self)) |
| 32 | + |
| 33 | + let refactored = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 34 | + |
| 35 | + AssertStringsEqualWithDiff(expected.description, refactored.description) |
| 36 | + } |
| 37 | + |
| 38 | + func testIdempotence() throws { |
| 39 | + let baselineSyntax: StmtSyntax = """ |
| 40 | + if let x = x {} |
| 41 | + """ |
| 42 | + |
| 43 | + let baseline = try XCTUnwrap(baselineSyntax.as(IfStmtSyntax.self)) |
| 44 | + |
| 45 | + let refactored = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 46 | + let refactoredAgain = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 47 | + |
| 48 | + AssertStringsEqualWithDiff(refactored.description, refactoredAgain.description) |
| 49 | + } |
| 50 | + |
| 51 | + func testMultiBinding() throws { |
| 52 | + let baselineSyntax: StmtSyntax = """ |
| 53 | + if let x = x, var y = y, let z = z {} |
| 54 | + """ |
| 55 | + |
| 56 | + let expectedSyntax: StmtSyntax = """ |
| 57 | + if let x, var y, let z {} |
| 58 | + """ |
| 59 | + |
| 60 | + let baseline = try XCTUnwrap(baselineSyntax.as(IfStmtSyntax.self)) |
| 61 | + let expected = try XCTUnwrap(expectedSyntax.as(IfStmtSyntax.self)) |
| 62 | + |
| 63 | + let refactored = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 64 | + |
| 65 | + AssertStringsEqualWithDiff(expected.description, refactored.description) |
| 66 | + } |
| 67 | + |
| 68 | + func testMixedBinding() throws { |
| 69 | + let baselineSyntax: StmtSyntax = """ |
| 70 | + if let x = x, var y = x, let z = y.w {} |
| 71 | + """ |
| 72 | + |
| 73 | + let expectedSyntax: StmtSyntax = """ |
| 74 | + if let x, var y = x, let z = y.w {} |
| 75 | + """ |
| 76 | + |
| 77 | + let baseline = try XCTUnwrap(baselineSyntax.as(IfStmtSyntax.self)) |
| 78 | + let expected = try XCTUnwrap(expectedSyntax.as(IfStmtSyntax.self)) |
| 79 | + |
| 80 | + let refactored = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 81 | + |
| 82 | + AssertStringsEqualWithDiff(expected.description, refactored.description) |
| 83 | + } |
| 84 | + |
| 85 | + func testConditions() throws { |
| 86 | + let baselineSyntax: StmtSyntax = """ |
| 87 | + if let x = x + 1, x == x, !x {} |
| 88 | + """ |
| 89 | + |
| 90 | + let expectedSyntax: StmtSyntax = """ |
| 91 | + if let x = x + 1, x == x, !x {} |
| 92 | + """ |
| 93 | + |
| 94 | + let baseline = try XCTUnwrap(baselineSyntax.as(IfStmtSyntax.self)) |
| 95 | + let expected = try XCTUnwrap(expectedSyntax.as(IfStmtSyntax.self)) |
| 96 | + |
| 97 | + let refactored = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 98 | + |
| 99 | + AssertStringsEqualWithDiff(expected.description, refactored.description) |
| 100 | + } |
| 101 | + |
| 102 | + func testWhitespaceNormalization() throws { |
| 103 | + let baselineSyntax: StmtSyntax = """ |
| 104 | + if let x = x , let y = y {} |
| 105 | + """ |
| 106 | + |
| 107 | + let expectedSyntax: StmtSyntax = """ |
| 108 | + if let x, let y {} |
| 109 | + """ |
| 110 | + |
| 111 | + let baseline = try XCTUnwrap(baselineSyntax.as(IfStmtSyntax.self)) |
| 112 | + let expected = try XCTUnwrap(expectedSyntax.as(IfStmtSyntax.self)) |
| 113 | + |
| 114 | + let refactored = try XCTUnwrap(MigrateToNewIfLetSyntax.refactor(syntax: baseline)) |
| 115 | + |
| 116 | + AssertStringsEqualWithDiff(expected.description, refactored.description) |
| 117 | + } |
| 118 | +} |
0 commit comments