|
| 1 | +import Foundation |
| 2 | +import XCTest |
| 3 | +import SwiftSyntax |
| 4 | + |
| 5 | +@testable import Rules |
| 6 | + |
| 7 | +public class ValidateDocumentationCommentsTests: DiagnosingTestCase { |
| 8 | + public func testParameterDocumentation() { |
| 9 | + let input = |
| 10 | + """ |
| 11 | + /// Uses 'Parameters' when it only has one parameter. |
| 12 | + /// |
| 13 | + /// - Parameters singular: singular description. |
| 14 | + /// - Returns: A string containing the contents of a |
| 15 | + /// description |
| 16 | + func testPluralParamDesc(singular: String) -> Bool {} |
| 17 | +
|
| 18 | + /// Uses 'Parameter' with a list of parameters. |
| 19 | + /// |
| 20 | + /// - Parameter |
| 21 | + /// - command: The command to execute in the shell environment. |
| 22 | + /// - stdin: The string to use as standard input. |
| 23 | + /// - Returns: A string containing the contents of the invoked process's |
| 24 | + /// standard output. |
| 25 | + func execute(command: String, stdin: String) -> String { |
| 26 | + // ... |
| 27 | + } |
| 28 | +
|
| 29 | + /// Returns the output generated by executing a command with the given string |
| 30 | + /// used as standard input. |
| 31 | + /// |
| 32 | + /// - Parameter command: The command to execute in the shell environment. |
| 33 | + /// - Parameter stdin: The string to use as standard input. |
| 34 | + /// - Returns: A string containing the contents of the invoked process's |
| 35 | + /// standard output. |
| 36 | + func testInvalidParameterDesc(command: String, stdin: String) -> String {} |
| 37 | + """ |
| 38 | + performLint(ValidateDocumentationComments.self, input: input) |
| 39 | + XCTAssertDiagnosed(.useSingularParameter) |
| 40 | + XCTAssertDiagnosed(.usePluralParameters) |
| 41 | + XCTAssertDiagnosed(.usePluralParameters) |
| 42 | + } |
| 43 | + |
| 44 | + public func testParametersName() { |
| 45 | + let input = |
| 46 | + """ |
| 47 | + /// Parameters dont match. |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - sum: The sum of all numbers. |
| 51 | + /// - avg: The average of all numbers. |
| 52 | + /// - Returns: The sum of sum and avg. |
| 53 | + func sum(avg: Int, sum: Int) -> Int {} |
| 54 | +
|
| 55 | + /// Missing one parameter documentation. |
| 56 | + /// |
| 57 | + /// - Parameters: |
| 58 | + /// - p1: Parameter 1. |
| 59 | + /// - p2: Parameter 2. |
| 60 | + /// - Returns: an integer. |
| 61 | + func foo(p1: Int, p2: Int, p3: Int) -> Int {} |
| 62 | + """ |
| 63 | + performLint(ValidateDocumentationComments.self, input: input) |
| 64 | + XCTAssertDiagnosed(.parametersDontMatch(funcName: "sum")) |
| 65 | + XCTAssertDiagnosed(.parametersDontMatch(funcName: "foo")) |
| 66 | + } |
| 67 | + |
| 68 | + public func testReturnDocumentation() { |
| 69 | + let input = |
| 70 | + """ |
| 71 | + /// One sentence summary. |
| 72 | + /// |
| 73 | + /// - Parameters: |
| 74 | + /// - p1: Parameter 1. |
| 75 | + /// - p2: Parameter 2. |
| 76 | + /// - Returns: an integer. |
| 77 | + func noReturn(p1: Int, p2: Int, p3: Int) {} |
| 78 | +
|
| 79 | + /// One sentence summary. |
| 80 | + /// |
| 81 | + /// - Parameters: |
| 82 | + /// - p1: Parameter 1. |
| 83 | + /// - p2: Parameter 2. |
| 84 | + func foo(p1: Int, p2: Int, p3: Int) -> Int {} |
| 85 | + """ |
| 86 | + performLint(ValidateDocumentationComments.self, input: input) |
| 87 | + XCTAssertDiagnosed(.removeReturnComment(funcName: "noReturn")) |
| 88 | + XCTAssertDiagnosed(.documentReturnValue(funcName: "foo")) |
| 89 | + } |
| 90 | + |
| 91 | + public func testValidDocumentation() { |
| 92 | + let input = |
| 93 | + """ |
| 94 | + /// Returns the output generated by executing a command. |
| 95 | + /// |
| 96 | + /// - Parameter command: The command to execute in the shell environment. |
| 97 | + /// - Returns: A string containing the contents of the invoked process's |
| 98 | + /// standard output. |
| 99 | + func singularParam(command: String) -> String { |
| 100 | + // ... |
| 101 | + } |
| 102 | +
|
| 103 | + /// Returns the output generated by executing a command with the given string |
| 104 | + /// used as standard input. |
| 105 | + /// |
| 106 | + /// - Parameters: |
| 107 | + /// - command: The command to execute in the shell environment. |
| 108 | + /// - stdin: The string to use as standard input. |
| 109 | + /// - Returns: A string containing the contents of the invoked process's |
| 110 | + /// standard output. |
| 111 | + func pluralParam(command: String, stdin: String) -> String { |
| 112 | + // ... |
| 113 | + } |
| 114 | +
|
| 115 | + /// Parameter(s) and Returns tags may be omitted only if the single-sentence |
| 116 | + /// brief summary fully describes the meaning of those items and including the |
| 117 | + /// tags would only repeat what has already been said |
| 118 | + func ommitedFunc(p1: Int) |
| 119 | + """ |
| 120 | + performLint(ValidateDocumentationComments.self, input: input) |
| 121 | + XCTAssertNotDiagnosed(.useSingularParameter) |
| 122 | + XCTAssertNotDiagnosed(.usePluralParameters) |
| 123 | + |
| 124 | + XCTAssertNotDiagnosed(.documentReturnValue(funcName: "singularParam")) |
| 125 | + XCTAssertNotDiagnosed(.removeReturnComment(funcName: "singularParam")) |
| 126 | + XCTAssertNotDiagnosed(.parametersDontMatch(funcName: "singularParam")) |
| 127 | + |
| 128 | + XCTAssertNotDiagnosed(.documentReturnValue(funcName: "pluralParam")) |
| 129 | + XCTAssertNotDiagnosed(.removeReturnComment(funcName: "pluralParam")) |
| 130 | + XCTAssertNotDiagnosed(.parametersDontMatch(funcName: "pluralParam")) |
| 131 | + |
| 132 | + XCTAssertNotDiagnosed(.documentReturnValue(funcName: "ommitedFunc")) |
| 133 | + XCTAssertNotDiagnosed(.removeReturnComment(funcName: "ommitedFunc")) |
| 134 | + XCTAssertNotDiagnosed(.parametersDontMatch(funcName: "ommitedFunc")) |
| 135 | + } |
| 136 | + |
| 137 | + #if !os(macOS) |
| 138 | + static let allTests = [ |
| 139 | + ValidateDocumentationCommentsTests.testParameterDocumentation, |
| 140 | + ValidateDocumentationCommentsTests.testParametersName, |
| 141 | + ValidateDocumentationCommentsTests.tesReturnDocumentation, |
| 142 | + ValidateDocumentationCommentsTests.testValidDocumentation |
| 143 | + ] |
| 144 | + #endif |
| 145 | +} |
0 commit comments