11import 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
425422final 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
470464final 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