|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2022 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 XCTest |
| 12 | +@testable import SwiftDocC |
| 13 | + |
| 14 | +class DocumentationContext_MixedLanguageSourceLanguagesTests: XCTestCase { |
| 15 | + func testArticleAvailableSourceLanguagesIsSwiftInSwiftModule() throws { |
| 16 | + try assertArticleAvailableSourceLanguages( |
| 17 | + moduleAvailableLanguages: [.swift], |
| 18 | + expectedArticleDefaultLanguage: .swift |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + func testArticleAvailableSourceLanguagesIsMixedLanguageInMixedLanguageModule() throws { |
| 23 | + try assertArticleAvailableSourceLanguages( |
| 24 | + moduleAvailableLanguages: [.swift, .objectiveC], |
| 25 | + expectedArticleDefaultLanguage: .swift |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + func testArticleAvailableSourceLanguagesIsObjectiveCInObjectiveCModule() throws { |
| 30 | + try assertArticleAvailableSourceLanguages( |
| 31 | + moduleAvailableLanguages: [.objectiveC], |
| 32 | + expectedArticleDefaultLanguage: .objectiveC |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + func assertArticleAvailableSourceLanguages( |
| 37 | + moduleAvailableLanguages: Set<SourceLanguage>, |
| 38 | + expectedArticleDefaultLanguage: SourceLanguage, |
| 39 | + file: StaticString = #file, |
| 40 | + line: UInt = #line |
| 41 | + ) throws { |
| 42 | + precondition( |
| 43 | + moduleAvailableLanguages.allSatisfy { [.swift, .objectiveC].contains($0) }, |
| 44 | + "moduleAvailableLanguages can only contain Swift and Objective-C as languages." |
| 45 | + ) |
| 46 | + |
| 47 | + let (_, _, context) = try testBundleAndContext(copying: "MixedLanguageFramework") { url in |
| 48 | + try """ |
| 49 | + # MyArticle |
| 50 | + |
| 51 | + The framework this article is documenting is available in the following languages: \ |
| 52 | + \(moduleAvailableLanguages.map(\.name).joined(separator: ",")). |
| 53 | + """.write(to: url.appendingPathComponent("myarticle.md"), atomically: true, encoding: .utf8) |
| 54 | + |
| 55 | + func removeSymbolGraph(compiler: String) throws { |
| 56 | + try FileManager.default.removeItem( |
| 57 | + at: url.appendingPathComponent("symbol-graphs").appendingPathComponent(compiler) |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + if !moduleAvailableLanguages.contains(.swift) { |
| 62 | + try removeSymbolGraph(compiler: "swift") |
| 63 | + } |
| 64 | + |
| 65 | + if !moduleAvailableLanguages.contains(.objectiveC) { |
| 66 | + try removeSymbolGraph(compiler: "clang") |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + let articleNode = try XCTUnwrap( |
| 71 | + context.documentationCache.first { |
| 72 | + $0.key.path == "/documentation/MixedLanguageFramework/myarticle" |
| 73 | + }?.value, |
| 74 | + file: file, |
| 75 | + line: line |
| 76 | + ) |
| 77 | + |
| 78 | + XCTAssertEqual( |
| 79 | + articleNode.availableSourceLanguages, |
| 80 | + moduleAvailableLanguages, |
| 81 | + "Expected the article's source languages to have inherited from the module's available source languages.", |
| 82 | + file: file, |
| 83 | + line: line |
| 84 | + ) |
| 85 | + |
| 86 | + XCTAssertEqual( |
| 87 | + articleNode.sourceLanguage, |
| 88 | + expectedArticleDefaultLanguage, |
| 89 | + file: file, |
| 90 | + line: line |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
0 commit comments