@@ -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