Skip to content

Commit

Permalink
Merge pull request #5 from taji-taji/use-general-mock
Browse files Browse the repository at this point in the history
Use general mock
  • Loading branch information
taji-taji authored Jul 24, 2022
2 parents 16887cf + 92be3ba commit 675730e
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 88 deletions.
120 changes: 87 additions & 33 deletions Tests/DangerSwiftPeripheryTests/DangerSwiftPeripheryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,109 @@ import Danger
@testable import DangerSwiftPeriphery

final class DangerSwiftPeripheryTests: XCTestCase {
func testScanErrorOccuredWhileScanning() throws {
let scanExecutor = ErrorScanExecutor(errorMessage: "test error")
private var scanExecutor: PeripheryScanExecutableMock!
private var outputParser: CheckstyleOutputParsableMock!
private var diffProvider: PullRequestDiffProvidableMock!

override func setUp() {
super.setUp()

scanExecutor = PeripheryScanExecutableMock()
outputParser = CheckstyleOutputParsableMock(projectRootPath: DefaultCurrentPathProvider().currentPath)
diffProvider = PullRequestDiffProvidableMock()
}

func testScanErrorOccurredWhileScanning() throws {
scanExecutor.executeHandler = {
throw TestError.scanError
}
outputParser.parseHandler = { _ in
[]
}
diffProvider.diffHandler = { _ in
.success(.modified(hunks: []))
}
let result = DangerPeriphery.scan(scanExecutor: scanExecutor,
outputParser: CheckstyleOutputParser(projectRootPath: DefaultCurrentPathProvider().currentPath),
diffProvider: DiffProviderMock(result: .failure(TestError.scanError(messege: ""))))
outputParser: outputParser,
diffProvider: diffProvider)
switch result {
case .success:
XCTFail("Unexpected success")
case .failure(let error as TestError):
switch error {
case .scanError(let message):
XCTAssertEqual(message, "test error")
case .scanError:
// noop
break
default:
XCTFail("Unexpected error")
}
default:
XCTFail("Unexpected result")
}
}

func testScanErrorOccuredWhileParsingResult() throws {

}
}

private extension DangerSwiftPeripheryTests {
enum TestError: Error {
case scanError(messege: String)
}

final class ErrorScanExecutor: PeripheryScanExecutable {
let errorMessage: String

init(errorMessage: String) {
self.errorMessage = errorMessage
func testScanErrorOccurredWhileParsingResult() throws {
scanExecutor.executeHandler = {
"test"
}

func execute() throws -> String {
throw TestError.scanError(messege: errorMessage)
outputParser.parseHandler = { _ in
throw TestError.parseError
}
diffProvider.diffHandler = { _ in
.success(.modified(hunks: []))
}
let result = DangerPeriphery.scan(scanExecutor: scanExecutor,
outputParser: outputParser,
diffProvider: diffProvider)
switch result {
case .success:
XCTFail("Unexpected success")
case .failure(let error as TestError):
switch error {
case .parseError:
// noop
break
default:
XCTFail("Unexpected error")
}
default:
XCTFail("Unexpected result")
}
}

final class DiffProviderMock: PullRequestDiffProvidable {
private let result: Result<FileDiff.Changes, Error>

init(result: Result<FileDiff.Changes, Error>) {
self.result = result

func testScanNoCommentViolationsWithoutCreatedOrModifiedDiff() {
scanExecutor.executeHandler = { "test" }
outputParser.parseHandler = { _ in
[
.init(filePath: "path1", line: 1, message: "1"),
.init(filePath: "path2", line: 2, message: "2"),
]
}

func diff(forFile: String) -> Result<FileDiff.Changes, Error> {
result
diffProvider.diffHandler = { filePath in
switch filePath {
case "path1":
return .success(.deleted(deletedLines: []))
case "path2":
return .success(.renamed(oldPath: "", hunks: []))
default:
return .success(.deleted(deletedLines: []))
}
}
let result = DangerPeriphery.scan(scanExecutor: scanExecutor,
outputParser: outputParser,
diffProvider: diffProvider)
switch result {
case let .success(violationsForComment):
XCTAssertEqual(violationsForComment.count, 0)
case .failure:
XCTFail("Unexpected error")
}
}
}

private extension DangerSwiftPeripheryTests {
enum TestError: Error {
case scanError
case parseError
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by 多鹿豊 on 2022/07/24.
//

import Foundation
@testable import DangerSwiftPeriphery

final class CheckstyleOutputParsableMock: CheckstyleOutputParsable {
var parseHandler: ((String) throws -> [Violation])?
var projectRootPath: String

init(projectRootPath: String) {
self.projectRootPath = projectRootPath
}

func parse(xml: String) throws -> [Violation] {
guard let handler = parseHandler else {
fatalError("parseHandler is nil.")
}
return try handler(xml)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by 多鹿豊 on 2022/07/24.
//

import Foundation
@testable import DangerSwiftPeriphery

final class PeripheryScanExecutableMock: PeripheryScanExecutable {
var executeHandler: (() throws -> String)?

func execute() throws -> String {
guard let executeHandler = executeHandler else {
fatalError("executeHandler is nil.")
}
return try executeHandler()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created by 多鹿豊 on 2022/07/24.
//

import Foundation
import Danger
@testable import DangerSwiftPeriphery

final class PullRequestDiffProvidableMock: PullRequestDiffProvidable {
var diffHandler: ((String) -> Result<FileDiff.Changes, Error>)?

func diff(forFile: String) -> Result<FileDiff.Changes, Error> {
guard let handler = diffHandler else {
fatalError("diffHandler is nil.")
}
return handler(forFile)
}
}
21 changes: 21 additions & 0 deletions Tests/DangerSwiftPeripheryTests/Mocks/ShellExecutableMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by 多鹿豊 on 2022/07/24.
//

import Foundation
@testable import DangerSwiftPeriphery

final class ShellExecutableMock: ShellExecutable {
var executeHandler: ((String, [String]) -> Result<String, CommandError>)?

func execute(_ command: String) -> Result<String, CommandError> {
execute(command, arguments: [])
}

func execute(_ command: String, arguments: [String]) -> Result<String, CommandError> {
guard let handler = executeHandler else {
fatalError("executeHandler is nil.")
}
return handler(command, arguments)
}
}
83 changes: 28 additions & 55 deletions Tests/DangerSwiftPeripheryTests/PeripheryScanExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ final class PeripheryScanExecutorTests: XCTestCase {
}

func testExecuteThrowCommandError() throws {
let shellExecutor = ShellExecutableMock()
shellExecutor.executeHandler = { _, _ in
.failure(.init(status: 9999, description: "test error"))
}
executor = PeripheryScanExecutor(commandBuilder: commandBuilder,
shellExecutor: ErrorShellExecutor(status: 9999, description: "test error"))
shellExecutor: shellExecutor)

do {
_ = try executor.execute()
Expand All @@ -38,15 +42,18 @@ final class PeripheryScanExecutorTests: XCTestCase {
}

func testExecuteSucceed() throws {
let succeedShellExecutor = SucccedShellExecutor(output: """
test
test2
test3
test4
""")

let shellExecutor = ShellExecutableMock()
shellExecutor.executeHandler = { _, _ in
.success("""
test
test2
test3
test4
""")
}

executor = PeripheryScanExecutor(commandBuilder: commandBuilder,
shellExecutor: succeedShellExecutor)
shellExecutor: shellExecutor)

do {
let output = try executor.execute()
Expand All @@ -64,17 +71,19 @@ final class PeripheryScanExecutorTests: XCTestCase {
}

func testExecuteSucceedWithWarning() throws {
let succeedShellExecutor = SucccedShellExecutor(output: """
test
warning: hoge
test2
warning: fuga
test3
test4
""")

let shellExecutor = ShellExecutableMock()
shellExecutor.executeHandler = { _, _ in
.success("""
test
warning: hoge
test2
warning: fuga
test3
test4
""")
}
executor = PeripheryScanExecutor(commandBuilder: commandBuilder,
shellExecutor: succeedShellExecutor)
shellExecutor: shellExecutor)

do {
let output = try executor.execute()
Expand All @@ -91,39 +100,3 @@ final class PeripheryScanExecutorTests: XCTestCase {
}
}
}

private extension PeripheryScanExecutorTests {
final class ErrorShellExecutor: ShellExecutable {
private let status: Int32
private let description: String

init(status: Int32, description: String) {
self.status = status
self.description = description
}

func execute(_ command: String) -> Result<String, CommandError> {
execute(command, arguments: [])
}

func execute(_ command: String, arguments: [String]) -> Result<String, CommandError> {
.failure(.init(status: status, description: description))
}
}

final class SucccedShellExecutor: ShellExecutable {
private let output: String

init(output: String) {
self.output = output
}

func execute(_ command: String) -> Result<String, CommandError> {
execute(command, arguments: [])
}

func execute(_ command: String, arguments: [String]) -> Result<String, CommandError> {
.success(output)
}
}
}

0 comments on commit 675730e

Please sign in to comment.