Skip to content

Commit 841f337

Browse files
committed
Test coverage for imports wrapped in conditional.
These don't receive any special treatment, and instead are sorted as code blocks. That's done intentionally because the code block in a conditional can be complex, containing multiple imports or even non-import code (e.g. a typealias). Sorting them in with other imports would only be a sepcial case for the simplest conditional blocks. I considered treating these like ignored imports, which are kept as-is. That has the drawback of forcing all imports before and after the conditional to be kept in place, which prevents sorting.
1 parent 97aaed9 commit 841f337

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Tests/SwiftFormatRulesTests/OrderedImportsTests.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,82 @@ final class OrderedImportsTests: LintOrFormatRuleTestCase {
541541

542542
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
543543
}
544+
545+
func testConditionalImports() {
546+
let input =
547+
"""
548+
import Zebras
549+
import Apples
550+
#if canImport(Darwin)
551+
import Darwin
552+
#elseif canImport(Glibc)
553+
import Glibc
554+
#endif
555+
import Aardvarks
556+
557+
foo()
558+
bar()
559+
baz()
560+
"""
561+
562+
let expected =
563+
"""
564+
import Aardvarks
565+
import Apples
566+
import Zebras
567+
568+
#if canImport(Darwin)
569+
import Darwin
570+
typealias SomeNativeTypeHandle = some_darwin_type_t
571+
#elseif canImport(Glibc)
572+
import Glibc
573+
typealias SomeNativeTypeHandle = some_glibc_type_t
574+
#endif
575+
576+
foo()
577+
bar()
578+
baz()
579+
"""
580+
581+
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
582+
}
583+
584+
func testIgnoredConditionalImports() {
585+
let input =
586+
"""
587+
import Zebras
588+
import Apples
589+
#if canImport(Darwin)
590+
import Darwin
591+
#elseif canImport(Glibc)
592+
import Glibc
593+
#endif
594+
// swift-format-ignore
595+
import Aardvarks
596+
597+
foo()
598+
bar()
599+
baz()
600+
"""
601+
602+
let expected =
603+
"""
604+
import Apples
605+
import Zebras
606+
607+
#if canImport(Darwin)
608+
import Darwin
609+
#elseif canImport(Glibc)
610+
import Glibc
611+
#endif
612+
// swift-format-ignore
613+
import Aardvarks
614+
615+
foo()
616+
bar()
617+
baz()
618+
"""
619+
620+
XCTAssertFormatting(OrderedImports.self, input: input, expected: expected)
621+
}
544622
}

Tests/SwiftFormatRulesTests/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@ extension OrderedImportsTests {
264264
// `swift test --generate-linuxmain`
265265
// to regenerate.
266266
static let __allTests__OrderedImportsTests = [
267+
("testConditionalImports", testConditionalImports),
267268
("testDisableOrderedImports", testDisableOrderedImports),
268269
("testDisableOrderedImportsMovingComments", testDisableOrderedImportsMovingComments),
269270
("testDuplicateAttributedImports", testDuplicateAttributedImports),
270271
("testDuplicateCommentedImports", testDuplicateCommentedImports),
271272
("testDuplicateIgnoredImports", testDuplicateIgnoredImports),
272273
("testEmptyFile", testEmptyFile),
274+
("testIgnoredConditionalImports", testIgnoredConditionalImports),
273275
("testImportsOrderWithDocComment", testImportsOrderWithDocComment),
274276
("testImportsOrderWithoutModuleType", testImportsOrderWithoutModuleType),
275277
("testInvalidImportsOrder", testInvalidImportsOrder),

0 commit comments

Comments
 (0)