Skip to content

Commit

Permalink
Merge pull request Quick#132 from Quick/swift-2
Browse files Browse the repository at this point in the history
Support Swift 2.0 on separate branch
  • Loading branch information
modocache committed Jun 9, 2015
2 parents bd5b793 + 55d984f commit 3102770
Show file tree
Hide file tree
Showing 27 changed files with 90 additions and 74 deletions.
5 changes: 4 additions & 1 deletion Nimble.xcodeproj/xcshareddata/xcschemes/Nimble-OSX.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0600"
LastUpgradeVersion = "0700"
version = "2.0">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -39,6 +39,8 @@
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = ""
Expand All @@ -49,6 +51,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugXPCServices = "NO"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
5 changes: 4 additions & 1 deletion Nimble.xcodeproj/xcshareddata/xcschemes/Nimble-iOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0600"
LastUpgradeVersion = "0700"
version = "2.0">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -39,6 +39,8 @@
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = ""
Expand All @@ -49,6 +51,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugXPCServices = "NO"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
12 changes: 6 additions & 6 deletions Nimble/Adapters/AssertionRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
///
/// @see AssertionRecorder
/// @see AssertionHandler
public struct AssertionRecord: Printable {
public struct AssertionRecord: CustomStringConvertible {
/// Whether the assertion succeeded or failed
public let success: Bool
/// The failure message the assertion would display on failure.
Expand Down Expand Up @@ -66,7 +66,7 @@ public func withAssertionHandler(tempAssertionHandler: AssertionHandler, closure
/// @see gatherFailingExpectations
public func gatherExpectations(silently: Bool = false, closure: () -> Void) -> [AssertionRecord] {
let previousRecorder = NimbleAssertionHandler
var recorder = AssertionRecorder()
let recorder = AssertionRecorder()
let handlers: [AssertionHandler]

if silently {
Expand All @@ -75,8 +75,8 @@ public func gatherExpectations(silently: Bool = false, closure: () -> Void) -> [
handlers = [recorder, previousRecorder]
}

var dispatcher = AssertionDispatcher(handlers: handlers)
withAssertionHandler(dispatcher, closure)
let dispatcher = AssertionDispatcher(handlers: handlers)
withAssertionHandler(dispatcher, closure: closure)
return recorder.assertions
}

Expand All @@ -92,8 +92,8 @@ public func gatherExpectations(silently: Bool = false, closure: () -> Void) -> [
/// @see gatherExpectations
/// @see raiseException source for an example use case.
public func gatherFailingExpectations(silently: Bool = false, closure: () -> Void) -> [AssertionRecord] {
let assertions = gatherExpectations(silently: silently, closure)
return filter(assertions) { assertion in
let assertions = gatherExpectations(silently, closure: closure)
return assertions.filter { assertion in
!assertion.success
}
}
7 changes: 4 additions & 3 deletions Nimble/DSL+Wait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
/// bridges to Objective-C via the @objc keyword. This class encapsulates callback-style
/// asynchronous waiting logic so that it may be called from Objective-C and Swift.
@objc internal class NMBWait {
internal class func until(#timeout: NSTimeInterval, file: String = __FILE__, line: UInt = __LINE__, action: (() -> Void) -> Void) -> Void {
internal class func until(timeout timeout: NSTimeInterval, file: String = __FILE__, line: UInt = __LINE__, action: (() -> Void) -> Void) -> Void {
var completed = false
var token: dispatch_once_t = 0
let result = pollBlock(pollInterval: 0.01, timeoutInterval: timeout) {
Expand All @@ -23,6 +23,7 @@ import Foundation
}
}

@objc(untilFile:line:action:)
internal class func until(file: String = __FILE__, line: UInt = __LINE__, action: (() -> Void) -> Void) -> Void {
until(timeout: 1, file: file, line: line, action: action)
}
Expand All @@ -31,13 +32,13 @@ import Foundation
/// Wait asynchronously until the done closure is called.
///
/// This will advance the run loop.
public func waitUntil(#timeout: NSTimeInterval, file: String = __FILE__, line: UInt = __LINE__, action: (() -> Void) -> Void) -> Void {
public func waitUntil(timeout timeout: NSTimeInterval, file: String = __FILE__, line: UInt = __LINE__, action: (() -> Void) -> Void) -> Void {
NMBWait.until(timeout: timeout, file: file, line: line, action: action)
}

/// Wait asynchronously until the done closure is called.
///
/// This will advance the run loop.
public func waitUntil(file: String = __FILE__, line: UInt = __LINE__, action: (() -> Void) -> Void) -> Void {
NMBWait.until(file: file, line: line, action: action)
NMBWait.until(timeout: 1, file: file, line: line, action: action)
}
2 changes: 1 addition & 1 deletion Nimble/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public func expect<T>(file: String = __FILE__, line: UInt = __LINE__, expression
}

/// Always fails the test with a message and a specified location.
public func fail(message: String, #location: SourceLocation) {
public func fail(message: String, location: SourceLocation) {
NimbleAssertionHandler.assert(false, message: FailureMessage(stringValue: message), location: location)
}

Expand Down
12 changes: 6 additions & 6 deletions Nimble/Expectation.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

internal func expressionMatches<T, U where U: Matcher, U.ValueType == T>(expression: Expression<T>, matcher: U, #to: String) -> (Bool, FailureMessage) {
var msg = FailureMessage()
internal func expressionMatches<T, U where U: Matcher, U.ValueType == T>(expression: Expression<T>, matcher: U, to: String) -> (Bool, FailureMessage) {
let msg = FailureMessage()
msg.to = to
let pass = matcher.matches(expression, failureMessage: msg)
if msg.actualValue == "" {
Expand All @@ -10,8 +10,8 @@ internal func expressionMatches<T, U where U: Matcher, U.ValueType == T>(express
return (pass, msg)
}

internal func expressionDoesNotMatch<T, U where U: Matcher, U.ValueType == T>(expression: Expression<T>, matcher: U, #toNot: String) -> (Bool, FailureMessage) {
var msg = FailureMessage()
internal func expressionDoesNotMatch<T, U where U: Matcher, U.ValueType == T>(expression: Expression<T>, matcher: U, toNot: String) -> (Bool, FailureMessage) {
let msg = FailureMessage()
msg.to = toNot
let pass = matcher.doesNotMatch(expression, failureMessage: msg)
if msg.actualValue == "" {
Expand All @@ -29,13 +29,13 @@ public struct Expectation<T> {

/// Tests the actual value using a matcher to match.
public func to<U where U: Matcher, U.ValueType == T>(matcher: U) {
let (pass, msg) = expressionMatches(expression, matcher, to: "to")
let (pass, msg) = expressionMatches(expression, matcher: matcher, to: "to")
verify(pass, msg)
}

/// Tests the actual value using a matcher to not match.
public func toNot<U where U: Matcher, U.ValueType == T>(matcher: U) {
let (pass, msg) = expressionDoesNotMatch(expression, matcher, toNot: "to not")
let (pass, msg) = expressionDoesNotMatch(expression, matcher: matcher, toNot: "to not")
verify(pass, msg)
}

Expand Down
2 changes: 1 addition & 1 deletion Nimble/FailureMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Foundation
}

internal func stripNewlines(str: String) -> String {
var lines: [String] = (str as NSString).componentsSeparatedByString("\n") as! [String]
var lines: [String] = (str as NSString).componentsSeparatedByString("\n") as [String]
let whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet()
lines = lines.map { line in line.stringByTrimmingCharactersInSet(whitespace) }
return "".join(lines)
Expand Down
2 changes: 1 addition & 1 deletion Nimble/Matchers/AllPass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public func allPass<T,U where U: SequenceType, U.Generator.Element == T>
}

public func allPass<T,U where U: SequenceType, U.Generator.Element == T>
(passName:String, passFunc: (T?) -> Bool) -> NonNilMatcherFunc<U> {
(passName: String, _ passFunc: (T?) -> Bool) -> NonNilMatcherFunc<U> {
return createAllPassMatcher() {
expression, failureMessage in
failureMessage.postfixMessage = passName
Expand Down
6 changes: 3 additions & 3 deletions Nimble/Matchers/BeCloseTo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal func isCloseTo(actualValue: Double?, expectedValue: Double, delta: Doub
/// @see equal
public func beCloseTo(expectedValue: Double, within delta: Double = DefaultDelta) -> NonNilMatcherFunc<Double> {
return NonNilMatcherFunc { actualExpression, failureMessage in
return isCloseTo(actualExpression.evaluate(), expectedValue, delta, failureMessage)
return isCloseTo(actualExpression.evaluate(), expectedValue: expectedValue, delta: delta, failureMessage: failureMessage)
}
}

Expand All @@ -28,7 +28,7 @@ public func beCloseTo(expectedValue: Double, within delta: Double = DefaultDelta
/// @see equal
public func beCloseTo(expectedValue: NMBDoubleConvertible, within delta: Double = DefaultDelta) -> NonNilMatcherFunc<NMBDoubleConvertible> {
return NonNilMatcherFunc { actualExpression, failureMessage in
return isCloseTo(actualExpression.evaluate()?.doubleValue, expectedValue.doubleValue, delta, failureMessage)
return isCloseTo(actualExpression.evaluate()?.doubleValue, expectedValue: expectedValue.doubleValue, delta: delta, failureMessage: failureMessage)
}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public func beCloseTo(expectedValues: [Double], within delta: Double = DefaultDe
if actual.count != expectedValues.count {
return false
} else {
for (index, actualItem) in enumerate(actual) {
for (index, actualItem) in actual.enumerate() {
if fabs(actualItem - expectedValues[index]) > delta {
return false
}
Expand Down
10 changes: 10 additions & 0 deletions Nimble/Matchers/BeEmpty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public func beEmpty<S: SequenceType>() -> NonNilMatcherFunc<S> {

/// A Nimble matcher that succeeds when a value is "empty". For collections, this
/// means the are no items in that collection. For strings, it is an empty string.
public func beEmpty() -> NonNilMatcherFunc<String> {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "be empty"
let actualString = actualExpression.evaluate()
return actualString == nil || (actualString! as NSString).length == 0
}
}

/// A Nimble matcher that succeeds when a value is "empty". For collections, this
/// means the are no items in that collection. For NSString instances, it is an empty string.
public func beEmpty() -> NonNilMatcherFunc<NSString> {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "be empty"
Expand Down
2 changes: 1 addition & 1 deletion Nimble/Matchers/BeginWith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension NMBObjCMatcher {
public class func beginWithMatcher(expected: AnyObject) -> NMBObjCMatcher {
return NMBObjCMatcher(canMatchNil: false) { actualExpression, failureMessage in
let actual = actualExpression.evaluate()
if let actualString = actual as? String {
if let _ = actual as? String {
let expr = actualExpression.cast { $0 as? String }
return beginWith(expected as! String).matches(expr, failureMessage: failureMessage)
} else {
Expand Down
8 changes: 3 additions & 5 deletions Nimble/Matchers/Contain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public func contain<S: SequenceType, T: Equatable where S.Generator.Element == T
failureMessage.postfixMessage = "contain <\(arrayAsString(items))>"
if let actual = actualExpression.evaluate() {
return all(items) {
return contains(actual, $0)
return actual.contains($0)
}
}
return false
Expand All @@ -20,7 +20,7 @@ public func contain(substrings: String...) -> NonNilMatcherFunc<String> {
if let actual = actualExpression.evaluate() {
return all(substrings) {
let scanRange = Range(start: actual.startIndex, end: actual.endIndex)
let range = actual.rangeOfString($0, options: nil, range: scanRange, locale: nil)
let range = actual.rangeOfString($0, options: [], range: scanRange, locale: nil)
return range != nil && !range!.isEmpty
}
}
Expand All @@ -33,9 +33,7 @@ public func contain(substrings: NSString...) -> NonNilMatcherFunc<NSString> {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "contain <\(arrayAsString(substrings))>"
if let actual = actualExpression.evaluate() {
return all(substrings) {
return actual.containsString($0.description)
}
return all(substrings) { actual.rangeOfString($0.description).length != 0 }
}
return false
}
Expand Down
5 changes: 2 additions & 3 deletions Nimble/Matchers/EndWith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public func endWith<S: SequenceType, T: Equatable where S.Generator.Element == T
var actualGenerator = actualValue.generate()
var lastItem: T?
var item: T?
do {
repeat {
lastItem = item
item = actualGenerator.next()
} while(item != nil)
Expand Down Expand Up @@ -50,9 +50,8 @@ public func endWith(endingSubstring: String) -> NonNilMatcherFunc<String> {
extension NMBObjCMatcher {
public class func endWithMatcher(expected: AnyObject) -> NMBObjCMatcher {
return NMBObjCMatcher(canMatchNil: false) { actualExpression, failureMessage in
let location = actualExpression.location
let actual = actualExpression.evaluate()
if let actualString = actual as? String {
if let _ = actual as? String {
let expr = actualExpression.cast { $0 as? String }
return endWith(expected as! String).matches(expr, failureMessage: failureMessage)
} else {
Expand Down
4 changes: 2 additions & 2 deletions Nimble/Matchers/Equal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public func equal<T>(expectedValue: Set<T>?) -> NonNilMatcherFunc<Set<T>> {
public func equal<T: Comparable>(expectedValue: Set<T>?) -> NonNilMatcherFunc<Set<T>> {
return equal(expectedValue, stringify: {
if let set = $0 {
return stringify(Array(set).sorted { $0 < $1 })
return stringify(Array(set).sort { $0 < $1 })
} else {
return "nil"
}
})
}

private func equal<T>(expectedValue: Set<T>?, #stringify: Set<T>? -> String) -> NonNilMatcherFunc<Set<T>> {
private func equal<T>(expectedValue: Set<T>?, stringify: Set<T>? -> String) -> NonNilMatcherFunc<Set<T>> {
return NonNilMatcherFunc { actualExpression, failureMessage in
failureMessage.postfixMessage = "equal <\(stringify(expectedValue))>"

Expand Down
1 change: 0 additions & 1 deletion Nimble/Matchers/MatcherProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ extension NSArray : NMBOrderedCollection {}
var doubleValue: CDouble { get }
}
extension NSNumber : NMBDoubleConvertible { }
extension NSDecimalNumber : NMBDoubleConvertible { } // TODO: not the best to downsize

/// Protocol for types to support beLessThan(), beLessThanOrEqualTo(),
/// beGreaterThan(), beGreaterThanOrEqualTo(), and equal() matchers.
Expand Down
12 changes: 6 additions & 6 deletions Nimble/Matchers/RaisesException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import Foundation
/// nil arguments indicates that the matcher should not attempt to match against
/// that parameter.
public func raiseException(
named: String? = nil,
named named: String? = nil,
reason: String? = nil,
userInfo: NSDictionary? = nil,
closure: ((NSException) -> Void)? = nil) -> MatcherFunc<Any> {
return MatcherFunc { actualExpression, failureMessage in

var exception: NSException?
var capture = NMBExceptionCapture(handler: ({ e in
let capture = NMBExceptionCapture(handler: ({ e in
exception = e
}), finally: nil)

Expand All @@ -26,8 +26,8 @@ public func raiseException(
return
}

setFailureMessageForException(failureMessage, exception, named, reason, userInfo, closure)
return exceptionMatchesNonNilFieldsOrClosure(exception, named, reason, userInfo, closure)
setFailureMessageForException(failureMessage, exception: exception, named: named, reason: reason, userInfo: userInfo, closure: closure)
return exceptionMatchesNonNilFieldsOrClosure(exception, named: named, reason: reason, userInfo: userInfo, closure: closure)
}
}

Expand All @@ -49,7 +49,7 @@ internal func setFailureMessageForException(
if let userInfo = userInfo {
failureMessage.postfixMessage += " with userInfo <\(userInfo)>"
}
if let closure = closure {
if let _ = closure {
failureMessage.postfixMessage += " that satisfies block"
}
if named == nil && reason == nil && userInfo == nil && closure == nil {
Expand Down Expand Up @@ -87,7 +87,7 @@ internal func exceptionMatchesNonNilFieldsOrClosure(
let assertions = gatherFailingExpectations {
closure(exception)
}
let messages = map(assertions) { $0.message }
let messages = assertions.map { $0.message }
if messages.count > 0 {
matches = false
}
Expand Down
2 changes: 1 addition & 1 deletion Nimble/ObjCExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class NMBExpectation : NSObject {
}

private var expectValue: Expectation<NSObject> {
return expect(file: _file, line: _line){
return expect(_file, line: _line){
self._actualBlock() as NSObject?
}
}
Expand Down

0 comments on commit 3102770

Please sign in to comment.