1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
@testable import SwiftDocC
import SwiftDocCTestUtilities
typealias Node = NavigatorTree.Node
typealias PageType = NavigatorIndex.PageType
let testBundleIdentifier = "org.swift.docc.example"
class NavigatorIndexingTests: XCTestCase {
struct Language: OptionSet {
let rawValue: UInt8
static let swift = Language(rawValue: 1 << 0)
static let objC = Language(rawValue: 1 << 1)
static let perl = Language(rawValue: 1 << 2)
static let all: Language = [.swift, .objC, .perl]
}
func generateLargeTree() -> Node {
var index = 1
let rootItem = NavigatorItem(pageType: 1, languageID: Language.all.rawValue, title: "Root", platformMask: 1, availabilityID: 1)
let root = Node(item: rootItem, bundleIdentifier: "org.swift.docc.example")
@discardableResult func addItems(n: Int, items: [Node], language: Language) -> [Node] {
var leaves = [Node]()
for _ in 0..<n {
guard let parent = items.randomElement() else { fatalError("The provided array of node is empty.") }
let item = NavigatorItem(pageType: 1, languageID: language.rawValue, title: "Index-\(index)", platformMask: 1, availabilityID: 1)
guard Language(rawValue: parent.item.languageID).contains(language) else {
fatalError("The parent must include the language of a child. Having children with languages not included by the parent is not allowed.")
}
let node = Node(item: item, bundleIdentifier: "org.swift.docc.example")
parent.add(child: node)
leaves.append(node)
index += 1
}
return leaves
}
let leaves1 = addItems(n: 50, items: [root], language: [.swift, .objC])
var leaves2 = addItems(n: 1000, items: leaves1, language: [.swift, .objC])
var leaves3 = addItems(n: 5000, items: leaves2, language: [.swift, .objC])
addItems(n: 10000, items: leaves3, language: [.swift, .objC])
leaves2 = addItems(n: 10000, items: leaves1, language: .swift)
leaves3 = addItems(n: 15000, items: leaves2, language: .swift)
addItems(n: 100000, items: leaves3, language: .swift)
leaves2 = addItems(n: 5000, items: leaves1, language: .objC)
leaves3 = addItems(n: 10000, items: leaves2, language: .objC)
addItems(n: 500000, items: leaves3, language: .objC)
return root
}
func generateSmallTree(bundleIdentifier: String = testBundleIdentifier) -> Node {
var index = 1
let rootItem = NavigatorItem(pageType: 1, languageID: Language.all.rawValue, title: "Root", platformMask: 1, availabilityID: 1)
let root = Node(item: rootItem, bundleIdentifier: bundleIdentifier)
@discardableResult func addItems(n: Int, items: [Node], language: Language) -> [Node] {
var leaves = [Node]()
for _ in 0..<n {
guard let parent = items.randomElement() else { fatalError("The provided array of node is empty.") }
let item = NavigatorItem(pageType: 1, languageID: language.rawValue, title: "Index-\(index)", platformMask: 1, availabilityID: 1)
guard Language(rawValue: parent.item.languageID).contains(language) else {
fatalError("The parent must include the language of a child. Having children with languages not included by the parent is not allowed.")
}
let node = Node(item: item, bundleIdentifier: "org.swift.docc.example")
parent.add(child: node)
leaves.append(node)
index += 1
}
return leaves
}
let leaves1 = addItems(n: 2, items: [root], language: [.swift, .objC])
let leaves2 = addItems(n: 4, items: leaves1, language: [.swift, .objC])
let leaves3 = addItems(n: 8, items: leaves2, language: [.swift, .objC])
addItems(n: 16, items: leaves3, language: [.swift, .objC])
return root
}
func testBasicTree() {
let rootItem = NavigatorItem(pageType: 1, languageID: 1, title: "Root", platformMask: 1, availabilityID: 1)
let root = Node(item: rootItem, bundleIdentifier: "org.swift.docc.example")
for i in 0..<2 {
let item = NavigatorItem(pageType: 1, languageID: 1, title: "Sub Item \(i)", platformMask: 1, availabilityID: 1)
root.add(child: Node(item: item, bundleIdentifier: "org.swift.docc.example"))
}
for child in root.children {
for i in 0..<3 {
let item = NavigatorItem(pageType: 1, languageID: 1, title: "\(child.item.title) - \(i)", platformMask: 1, availabilityID: 1)
child.add(child: Node(item: item, bundleIdentifier: "org.swift.docc.example"))
}
}
let dumpString = """
Root
┣╸Sub Item 0
┃ ┣╸Sub Item 0 - 0
┃ ┣╸Sub Item 0 - 1
┃ ┗╸Sub Item 0 - 2
┗╸Sub Item 1
┣╸Sub Item 1 - 0
┣╸Sub Item 1 - 1
┗╸Sub Item 1 - 2
"""
XCTAssertEqual(root.countItems(), 9)
XCTAssertEqual(root.dumpTree(), dumpString)
let rootCopy = root.copy()
XCTAssertEqual(root, rootCopy)
XCTAssertEqual(rootCopy.dumpTree(), dumpString)
}
func testNavigatorItemRawDump() {
let item = NavigatorItem(pageType: 1, languageID: 4, title: "My Title", platformMask: 256, availabilityID: 1024)
let data = item.rawValue
let fromData = NavigatorItem(rawValue: data)
XCTAssertEqual(item, fromData)
}
func testObjCLanguage() {
let root = generateLargeTree()
var objcFiltered: Node?
objcFiltered = root.filter({ (item) -> Bool in
Language(rawValue: item.languageID).contains(.objC)
})
XCTAssertEqual(objcFiltered?.countItems(), 531051)
}
func test2Languages() {
let root = generateLargeTree()
var bothFiltered: Node?
bothFiltered = root.filter({ (item) -> Bool in
Language(rawValue: item.languageID).contains([.swift, .objC])
})
XCTAssertEqual(bothFiltered?.countItems(), 16051)
}
func testSwiftLanguage() {
let root = generateLargeTree()
var swiftFiltered: Node?
swiftFiltered = root.filter({ (item) -> Bool in
Language(rawValue: item.languageID).contains(.swift)
})
XCTAssertEqual(swiftFiltered?.countItems(), 141051)
}
func testNavigationTreeDumpAndRead() throws {
let targetURL = try createTemporaryDirectory()
let indexURL = targetURL.appendingPathComponent("nav.index")
let root = generateSmallTree()
XCTAssertEqual(root.countItems(), 31)
let original = NavigatorTree(root: root)
try original.write(to: indexURL)
let readTree = try NavigatorTree.read(from: indexURL, bundleIdentifier: testBundleIdentifier, interfaceLanguages: [.swift], atomically: true)
XCTAssertEqual(original.root.countItems(), readTree.root.countItems())
XCTAssertTrue(compare(lhs: original.root, rhs: readTree.root))
let idValidator: (NavigatorTree.Node) -> Bool = { node in
return node.id != nil
}
let bundleIdentifierValidator: (NavigatorTree.Node) -> Bool = { node in
return !node.bundleIdentifier.isEmpty
}
let emptyPresentationIdentifierValidator: (NavigatorTree.Node) -> Bool = { node in
return node.presentationIdentifier == nil
}
XCTAssertTrue(validateTree(node: readTree.root, validator: idValidator), "The tree has IDs missing.")
XCTAssertTrue(validateTree(node: readTree.root, validator: bundleIdentifierValidator), "The tree has bundle identifier missing.")
XCTAssertTrue(validateTree(node: readTree.root, validator: emptyPresentationIdentifierValidator), "The tree has a presentation identifier set which should not be present.")
let treeWithPresentationIdentifier = try NavigatorTree.read(from: indexURL, bundleIdentifier: testBundleIdentifier, interfaceLanguages: [.swift], atomically: true, presentationIdentifier: "com.example.test")
let presentationIdentifierValidator: (NavigatorTree.Node) -> Bool = { node in
return node.presentationIdentifier == "com.example.test"
}
XCTAssertTrue(validateTree(node: treeWithPresentationIdentifier.root, validator: idValidator), "The tree has IDs missing.")
XCTAssertTrue(validateTree(node: treeWithPresentationIdentifier.root, validator: bundleIdentifierValidator), "The tree has bundle identifier missing.")
XCTAssertTrue(validateTree(node: treeWithPresentationIdentifier.root, validator: presentationIdentifierValidator), "The tree lacks the presentation identifier.")
func addAttributes(node: NavigatorTree.Node) -> Void {
node.attributes["Attribute"] = true
}
let treeWithAttributes = try NavigatorTree.read(
from: indexURL,
bundleIdentifier: testBundleIdentifier,
interfaceLanguages: [.swift],
atomically: true,
presentationIdentifier: "com.example.test",
onNodeRead: addAttributes
)
let attributesValidator: (NavigatorTree.Node) -> Bool = { node in
return (node.attributes["Attribute"] as? Bool) == true
}
XCTAssertTrue(validateTree(node: treeWithAttributes.root, validator: idValidator), "The tree has IDs missing.")
XCTAssertTrue(validateTree(node: treeWithAttributes.root, validator: bundleIdentifierValidator), "The tree has bundle identifier missing.")
XCTAssertTrue(validateTree(node: treeWithAttributes.root, validator: presentationIdentifierValidator), "The tree lacks the presentation identifier.")
XCTAssertTrue(validateTree(node: treeWithAttributes.root, validator: attributesValidator), "The tree lacks the correct attributes.")
// Test non-atomic read.
let treeWithAttributesNonAtomic = try NavigatorTree.read(
from: indexURL,
bundleIdentifier: testBundleIdentifier,
interfaceLanguages: [.swift],
atomically: false,
presentationIdentifier: "com.example.test",
onNodeRead: addAttributes
)
XCTAssertTrue(validateTree(node: treeWithAttributesNonAtomic.root, validator: idValidator), "The tree has IDs missing.")
XCTAssertTrue(validateTree(node: treeWithAttributesNonAtomic.root, validator: bundleIdentifierValidator), "The tree has bundle identifier missing.")
XCTAssertTrue(validateTree(node: treeWithAttributesNonAtomic.root, validator: presentationIdentifierValidator), "The tree lacks the presentation identifier.")
XCTAssertTrue(validateTree(node: treeWithAttributesNonAtomic.root, validator: attributesValidator), "The tree lacks the correct attributes.")
}
func testLoadingNavigatorIndexDoesNotCacheReferences() throws {
let uniqueTestBundleIdentifier = #function
let targetURL = try createTemporaryDirectory()
let indexURL = targetURL.appendingPathComponent("nav.index")
let root = generateSmallTree(bundleIdentifier: uniqueTestBundleIdentifier)
let original = NavigatorTree(root: root)
try original.write(to: indexURL)
_ = try NavigatorTree.read(
from: indexURL,
bundleIdentifier: uniqueTestBundleIdentifier,
interfaceLanguages: [.swift],
atomically: true
)
XCTAssertNil(ResolvedTopicReference._numberOfCachedReferences(bundleID: uniqueTestBundleIdentifier))
}
func testNavigationTreeLargeDumpAndRead() throws {
try XCTSkipIf(true, "These performance measurements aren't compared to previous results.")
#if os(OSX)
let targetURL = try createTemporaryDirectory()
let indexURL = targetURL.appendingPathComponent("nav.index")
let root = generateLargeTree()
let original = NavigatorTree(root: root)
try original.write(to: indexURL)
measure {
_ = try! NavigatorTree.read(from: indexURL, interfaceLanguages: [.swift], atomically: true)
}
#endif
}
// This test has been disabled because of frequent failures in Swift CI.
//
// rdar://87737744 tracks updating this test to remove any flakiness.
func disabled_testNavigationTreeLargeDumpAndReadAsync() throws {
let targetURL = try createTemporaryDirectory()
let indexURL = targetURL.appendingPathComponent("nav.index")
let root = generateLargeTree()
let original = NavigatorTree(root: root)
try original.write(to: indexURL)
// Counts the number of times the broadcast callback is called.
var counter = 0
let expectation = XCTestExpectation(description: "Load the tree asynchronously.")
let readTree = NavigatorTree()
try! readTree.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main) { (nodes, completed, error) in
counter += 1
XCTAssertNil(error)
if completed { expectation.fulfill() }
}
wait(for: [expectation], timeout: 10.0)
XCTAssert(counter > 2, "The broadcast callback has to be called at least 2 times.")
XCTAssertEqual(original.root.countItems(), readTree.root.countItems())
XCTAssertTrue(compare(lhs: original.root, rhs: readTree.root))
let expectation2 = XCTestExpectation(description: "Load the tree asynchronously, again with presentation identifier.")
let readTreePresentationIdentifier = NavigatorTree()
try! readTreePresentationIdentifier.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main, presentationIdentifier: "com.example.test") { (nodes, completed, error) in
XCTAssertNil(error)
if completed { expectation2.fulfill() }
}
wait(for: [expectation2], timeout: 10.0)
XCTAssertEqual(original.root.countItems(), readTreePresentationIdentifier.root.countItems())
let presentationIdentifierValidator: (NavigatorTree.Node) -> Bool = { node in
return node.presentationIdentifier == "com.example.test"
}
XCTAssertTrue(validateTree(node: readTreePresentationIdentifier.root, validator: presentationIdentifierValidator), "The tree lacks the presentation identifier.")
}
func testNavigatorIndexGenerationEmpty() throws {
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier)
builder.setup()
builder.finalize()
XCTAssertNotNil(builder.navigatorIndex)
let indexURL = targetURL.appendingPathComponent("nav.index")
let readTree = NavigatorTree()
XCTAssertThrowsError(try readTree.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main, broadcast: nil))
try XCTAssertEqual(
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
RenderIndex.fromString(
"""
{
"interfaceLanguages": {},
"includedArchiveIdentifiers": [
"org.swift.docc.example"
],
"schemaVersion": {
"major": 0,
"minor": 1,
"patch": 2
}
}
"""
)
)
}
func testNavigatorIndexGenerationOneNode() throws {
let targetURL = try createTemporaryDirectory()
let indexURL = targetURL.appendingPathComponent("nav.index")
let original = NavigatorTree(root: NavigatorTree.rootNode(bundleIdentifier: NavigatorIndex.UnknownBundleIdentifier))
try original.write(to: indexURL)
// Counts the number of times the broadcast callback is called.
var counter = 0
let expectation = XCTestExpectation(description: "Load the tree asynchronously.")
let readTree = NavigatorTree()
try! readTree.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main) { (nodes, completed, error) in
counter += 1
XCTAssertNil(error)
if completed { expectation.fulfill() }
}
wait(for: [expectation], timeout: 10.0)
XCTAssert(counter == 1, "The broadcast callback has to be called at exactly 1 time.")
XCTAssertEqual(original.root.countItems(), readTree.root.countItems())
XCTAssertTrue(compare(lhs: original.root, rhs: readTree.root))
}
func testNavigatorIndexGenerationOperator() throws {
let operatorURL = Bundle.module.url(
forResource: "Operator", withExtension: "json", subdirectory: "Test Resources")!
let renderNode = try RenderNode.decode(fromJSON: Data(contentsOf: operatorURL))
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier)
builder.setup()
try builder.index(renderNode: renderNode)
builder.finalize(emitJSONRepresentation: false, emitLMDBRepresentation: false)
XCTAssertNotNil(builder.navigatorIndex)
}
func testNavigatorIndexGeneration() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
var results = Set<String>()
// Create an index 10 times to ensure we have not non-deterministic behavior across builds
for _ in 0..<10 {
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let renderIndex = try RenderIndex.fromURL(targetURL.appendingPathComponent("index.json"))
XCTAssertEqual(renderIndex.interfaceLanguages.keys.count, 1)
XCTAssertEqual(renderIndex.interfaceLanguages["swift"]?.count, 29)
XCTAssertEqual(renderIndex.interfaceLanguages["swift"]?.first?.title, "Functions")
XCTAssertEqual(renderIndex.interfaceLanguages["swift"]?.first?.path, nil)
XCTAssertEqual(renderIndex.interfaceLanguages["swift"]?.first?.type, "groupMarker")
let navigatorIndex = builder.navigatorIndex!
XCTAssertEqual(
navigatorIndex.availabilityIndex.platforms,
[.watchOS, .macCatalyst, .iOS, .tvOS, .macOS, .iPadOS]
)
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .iOS), Set([
Platform.Version(string: "13.0")!,
Platform.Version(string: "10.15")!,
Platform.Version(string: "11.1")!,
Platform.Version(string: "14.0")!,
]))
XCTAssertEqual(Set(navigatorIndex.languages), Set(["Swift"]))
XCTAssertEqual(navigatorIndex.navigatorTree.root.countItems(), navigatorIndex.navigatorTree.numericIdentifierToNode.count)
XCTAssertTrue(validateTree(node: navigatorIndex.navigatorTree.root, validator: { (node) -> Bool in
return node.bundleIdentifier == testBundleIdentifier
}))
let allNodes = navigatorIndex.navigatorTree.numericIdentifierToNode.values
let symbolPages = allNodes.filter { NavigatorIndex.PageType(rawValue: $0.item.pageType)! == .symbol }
// Pages with type `symbol` should be 6 (collectionGroup type of pages) as all the others should have a proper type.
XCTAssertEqual(symbolPages.count, 6)
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
results.insert(navigatorIndex.navigatorTree.root.dumpTree())
try FileManager.default.removeItem(at: targetURL)
}
XCTAssertEqual(results.count, 1)
assertEqualDumps(results.first ?? "", try testTree(named: "testNavigatorIndexGeneration"))
}
func testNavigatorIndexGenerationWithCyclicCuration() throws {
// This is a documentation hierarchy where every page exist in more than one place in the navigator,
// through a mix of automatic and manual curation, with a cycle between the two "leaf" nodes:
//
// ModuleName ─────┐
// │ ▼
// │ API Collection
// │ │
// ┌─────┴────────┐ │
// │┌─────────────┼┬┘
// ▼▼ ▼▼
// Container ──▶ OtherSymbol
// │ │
// ├────────────┐│
// ▼ ▼▼
// first() ◀──▶ second()
let exampleDocumentation = Folder(name: "unit-test.docc", content: [
InfoPlist(identifier: testBundleIdentifier),
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(
moduleName: "ModuleName",
symbols: [
.init(
identifier: .init(precise: "some-container-symbol-id", interfaceLanguage: SourceLanguage.swift.id),
names: .init(title: "Container", navigator: [.init(kind: .identifier, spelling: "Container", preciseIdentifier: nil)], subHeading: nil, prose: nil),
pathComponents: ["Container"],
docComment: nil,
accessLevel: .public,
kind: .init(parsedIdentifier: .class, displayName: "Kind Display Name"),
mixins: [:]
),
.init(
identifier: .init(precise: "some-other-symbol-id", interfaceLanguage: SourceLanguage.swift.id),
names: .init(title: "OtherSymbol", navigator: [.init(kind: .identifier, spelling: "OtherSymbol", preciseIdentifier: nil)], subHeading: nil, prose: nil),
pathComponents: ["OtherSymbol"],
docComment: nil,
accessLevel: .public,
kind: .init(parsedIdentifier: .class, displayName: "Kind Display Name"),
mixins: [:]
),
.init(
identifier: .init(precise: "first-member-symbol-id", interfaceLanguage: SourceLanguage.swift.id),
names: .init(title: "first()", navigator: [.init(kind: .identifier, spelling: "first()", preciseIdentifier: nil)], subHeading: nil, prose: nil),
pathComponents: ["Container", "first()"],
docComment: nil,
accessLevel: .public,
kind: .init(parsedIdentifier: .method, displayName: "Kind Display Name"),
mixins: [:]
),
.init(
identifier: .init(precise: "second-member-symbol-id", interfaceLanguage: SourceLanguage.swift.id),
names: .init(title: "second()", navigator: [.init(kind: .identifier, spelling: "second()", preciseIdentifier: nil)], subHeading: nil, prose: nil),
pathComponents: ["Container", "second()"],
docComment: nil,
accessLevel: .public,
kind: .init(parsedIdentifier: .method, displayName: "Kind Display Name"),
mixins: [:]
),
], relationships: [
.init(source: "some-container-symbol-id", target: "first-member-symbol-id", kind: .memberOf, targetFallback: nil),
.init(source: "some-container-symbol-id", target: "second-member-symbol-id", kind: .memberOf, targetFallback: nil),
])
),
TextFile(name: "Container.md", utf8Content: """
# ``Container``
The container curates one of the members and the other symbol
## Topics
### Manual curation
- ``first()``
- ``OtherSymbol``
"""),
TextFile(name: "OtherSymbol.md", utf8Content: """
# ``OtherSymbol``
The other symbol curates the other member
## Topics
### Manual curation
- ``Container/second()``
"""),
TextFile(name: "first.md", utf8Content: """
# ``Container/first()``
Both members curate each other
## Topics
### Manual curation
- ``second()``
"""),
TextFile(name: "second.md", utf8Content: """
# ``Container/second()``
Both members curate each other
## Topics
### Manual curation
- ``first()``
"""),
TextFile(name: "API-Collection.md", utf8Content: """
# An API collection
The API collection curates both top-level symbols
## Topics
### Manual curation
- ``Container``
- ``OtherSymbol``
"""),
TextFile(name: "Module.md", utf8Content: """
# ``ModuleName``
The module curates the API collection
## Topics
### Manual curation
- <doc:API-Collection>
"""),
])
let tempURL = try createTempFolder(content: [exampleDocumentation])
let (_, bundle, context) = try loadBundle(from: tempURL)
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let navigatorIndex = try XCTUnwrap(builder.navigatorIndex)
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), """
[Root]
┗╸ModuleName
┣╸Manual curation
┗╸An API collection
┣╸Manual curation
┣╸Container
┃ ┣╸Manual curation
┃ ┣╸first()
┃ ┃ ┣╸Manual curation
┃ ┃ ┗╸second()
┃ ┗╸OtherSymbol
┃ ┣╸Manual curation
┃ ┗╸second()
┃ ┣╸Manual curation
┃ ┗╸first()
┗╸OtherSymbol
┣╸Manual curation
┗╸second()
┣╸Manual curation
┗╸first()
""")
}
func testNavigatorWithDifferentSwiftAndObjectiveCHierarchies() throws {
let (_, bundle, context) = try testBundleAndContext(named: "GeometricalShapes")
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
let fromMemoryBuilder = NavigatorIndex.Builder(outputURL: try createTemporaryDirectory(), bundleIdentifier: bundle.identifier, sortRootChildrenByName: true, groupByLanguage: true)
let fromDecodedBuilder = NavigatorIndex.Builder(outputURL: try createTemporaryDirectory(), bundleIdentifier: bundle.identifier, sortRootChildrenByName: true, groupByLanguage: true)
fromMemoryBuilder.setup()
fromDecodedBuilder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
XCTAssertNil(renderNode.variantOverrides)
try fromMemoryBuilder.index(renderNode: renderNode)
let encoded = try RenderJSONEncoder.makeEncoder(emitVariantOverrides: true).encode(renderNode)
let decoded = try RenderJSONDecoder.makeDecoder().decode(RenderNode.self, from: encoded)
XCTAssertNotNil(decoded.variantOverrides)
try fromDecodedBuilder.index(renderNode: decoded)
}
fromMemoryBuilder.finalize()
fromDecodedBuilder.finalize()
let fromMemoryNavigatorTree = try XCTUnwrap(fromMemoryBuilder.navigatorIndex).navigatorTree.root
let fromDecodedNavigatorTree = try XCTUnwrap(fromDecodedBuilder.navigatorIndex).navigatorTree.root
XCTAssertEqual(fromMemoryNavigatorTree.dumpTree(), fromDecodedNavigatorTree.dumpTree())
XCTAssertEqual(fromMemoryNavigatorTree.dumpTree(), """
[Root]
┣╸Objective-C
┃ ┗╸GeometricalShapes
┃ ┣╸Structures
┃ ┣╸TLACircle
┃ ┃ ┣╸Instance Properties
┃ ┃ ┣╸center
┃ ┃ ┗╸radius
┃ ┣╸Variables
┃ ┣╸TLACircleDefaultRadius
┃ ┣╸TLACircleNull
┃ ┣╸TLACircleZero
┃ ┣╸Functions
┃ ┣╸TLACircleToString
┃ ┣╸TLACircleFromString
┃ ┣╸TLACircleIntersects
┃ ┣╸TLACircleIsEmpty
┃ ┣╸TLACircleIsNull
┃ ┗╸TLACircleMake
┗╸Swift
┗╸GeometricalShapes
┣╸Structures
┗╸Circle
┣╸Initializers
┣╸init()
┣╸init(center: CGPoint, radius: CGFloat)
┣╸init(string: String)
┣╸Instance Properties
┣╸var center: CGPoint
┣╸var debugDescription: String
┣╸var isEmpty: Bool
┣╸var isNull: Bool
┣╸var radius: CGFloat
┣╸Instance Methods
┣╸func intersects(Circle) -> Bool
┣╸Type Properties
┣╸static let defaultRadius: CGFloat
┣╸static let null: Circle
┗╸static let zero: Circle
""")
}
func testNavigatorIndexGenerationVariantsPayload() throws {
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: false)
}
func testNavigatorIndexGenerationVariantsPayloadIgnoringLanguage() throws {
try testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: true)
}
private func testNavigatorIndexGenerationVariantsPayload(ignoringLanguage: Bool) throws {
let jsonFile = Bundle.module.url(forResource: "Variant-render-node", withExtension: "json", subdirectory: "Test Resources")!
let jsonData = try Data(contentsOf: jsonFile)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true, groupByLanguage: true)
builder.setup()
let renderNode = try XCTUnwrap(RenderJSONDecoder.makeDecoder().decode(RenderNode.self, from: jsonData))
try builder.index(renderNode: renderNode, ignoringLanguage: ignoringLanguage)
builder.finalize()
let navigatorIndex = builder.navigatorIndex!
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
var expectedDump = """
[Root]
"""
if !ignoringLanguage {
expectedDump += """
┣╸Objective-C
┃ ┗╸My Article in Objective-C
┃ ┣╸Task Group 1
┃ ┣╸Task Group 2
┃ ┗╸Task Group 3
"""
}
expectedDump += """
┗╸Swift
┗╸My Article
┣╸Task Group 1
┣╸Task Group 2
┗╸Task Group 3
"""
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), expectedDump)
var expectedRenderIndexString = """
{
"interfaceLanguages": {
"""
if !ignoringLanguage {
expectedRenderIndexString += #"""
"occ": [
{
"children": [
{
"title": "Task Group 1",
"type": "groupMarker"
},
{
"title": "Task Group 2",
"type": "groupMarker"
},
{
"title": "Task Group 3",
"type": "groupMarker"
}
],
"path": "\/documentation\/mykit\/my-article",
"title": "My Article in Objective-C",
"type": "article"
}
],
"""#
}
expectedRenderIndexString += #"""
"swift": [
{
"children": [
{
"title": "Task Group 1",
"type": "groupMarker"
},
{
"title": "Task Group 2",
"type": "groupMarker"
},
{
"title": "Task Group 3",
"type": "groupMarker"
}
],
"path": "\/documentation\/mykit\/my-article",
"title": "My Article",
"type": "article"
}
]
"""#
expectedRenderIndexString += #"""
},
"includedArchiveIdentifiers": [
"org.swift.docc.example"
],
"schemaVersion": {
"major": 0,
"minor": 1,
"patch": 2
}
}
"""#
try XCTAssertEqual(
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
RenderIndex.fromString(expectedRenderIndexString)
)
try FileManager.default.removeItem(at: targetURL)
}
func testDoesNotCurateUncuratedPagesInLanguageThatAreCuratedInAnotherLanguage() throws {
let navigatorIndex = try generatedNavigatorIndex(for: "MixedLanguageFramework", bundleIdentifier: "org.swift.mixedlanguageframework")
XCTAssertEqual(
navigatorIndex.navigatorTree.root.children
.first { $0.item.title == "Objective-C" }?
.children
.allSatisfy { $0.item.pageType == NavigatorIndex.PageType.framework.rawValue },
true,
"""
Expected the top-level items of the Objective-C tree to only include framework nodes. Specifically, the \
node for the page "Article curated in a single-language page" should not appear as a top-level item \
because it is curated in a Swift-only node, so it should be curated in the Swift navigator only.
"""
)
XCTAssertEqual(
navigatorIndex.navigatorTree.root.children
.first { $0.item.title == "Swift" }?
.children
.first { $0.item.title == "MixedLanguageFramework" }?
.children
.first { $0.item.title == "SwiftOnlyStruct" }?
.children
.contains { $0.item.title == "Article curated in a single-language page" },
true,
"""
Expected the node with title "Article curated in a single-language page" to be curated in the Swift \
navigator tree.
"""
)
}
func testMultiCuratesChildrenOfMultiCuratedPages() throws {
let navigatorIndex = try generatedNavigatorIndex(for: "MultiCuratedSubtree", bundleIdentifier: "org.swift.MultiCuratedSubtree")
XCTAssertEqual(
navigatorIndex.navigatorTree.root.dumpTree(),
"""
[Root]
┗╸Swift
┗╸MultiCuratedSubtree
┣╸Curation Roots
┣╸FirstCurationRoot
┃ ┣╸Multicurated trees
┃ ┗╸MultiCuratedStruct
┃ ┣╸Enumerations
┃ ┗╸MultiCuratedStruct.MultiCuratedEnum
┃ ┣╸Enumeration Cases
┃ ┣╸MultiCuratedStruct.MultiCuratedEnum.firstCase
┃ ┗╸MultiCuratedStruct.MultiCuratedEnum.secondCase
┗╸SecondCurationRoot
┣╸Multicurated trees
┗╸MultiCuratedStruct
┣╸Enumerations
┗╸MultiCuratedStruct.MultiCuratedEnum
┣╸Enumeration Cases
┣╸MultiCuratedStruct.MultiCuratedEnum.firstCase
┗╸MultiCuratedStruct.MultiCuratedEnum.secondCase
"""
)
}
func testNavigatorIndexUsingPageTitleGeneration() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
var results = Set<String>()
// Create an index 10 times to ensure we have not non-deterministic behavior across builds
for _ in 0..<10 {
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true, usePageTitle: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let navigatorIndex = builder.navigatorIndex!
XCTAssertEqual(navigatorIndex.availabilityIndex.platforms, [.watchOS, .macCatalyst, .iOS, .tvOS, .macOS, .iPadOS])
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .iOS), Set([
Platform.Version(string: "13.0")!,
Platform.Version(string: "10.15")!,
Platform.Version(string: "11.1")!,
Platform.Version(string: "14.0")!,
]))
XCTAssertEqual(Set(navigatorIndex.languages), Set(["Swift"]))
XCTAssertEqual(navigatorIndex.navigatorTree.root.countItems(), navigatorIndex.navigatorTree.numericIdentifierToNode.count)
XCTAssertTrue(validateTree(node: navigatorIndex.navigatorTree.root, validator: { (node) -> Bool in
return node.bundleIdentifier == testBundleIdentifier
}))
let allNodes = navigatorIndex.navigatorTree.numericIdentifierToNode.values
let symbolPages = allNodes.filter { NavigatorIndex.PageType(rawValue: $0.item.pageType)! == .symbol }
// Pages with type `symbol` should be 6 (collectionGroup type of pages) as all the others should have a proper type.
XCTAssertEqual(symbolPages.count, 6)
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
results.insert(navigatorIndex.navigatorTree.root.dumpTree())
try FileManager.default.removeItem(at: targetURL)
}
XCTAssertEqual(results.count, 1)
assertEqualDumps(results.first ?? "", try testTree(named: "testNavigatorIndexPageTitleGeneration"))
}
func testNavigatorIndexGenerationNoPaths() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let converter = DocumentationNodeConverter(bundle: bundle, context: context)
var results = Set<String>()
// Create an index 10 times to ensure we have not non-deterministic behavior across builds
for _ in 0..<10 {
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true, writePathsOnDisk: false)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try converter.convert(entity, at: source)
try builder.index(renderNode: renderNode)
}
builder.finalize()
// Read the index back from disk
let navigatorIndex = try NavigatorIndex.readNavigatorIndex(url: targetURL)
XCTAssertEqual(navigatorIndex.availabilityIndex.platforms, [.watchOS, .macCatalyst, .iOS, .tvOS, .macOS, .iPadOS])
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .iOS), Set([
Platform.Version(string: "13.0")!,
Platform.Version(string: "10.15")!,
Platform.Version(string: "11.1")!,
Platform.Version(string: "14.0")!,
]))
XCTAssertEqual(Set(navigatorIndex.languages), Set(["Swift"]))
XCTAssertEqual(navigatorIndex.navigatorTree.root.countItems(), navigatorIndex.navigatorTree.numericIdentifierToNode.count)
XCTAssertTrue(validateTree(node: navigatorIndex.navigatorTree.root, validator: { (node) -> Bool in
return node.bundleIdentifier == testBundleIdentifier
}))
let allNodes = navigatorIndex.navigatorTree.numericIdentifierToNode.values
let symbolPages = allNodes.filter { NavigatorIndex.PageType(rawValue: $0.item.pageType)! == .symbol }
// Pages with type `symbol` should be 6 (collectionGroup type of pages) as all the others should have a proper type.
XCTAssertEqual(symbolPages.count, 6)
// Test path persistence
XCTAssertNil(navigatorIndex.path(for: 0)) // Root should have not path persisted.
XCTAssertEqual(navigatorIndex.path(for: 1), "/documentation/fillintroduced")
XCTAssertEqual(navigatorIndex.path(for: 4), "/tutorials/testoverview")
XCTAssertEqual(navigatorIndex.path(for: 9), "/documentation/fillintroduced/maccatalystonlydeprecated()")
XCTAssertEqual(navigatorIndex.path(for: 10), "/documentation/fillintroduced/maccatalystonlyintroduced()")
XCTAssertEqual(navigatorIndex.path(for: 21), "/documentation/mykit/globalfunction(_:considering:)")
XCTAssertEqual(navigatorIndex.path(for: 23), "/documentation/sidekit/uncuratedclass/angle")
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
results.insert(navigatorIndex.navigatorTree.root.dumpTree())
}
XCTAssertEqual(results.count, 1)
assertEqualDumps(results.first ?? "", try testTree(named: "testNavigatorIndexGeneration"))
}
func testNavigatorIndexGenerationWithLanguageGrouping() throws {
let navigatorIndex = try generatedNavigatorIndex(for: "TestBundle", bundleIdentifier: testBundleIdentifier)
XCTAssertEqual(navigatorIndex.availabilityIndex.platforms, [.watchOS, .macCatalyst, .iOS, .tvOS, .macOS, .iPadOS])
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .iOS), Set([
Platform.Version(string: "13.0")!,
Platform.Version(string: "10.15")!,
Platform.Version(string: "14.0")!,
Platform.Version(string: "11.1")!,
]))
XCTAssertEqual(Set(navigatorIndex.languages), Set(["Swift"]))
// Get the Swift language group.
XCTAssertEqual(navigatorIndex.navigatorTree.numericIdentifierToNode[1]?.children.count, 4)
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
assertEqualDumps(navigatorIndex.navigatorTree.root.dumpTree(), try testTree(named: "testNavigatorIndexGenerationWithLanguageGrouping"))
}
func testNavigatorIndexGenerationWithCuratedFragment() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
var results = Set<String>()
// Create an index 10 times to ensure we have no non-deterministic behavior across builds
for _ in 0..<10 {
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
var renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
if renderNode.identifier.path == "/documentation/MyKit" {
guard let reference = renderNode.topicSections.first?.identifiers.first else {
XCTFail("A topic section is missing a reference.")
return
}
let referenceWithFragment = reference.appending("#Section")
let topicsSections: [TaskGroupRenderSection] = renderNode.topicSections.compactMap { section in
if section.identifiers.contains(reference) {
var identifiers = section.identifiers
identifiers.append(referenceWithFragment)
return TaskGroupRenderSection(title: section.title,
abstract: section.abstract,
discussion: section.discussion,
identifiers: identifiers,
generated: section.generated,
anchor: section.title.map(urlReadableFragment))
}
return section
}
renderNode.topicSections = topicsSections
guard var topicReference = renderNode.references[reference] as? TopicRenderReference else {
XCTFail("Missing expected reference \(reference)")
return
}
topicReference.identifier = RenderReferenceIdentifier(referenceWithFragment)
topicReference.url = topicReference.url.appending("#Section")
renderNode.references[referenceWithFragment] = topicReference
}
try builder.index(renderNode: renderNode)
}
builder.finalize()
let navigatorIndex = try XCTUnwrap(builder.navigatorIndex)
assertUniqueIDs(node: navigatorIndex.navigatorTree.root)
results.insert(navigatorIndex.navigatorTree.root.dumpTree())
try FileManager.default.removeItem(at: targetURL)
}
XCTAssertEqual(results.count, 1)
assertEqualDumps(results.first ?? "", try testTree(named: "testNavigatorIndexGeneration"))
}
func testNavigatorIndexAvailabilityGeneration() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let navigatorIndex = try NavigatorIndex.readNavigatorIndex(url: targetURL)
XCTAssertEqual(navigatorIndex.pathHasher, .md5)
XCTAssertEqual(navigatorIndex.bundleIdentifier, testBundleIdentifier)
XCTAssertEqual(navigatorIndex.availabilityIndex.platforms, [.watchOS, .iOS, .macCatalyst, .tvOS, .macOS, .iPadOS])
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .macOS), Set([
Platform.Version(string: "10.9")!,
Platform.Version(string: "10.10")!,
Platform.Version(string: "10.15")!,
Platform.Version(string: "10.16")!,
]))
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .iOS), Set([
Platform.Version(string: "13.0")!,
Platform.Version(string: "14.0")!,
Platform.Version(string: "10.15")!,
Platform.Version(string: "11.1")!,
]))
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .watchOS), Set([
Platform.Version(string: "6.0")!,
Platform.Version(string: "13.3")!,
]))
XCTAssertEqual(navigatorIndex.availabilityIndex.versions(for: .tvOS), Set([
Platform.Version(string: "12.2")!,
Platform.Version(string: "13.0")!,
]))
XCTAssertEqual(Set(navigatorIndex.languages), Set(["Swift"]))
XCTAssertEqual(Set(navigatorIndex.availabilityIndex.platforms(for: InterfaceLanguage.swift) ?? []), Set([.watchOS, .iOS, .macCatalyst, .tvOS, .macOS, .iPadOS]))
XCTAssertEqual(navigatorIndex.availabilityIndex.platform(named: "macOS"), .macOS)
XCTAssertEqual(navigatorIndex.availabilityIndex.platform(named: "watchOS"), .watchOS)
XCTAssertEqual(navigatorIndex.availabilityIndex.platform(named: "tvOS"), .tvOS)
XCTAssertEqual(navigatorIndex.availabilityIndex.platform(named: "ios"), .undefined, "Incorrect capitalization")
XCTAssertEqual(navigatorIndex.availabilityIndex.platform(named: "iOS"), .iOS)
XCTAssertEqual(navigatorIndex.availabilityIndex.platform(named: "iPadOS"), .iPadOS)
// Check ID mapping
XCTAssertNotNil(navigatorIndex.id(for:"/documentation/sidekit/sideclass", with: .swift))
XCTAssertNotNil(navigatorIndex.id(for:"/documentation/sidekit/sideclass/myfunction()", with: .swift))
XCTAssertNotNil(navigatorIndex.id(for:"/documentation/sidekit/sideclass/path", with: .swift))
XCTAssertNil(navigatorIndex.id(for:"/non/exisint/path", with: .swift))
// Check USR mapping
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC", language: .swift), "/documentation/sidekit/sideclass")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC10myFunctionyyF"), "/documentation/sidekit/sideclass/myfunction()")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC4pathSSvp", language: .swift), "/documentation/sidekit/sideclass/path")
XCTAssertNil(navigatorIndex.path(for: "s:5SideKit"))
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC", language: .swift, hashed: false), "/documentation/sidekit/sideclass")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC10myFunctionyyF", language: .swift, hashed: false), "/documentation/sidekit/sideclass/myfunction()")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC4pathSSvp", hashed: false), "/documentation/sidekit/sideclass/path")
XCTAssertNil(navigatorIndex.path(for: "s:5SideKit", hashed: false))
XCTAssertEqual(navigatorIndex.path(for: "18xs4rl", hashed: true), "/documentation/sidekit/sideclass")
XCTAssertEqual(navigatorIndex.path(for: "1ug5ui4", hashed: true), "/documentation/sidekit/sideclass/myfunction()")
XCTAssertEqual(navigatorIndex.path(for: "1wfp7eu", hashed: true), "/documentation/sidekit/sideclass/path")
XCTAssertNil(navigatorIndex.path(for: "1m2njn2", hashed: true))
// Check we don't return valid values for other languages
XCTAssertNil(navigatorIndex.path(for: "s:7SideKit0A5ClassC", language: .objc))
XCTAssertNil(navigatorIndex.path(for: "s:7SideKit0A5ClassC4pathSSvp", language: .data))
XCTAssertNil(navigatorIndex.path(for: "1ug5ui4", language: .objc, hashed: true))
XCTAssertNil(navigatorIndex.path(for: "1wfp7eu", language: .data, hashed: true))
let sideClassNode = try XCTUnwrap(search(node: navigatorIndex.navigatorTree.root) { navigatorIndex.path(for: $0.id!) == "/documentation/sidekit/sideclass" })
let availabilities = navigatorIndex.availabilities(for: sideClassNode.item.availabilityID)
XCTAssertEqual(availabilities.count, 3)
// Extract availability and check it against some queries.
var availabilityInfo = availabilities[0]
XCTAssertFalse(availabilityInfo.belongs(to: .macOS))
XCTAssertTrue(availabilityInfo.belongs(to: .iOS))
XCTAssertFalse(availabilityInfo.isDeprecated(on: Platform(name: .iOS, version: Platform.Version(string: "13.0")!)))
XCTAssertTrue(availabilityInfo.isAvailable(on: Platform(name: .iOS, version: Platform.Version(string: "13.0")!)))
XCTAssertFalse(availabilityInfo.isAvailable(on: Platform(name: .iOS, version: Platform.Version(string: "10.0")!)))
availabilityInfo = availabilities[1]
XCTAssertFalse(availabilityInfo.belongs(to: .macOS))
XCTAssertTrue(availabilityInfo.belongs(to: .iPadOS))
XCTAssertTrue(availabilityInfo.isAvailable(on: Platform(name: .iPadOS, version: Platform.Version(string: "10.15.0")!)))
availabilityInfo = availabilities[2]
XCTAssertTrue(availabilityInfo.belongs(to: .macCatalyst))
XCTAssertTrue(availabilityInfo.isAvailable(on: Platform(name: .macCatalyst, version: Platform.Version(string: "13.0")!)))
// Ensure we can't write to an index which is read-only.
let availabilityDB = try XCTUnwrap(navigatorIndex.environment).openDatabase(named: "availability")
XCTAssertThrowsError(try availabilityDB.put(key: "content", value: "test"))
XCTAssertNil(availabilityDB.get(type: String.self, forKey: "content"))
}
func testCustomIconsInNavigator() throws {
let (bundle, context) = try testBundleAndContext(named: "BookLikeContent") // This content has a @PageImage with the "icon" purpose
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: bundle.identifier, sortRootChildrenByName: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let renderIndexData = try Data(contentsOf: targetURL.appendingPathComponent("index.json"))
let renderIndex = try JSONDecoder().decode(RenderIndex.self, from: renderIndexData)
let imageReference = try XCTUnwrap(renderIndex.references["plus.svg"])
XCTAssertEqual(imageReference.asset.variants.values.map(\.path).sorted(), [
"/images/\(bundle.identifier)/plus.svg",
])
}
func testNavigatorIndexDifferentHasherGeneration() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: testBundleIdentifier, sortRootChildrenByName: true)
builder.setup()
// Change the path hasher to the FNV-1 implementation and make sure paths and mappings are still working.
builder.navigatorIndex?.pathHasher = .fnv1
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let navigatorIndex = try NavigatorIndex.readNavigatorIndex(url: targetURL)
XCTAssertEqual(navigatorIndex.pathHasher, .fnv1)
// Check ID mapping
XCTAssertNotNil(navigatorIndex.id(for:"/documentation/sidekit/sideclass", with: .swift))
XCTAssertNotNil(navigatorIndex.id(for:"/documentation/sidekit/sideclass/myfunction()", with: .swift))
XCTAssertNotNil(navigatorIndex.id(for:"/documentation/sidekit/sideclass/path", with: .swift))
XCTAssertNil(navigatorIndex.id(for:"/non/exisint/path", with: .swift))
// Check USR mapping
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC", language: .swift), "/documentation/sidekit/sideclass")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC10myFunctionyyF"), "/documentation/sidekit/sideclass/myfunction()")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC4pathSSvp", language: .swift), "/documentation/sidekit/sideclass/path")
XCTAssertNil(navigatorIndex.path(for: "s:5SideKit"))
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC", language: .swift, hashed: false), "/documentation/sidekit/sideclass")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC10myFunctionyyF", language: .swift, hashed: false), "/documentation/sidekit/sideclass/myfunction()")
XCTAssertEqual(navigatorIndex.path(for: "s:7SideKit0A5ClassC4pathSSvp", hashed: false), "/documentation/sidekit/sideclass/path")
XCTAssertNil(navigatorIndex.path(for: "s:5SideKit", hashed: false))
XCTAssertEqual(navigatorIndex.path(for: "18xs4rl", hashed: true), "/documentation/sidekit/sideclass")
XCTAssertEqual(navigatorIndex.path(for: "1ug5ui4", hashed: true), "/documentation/sidekit/sideclass/myfunction()")
XCTAssertEqual(navigatorIndex.path(for: "1wfp7eu", hashed: true), "/documentation/sidekit/sideclass/path")
XCTAssertNil(navigatorIndex.path(for: "1m2njn2", hashed: true))
// Check we don't return valid values for other languages
XCTAssertNil(navigatorIndex.path(for: "s:7SideKit0A5ClassC", language: .objc))
XCTAssertNil(navigatorIndex.path(for: "s:7SideKit0A5ClassC4pathSSvp", language: .data))
XCTAssertNil(navigatorIndex.path(for: "1ug5ui4", language: .objc, hashed: true))
XCTAssertNil(navigatorIndex.path(for: "1wfp7eu", language: .data, hashed: true))
let sideClassNode = try XCTUnwrap(search(node: navigatorIndex.navigatorTree.root) { navigatorIndex.path(for: $0.id!) == "/documentation/sidekit/sideclass" })
let availabilities = navigatorIndex.availabilities(for: sideClassNode.item.availabilityID)
XCTAssertEqual(availabilities.count, 3)
}
func testPlatformVersion() {
guard let version1 = Platform.Version(string: "12.0") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version2 = Platform.Version(string: "12.1") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version3 = Platform.Version(string: "12.1.1") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version4 = Platform.Version(string: "12.1.2") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version5 = Platform.Version(string: "12.2.0") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version6 = Platform.Version(string: "13") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version2alt = Platform.Version(string: "12.1.0") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
guard let version10 = Platform.Version(string: "13.0.1") else {
XCTFail("The version string is valid, but failed to be processed.")
return
}
// Verify progressive versions
XCTAssert(version1 < version2)
XCTAssert(version1 < version3)
XCTAssert(version1 < version4)
XCTAssert(version1 < version5)
XCTAssert(version1 < version6)
XCTAssert(version2 < version3)
XCTAssert(version2 < version4)
XCTAssert(version2 < version5)
XCTAssert(version2 < version6)
XCTAssert(version3 < version4)
XCTAssert(version3 < version5)
XCTAssert(version3 < version6)
XCTAssert(version4 < version5)
XCTAssert(version4 < version6)
XCTAssert(version5 < version6)
XCTAssertFalse(version10 < version3)
// Verify inversion
XCTAssert(version2 > version1)
XCTAssert(version3 > version1)
XCTAssert(version4 > version1)
XCTAssert(version5 > version1)
XCTAssert(version6 > version1)
// Verify equality
XCTAssert(version1 != version2)
XCTAssert(version1 != version3)
XCTAssert(version1 != version4)
XCTAssert(version1 != version5)
XCTAssert(version1 != version6)
XCTAssert(version2 == version2alt)
XCTAssertFalse(version2 < version2alt)
XCTAssertFalse(version2 > version2alt)
// Checks invalid inputs
XCTAssertNil(Platform.Version(string: "192.168.0.0"))
XCTAssertNil(Platform.Version(string: "lorem ipsum"))
XCTAssertNil(Platform.Version(string: "12.1.2a"))
// Check the UInt32 encoding
XCTAssertEqual(version2.uint32, version2alt.uint32)
XCTAssertEqual(version2, Platform.Version(uint32: version2alt.uint32))
// This should be 13.3.0 composed by:
// UInt8(0) UInt8(13) UInt8(3) UInt8(0)
let bitVersion: UInt32 = 0b00000000000011010000001100000000
let converted = Platform.Version(uint32: bitVersion)
XCTAssertEqual(converted, Platform.Version(string: "13.3.0"))
}
func testNavigatorIndexLoopBreak() throws {
let navigatorNode1 = NavigatorTree.Node(item: NavigatorItem(pageType: 0,
languageID: 0,
title: "Top Page",
platformMask: 0,
availabilityID: 0),
bundleIdentifier: "com.test.bundle")
let navigatorNode2 = NavigatorTree.Node(item: NavigatorItem(pageType: 0,
languageID: 0,
title: "Middle Page",
platformMask: 0,
availabilityID: 0),
bundleIdentifier: "com.test.bundle")
let navigatorNode3 = NavigatorTree.Node(item: NavigatorItem(pageType: 0,
languageID: 0,
title: "Bottom Page",
platformMask: 0,
availabilityID: 0),
bundleIdentifier: "com.test.bundle")
let navigatorNode4 = NavigatorTree.Node(item: NavigatorItem(pageType: 0,
languageID: 0,
title: "Multi Page",
platformMask: 0,
availabilityID: 0),
bundleIdentifier: "com.test.bundle")
navigatorNode1.add(child: navigatorNode2)
navigatorNode1.add(child: navigatorNode4.copy())
navigatorNode2.add(child: navigatorNode3)
navigatorNode2.add(child: navigatorNode4.copy())
// Create a cycle
navigatorNode3.add(child: navigatorNode1)
let copy = navigatorNode1.copy()
XCTAssertEqual(copy.dumpTree(), """
Top Page
┣╸Middle Page
┃ ┣╸Bottom Page
┃ ┗╸Multi Page
┗╸Multi Page
""")
}
func testAvailabilityIndexCreation() throws {
#if !os(Linux) && !os(Android)
let availabilityIndex = AvailabilityIndex()
let macOS_10_14 = Platform(name: .macOS, version: Platform.Version(string: "10.14")!)
let macOS_10_14_9 = Platform(name: .macOS, version: Platform.Version(string: "10.14.9")!)
let iOS_10_15 = Platform(name: .iOS, version: Platform.Version(string: "10.15")!)
let iOS_11_11 = Platform(name: .iOS, version: Platform.Version(string: "11.1")!)
let iOS_9_1 = Platform(name: .iOS, version: Platform.Version(string: "9.1")!)
let iOS_9 = Platform(name: .iOS, version: Platform.Version(string: "9")!)
let iOS_6 = Platform(name: .iOS, version: Platform.Version(string: "6.0")!)
let iOS_5 = Platform(name: .iOS, version: Platform.Version(string: "5.0")!)
let info0 = AvailabilityIndex.Info(platformName: .macOS, introduced: Platform.Version(string: "10.15"))
let info1 = AvailabilityIndex.Info(platformName: .iOS, introduced: Platform.Version(string: "6.0"), deprecated: Platform.Version(string: "11.0"))
let info2 = AvailabilityIndex.Info(platformName: .iOS, introduced: Platform.Version(string: "12.0"))
let info3 = AvailabilityIndex.Info(platformName: .iOS, introduced: Platform.Version(string: "9.0"), deprecated: Platform.Version(string: "12.0"))
let info1alt = AvailabilityIndex.Info(platformName: .iOS, introduced: Platform.Version(string: "6.0"), deprecated: Platform.Version(string: "11.0"))
let infoMissing = AvailabilityIndex.Info(platformName: .watchOS, introduced: Platform.Version(string: "2.0"))
let platformOnly = AvailabilityIndex.Info(platformName: .iOS)
// Queries
XCTAssertFalse(info0.isDeprecated(on: iOS_10_15))
XCTAssertTrue(info1.isDeprecated(on: iOS_11_11))
XCTAssertFalse(info1.isDeprecated(on: iOS_9))
XCTAssertFalse(info0.isAvailable(on: macOS_10_14_9))
XCTAssertTrue(info1.isAvailable(on: iOS_9_1))
XCTAssertFalse(info1.isAvailable(on: iOS_5))
XCTAssertFalse(info0.isIntroduced(on: macOS_10_14))
XCTAssertTrue(info1.isIntroduced(on: iOS_6))
XCTAssertFalse(info1.isIntroduced(on: iOS_9))
XCTAssertTrue(platformOnly.isAvailable(on: iOS_9_1))
// Creation
XCTAssertEqual(availabilityIndex.id(for: info0, createIfMissing: true), 1)
XCTAssertEqual(availabilityIndex.id(for: info1, createIfMissing: true), 2)
XCTAssertEqual(availabilityIndex.id(for: info2, createIfMissing: true), 3)
XCTAssertEqual(availabilityIndex.id(for: info3, createIfMissing: true), 4)
// Ensure we match
XCTAssertEqual(availabilityIndex.id(for: info0), 1)
XCTAssertEqual(availabilityIndex.id(for: info1), 2)
XCTAssertEqual(availabilityIndex.id(for: info2), 3)
XCTAssertEqual(availabilityIndex.id(for: info3), 4)
// Alternate version
XCTAssertEqual(availabilityIndex.id(for: info1alt, createIfMissing: true), 2)
// Missing
XCTAssertNil(availabilityIndex.id(for: infoMissing))
XCTAssertEqual(availabilityIndex.platforms.count, 2)
XCTAssertEqual(availabilityIndex.versions(for: .iOS)?.count, 4)
XCTAssertEqual(availabilityIndex.versions(for: .macOS)?.count, 1)
let targetFolder = try createTemporaryDirectory()
let targetURL = targetFolder.appendingPathComponent("availability.index")
let jsonEncoder = JSONEncoder()
let data = try jsonEncoder.encode(availabilityIndex)
try data.write(to: targetURL)
let readData = try Data(contentsOf: targetURL)
let decodedIndex = try JSONDecoder().decode(AvailabilityIndex.self, from: readData)
// Ensure we still match
XCTAssertEqual(decodedIndex.id(for: info0), 1)
XCTAssertEqual(decodedIndex.id(for: info1), 2)
XCTAssertEqual(decodedIndex.id(for: info2), 3)
XCTAssertEqual(decodedIndex.id(for: info3), 4)
XCTAssertEqual(decodedIndex.platforms.count, 2)
XCTAssertEqual(decodedIndex.versions(for: .iOS)?.count, 4)
XCTAssertEqual(decodedIndex.versions(for: .macOS)?.count, 1)
#endif
}
func testAvailabilityIndexInterfaceLanguageBackwardsCompatibility() throws {
// Tests for backwards compatibility with an encoded `InterfaceLanguage` that does not include
// an `id`.
let plistWithoutLanguageID = """
<plist version="1.0">
<dict>
<key>data</key>
<dict>
</dict>
<key>interfaceLanguages</key>
<array>
<dict>
<key>mask</key>
<integer>1</integer>
<key>name</key>
<string>Swift</string>
</dict>
</array>
<key>languageToPlatforms</key>
<array>
</array>
<key>platforms</key>
<array>
</array>
</dict>
</plist>
"""
let availabilityIndex = try PropertyListDecoder().decode(
AvailabilityIndex.self,
from: Data(plistWithoutLanguageID.utf8)
)
XCTAssertEqual(availabilityIndex.interfaceLanguages.first?.name, "Swift")
XCTAssertEqual(availabilityIndex.interfaceLanguages.first?.id, "swift")
XCTAssertEqual(availabilityIndex.interfaceLanguages.first?.mask, 1)
}
func testRenderNodeToPageType() {
XCTAssertEqual(PageType(role: "symbol"), .symbol)
XCTAssertEqual(PageType(role: "containersymbol"), .symbol)
XCTAssertEqual(PageType(role: "restrequestsymbol"), .httpRequest)
XCTAssertEqual(PageType(role: "dictionarysymbol"), .dictionarySymbol)
XCTAssertEqual(PageType(role: "pseudosymbol"), .symbol)
XCTAssertEqual(PageType(role: "pseudocollection"), .framework)
XCTAssertEqual(PageType(role: "collection"), .framework)
XCTAssertEqual(PageType(role: "collectiongroup"), .symbol)
XCTAssertEqual(PageType(role: "article"), .article)
XCTAssertEqual(PageType(role: "samplecode"), .sampleCode)
XCTAssertEqual(PageType(symbolKind: "module"), .framework)
XCTAssertEqual(PageType(symbolKind: "class"), .class)
XCTAssertEqual(PageType(symbolKind: "cl"), .class)
XCTAssertEqual(PageType(symbolKind: "struct"), .structure)
XCTAssertEqual(PageType(symbolKind: "tag"), .structure)
XCTAssertEqual(PageType(symbolKind: "intf"), .protocol)
XCTAssertEqual(PageType(symbolKind: "protocol"), .protocol)
XCTAssertEqual(PageType(symbolKind: "enum"), .enumeration)
XCTAssertEqual(PageType(symbolKind: "func"), .function)
XCTAssertEqual(PageType(symbolKind: "function"), .function)
XCTAssertEqual(PageType(symbolKind: "extension"), .extension)
XCTAssertEqual(PageType(symbolKind: "data"), .globalVariable)
XCTAssertEqual(PageType(symbolKind: "tdef"), .typeAlias)
XCTAssertEqual(PageType(symbolKind: "typealias"), .typeAlias)
XCTAssertEqual(PageType(symbolKind: "intftdef"), .associatedType)
XCTAssertEqual(PageType(symbolKind: "op"), .operator)
XCTAssertEqual(PageType(symbolKind: "opfunc"), .operator)
XCTAssertEqual(PageType(symbolKind: "intfopfunc"), .operator)
XCTAssertEqual(PageType(symbolKind: "macro"), .macro)
XCTAssertEqual(PageType(symbolKind: "union"), .union)
XCTAssertEqual(PageType(symbolKind: "property"), .instanceProperty)
XCTAssertEqual(PageType(symbolKind: "dict"), .dictionarySymbol)
XCTAssertEqual(PageType(symbolKind: "namespace"), .namespace)
func verifySymbolKind(_ inputs: [String], _ result: PageType) {
for input in inputs {
XCTAssertEqual(PageType(symbolKind:input), result)
}
}
verifySymbolKind(["enumelt", "econst"], .enumerationCase)
verifySymbolKind(["enumctr", "structctr", "instctr", "intfctr", "constructor", "initializer"], .initializer)
verifySymbolKind(["enumm", "structm", "instm", "intfm"], .instanceMethod)
verifySymbolKind(["enump", "structp", "instp", "intfp", "unionp", "pseudo", "variable"], .instanceProperty)
verifySymbolKind(["enumdata", "structdata", "cldata", "clconst", "intfdata"], .instanceVariable)
verifySymbolKind(["enumsub", "structsub", "instsub", "intfsub"], .subscript)
verifySymbolKind(["enumcm", "structcm", "clm", "intfcm"], .typeMethod)
verifySymbolKind(["httpget", "httpput", "httppost", "httppatch", "httpdelete"], .httpRequest)
// Verify mappings provided from Delphi to SymbolKit
XCTAssertEqual(PageType(symbolKind: "tdef"), PageType(symbolKind: "typealias"))
XCTAssertEqual(PageType(symbolKind: "data"), PageType(symbolKind: "var"))
XCTAssertEqual(PageType(symbolKind: "func"), PageType(symbolKind: "func"))
XCTAssertEqual(PageType(symbolKind: "opfunc"), PageType(symbolKind: "func.op"))
XCTAssertEqual(PageType(symbolKind: "enum"), PageType(symbolKind: "enum"))
XCTAssertEqual(PageType(symbolKind: "enumdata"), PageType(symbolKind: "type.property"))
XCTAssertEqual(PageType(symbolKind: "enumcm"), PageType(symbolKind: "type.method"))
XCTAssertEqual(PageType(symbolKind: "enumctr"), PageType(symbolKind: "init"))
XCTAssertEqual(PageType(symbolKind: "enumm"), PageType(symbolKind: "method"))
XCTAssertEqual(PageType(symbolKind: "enumsub"), PageType(symbolKind: "subscript"))
XCTAssertEqual(PageType(symbolKind: "enump"), PageType(symbolKind: "property"))
XCTAssertEqual(PageType(symbolKind: "enumelt"), PageType(symbolKind: "enum.case"))
XCTAssertEqual(PageType(symbolKind: "struct"), PageType(symbolKind: "struct"))
XCTAssertEqual(PageType(symbolKind: "structcm"), PageType(symbolKind: "type.method"))
XCTAssertEqual(PageType(symbolKind: "structctr"), PageType(symbolKind: "init"))
XCTAssertEqual(PageType(symbolKind: "structdata"), PageType(symbolKind: "type.property"))
XCTAssertEqual(PageType(symbolKind: "structm"), PageType(symbolKind: "method"))
XCTAssertEqual(PageType(symbolKind: "structsub"), PageType(symbolKind: "subscript"))
XCTAssertEqual(PageType(symbolKind: "structp"), PageType(symbolKind: "property"))
XCTAssertEqual(PageType(symbolKind: "cl"), PageType(symbolKind: "class"))
XCTAssertEqual(PageType(symbolKind: "cldata"), PageType(symbolKind: "type.property"))
XCTAssertEqual(PageType(symbolKind: "clm"), PageType(symbolKind: "type.method"))
XCTAssertEqual(PageType(symbolKind: "instctr"), PageType(symbolKind: "init"))
XCTAssertEqual(PageType(symbolKind: "instm"), PageType(symbolKind: "method"))
XCTAssertEqual(PageType(symbolKind: "instsub"), PageType(symbolKind: "subscript"))
XCTAssertEqual(PageType(symbolKind: "instp"), PageType(symbolKind: "property"))
XCTAssertEqual(PageType(symbolKind: "intf"), PageType(symbolKind: "protocol"))
XCTAssertEqual(PageType(symbolKind: "intfdata"), PageType(symbolKind: "type.property"))
XCTAssertEqual(PageType(symbolKind: "intfcm"), PageType(symbolKind: "type.method"))
XCTAssertEqual(PageType(symbolKind: "intfctr"), PageType(symbolKind: "init"))
XCTAssertEqual(PageType(symbolKind: "intfm"), PageType(symbolKind: "method"))
XCTAssertEqual(PageType(symbolKind: "intfsub"), PageType(symbolKind: "subscript"))
XCTAssertEqual(PageType(symbolKind: "intfp"), PageType(symbolKind: "property"))
XCTAssertEqual(PageType(symbolKind: "intfopfunc"), PageType(symbolKind: "func.op"))
XCTAssertEqual(PageType(symbolKind: "intftdef"), PageType(symbolKind: "associatedtype"))
}
// rdar://84986427
// Mounting and unmounting the dmg creates noise on the bots when it fails.
// If the test fails before unmounting, the resource is leaked.
// This is currently the only test mounting anything, but with tests running
// in parallel, this could cause collisions.
func skip_testNavigatorIndexOnReadOnlyFilesystem() throws {
#if os(macOS)
// To verify we're able to open a read-only index, we need to mount a small DMG in read-only mode.
let dmgPath = Bundle.module.url(
forResource: "read-only-index", withExtension: "dmg", subdirectory: "Test Resources")!
// Mount the DMG.
let mountProcess = Process()
mountProcess.launchPath = "/usr/bin/hdiutil"
mountProcess.arguments = ["attach", dmgPath.path]
mountProcess.launch()
mountProcess.waitUntilExit()
// Check mounting worked.
guard mountProcess.terminationStatus == 0 else {
XCTFail("Read-only DMG mounting failed.")
return
}
// Verify we can open the index without errors.
let path = URL(fileURLWithPath: "/Volumes/ReadOnlyIndex/index")
XCTAssertNoThrow(try NavigatorIndex.readNavigatorIndex(url: path))
// Detatch the Volume.
let detatchProcess = Process()
detatchProcess.launchPath = "/usr/bin/hdiutil"
detatchProcess.arguments = ["detach", "/Volumes/ReadOnlyIndex"]
detatchProcess.launch()
detatchProcess.waitUntilExit()
XCTAssertEqual(detatchProcess.terminationStatus, 0)
#endif
}
func testNavigatorIndexAsReadOnlyFile() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let converter = DocumentationNodeConverter(bundle: bundle, context: context)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: "org.swift.docc.test", sortRootChildrenByName: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try converter.convert(entity, at: source)
try builder.index(renderNode: renderNode)
}
builder.finalize()
// Get the database file.
let dataFileURL = targetURL.appendingPathComponent("data.mdb")
// Set data file as read-only so we make sure we don't crash if the user has not writing permission on the database file.
try FileManager.default.setAttributes([FileAttributeKey.posixPermissions: NSNumber(value: 0o400)], ofItemAtPath: dataFileURL.path)
// Ensure we can read the navigator index even if the data file is read-only.
_ = try NavigatorIndex.readNavigatorIndex(url: targetURL, readNavigatorTree: false)
// Remove all permissions to the file.
try FileManager.default.setAttributes([FileAttributeKey.posixPermissions: NSNumber(value: 0o000)], ofItemAtPath: dataFileURL.path)
// Make sure we throw if an index can't be opened even after the fallback, avoiding entering an infinite loop.
XCTAssertThrowsError(try NavigatorIndex.readNavigatorIndex(url: targetURL, readNavigatorTree: false))
}
func testNavigatorTitle() throws {
var json = buildRenderJSON(title: "Failure", symbolKind: "associatedtype", fragments: """
[
{
"text": "associatedtype",
"kind": "keyword"
},
{
"kind": "text",
"text": " "
},
{
"text": "Failure",
"kind": "identifier"
},
{
"text": " : ",
"kind": "text"
},
{
"kind": "typeIdentifier",
"preciseIdentifier": "s:s5ErrorP",
"text": "Error"
}
]
"""
)
var renderNode = try RenderNode.decode(fromJSON: Data(json.utf8))
XCTAssertEqual(renderNode.navigatorTitle(), "Failure")
json = buildRenderJSON(title: "Subscriber", symbolKind: "protocol", fragments: """
[
{
"text": "protocol",
"kind": "keyword"
},
{
"kind": "text",
"text": " "
},
{
"text": "Subscriber",
"kind": "identifier"
}
]
"""
)
renderNode = try RenderNode.decode(fromJSON: Data(json.utf8))
XCTAssertEqual(renderNode.navigatorTitle(), "Subscriber")
json = buildRenderJSON(title: "receive(subscription:)", symbolKind: "method", fragments: """
[
{
"kind": "keyword",
"text": "func"
},
{
"text": " ",
"kind": "text"
},
{
"text": "receive",
"kind": "identifier"
},
{
"kind": "text",
"text": "("
},
{
"text": "subscription",
"kind": "externalParam"
},
{
"kind": "text",
"text": ": "
},
{
"kind": "typeIdentifier",
"preciseIdentifier": "s:7Combine12SubscriptionP",
"text": "Subscription"
},
{
"text": ")",
"kind": "text"
}
]
"""
)
renderNode = try RenderNode.decode(fromJSON: Data(json.utf8))
XCTAssertEqual(renderNode.navigatorTitle(), "func receive(subscription: Subscription)")
json = buildRenderJSON(title: "init(_:)", symbolKind: "structctr", fragments: """
[
{
"kind": "identifier",
"text": "init"
},
{
"kind": "text",
"text": "(Double)"
}
]
"""
)
renderNode = try RenderNode.decode(fromJSON: Data(json.utf8))
XCTAssertEqual(renderNode.navigatorTitle(), "init(Double)")
}
func testNavigatorTitleForEmptyMetadataNavigatorTitle() throws {
let json = buildRenderJSON(
title: "init(_:)",
symbolKind: "not-struct",
fragments: """
[
{
"kind": "identifier",
"text": "Fragment Value"
}
]
""",
language: "occ"
)
let renderNode = try RenderNode.decode(fromJSON: Data(json.utf8))
XCTAssertEqual(
renderNode.navigatorTitle(),
"Fragment Value"
)
}
func testSavesNodePresentationDisambiguator() {
let node = Node(
item: NavigatorItem(
pageType: PageType.article.rawValue,
languageID: Language.swift.rawValue,
title: "",
platformMask: 0,
availabilityID: 0),
bundleIdentifier: ""
)
node.presentationIdentifier = "the-disambiguator"
XCTAssertEqual(node.presentationIdentifier, "the-disambiguator")
}
func testPathHasher() throws {
let pathHasher = try XCTUnwrap(PathHasher(rawValue: "MD5"))
// Test that the results are stable for the given inputs
(0...100).forEach { _ in
XCTAssertEqual("41dc6c05a0b5", pathHasher.hash("/documentation/foundation/nsurlsessionwebsockettask"))
XCTAssertEqual("ffdc704430d3", pathHasher.hash("/documentation/foundation/urlsessionwebsockettask/3281790-send"))
XCTAssertEqual("1161063e700c", pathHasher.hash("/documentation/swiftui/texteditor/disableautocorrection(_:)"))
XCTAssertEqual("e47cfd13c4af", pathHasher.hash("/mykit/myclass/myfunc"))
}
}
func testNormalizedNavigatorIndexIdentifier() throws {
let topicReference = ResolvedTopicReference(
bundleIdentifier: "org.swift.example",
path: "/documentation/path/sub-path",
fragment: nil,
sourceLanguage: .swift
)
XCTAssertEqual(
topicReference.normalizedNavigatorIndexIdentifier(forLanguage: 0),
NavigatorIndex.Identifier(
bundleIdentifier: "org.swift.example",
path: "/documentation/path/sub-path",
fragment: nil,
languageIdentifier: 0
)
)
let topicReferenceWithCapitalization = ResolvedTopicReference(
bundleIdentifier: "org.Swift.Example",
path: "/documentation/Path/subPath",
fragment: nil,
sourceLanguage: .swift
)
XCTAssertEqual(
topicReferenceWithCapitalization.normalizedNavigatorIndexIdentifier(forLanguage: 1),
NavigatorIndex.Identifier(
bundleIdentifier: "org.swift.example",
path: "/documentation/path/subpath",
fragment: nil,
languageIdentifier: 1
)
)
let topicReferenceWithFragment = ResolvedTopicReference(
bundleIdentifier: "org.Swift.Example",
path: "/documentation/Path/subPath",
fragment: "FRAGMENT",
sourceLanguage: .swift
)
XCTAssertEqual(
topicReferenceWithFragment.normalizedNavigatorIndexIdentifier(forLanguage: 1),
NavigatorIndex.Identifier(
bundleIdentifier: "org.swift.example",
path: "/documentation/path/subpath",
fragment: "FRAGMENT",
languageIdentifier: 1
)
)
}
func testAnonymousTopicGroups() throws {
let navigatorIndex = try generatedNavigatorIndex(
for: "AnonymousTopicGroups",
bundleIdentifier: "org.swift.docc.example"
)
// The root page curates 'My Article' once without a topic group heading, and once with.
XCTAssertEqual(
navigatorIndex.navigatorTree.root.dumpTree(),
"""
[Root]
┗╸Swift
┗╸AnonymousTopicGroups
┣╸My Article
┣╸My Topic Group
┗╸My Article
"""
)
}
func testNavigatorDoesNotContainOverloads() throws {
enableFeatureFlag(\.isExperimentalOverloadedSymbolPresentationEnabled)
let navigatorIndex = try generatedNavigatorIndex(
for: "OverloadedSymbols",
bundleIdentifier: "com.shapes.ShapeKit")
XCTAssertEqual(
navigatorIndex.navigatorTree.root.dumpTree(),
"""
[Root]
┗╸Swift
┗╸ShapeKit
┣╸Protocols
┣╸OverloadedProtocol
┃ ┣╸Instance Methods
┃ ┗╸func fourthTestMemberName(test:)
┣╸Structures
┣╸OverloadedByCaseStruct
┃ ┣╸Instance Properties
┃ ┣╸let ThirdTestMemberName: Int
┃ ┣╸let thirdTestMemberNamE: Int
┃ ┣╸let thirdTestMemberName: Int
┃ ┗╸let thirdtestMemberName: Int
┣╸OverloadedParentStruct
┃ ┣╸Type Properties
┃ ┗╸static let fifthTestMember: Int
┣╸OverloadedStruct
┃ ┣╸Instance Properties
┃ ┣╸let secondTestMemberName: Int
┃ ┣╸Type Properties
┃ ┗╸static let secondTestMemberName: Int
┣╸RegularParent
┃ ┣╸Instance Properties
┃ ┣╸let firstMember: Int
┃ ┣╸Instance Methods
┃ ┣╸func secondMember(first: Int, second: String)
┃ ┣╸Type Properties
┃ ┣╸static let thirdMember: Int
┃ ┣╸Enumerations
┃ ┗╸RegularParent.FourthMember
┣╸overloadedparentstruct
┃ ┣╸Instance Properties
┃ ┗╸let fifthTestMember: Int
┣╸Enumerations
┗╸OverloadedEnum
┣╸Enumeration Cases
┣╸case firstTestMemberName(String)
┣╸Instance Methods
┗╸func firstTestMemberName(_:)
"""
)
}
func generatedNavigatorIndex(for testBundleName: String, bundleIdentifier: String) throws -> NavigatorIndex {
let (bundle, context) = try testBundleAndContext(named: testBundleName)
let renderContext = RenderContext(documentationContext: context, bundle: bundle)
let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext)
let targetURL = try createTemporaryDirectory()
let builder = NavigatorIndex.Builder(outputURL: targetURL, bundleIdentifier: bundleIdentifier, sortRootChildrenByName: true, groupByLanguage: true)
builder.setup()
for identifier in context.knownPages {
let source = context.documentURL(for: identifier)
let entity = try context.entity(with: identifier)
let renderNode = try XCTUnwrap(converter.renderNode(for: entity, at: source))
try builder.index(renderNode: renderNode)
}
builder.finalize()
let navigatorIndex = try NavigatorIndex.readNavigatorIndex(url: targetURL)
let expectation = XCTestExpectation(description: "Load the tree asynchronously.")
try navigatorIndex.readNavigatorTree(timeout: 1.0, queue: DispatchQueue(label: "org.swift.docc.example.queue")) { (_, isCompleted, error) in
XCTAssertNil(error)
if isCompleted {
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 10.0)
return navigatorIndex
}
}
/// This function compares two nodes to ensure their data is equal.
fileprivate func compare(lhs: Node, rhs: Node) -> Bool {
func dump(node: Node) -> [NavigatorItem] {
var index = 0
var queue = [node]
while index < queue.count {
let node = queue[index]
if node.children.count > 0 {
queue.append(contentsOf: node.children)
}
index += 1
}
return queue.map { $0.item }
}
let dump1 = dump(node: lhs)
let dump2 = dump(node: rhs)
return dump1 == dump2
}
/// Search for the first node with
fileprivate func search(node: Node, matching predicate: (Node) -> Bool) -> Node? {
if predicate(node) { return node }
for child in node.children {
if let result = search(node: child, matching: predicate) {
return result
}
}
return nil
}
/// Validate a tree with a given validator function
fileprivate func validateTree(node: NavigatorTree.Node, validator: (NavigatorTree.Node) -> Bool) -> Bool {
if validator(node) == false { return false }
for child in node.children {
if validateTree(node: child, validator: validator) == false { return false }
}
return true
}
fileprivate func assertUniqueIDs(node: NavigatorTree.Node, message: String = "The tree has duplicated IDs.", file: StaticString = #file, line: UInt = #line) {
var collector = Set<UInt32>()
var brokenItemTitle = ""
let valid = validateTree(node: node) { (node) -> Bool in
guard let id = node.id, !collector.contains(id) else {
brokenItemTitle = node.item.title
return false
}
collector.insert(id)
return true
}
XCTAssertTrue(valid, message + " Item title: \"\(brokenItemTitle)\".", file: file, line: line)
}
fileprivate func testTree(named name: String) throws -> String {
let fileURL = Bundle.module.url(
forResource: name, withExtension: "txt", subdirectory: "Test Resources")!
return try String(contentsOf: fileURL).trimmingCharacters(in: .newlines)
}
fileprivate func buildRenderJSON(
title: String,
symbolKind: String,
fragments: String,
language: String = "swift"
) -> String {
return """
{
"abstract": [],
"hierarchy": { "paths": [] },
"identifier": { "interfaceLanguage": "\(language)", "url": "doc://org.swift.docc.example/documentation/test-item" },
"kind": "symbol",
"metadata": {
"modules": [ { "name": "MyKit" } ],
"roleHeading": "My Heading",
"title": "\(title)",
"symbolKind": "\(symbolKind)",
"fragments": \(fragments)
},
"primaryContentSections": [],
"references": {},
"schemaVersion": { "major": 1, "minor": 0, "patch": 0 },
"sections": [],
"seeAlsoSections": [],
"topicSections": []
}
"""
}
|