|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2025 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 | +import Markdown |
| 14 | + |
| 15 | +class InvalidCodeBlockOptionTests: XCTestCase { |
| 16 | + |
| 17 | + func testOption() { |
| 18 | + let markupSource = """ |
| 19 | +```nocopy |
| 20 | +let a = 1 |
| 21 | +``` |
| 22 | +""" |
| 23 | + let document = Document(parsing: markupSource, options: []) |
| 24 | + var checker = InvalidCodeBlockOption(sourceFile: nil) |
| 25 | + checker.visit(document) |
| 26 | + XCTAssertTrue(checker.problems.isEmpty) |
| 27 | + } |
| 28 | + |
| 29 | + func testMultipleOptionTypos() { |
| 30 | + let markupSource = """ |
| 31 | +```nocoy |
| 32 | +let b = 2 |
| 33 | +``` |
| 34 | +
|
| 35 | +```nocoy |
| 36 | +let c = 3 |
| 37 | +``` |
| 38 | +""" |
| 39 | + let document = Document(parsing: markupSource, options: []) |
| 40 | + var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file)) |
| 41 | + checker.visit(document) |
| 42 | + XCTAssertEqual(2, checker.problems.count) |
| 43 | + |
| 44 | + for problem in checker.problems { |
| 45 | + XCTAssertEqual("org.swift.docc.InvalidCodeBlockOption", problem.diagnostic.identifier) |
| 46 | + XCTAssertEqual(problem.diagnostic.summary, "Unknown option 'nocoy' in code block.") |
| 47 | + XCTAssertEqual(problem.possibleSolutions.map(\.summary), ["Replace 'nocoy' with 'nocopy'."]) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + func testOptionDifferentTypos() throws { |
| 52 | + let markupSource = """ |
| 53 | +```swift, nocpy |
| 54 | +let d = 4 |
| 55 | +``` |
| 56 | +
|
| 57 | +```unknown, nocpoy |
| 58 | +let e = 5 |
| 59 | +``` |
| 60 | +
|
| 61 | +```nocopy |
| 62 | +let f = 6 |
| 63 | +``` |
| 64 | +
|
| 65 | +```ncopy |
| 66 | +let g = 7 |
| 67 | +``` |
| 68 | +""" |
| 69 | + let document = Document(parsing: markupSource, options: []) |
| 70 | + var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file)) |
| 71 | + checker.visit(document) |
| 72 | + |
| 73 | + XCTAssertEqual(3, checker.problems.count) |
| 74 | + |
| 75 | + let summaries = checker.problems.map { $0.diagnostic.summary } |
| 76 | + XCTAssertEqual(summaries, [ |
| 77 | + "Unknown option 'nocpy' in code block.", |
| 78 | + "Unknown option 'nocpoy' in code block.", |
| 79 | + "Unknown option 'ncopy' in code block.", |
| 80 | + ]) |
| 81 | + |
| 82 | + for problem in checker.problems { |
| 83 | + XCTAssertEqual( |
| 84 | + "org.swift.docc.InvalidCodeBlockOption", |
| 85 | + problem.diagnostic.identifier |
| 86 | + ) |
| 87 | + |
| 88 | + XCTAssertEqual(problem.possibleSolutions.count, 1) |
| 89 | + let solution = try XCTUnwrap(problem.possibleSolutions.first) |
| 90 | + XCTAssert(solution.summary.hasSuffix("with 'nocopy'.")) |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments