-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from rheinfabrik/swift-1.2
Backport Swift 2.0 changes to Swift 1.2
- Loading branch information
Showing
20 changed files
with
676 additions
and
503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
github "Quick/Quick" ~> 0.3 | ||
github "Quick/Nimble" ~> 0.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
github "robrix/Box" "1.2.2" | ||
github "Quick/Nimble" "v0.4.2" | ||
github "Quick/Quick" "v0.3.1" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// A disposable that will run an action upon disposal. | ||
public final class Disposable { | ||
private var action: (() -> ())? | ||
|
||
/// Whether this disposable has been disposed already. | ||
public var disposed: Bool { | ||
return action == nil | ||
} | ||
|
||
/// Initializes a new disposable with the given action. | ||
public init(action: () -> ()) { | ||
self.action = action | ||
} | ||
|
||
/// Performs the disposal, running the associated action. | ||
public func dispose() { | ||
action?() | ||
action = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/// An expectation that can be matched with a value. | ||
public struct Expectation<Value>: Printable { | ||
public private(set) var description: String | ||
|
||
private let matchesFunc: Value -> Bool | ||
|
||
/// Initializes a new expectation with the given description and matching | ||
/// function. | ||
public init(description: String = "<func>", matches matchesFunc: Value -> Bool) { | ||
self.description = description | ||
self.matchesFunc = matchesFunc | ||
} | ||
|
||
/// Checks whether this expectation matches the given value. | ||
public func matches(value: Value) -> Bool { | ||
return matchesFunc(value) | ||
} | ||
} | ||
|
||
/// Returns a new expectation with the given matching function. | ||
/// | ||
/// - SeeAlso: `Expectation.init<Value>(description: String, matches: Value -> Bool)` | ||
public func matches<Value>(matches: Value -> Bool) -> Expectation<Value> { | ||
return Expectation(matches: matches) | ||
} | ||
|
||
/// Returns a new expectation that matches anything. | ||
/// | ||
/// - SeeAlso: `Expectation.init<Value>()` | ||
public func any<Value>() -> Expectation<Value> { | ||
return Expectation(description: "_", matches: { _ in true }) | ||
} | ||
|
||
/// Returns a new expectation that matches the given value. | ||
/// | ||
/// - SeeAlso: `Expectation.init<Value>(value: Value)` | ||
public func equals<Value: Equatable>(value: Value) -> Expectation<Value> { | ||
return Expectation(description: "\(value)", matches: { actualValue in value == actualValue }) | ||
} | ||
|
||
/// Returns a new expectation that matches the given 2-tuple. | ||
public func equals<A: Equatable, B: Equatable>(values: (A, B)) -> Expectation<(A, B)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0 == actualValues.0 | ||
&& values.1 == actualValues.1 | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 3-tuple. | ||
public func equals<A: Equatable, B: Equatable, C: Equatable>(values: (A, B, C)) -> Expectation<(A, B, C)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0 == actualValues.0 | ||
&& values.1 == actualValues.1 | ||
&& values.2 == actualValues.2 | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 4-tuple. | ||
public func equals<A: Equatable, B: Equatable, C: Equatable, D: Equatable>(values: (A, B, C, D)) -> Expectation<(A, B, C, D)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0 == actualValues.0 | ||
&& values.1 == actualValues.1 | ||
&& values.2 == actualValues.2 | ||
&& values.3 == actualValues.3 | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 5-tuple. | ||
public func equals<A: Equatable, B: Equatable, C: Equatable, D: Equatable, E: Equatable>(values: (A, B, C, D, E)) -> Expectation<(A, B, C, D, E)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
// Expression was too complex to be solved in reasonable time; [...] | ||
let equals = values.0 == actualValues.0 | ||
&& values.1 == actualValues.1 | ||
&& values.2 == actualValues.2 | ||
&& values.3 == actualValues.3 | ||
&& values.4 == actualValues.4 | ||
return equals | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given array. | ||
public func equals<Element: Equatable>(value: [Element]) -> Expectation<[Element]> { | ||
return Expectation(description: "\(value)") { actualValue in value == actualValue } | ||
} | ||
|
||
/// Returns a new expectation that matches the given dictionary. | ||
public func equals<Key: Equatable, Value: Equatable>(value: [Key: Value]) -> Expectation<[Key: Value]> { | ||
return Expectation(description: "\(value)") { actualValue in value == actualValue } | ||
} | ||
|
||
/// Conforming types can be converted to an expectation. | ||
public protocol ExpectationConvertible { | ||
/// The type of value with which this type, when converted to an | ||
/// expectation, can be matched. | ||
typealias ValueType | ||
|
||
/// Converts this type to an expectation. | ||
func expectation() -> Expectation<ValueType> | ||
} | ||
|
||
extension Expectation: ExpectationConvertible { | ||
public func expectation() -> Expectation<Value> { | ||
return self | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 2-tuple of expectations. | ||
public func matches<A: ExpectationConvertible, B: ExpectationConvertible>(values: (A, B)) -> Expectation<(A.ValueType, B.ValueType)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0.expectation().matches(actualValues.0) | ||
&& values.1.expectation().matches(actualValues.1) | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 3-tuple of expectations. | ||
public func matches<A: ExpectationConvertible, B: ExpectationConvertible, C: ExpectationConvertible>(values: (A, B, C)) -> Expectation<(A.ValueType, B.ValueType, C.ValueType)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0.expectation().matches(actualValues.0) | ||
&& values.1.expectation().matches(actualValues.1) | ||
&& values.2.expectation().matches(actualValues.2) | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 4-tuple of expectations. | ||
public func matches<A: ExpectationConvertible, B: ExpectationConvertible, C: ExpectationConvertible, D: ExpectationConvertible>(values: (A, B, C, D)) -> Expectation<(A.ValueType, B.ValueType, C.ValueType, D.ValueType)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0.expectation().matches(actualValues.0) | ||
&& values.1.expectation().matches(actualValues.1) | ||
&& values.2.expectation().matches(actualValues.2) | ||
&& values.3.expectation().matches(actualValues.3) | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given 5-tuple of expectations. | ||
public func matches<A: ExpectationConvertible, B: ExpectationConvertible, C: ExpectationConvertible, D: ExpectationConvertible, E: ExpectationConvertible>(values: (A, B, C, D, E)) -> Expectation<(A.ValueType, B.ValueType, C.ValueType, D.ValueType, E.ValueType)> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
return values.0.expectation().matches(actualValues.0) | ||
&& values.1.expectation().matches(actualValues.1) | ||
&& values.2.expectation().matches(actualValues.2) | ||
&& values.3.expectation().matches(actualValues.3) | ||
&& values.4.expectation().matches(actualValues.4) | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given array of expectations. | ||
public func matches<Element: ExpectationConvertible>(values: [Element]) -> Expectation<[Element.ValueType]> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
if values.count != actualValues.count { | ||
return false | ||
} | ||
|
||
for (element, actualElement) in zip(values, actualValues) { | ||
if !element.expectation().matches(actualElement) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} | ||
|
||
/// Returns a new expectation that matches the given dictionary of expectations. | ||
public func matches<Key: Hashable, Value: ExpectationConvertible>(values: [Key: Value]) -> Expectation<[Key: Value.ValueType]> { | ||
return Expectation(description: "\(values)") { actualValues in | ||
if values.count != actualValues.count { | ||
return false | ||
} | ||
|
||
for (key, value) in values { | ||
if !(actualValues[key].map { actualValue in value.expectation().matches(actualValue) } ?? false) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
import Box | ||
|
||
extension Bool: ArgumentConvertible { | ||
public func argument() -> Argument<Bool> { | ||
return Argument.Value(Box(self)) | ||
extension Bool: ExpectationConvertible { | ||
public func expectation() -> Expectation<Bool> { | ||
return Expectation(description: "\(self)", matches: { actualValue in self == actualValue }) | ||
} | ||
} | ||
|
||
extension Int: ArgumentConvertible { | ||
public func argument() -> Argument<Int> { | ||
return Argument.Value(Box(self)) | ||
extension Int: ExpectationConvertible { | ||
public func expectation() -> Expectation<Int> { | ||
return Expectation(description: "\(self)", matches: { actualValue in self == actualValue }) | ||
} | ||
} | ||
|
||
extension Float: ArgumentConvertible { | ||
public func argument() -> Argument<Float> { | ||
return Argument.Value(Box(self)) | ||
extension Float: ExpectationConvertible { | ||
public func expectation() -> Expectation<Float> { | ||
return Expectation(description: "\(self)", matches: { actualValue in self == actualValue }) | ||
} | ||
} | ||
|
||
extension Double: ArgumentConvertible { | ||
public func argument() -> Argument<Double> { | ||
return Argument.Value(Box(self)) | ||
extension Double: ExpectationConvertible { | ||
public func expectation() -> Expectation<Double> { | ||
return Expectation(description: "\(self)", matches: { actualValue in self == actualValue }) | ||
} | ||
} | ||
|
||
extension String: ArgumentConvertible { | ||
public func argument() -> Argument<String> { | ||
return Argument.Value(Box(self)) | ||
extension String: ExpectationConvertible { | ||
public func expectation() -> Expectation<String> { | ||
return Expectation(description: "\(self)", matches: { actualValue in self == actualValue }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.