Skip to content

Commit ba971c1

Browse files
authored
Sort equal weighted "Mentioned In" articles alphabetically (#1221)
If articles have same # of mentions, sort them alphabetically. Resolves: rdar://150870055
1 parent f47942a commit ba971c1

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

Sources/SwiftDocC/Semantics/Article/ArticleSymbolMentions.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ struct ArticleSymbolMentions {
3232
// Mentions are sorted on demand based on the number of mentions.
3333
// This could change in the future.
3434
return mentions[symbol, default: [:]].sorted {
35-
$0.value > $1.value
35+
// If a pair of articles have the same number of mentions, sort
36+
// them alphabetically.
37+
if $0.value == $1.value {
38+
return $0.key.description < $1.key.description
39+
}
40+
41+
// Otherwise, sort articles with more mentions first.
42+
return $0.value > $1.value
3643
}
3744
.map { $0.key }
3845
}

Tests/SwiftDocCTests/Semantics/ArticleSymbolMentionsTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,56 @@ class ArticleSymbolMentionsTests: XCTestCase {
3636
XCTAssertEqual(gottenArticle, article)
3737
}
3838

39+
// Test the sorting of articles mentioning a given symbol
40+
func testArticlesMentioningSorting() throws {
41+
let bundleID: DocumentationBundle.Identifier = "org.swift.test"
42+
let articles = ["a", "b", "c", "d", "e", "f"].map { letter in
43+
ResolvedTopicReference(
44+
bundleID: bundleID,
45+
path: "/\(letter)",
46+
sourceLanguage: .swift
47+
)
48+
}
49+
let symbol = ResolvedTopicReference(
50+
bundleID: bundleID,
51+
path: "/z",
52+
sourceLanguage: .swift
53+
)
54+
55+
var mentions = ArticleSymbolMentions()
56+
XCTAssertTrue(mentions.articlesMentioning(symbol).isEmpty)
57+
58+
// test that mentioning articles are sorted by weight
59+
mentions.article(articles[0], didMention: symbol, weight: 10)
60+
mentions.article(articles[1], didMention: symbol, weight: 42)
61+
mentions.article(articles[2], didMention: symbol, weight: 1)
62+
mentions.article(articles[3], didMention: symbol, weight: 14)
63+
mentions.article(articles[4], didMention: symbol, weight: 2)
64+
mentions.article(articles[5], didMention: symbol, weight: 6)
65+
XCTAssertEqual(mentions.articlesMentioning(symbol), [
66+
articles[1],
67+
articles[3],
68+
articles[0],
69+
articles[5],
70+
articles[4],
71+
articles[2],
72+
])
73+
74+
// test that mentioning articles w/ same weights are sorted alphabetically
75+
//
76+
// note: this test is done multiple times with a shuffled list to ensure
77+
// that it isn't just passing by pure chance due to the unpredictable
78+
// order of Swift dictionaries
79+
for _ in 1...10 {
80+
mentions = ArticleSymbolMentions()
81+
XCTAssertTrue(mentions.articlesMentioning(symbol).isEmpty)
82+
for article in articles.shuffled() {
83+
mentions.article(article, didMention: symbol, weight: 1)
84+
}
85+
XCTAssertEqual(mentions.articlesMentioning(symbol), articles)
86+
}
87+
}
88+
3989
func testSymbolLinkCollectorEnabled() throws {
4090
let (bundle, context) = try createMentionedInTestBundle()
4191

0 commit comments

Comments
 (0)