Skip to content

Commit

Permalink
testを書き直した
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyome22 committed Mar 11, 2022
1 parent 9c64ac4 commit 94f6aed
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 137 deletions.
13 changes: 13 additions & 0 deletions Sources/LineCounterFramework/LCError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ enum LCError: Error {
case couldNotRead(file: String)
case skipped(file: String)
}

extension LCError: Equatable {
static func ==(lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.couldNotRead(_), .couldNotRead(_)):
return true
case (.skipped(_), .skipped(_)):
return true
default:
return false
}
}
}
202 changes: 65 additions & 137 deletions Tests/LineCounterFrameworkTests/LineCounterFrameworkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,72 @@ import XCTest
@testable import LineCounterFramework

final class LineCounterFrameworkTests: XCTestCase {

var rootPath: String {
return URL(fileURLWithPath: #file).path.components(separatedBy: "Tests/").first!
}

var srcURL: URL {
return URL(fileURLWithPath: "\(rootPath)Sources/")
}

func testRootPath() {
Swift.print("🎯", srcURL.path)
}

func testEnumerateFilePaths() {
let sut = LineCounter()
let actual = sut.enumerateFilePaths(url: srcURL)
.map { url -> String in
let components = url.pathComponents
return components[components.count - 2 ..< components.count]
.joined(separator: "/")
}
.sorted()
let expect = [
"LineCounterFramework/LCError.swift",
"LineCounterFramework/LCVersion.swift",
"LineCounterFramework/LineCounter.swift",
"lc/LC.swift",
"lc/main.swift"
]
XCTAssertEqual(actual, expect)
}

func testCountLine() throws {
let sut = LineCounter()
let mainFileURL = srcURL.appendingPathComponent("lc/main.swift")
let actual1 = try sut.countLine(mainFileURL, [])
XCTAssertEqual(actual1, 9)
let actual2 = try sut.countLine(mainFileURL, ["swift"])
XCTAssertEqual(actual2, 9)
}

func testCountLineThrowLCErrorSkipped() {
let sut = LineCounter()
let mainFileURL = srcURL.appendingPathComponent("lc/main.swift")
XCTAssertThrowsError(try sut.countLine(mainFileURL, ["txt"])) { error in
XCTAssertEqual(error as? LCError, LCError.skipped(file: ""))
}
}

func testCountLineThrowLCErrorCouldNotRead() {
let sut = LineCounter()
let dummyFileURL = srcURL.appendingPathComponent("lc/dummy.swift")
XCTAssertThrowsError(try sut.countLine(dummyFileURL, [])) { error in
XCTAssertEqual(error as? LCError, LCError.couldNotRead(file: ""))
}
}

func testMain() {
Swift.print("hello")
XCTAssertTrue(true)
func testOutput() {
let sut = LineCounter()
let filePaths = [
srcURL.appendingPathComponent("lc/LC.swift"),
srcURL.appendingPathComponent("lc/main.swift")
]
let actual = sut.output(filePaths, ["swift"], true)
let lines = actual.components(separatedBy: CharacterSet.newlines).count
XCTAssertEqual(lines, 3)
}

// var rootPath: String {
// return URL(fileURLWithPath: #file).path.components(separatedBy: "Tests/").first!
// }
//
// func testNoArguments() {
// XCTAssertThrowsError(try resolveArgs(args: [])) { error in
// XCTAssertEqual(error as? LCError, LCError.invalidOptions)
// }
// }
//
// func testHelp() {
// XCTAssertThrowsError(try resolveArgs(args: ["-h"])) { error in
// XCTAssertEqual(error as? LCError, LCError.help)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["--help"])) { error in
// XCTAssertEqual(error as? LCError, LCError.help)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["-h", "-p"])) { error in
// XCTAssertEqual(error as? LCError, LCError.help)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["--help", "-p", "./", "-e", "swift"])) { error in
// XCTAssertEqual(error as? LCError, LCError.help)
// }
// }
//
// func testInvalidOptions() {
// XCTAssertThrowsError(try resolveArgs(args: ["-a"]), "存在しないオプションは使えない") { error in
// XCTAssertEqual(error as? LCError, LCError.invalidOptions)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["-p"]), "具体的なパスが指定されていない") { error in
// XCTAssertEqual(error as? LCError, LCError.invalidOptions)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["-e", "swift"]), "パスは必ず指定する必要がある") { error in
// XCTAssertEqual(error as? LCError, LCError.invalidOptions)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["-p", "./", "-e"]), "具体的な拡張子が指定されていない") { error in
// XCTAssertEqual(error as? LCError, LCError.invalidOptions)
// }
//
// XCTAssertThrowsError(try resolveArgs(args: ["-p", "./", "./src/"]), "パスはオプション1つにつき1個まで") { error in
// XCTAssertEqual(error as? LCError, LCError.invalidOptions)
// }
// }
//
// func testSinglePath() throws {
// let (paths, _) = try resolveArgs(args: ["-p", "./"])
// XCTAssertEqual(paths.count, 1)
// XCTAssertEqual(paths[0], "./")
//
// let (paths2, _) = try resolveArgs(args: ["--path", "./"])
// XCTAssertEqual(paths2.count, 1)
// XCTAssertEqual(paths2[0], "./")
// }
//
// func testMultiplePaths() throws {
// let (paths, _) = try resolveArgs(args: ["-p", "./", "--path", "../src"])
// XCTAssertEqual(paths.count, 2)
// XCTAssertEqual(paths[0], "./")
// XCTAssertEqual(paths[1], "../src")
// }
//
// func testExtension() throws {
// let (_, ext) = try resolveArgs(args: ["-p", "./", "-e", "swift"])
// XCTAssertEqual(ext, "swift")
//
// let (_, ext2) = try resolveArgs(args: ["-p", "./", "-e", "swift", "-e", "cpp"])
// XCTAssertEqual(ext2, "cpp")
// }
//
// func testEnumerateFileURLs() {
// let url = URL(fileURLWithPath: "\(rootPath)/Sources/")
// let fileURLs = enumerateFileURLs(fileURL: url)
// let sut = fileURLs.map { url -> String in
// let components = url.pathComponents
// return components[components.count - 2 ..< components.count].joined(separator: "/")
// }.sorted()
// let expect = [
// "LineCounter/main.swift",
// "LineCounterLibrary/LCError.swift",
// "LineCounterLibrary/LCFoundation.swift"
// ]
// XCTAssertEqual(sut, expect)
// }
//
// func testCountLineSkip() {
// let url = URL(fileURLWithPath: "main.cpp")
// XCTAssertThrowsError(try countLine(with: url, ext: "swift"), "拡張子が一致しなければスキップ") { error in
// XCTAssertEqual(error as? LCError, LCError.skip)
// }
// }
//
// func testCountLineCouldNotRead() {
// let url = URL(fileURLWithPath: "hoge.swift")
// XCTAssertThrowsError(try countLine(with: url, ext: nil), "ファイルを読み込めなければエラー") { error in
// XCTAssertEqual(error as? LCError, LCError.couldNotRead)
// }
// }
//
// func testCountLine() throws {
// let url = URL(fileURLWithPath: "\(rootPath)/Sources/LineCounter/main.swift")
// let cnt = try countLine(with: url, ext: "swift")
// XCTAssertGreaterThan(cnt, 0)
// }
//
// func testOutput() {
// let urls = [
// URL(fileURLWithPath: "\(rootPath)/Sources/LineCounter/main.swift"),
// URL(fileURLWithPath: "\(rootPath)/Sources/LineCounterLibrary/LCError.swift"),
// URL(fileURLWithPath: "\(rootPath)/Sources/LineCounterLibrary/LCFoundation.swift")
// ]
// let sut = output(fileURLs: urls, ext: "swift")
// let cnt = sut.components(separatedBy: CharacterSet.newlines).count
// XCTAssertEqual(cnt, 4)
// }
//
// static var allTests = [
// ("testNoArguments", testNoArguments),
// ("testHelp", testHelp),
// ("testInvalidOptions", testInvalidOptions),
// ("testSinglePath", testSinglePath),
// ("testMultiplePaths", testMultiplePaths),
// ("testExtension", testExtension),
// ("testEnumerateFileURLs", testEnumerateFileURLs),
// ("testCountLineSkip", testCountLineSkip),
// ("testCountLineCouldNotRead", testCountLineCouldNotRead),
// ("testCountLine", testCountLine),
// ("testOutput", testOutput),
// ]
}

0 comments on commit 94f6aed

Please sign in to comment.