Skip to content

Commit 2311f65

Browse files
committed
Test for the tool
1 parent 02776d0 commit 2311f65

File tree

9 files changed

+92
-108
lines changed

9 files changed

+92
-108
lines changed

Sources/SwiftSource/SwiftAccessLevel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import SwiftSyntax
2525

2626

27-
public enum SwiftAccessLevel: Int {
27+
public enum SwiftAccessLevel: Int, Decodable {
2828
case `open`
2929
case `public`
3030
case `internal`

Sources/SwiftSource/SwiftComment.swift

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,26 @@
2424
import SwiftSyntax
2525

2626

27-
public struct SwiftComment {
28-
private let piece: TriviaPiece
29-
30-
public var text: String {
31-
switch piece {
32-
case .lineComment(let text): return text
33-
case .blockComment(let text): return text
34-
case .docLineComment(let text): return text
35-
case .docBlockComment(let text): return text
36-
default: return ""
37-
}
38-
}
39-
40-
public var isDoc: Bool {
41-
switch piece {
42-
case .docLineComment: fallthrough
43-
case .docBlockComment: return true
44-
default: return false
45-
}
46-
}
27+
public struct SwiftComment: Decodable {
28+
public let text: String
29+
public var isDoc: Bool
4730

4831
init?(piece: TriviaPiece) {
4932
switch piece {
50-
case .lineComment: fallthrough
51-
case .blockComment: fallthrough
52-
case .docLineComment: fallthrough
53-
case .docBlockComment: self.piece = piece
54-
default: return nil
33+
case .lineComment(let value):
34+
text = value
35+
isDoc = false
36+
case .blockComment(let value):
37+
text = value
38+
isDoc = false
39+
case .docLineComment(let value):
40+
text = value
41+
isDoc = true
42+
case .docBlockComment(let value):
43+
text = value
44+
isDoc = true
45+
default:
46+
return nil
5547
}
5648
}
5749
}

Sources/SwiftSource/SwiftDeclaration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fileprivate struct StringBuilder {
4747
}
4848
}
4949

50-
public struct SwiftDeclaration {
50+
public struct SwiftDeclaration: Decodable {
5151
public let comments: [SwiftComment]
5252
public let accessLevel: SwiftAccessLevel
5353
public var keyword: SwiftKeyword

Sources/SwiftSource/SwiftKeyword.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import SwiftSyntax
2525

2626

27-
public enum SwiftKeyword: String {
27+
public enum SwiftKeyword: String, Decodable {
2828
case actor
2929
case `associatedtype`
3030
case `case`

Sources/SwiftSource/SwiftSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Foundation
2525
import SwiftSyntax
2626

2727

28-
public struct SwiftSource {
28+
public struct SwiftSource: Decodable {
2929
public let url: URL?
3030
public let declarations: [SwiftDeclaration]
3131

Sources/swift-doc-coverage/main.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ struct SwiftDocCoverage: ParsableCommand {
5454

5555
static var configuration = CommandConfiguration(
5656
abstract: "Generates documentation coverage statistics for Swift files.",
57-
version: "1.0.0"
57+
version: "1.1.0"
5858
)
5959

60-
@Argument(help: "One or more paths to a directory containing Swift files.")
60+
@Argument(help: "One or more paths to directories or Swift files.")
6161
var inputs: [String]
6262

6363
@Option(name: .shortAndLong, help: "An option to skip hidden files.")
@@ -75,11 +75,12 @@ struct SwiftDocCoverage: ParsableCommand {
7575
@Option(name: .shortAndLong, help: "The file path for generated report.")
7676
var output: String?
7777

78+
var sources: [SwiftSource] = []
79+
7880
mutating func run() throws {
79-
var out: Output = TerminalOutput()
80-
if let path = output {
81-
out = try FileOutput(path: path)
82-
}
81+
let out: Output = output != nil
82+
? try FileOutput(path: output!)
83+
: TerminalOutput()
8384

8485
let urls = try inputs.flatMap {
8586
try Self.files(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles, ignoreFilenameRegex: ignoreFilenameRegex)
@@ -94,7 +95,7 @@ struct SwiftDocCoverage: ParsableCommand {
9495
let minAccessLevel = minimumAccessLevel.accessLevel.rawValue
9596

9697
// Sources
97-
let sources: [SwiftSource] = try urls.map { url in
98+
sources = try urls.map { url in
9899
let sourceTime = Date()
99100

100101
let source = try SwiftSource(fileURL: url)
@@ -142,7 +143,13 @@ struct SwiftDocCoverage: ParsableCommand {
142143
}
143144
}
144145

145-
static func files(path: String, ext: String, skipsHiddenFiles: Bool, ignoreFilenameRegex: String) throws -> [URL] {
146+
static func run(_ arguments: [String]? = nil) throws -> Self {
147+
var cmd = try Self.parse(arguments)
148+
try cmd.run()
149+
return cmd
150+
}
151+
152+
static func files(path: String, ext: String, skipsHiddenFiles: Bool, ignoreFilenameRegex: String) throws -> [URL] {
146153
var isDirectory: ObjCBool = false
147154
guard FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) else {
148155
throw "Path not found."
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
22

3-
struct Point {
3+
public struct Point {
44
var x = 0.0, y = 0.0
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
22

3-
struct Size {
3+
public struct Size {
44
var width = 0.0, height = 0.0
55
}

Tests/SwiftDocCoverageTests/SwiftDocCoverageTests.swift

Lines changed: 54 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import XCTest
22
/*@testable */import SwiftSource
3+
@testable import swift_doc_coverage
34

4-
extension String : LocalizedError {
5-
public var errorDescription: String? { self }
6-
}
75

8-
func tempDirectory() -> URL {
9-
let url = URL(fileURLWithPath: NSTemporaryDirectory())
10-
.appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString)
6+
func createTempDirectory() -> URL {
7+
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString)
118
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
129
return url
1310
}
@@ -424,20 +421,17 @@ final class DocTests: XCTestCase {
424421

425422
final class FileTests: XCTestCase {
426423

427-
static let directoryURL = Bundle.module.url(forResource: "Resources", withExtension: nil, subdirectory: nil)!
428-
static let fileURL = directoryURL.appendingPathComponent("Rect/Rect.swift")
429-
//static let readmeURL = directoryURL.appendingPathComponent("README.md")
430-
//static let emptyDirURL = directoryURL.appendingPathComponent("Empty")
431-
432424
func test_no_file() throws {
433-
let fileURL = URL(filePath: Self.directoryURL.appendingPathComponent("File.swift").path)
425+
let directoryURL = Bundle.module.url(forResource: "Resources", withExtension: nil, subdirectory: nil)!
426+
let fileURL = directoryURL.appendingPathComponent("File.swift")
434427
XCTAssertThrowsError(try SwiftSource(fileURL: fileURL)) { error in
435428
XCTAssert(error.localizedDescription == "The file “File.swift” couldn’t be opened because there is no such file.")
436429
}
437430
}
438431

439432
func test_file() throws {
440-
let source = try SwiftSource(fileURL: Self.fileURL)
433+
let fileURL = Bundle.module.url(forResource: "Rect", withExtension: "swift", subdirectory: "Resources/Rect")!
434+
let source = try SwiftSource(fileURL: fileURL)
441435
XCTAssert(source.declarations.count == 4)
442436
}
443437
}
@@ -469,53 +463,60 @@ extension Process {
469463

470464
final class ToolTests: XCTestCase {
471465

472-
lazy var swiftDocCoverageURL: URL = {
466+
lazy var executableURL: URL = {
473467
Bundle(for: Self.self).bundleURL.deletingLastPathComponent().appendingPathComponent("swift-doc-coverage")
474468
}()
475469

476470
let fileURL = Bundle.module.url(forResource: "Rect", withExtension: "swift", subdirectory: "Resources/Rect")!
477471

478-
func test_not_found() {
479-
let process = Process()
480-
do {
481-
let output = try process.run(swiftDocCoverageURL, arguments: ["NotFound"])
482-
XCTFail(output)
483-
}
484-
catch {
485-
XCTAssert(error.localizedDescription == "Error: Path not found.")
472+
func test_no_params() throws {
473+
XCTAssertThrowsError(try SwiftDocCoverage.run()) { error in
474+
// Error: Missing expected argument '<inputs> ...'
486475
}
487-
XCTAssert(process.terminationStatus == EXIT_FAILURE)
488476
}
489477

490-
// func test_directory() throws {
491-
// let coverage = try Coverage(paths: [Self.directoryURL.path])
492-
// let report = try coverage.report()
493-
//
494-
// XCTAssert(report.totalCount == 4)
495-
// XCTAssert(report.totalUndocumentedCount == 2)
496-
// print(report.sources.count == 1)
497-
// }
498-
499-
// func test_empty_directory() throws {
500-
// let tempDirectory = tempDirectory()
501-
// defer { try? FileManager.default.removeItem(at: tempDirectory) }
502-
//
503-
// XCTAssertThrowsError(try Coverage(paths: [tempDirectory.path])) { error in
504-
// XCTAssert(error.localizedDescription == "Swift files not found.")
505-
// }
506-
// }
478+
func test_no_file() {
479+
XCTAssertThrowsError(try SwiftDocCoverage.run(["File.swift"])) { error in
480+
XCTAssert(error.localizedDescription == "Path not found.")
481+
}
482+
}
507483

508-
//func test_not_found() throws {
509-
// XCTAssertThrowsError(try Coverage(paths: [Self.directoryURL.appendingPathComponent("NotFound").path])) { error in
510-
// XCTAssert(error.localizedDescription == "Path not found.")
511-
// }
512-
//}
484+
func test_empty_directory() throws {
485+
let temp = createTempDirectory()
486+
defer { try? FileManager.default.removeItem(at: temp) }
487+
488+
XCTAssertThrowsError(try SwiftDocCoverage.run([temp.path])) { error in
489+
XCTAssert(error.localizedDescription == "Swift files not found.")
490+
}
491+
}
492+
493+
func test_file() throws {
494+
let cmd = try SwiftDocCoverage.run([fileURL.path])
495+
XCTAssert(cmd.sources.count == 1)
496+
XCTAssert(cmd.sources[0].declarations.count == 4)
497+
}
513498

514-
//func test_not_swift_file() throws {
515-
// XCTAssertThrowsError(try Coverage(paths: [Self.readmeURL.path])) { error in
516-
// XCTAssert(error.localizedDescription == "Not swift file.")
517-
// }
518-
//}
499+
func test_directory() throws {
500+
let directoryURL = Bundle.module.url(forResource: "Resources", withExtension: nil, subdirectory: nil)!
501+
502+
let cmd = try SwiftDocCoverage.run([directoryURL.path])
503+
XCTAssert(cmd.sources.count == 5)
504+
505+
XCTAssert(cmd.sources[0].url?.lastPathComponent == "Rect.swift")
506+
XCTAssert(cmd.sources[0].declarations.count == 4)
507+
508+
XCTAssert(cmd.sources[1].url?.lastPathComponent == "CompactRect.swift")
509+
XCTAssert(cmd.sources[1].declarations.count == 4)
510+
511+
XCTAssert(cmd.sources[2].url?.lastPathComponent == "AlternativeRect.swift")
512+
XCTAssert(cmd.sources[2].declarations.count == 4)
513+
514+
XCTAssert(cmd.sources[3].url?.lastPathComponent == "Size.swift")
515+
XCTAssert(cmd.sources[3].declarations.count == 2)
516+
517+
XCTAssert(cmd.sources[4].url?.lastPathComponent == "Point.swift")
518+
XCTAssert(cmd.sources[4].declarations.count == 2)
519+
}
519520

520521
// func test_no_declarations() throws {
521522
// let coverage = try Coverage(paths: [Self.fileURL.path], minAccessLevel: .open)
@@ -524,30 +525,14 @@ final class ToolTests: XCTestCase {
524525
// }
525526
// }
526527

527-
// func test_report() throws {
528-
// let coverage = try Coverage(paths: [Self.fileURL.path])
529-
// let report = try coverage.report()
530-
//
531-
// XCTAssert(report.totalCount == 4)
532-
// XCTAssert(report.totalUndocumentedCount == 2)
533-
// XCTAssert(report.sources.count == 1)
534-
//
535-
// try coverage.reportStatistics()
536-
// }
537-
538528
func test_output() throws {
539529
let process = Process()
540-
let output = try process.run(swiftDocCoverageURL, arguments: [fileURL.path])
530+
let output = try process.run(executableURL, arguments: [fileURL.path])
541531

542532
XCTAssert(process.terminationStatus == EXIT_SUCCESS)
543533
XCTAssert(output.contains("Total: 50%"))
544534
}
545535

546-
// func test_warnings() throws {
547-
// let coverage = try Coverage(paths: [Self.fileURL.path])
548-
// try coverage.reportWarnings()
549-
// }
550-
551536
func test_warninigs() throws {
552537
// let process = Process()
553538
// let output = try process.run(swiftDocCoverageURL, arguments: [fileURL.path, "--report", "warnings"])
@@ -570,12 +555,12 @@ final class ToolTests: XCTestCase {
570555
}
571556

572557
func test_file_output() throws {
573-
let tempDirectory = tempDirectory()
558+
let tempDirectory = createTempDirectory()
574559
let outputFileURL = tempDirectory.appendingPathComponent("report.txt")
575560
defer { try? FileManager.default.removeItem(at: tempDirectory) }
576561

577562
let process = Process()
578-
let output = try process.run(swiftDocCoverageURL, arguments: [fileURL.path, "--output", outputFileURL.path])
563+
let output = try process.run(executableURL, arguments: [fileURL.path, "--output", outputFileURL.path])
579564
XCTAssert(process.terminationStatus == EXIT_SUCCESS)
580565
XCTAssert(output.isEmpty)
581566

0 commit comments

Comments
 (0)