Skip to content

Commit 6872300

Browse files
authored
SectionMap: Make iteration order predictable (#49)
This change makes the order in which sections are iterated over predictable. Before, a dictionary value iterator was used, which doesn’t guarantee any particular order. Now, sections are always iterated over in the order their IDs were defined in the current site’s `Website.SectionID` enum. (Also includes some minor tweaks to ensure that all tests run on Linux).
1 parent 07cdf70 commit 6872300

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

Sources/Publish/API/SectionMap.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public struct SectionMap<Site: Website> {
2828

2929
extension SectionMap: Sequence {
3030
public func makeIterator() -> AnyIterator<Section<Site>> {
31-
AnyIterator(sections.values.makeIterator())
31+
var ids = self.ids.makeIterator()
32+
33+
return AnyIterator {
34+
guard let nextID = ids.next() else { return nil }
35+
return self[nextID]
36+
}
3237
}
3338
}

Tests/PublishTests/Infrastructure/LinuxCompatibility.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ internal extension Linux {
3131
#if canImport(ObjectiveC)
3232
internal final class LinuxVerificationTests: XCTestCase {
3333
func testAllTestsRunOnLinux() {
34+
var totalLinuxTestCount = 0
35+
3436
for testCase in allTests() {
3537
let type = testCase.testCaseClass
3638

@@ -49,7 +51,19 @@ internal final class LinuxVerificationTests: XCTestCase {
4951
""")
5052
}
5153
}
54+
55+
totalLinuxTestCount += linuxTestNames.count
5256
}
57+
58+
XCTAssertEqual(
59+
XCTestSuite.default.testCaseCount - 1,
60+
totalLinuxTestCount,
61+
"""
62+
Linux and Apple Platforms test counts are not equal.
63+
Perhaps you added a new test case class?
64+
If so, you need to add it in XCTestManifests.swift.
65+
"""
66+
)
5367
}
5468
}
5569
#endif

Tests/PublishTests/Infrastructure/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public func allTests() -> [Linux.TestCase] {
1515
Linux.makeTestCase(using: FileIOTests.allTests),
1616
Linux.makeTestCase(using: HTMLGenerationTests.allTests),
1717
Linux.makeTestCase(using: MarkdownTests.allTests),
18+
Linux.makeTestCase(using: PathTests.allTests),
1819
Linux.makeTestCase(using: PlotComponentTests.allTests),
1920
Linux.makeTestCase(using: PluginTests.allTests),
2021
Linux.makeTestCase(using: PodcastFeedGenerationTests.allTests),
22+
Linux.makeTestCase(using: PublishingContextTests.allTests),
2123
Linux.makeTestCase(using: RSSFeedGenerationTests.allTests),
2224
Linux.makeTestCase(using: SiteMapGenerationTests.allTests),
2325
Linux.makeTestCase(using: WebsiteTests.allTests)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Publish
3+
* Copyright (c) John Sundell 2020
4+
* MIT license, see LICENSE file for details
5+
*/
6+
7+
import XCTest
8+
import Publish
9+
10+
final class PublishingContextTests: PublishTestCase {
11+
func testSectionIterationOrder() throws {
12+
let expectedOrder = WebsiteStub.SectionID.allCases
13+
var actualOrder = [WebsiteStub.SectionID]()
14+
15+
try publishWebsite(using: [
16+
.step(named: "Step") { context in
17+
context.sections.forEach { section in
18+
actualOrder.append(section.id)
19+
}
20+
}
21+
])
22+
23+
XCTAssertEqual(expectedOrder, actualOrder)
24+
}
25+
}
26+
27+
extension PublishingContextTests {
28+
static var allTests: Linux.TestList<PublishingContextTests> {
29+
[
30+
("testSectionIterationOrder", testSectionIterationOrder)
31+
]
32+
}
33+
}

0 commit comments

Comments
 (0)