Skip to content

Commit

Permalink
test: Adds tests for GlobalOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
devedbox committed Oct 12, 2018
1 parent bc25eaa commit dfd4db6
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Commander.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DC1DF5FD216DF777002EB271 /* CommandDescriber.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1DF5FC216DF777002EB271 /* CommandDescriber.swift */; };
DC1DF5FF216F5662002EB271 /* CommandPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1DF5FE216F5662002EB271 /* CommandPath.swift */; };
DC1DF603216F6357002EB271 /* CommandPathTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1DF602216F6357002EB271 /* CommandPathTests.swift */; };
DC71F3212170E774002DA345 /* GlobalOptionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC71F3202170E774002DA345 /* GlobalOptionsTests.swift */; };
OBJ_42 /* Commander.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* Commander.swift */; };
OBJ_43 /* Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* Error.swift */; };
OBJ_46 /* CommandRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_18 /* CommandRepresentable.swift */; };
Expand Down Expand Up @@ -81,6 +82,7 @@
DC1DF5FC216DF777002EB271 /* CommandDescriber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandDescriber.swift; sourceTree = "<group>"; };
DC1DF5FE216F5662002EB271 /* CommandPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandPath.swift; sourceTree = "<group>"; };
DC1DF602216F6357002EB271 /* CommandPathTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandPathTests.swift; sourceTree = "<group>"; };
DC71F3202170E774002DA345 /* GlobalOptionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalOptionsTests.swift; sourceTree = "<group>"; };
OBJ_11 /* Commander.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Commander.swift; sourceTree = "<group>"; };
OBJ_13 /* Error.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Error.swift; sourceTree = "<group>"; };
OBJ_18 /* CommandRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandRepresentable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -173,6 +175,7 @@
children = (
OBJ_30 /* CommanderTests.swift */,
DC0B9BA4216882D100C87253 /* CommandTests.swift */,
DC71F3202170E774002DA345 /* GlobalOptionsTests.swift */,
DC0B9B9C2163B56B00C87253 /* OptionsDecoderTests.swift */,
DC1DF602216F6357002EB271 /* CommandPathTests.swift */,
OBJ_32 /* XCTestManifests.swift */,
Expand Down Expand Up @@ -360,6 +363,7 @@
OBJ_72 /* CommanderTests.swift in Sources */,
OBJ_74 /* XCTestManifests.swift in Sources */,
DC0B9B9D2163B56B00C87253 /* OptionsDecoderTests.swift in Sources */,
DC71F3212170E774002DA345 /* GlobalOptionsTests.swift in Sources */,
DC1DF603216F6357002EB271 /* CommandPathTests.swift in Sources */,
DC0B9BA5216882D200C87253 /* CommandTests.swift in Sources */,
);
Expand Down
140 changes: 140 additions & 0 deletions Tests/CommanderTests/GlobalOptionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// GlobalOptionsTests.swift
// CommanderTests
//
// Created by devedbox on 2018/10/12.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import XCTest
@testable import Commander

class MockCommander: CommanderRepresentable {
struct TestsCommand: CommandRepresentable {
struct Options: OptionsRepresentable {
typealias GlobalOptions = MockCommander.Options
enum CodingKeys: String, CodingKeysRepresentable {
case target
}
static var keys: [TestsCommand.Options.CodingKeys : Character] = [
.target: "T"
]
static var descriptions: [TestsCommand.Options.CodingKeys: OptionDescription] = [
.target: .default(value: "Default", usage: "The target of the test command")
]
let target: String
}

static let symbol: String = "test"
static var usage: String = "Mocked command for tests"

static func main(_ options: TestsCommand.Options) throws {

}
}

struct TestsArgsCommand: CommandRepresentable {
struct Options: OptionsRepresentable {
typealias GlobalOptions = MockCommander.Options
typealias ArgumentsResolver = AnyArgumentsResolver<[String: UInt8]>
enum CodingKeys: String, CodingKeysRepresentable {
case target
}
static var keys: [TestsArgsCommand.Options.CodingKeys : Character] = [
.target: "T"
]
static var descriptions: [Options.CodingKeys: OptionDescription] = [
.target: .default(value: "Default", usage: "The target of the test command")
]
let target: String
}
static let subcommands: [AnyCommandRepresentable.Type] = [
TestsCommand.self
]
static let symbol: String = "test-args"
static var usage: String = "Mocked command for tests args"

static func main(_ options: Options) throws {

}
}

struct Options: OptionsRepresentable {
enum CodingKeys: String, CodingKeysRepresentable {
case mockDir = "mock-dir"
case verbose
}

static var keys: [MockCommander.Options.CodingKeys : Character] = [
.mockDir: "C",
.verbose: "v"
]

static var descriptions: [MockCommander.Options.CodingKeys : OptionDescription] = [
.verbose: .default(value: false, usage: "")
]

let mockDir: String
let verbose: Bool
}
static var errorHandler: ((Error) -> Void)? = nil
static var commands: [AnyCommandRepresentable.Type] = [
MockCommander.TestsCommand.self,
MockCommander.TestsArgsCommand.self
]

static var usage: String = "Mocked usage"
}

class GlobalOptionsTests: XCTestCase {
static let allTests = [
("testGlobalOptions", testGlobalOptions)
]

func testGlobalOptions() {
XCTAssertNoThrow(try MockCommander().dispatch(with: ["commander", "test"]))
XCTAssertNoThrow(try MockCommander().dispatch(with: ["commander", "test", "-C=path"]))
XCTAssertNoThrow(try MockCommander().dispatch(with: ["commander", "test", "-C=path", "-v"]))
XCTAssertNoThrow(try MockCommander().dispatch(with: ["commander", "test", "--mock-dir", "path"]))
XCTAssertNoThrow(try MockCommander().dispatch(with: ["commander", "test", "--mock-dir", "path", "--verbose"]))

do {
try MockCommander().dispatch(with: ["commander", "test", "--verbose"])
XCTFail()
} catch OptionsDecoder.Error.decodingError(DecodingError.keyNotFound(let key, let ctx)) {
XCTAssertEqual(key.stringValue, "mock-dir")
XCTAssertFalse(OptionsDecoder.Error.decodingError(DecodingError.keyNotFound(key, ctx)).description.isEmpty)
} catch {
XCTFail()
}

do {
try MockCommander().dispatch(with: ["commander", "test", "-C=path", "--verbose", "-s", "-r"])
XCTFail()
} catch CommanderError.unrecognizedOptions(let options, path: let path) {
XCTAssertEqual(options.set, ["s", "r"])
XCTAssertTrue(path.command == MockCommander.TestsCommand.self)
XCTAssertEqual(path.paths.set, ["commander"])
XCTAssertFalse(CommanderError.unrecognizedOptions(options, path: path).description.isEmpty)
} catch {
XCTFail()
}
}
}

0 comments on commit dfd4db6

Please sign in to comment.