Skip to content

Commit

Permalink
Switch to Spectre for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Oct 24, 2015
1 parent d6a42f7 commit 9926257
Show file tree
Hide file tree
Showing 29 changed files with 416 additions and 1,181 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.conche/
15 changes: 0 additions & 15 deletions Commander.playground/Contents.swift

This file was deleted.

4 changes: 0 additions & 4 deletions Commander.playground/contents.xcplayground

This file was deleted.

6 changes: 0 additions & 6 deletions Commander.playground/timeline.xctimeline

This file was deleted.

6 changes: 6 additions & 0 deletions Commander.podspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
"platforms": {
"ios": "8.0",
"osx": "10.9"
},
"test_specification": {
"source_files": "CommanderSpecs/*Spec.swift",
"dependencies": {
"Spectre": [ "~> 0.5.0" ]
}
}
}
469 changes: 0 additions & 469 deletions Commander.xcodeproj/project.pbxproj

This file was deleted.

This file was deleted.

100 changes: 0 additions & 100 deletions Commander.xcodeproj/xcshareddata/xcschemes/Commander.xcscheme

This file was deleted.

17 changes: 16 additions & 1 deletion Commander/ArgumentConvertible.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum ArgumentError : ErrorType, CustomStringConvertible {
public enum ArgumentError : ErrorType, Equatable, CustomStringConvertible {
case MissingValue(argument: String?)

/// Value is not convertible to type
Expand All @@ -25,6 +25,21 @@ public enum ArgumentError : ErrorType, CustomStringConvertible {
}
}


public func == (lhs: ArgumentError, rhs: ArgumentError) -> Bool {
switch (lhs, rhs) {
case let (.MissingValue(lhsKey), .MissingValue(rhsKey)):
return lhsKey == rhsKey
case let (.InvalidType(lhsValue, lhsType, lhsArgument), .InvalidType(rhsValue, rhsType, rhsArgument)):
return lhsValue == rhsValue && lhsType == rhsType && lhsArgument == rhsArgument
case let (.UnusedArgument(lhsArgument), .UnusedArgument(rhsArgument)):
return lhsArgument == rhsArgument
default:
return false
}
}


public protocol ArgumentConvertible : CustomStringConvertible {
/// Initialise the type with an ArgumentParser
init(parser: ArgumentParser) throws
Expand Down
17 changes: 11 additions & 6 deletions Commander/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ private enum Arg : CustomStringConvertible {
}


public struct ArgumentParserError : ErrorType, CustomStringConvertible {
public let description:String
public struct ArgumentParserError : ErrorType, Equatable, CustomStringConvertible {
public let description: String

init(description:String) {
public init(_ description: String) {
self.description = description
}
}


public func ==(lhs: ArgumentParserError, rhs: ArgumentParserError) -> Bool {
return lhs.description == rhs.description
}


public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible {
private var arguments:[Arg]

Expand Down Expand Up @@ -130,7 +135,7 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible
case .Argument(let value):
return value
default:
throw ArgumentParserError(description: "Unexpected \(argument.type) `\(argument)` as a value for `--\(name)`")
throw ArgumentParserError("Unexpected \(argument.type) `\(argument)` as a value for `--\(name)`")
}
}

Expand Down Expand Up @@ -223,14 +228,14 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible
case .Argument(let value):
return value
default:
throw ArgumentParserError(description: "Unexpected \(argument.type) `\(argument)` as a value for `-\(flag)`")
throw ArgumentParserError("Unexpected \(argument.type) `\(argument)` as a value for `-\(flag)`")
}
}

throw ArgumentError.MissingValue(argument: "-\(flag)")
}
}

return nil
}
}
13 changes: 12 additions & 1 deletion Commander/Group.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum GroupError : ErrorType, CustomStringConvertible {
public enum GroupError : ErrorType, Equatable, CustomStringConvertible {
/// No-subcommand was found with the given name
case UnknownCommand(String)

Expand All @@ -22,6 +22,17 @@ public enum GroupError : ErrorType, CustomStringConvertible {
}
}

public func == (lhs: GroupError, rhs: GroupError) -> Bool {
switch (lhs, rhs) {
case let (.UnknownCommand(lhsCommand), .UnknownCommand(rhsCommand)):
return lhsCommand == rhsCommand
case let (.NoCommand(lhsPath, lhsGroup), .NoCommand(rhsPath, rhsGroup)):
return lhsPath == rhsPath && lhsGroup === rhsGroup
default:
return false
}
}

/// Represents a group of commands
public class Group : CommandType {
var commands = [String:CommandType]()
Expand Down
95 changes: 95 additions & 0 deletions CommanderSpecs/ArgumentConvertibleSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import Spectre
import Commander


describe("ArgumentError") {
$0.it("has a human readable description for missing value") {
let error = ArgumentError.MissingValue(argument: nil)
try expect(error.description) == "Missing argument"
}

$0.it("has a human readable description for no value") {
let error = ArgumentError.InvalidType(value: "five", type: "number", argument: nil)
try expect(error.description) == "`five` is not a `number`"
}
}

describe("String ArgumentConvertible") {
$0.it("converts an argument to a string value") {
let parser = ArgumentParser(arguments: ["argument"])
let value = try String(parser: parser)

try expect(value) == "argument"
}

$0.it("handles when the argument parser doesn't have any values") {
let parser = ArgumentParser(arguments: [])
try expect(try String(parser: parser)).toThrow(ArgumentError.MissingValue(argument: nil))
}
}

describe("Int ArgumentConvertible") {
$0.it("converts a valid number as an integer") {
let parser = ArgumentParser(arguments: ["5"])
let value = try Int(parser: parser)

try expect(value) == 5
}

$0.it("errors on invalid input") {
let parser = ArgumentParser(arguments: ["five"])

try expect {
try Int(parser: parser)
}.toThrow(ArgumentError.InvalidType(value: "five", type: "number", argument: nil))
}

$0.it("handles when the argument parser doesn't have any values") {
let parser = ArgumentParser(arguments: [])
try expect(try Int(parser: parser)).toThrow(ArgumentError.MissingValue(argument: nil))
}
}

describe("Float ArgumentConvertible") {
$0.it("converts a valid number as an float") {
let parser = ArgumentParser(arguments: ["5"])
let value = try Float(parser: parser)

try expect(value) == 5
}

$0.it("errors on invalid input") {
let parser = ArgumentParser(arguments: ["five"])

try expect {
try Float(parser: parser)
}.toThrow(ArgumentError.InvalidType(value: "five", type: "number", argument: nil))
}

$0.it("handles when the argument parser doesn't have any values") {
let parser = ArgumentParser(arguments: [])
try expect(try Float(parser: parser)).toThrow(ArgumentError.MissingValue(argument: nil))
}
}

describe("Double ArgumentConvertible") {
$0.it("converts a valid number as an double") {
let parser = ArgumentParser(arguments: ["5"])
let value = try Double(parser: parser)

try expect(value) == 5
}

$0.it("errors on invalid input") {
let parser = ArgumentParser(arguments: ["five"])

try expect {
try Double(parser: parser)
}.toThrow(ArgumentError.InvalidType(value: "five", type: "number", argument: nil))
}

$0.it("handles when the argument parser doesn't have any values") {
let parser = ArgumentParser(arguments: [])
try expect(try Double(parser: parser)).toThrow(ArgumentError.MissingValue(argument: nil))
}
}

0 comments on commit 9926257

Please sign in to comment.