Skip to content

Commit

Permalink
Merge pull request #12 from rheinfabrik/swift-1.2
Browse files Browse the repository at this point in the history
Backport Swift 2.0 changes to Swift 1.2
  • Loading branch information
DerLobi committed Jul 28, 2015
2 parents 1f4e98f + 04b7adb commit 51f2492
Show file tree
Hide file tree
Showing 20 changed files with 676 additions and 503 deletions.
2 changes: 0 additions & 2 deletions Cartfile

This file was deleted.

1 change: 1 addition & 0 deletions Cartfile.private
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "Quick/Quick" ~> 0.3
github "Quick/Nimble" ~> 0.4
1 change: 0 additions & 1 deletion Cartfile.resolved
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"
88 changes: 26 additions & 62 deletions Dobby.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

50 changes: 0 additions & 50 deletions Dobby/Argument.swift

This file was deleted.

20 changes: 20 additions & 0 deletions Dobby/Disposable.swift
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
}
}
177 changes: 177 additions & 0 deletions Dobby/Expectation.swift
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
}
}
32 changes: 15 additions & 17 deletions Dobby/Extensions/SwiftExtensions.swift
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 })
}
}
2 changes: 1 addition & 1 deletion Dobby/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>de.rheinfabrik.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Loading

0 comments on commit 51f2492

Please sign in to comment.