Revert Descriptions to before "spring-cleanup" PR.

This commit is contained in:
Tadeas Kriz 2016-06-07 20:11:10 +02:00
parent fa9175c604
commit 50ff843740
8 changed files with 76 additions and 36 deletions

View File

@ -6,22 +6,56 @@
// Copyright © 2016 Brightify. All rights reserved.
//
public class Description: CustomStringConvertible {
public private(set) var description = ""
public protocol Description: class {
func append(text text: String) -> Self
func append(texts: Any...) -> Description {
texts.forEach {
description += "\($0)"
}
func append(character character: Character) -> Self
}
extension Description {
public func append(descriptionOf value: SelfDescribing) -> Self {
value.describeTo(self)
return self
}
func appendValue(value: Any) -> Description {
if let value = value as? SelfDescribing {
value.describeTo(self)
} else {
description += "\(value)"
public func append(value value: Any) -> Self {
if let selfDescribing = value as? SelfDescribing {
return append(descriptionOf: selfDescribing)
}
return append(text: "\(value)")
}
public func append(values values: Any..., start: String = "", separator: String = "", end: String = "") -> Self {
append(text: start)
values.enumerate().forEach {
if $0 > 0 {
append(text: separator)
}
append(value: $1)
}
append(text: end)
return self
}
}
public final class StringDescription: Description, CustomStringConvertible {
public private(set) var description = ""
public init(description: String = "") {
self.description = description
}
public func append(text text: String) -> StringDescription {
description += text
return self
}
public func append(character character: Character) -> StringDescription {
description.append(character)
return self
}
}

View File

@ -14,39 +14,39 @@ extension Optional: SelfDescribing {
public func describeTo(description: Description) {
switch self {
case .None:
description.append("nil")
description.append(text: "nil")
case .Some(let value):
description.append(value)
description.append(value: value)
}
}
}
extension String: SelfDescribing {
public func describeTo(description: Description) {
description.append("\"\(self)\"")
description.append(text: "\"\(self)\"")
}
}
extension Bool: SelfDescribing {
public func describeTo(description: Description) {
description.append("<\(self)>")
description.append(text: "<\(self)>")
}
}
extension Int: SelfDescribing {
public func describeTo(description: Description) {
description.append("<\(self)>")
description.append(text: "<\(self)>")
}
}
extension Float: SelfDescribing {
public func describeTo(description: Description) {
description.append("<\(self)>")
description.append(text: "<\(self)>")
}
}
extension Double: SelfDescribing {
public func describeTo(description: Description) {
description.append("<\(self)>")
description.append(text: "<\(self)>")
}
}

View File

@ -37,9 +37,9 @@ public struct AnyMatcher<T>: Matcher {
do {
return try describeMismatchFunction(input, to: description)
} catch TypeStripingError.CalledWithIncorrectType {
description.append("instance of", targetType)
description.append(text: "instance of").append(value: targetType)
} catch let error {
description.append("Unknown error occured while matching: \(error)")
description.append(text: "Unknown error occured while matching: \(error)")
}
}

View File

@ -29,7 +29,7 @@ public struct FunctionMatcher<T>: Matcher {
if let describeMismatchFunction = describeMismatchFunction {
describeMismatchFunction(input, to: description)
} else {
description.append("was ").appendValue(input)
description.append(text: "was ").append(value: input)
}
}

View File

@ -26,7 +26,7 @@ extension Matcher {
}
public func describeMismatch(input: MatchedType, to description: Description) {
description.append("was ", input)
description.append(text: "was ").append(value: input)
}
}
@ -40,7 +40,7 @@ extension Matcher {
return self.matches($0) || otherMatcher.matches($0)
}
let description: Description -> Void = {
$0.append("either ").appendValue(self).append(" or ").appendValue(otherMatcher)
$0.append(text: "either ").append(descriptionOf: self).append(text: " or ").append(descriptionOf: otherMatcher)
}
return FunctionMatcher(function: function, describeTo: description).typeErased()
@ -51,7 +51,7 @@ extension Matcher {
return self.matches($0) && otherMatcher.matches($0)
}
let description: Description -> Void = {
$0.append("both ").appendValue(self).append(" and ").appendValue(otherMatcher)
$0.append(text: "both ").append(descriptionOf: self).append(text: " and ").append(descriptionOf: otherMatcher)
}
return FunctionMatcher(function: function, describeTo: description).typeErased()

View File

@ -11,14 +11,14 @@ private func compareCalls(count: Int, whenNil: Bool = false, using function: (In
}
private func describeCallMismatch(calls: [StubCall], description: Description) {
description.append("was called ", calls.count, " times")
description.append(text: "was called ").append(value: calls.count).append(text: " times")
}
/// Returns a matcher ensuring a call was made **`count`** times.
@warn_unused_result
public func times(count: Int) -> AnyMatcher<[StubCall]> {
return FunctionMatcher(function: compareCalls(count, using: ==), describeMismatch: describeCallMismatch) {
$0.append("to be called ", count, " times")
$0.append(text: "to be called ").append(value: count).append(text: " times")
}.typeErased()
}
@ -38,7 +38,7 @@ public func atLeastOnce() -> AnyMatcher<[StubCall]> {
@warn_unused_result
public func atLeast(count: Int) -> AnyMatcher<[StubCall]> {
return FunctionMatcher(function: compareCalls(count, using: >=), describeMismatch: describeCallMismatch) {
$0.append("called at least ", count, " times")
$0.append(text: "called at least ").append(value: count).append(text: " times")
}.typeErased()
}
@ -46,7 +46,7 @@ public func atLeast(count: Int) -> AnyMatcher<[StubCall]> {
@warn_unused_result
public func atMost(count: Int) -> AnyMatcher<[StubCall]> {
return FunctionMatcher(function: compareCalls(count, whenNil: true, using: <=), describeMismatch: describeCallMismatch) {
$0.append("called at most ", count, " times")
$0.append(text: "called at most ").append(value: count).append(text: " times")
}.typeErased()
}
@ -84,7 +84,7 @@ public func equalTo<T: AnyObject>(value: T) -> AnyMatcher<T> {
@warn_unused_result
public func equalTo<T>(value: T, equalWhen equalityFunction: (T, T) -> Bool) -> AnyMatcher<T> {
return FunctionMatcher(original: value, function: equalityFunction) {
$0.appendValue($0)
$0.append(text: "Expected to be equal to")
}.typeErased()
}

View File

@ -15,6 +15,7 @@ public class MockManager<STUBBING: StubbingProxy, VERIFICATION: VerificationProx
public init() {
}
public func getter<T>(property: String, original: (Void -> T)? = nil) -> (Void -> T) {
return call(getterName(property), parameters: Void(), original: original)
}
@ -64,13 +65,18 @@ public class MockManager<STUBBING: StubbingProxy, VERIFICATION: VerificationProx
let handler = VerificationHandler(matcher: matcher, sourceLocation: sourceLocation) { method, sourceLocation, callMatcher, verificationMatcher in
let calls = self.stubCalls.filter(callMatcher.matches)
if !verificationMatcher.matches(calls) {
let description = Description()
description.append("Expected ").appendValue(verificationMatcher).append(", but ");
verificationMatcher.describeMismatch(calls, to: description);
if verificationMatcher.matches(calls) == false {
let description = StringDescription()
description
.append(text: "Expected ")
.append(descriptionOf: verificationMatcher)
.append(text: ", but ")
verificationMatcher.describeMismatch(calls, to: description)
XCTFail(description.description, file: sourceLocation.file, line: sourceLocation.line)
}
}
return VERIFICATION(handler: handler)
}
}

View File

@ -42,7 +42,7 @@ public struct VerificationHandler {
}
let description: Description -> Void = {
if method != method {
$0.append("method name ").appendValue(method)
$0.append(text: "method name ").append(text: method)
}
// FIXME Describe params
//typedMatcher.describeTo($0)