|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import XCTest |
| 13 | +@testable import SwiftDocC |
| 14 | +import SwiftDocCTestUtilities |
| 15 | + |
| 16 | +class HeadingAnchorTests: XCTestCase { |
| 17 | + func testEncodeHeadingAnchor() throws { |
| 18 | + let catalogURL = try createTempFolder(content: [ |
| 19 | + Folder(name: "unit-test.docc", content: [ |
| 20 | + TextFile(name: "Root.md", utf8Content: """ |
| 21 | + # My root page |
| 22 | + |
| 23 | + This single article defines two headings and links to them |
| 24 | + |
| 25 | + @Metadata { |
| 26 | + @TechnologyRoot |
| 27 | + } |
| 28 | +
|
| 29 | + ### テスト |
| 30 | + - <doc:#テスト> |
| 31 | + |
| 32 | + ### Some heading |
| 33 | + - <doc:#Some-heading> |
| 34 | + """), |
| 35 | + ]) |
| 36 | + ]) |
| 37 | + let (_, bundle, context) = try loadBundle(from: catalogURL) |
| 38 | + |
| 39 | + let reference = try XCTUnwrap(context.soleRootModuleReference) |
| 40 | + let node = try context.entity(with: reference) |
| 41 | + let renderContext = RenderContext(documentationContext: context, bundle: bundle) |
| 42 | + let converter = DocumentationContextConverter(bundle: bundle, context: context, renderContext: renderContext) |
| 43 | + let renderNode = try XCTUnwrap(converter.renderNode(for: node, at: nil)) |
| 44 | + |
| 45 | + // Check heading anchors are encoded |
| 46 | + let contentSection = try XCTUnwrap(renderNode.primaryContentSections.first as? ContentRenderSection) |
| 47 | + let headings: [RenderBlockContent.Heading] = contentSection.content.compactMap { |
| 48 | + if case .heading(let heading) = $0 { |
| 49 | + return heading |
| 50 | + } else { |
| 51 | + return nil |
| 52 | + } |
| 53 | + } |
| 54 | + XCTAssertEqual(headings[0].anchor, "%E3%83%86%E3%82%B9%E3%83%88") |
| 55 | + XCTAssertEqual(headings[1].anchor, "Some-heading") |
| 56 | + |
| 57 | + // Check links to them |
| 58 | + let testTopic0 = try XCTUnwrap(renderNode.references["doc://unit-test/documentation/Root#%E3%83%86%E3%82%B9%E3%83%88"] as? TopicRenderReference) |
| 59 | + XCTAssertEqual(testTopic0.url, "/documentation/root#%E3%83%86%E3%82%B9%E3%83%88") |
| 60 | + let testTopic1 = try XCTUnwrap(renderNode.references["doc://unit-test/documentation/Root#Some-heading"] as? TopicRenderReference) |
| 61 | + XCTAssertEqual(testTopic1.url, "/documentation/root#Some-heading") |
| 62 | + } |
| 63 | +} |
0 commit comments