-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
203 additions
and
207 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
2 changes: 1 addition & 1 deletion
2
Hamcrest.xcodeproj/xcshareddata/xcschemes/Hamcrest-iOS.xcscheme
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
2 changes: 1 addition & 1 deletion
2
Hamcrest.xcodeproj/xcshareddata/xcschemes/Hamcrest-macOS.xcscheme
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
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,36 +1,40 @@ | ||
public func equalTo<T: Equatable>(expectedValue: T) -> Matcher<T> { | ||
public func equalTo<T: Equatable>(_ expectedValue: T) -> Matcher<T> { | ||
return Matcher("equal to \(expectedValue)") {$0 == expectedValue} | ||
} | ||
|
||
public func closeTo(expectedValue: Double, _ delta: Double) -> Matcher<Double> { | ||
public func closeTo(_ expectedValue: Double, _ delta: Double) -> Matcher<Double> { | ||
return Matcher("within \(delta) of \(expectedValue)") { | ||
(value: Double) -> MatchResult in | ||
let actual = abs(value - expectedValue) | ||
return MatchResult(actual < delta, "difference of \(actual)") | ||
} | ||
} | ||
|
||
public func closeTo(expectedValue: Float, _ delta: Double) -> Matcher<Float> { | ||
public func closeTo(_ expectedValue: Float, _ delta: Double) -> Matcher<Float> { | ||
let matcher = closeTo(Double(expectedValue), delta) | ||
return Matcher(matcher.description) {matcher.matches(Double($0))} | ||
} | ||
|
||
public func greaterThan<T: Comparable>(expectedValue: T) -> Matcher<T> { | ||
public func greaterThan<T: Comparable>(_ expectedValue: T) -> Matcher<T> { | ||
return Matcher("greater than \(expectedValue)") {$0 > expectedValue} | ||
} | ||
|
||
public func greaterThanOrEqualTo<T: Comparable>(expectedValue: T) -> Matcher<T> { | ||
public func greaterThanOrEqualTo<T: Comparable>(_ expectedValue: T) -> Matcher<T> { | ||
return Matcher("greater than or equal to \(expectedValue)") {$0 >= expectedValue} | ||
} | ||
|
||
public func lessThan<T: Comparable>(expectedValue: T) -> Matcher<T> { | ||
public func lessThan<T: Comparable>(_ expectedValue: T) -> Matcher<T> { | ||
return Matcher("less than \(expectedValue)") {$0 < expectedValue} | ||
} | ||
|
||
public func lessThanOrEqualTo<T: Comparable>(expectedValue: T) -> Matcher<T> { | ||
public func lessThanOrEqualTo<T: Comparable>(_ expectedValue: T) -> Matcher<T> { | ||
return Matcher("less than or equal to \(expectedValue)") {$0 <= expectedValue} | ||
} | ||
|
||
public func inInterval<T, I: IntervalType where I.Bound == T>(expectedInterval: I) -> Matcher<T> { | ||
public func inInterval<T>(_ expectedInterval: ClosedRange<T>) -> Matcher<T> { | ||
return Matcher("in interval \(expectedInterval)") {expectedInterval.contains($0)} | ||
} | ||
|
||
public func inInterval<T>(_ expectedInterval: Range<T>) -> Matcher<T> { | ||
return Matcher("in interval \(expectedInterval)") {expectedInterval.contains($0)} | ||
} |
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
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,66 +1,66 @@ | ||
import Foundation | ||
|
||
func describe<T>(value: T) -> String { | ||
func describe<T>(_ value: T) -> String { | ||
if let stringArray = value as? [String] { | ||
return joinDescriptions(stringArray.map {describe($0)}) | ||
} | ||
if let string = value as? String { | ||
return "\"\(string)\"" | ||
} | ||
return String(value) | ||
return String(describing: value) | ||
} | ||
|
||
func describeAddress<T: AnyObject>(object: T) -> String { | ||
return NSString(format: "%p", unsafeBitCast(object, Int.self)) as String | ||
func describeAddress<T: AnyObject>(_ object: T) -> String { | ||
return NSString(format: "%p", unsafeBitCast(object, to: Int.self)) as String | ||
} | ||
|
||
func describeError(error: ErrorType) -> String { | ||
func describeError(_ error: Error) -> String { | ||
return "ERROR: \(error)" | ||
} | ||
|
||
func describeExpectedError() -> String { | ||
return "EXPECTED ERROR" | ||
} | ||
|
||
func describeExpectedError(description: String) -> String { | ||
func describeExpectedError(_ description: String) -> String { | ||
return "EXPECTED ERROR: \(description)" | ||
} | ||
|
||
func describeErrorMismatch<T>(error: T, _ description: String, _ mismatchDescription: String?) -> String { | ||
func describeErrorMismatch<T>(_ error: T, _ description: String, _ mismatchDescription: String?) -> String { | ||
return "GOT ERROR: " + describeActualValue(error, mismatchDescription) + ", EXPECTED ERROR: \(description)" | ||
} | ||
|
||
func describeMismatch<T>(value: T, _ description: String, _ mismatchDescription: String?) -> String { | ||
func describeMismatch<T>(_ value: T, _ description: String, _ mismatchDescription: String?) -> String { | ||
return "GOT: " + describeActualValue(value, mismatchDescription) + ", EXPECTED: \(description)" | ||
} | ||
|
||
func describeActualValue<T>(value: T, _ mismatchDescription: String?) -> String { | ||
func describeActualValue<T>(_ value: T, _ mismatchDescription: String?) -> String { | ||
return describe(value) + (mismatchDescription.map{" (\($0))"} ?? "") | ||
} | ||
|
||
func joinDescriptions(descriptions: [String]) -> String { | ||
func joinDescriptions(_ descriptions: [String]) -> String { | ||
return joinStrings(descriptions) | ||
} | ||
|
||
func joinDescriptions(descriptions: [String?]) -> String? { | ||
func joinDescriptions(_ descriptions: [String?]) -> String? { | ||
let notNil = filterNotNil(descriptions) | ||
return notNil.isEmpty ? nil : joinStrings(notNil) | ||
} | ||
|
||
func joinMatcherDescriptions<S: SequenceType, T where S.Generator.Element == Matcher<T>>(matchers: S, prefix: String = "all of") -> String { | ||
var generator = matchers.generate() | ||
if let first = generator.next() where generator.next() == nil { | ||
func joinMatcherDescriptions<S: Sequence, T>(_ matchers: S, prefix: String = "all of") -> String where S.Iterator.Element == Matcher<T> { | ||
var generator = matchers.makeIterator() | ||
if let first = generator.next(), generator.next() == nil { | ||
return first.description | ||
} else { | ||
return prefix + " " + joinDescriptions(matchers.map({$0.description})) | ||
} | ||
} | ||
|
||
private func joinStrings(strings: [String]) -> String { | ||
private func joinStrings(_ strings: [String]) -> String { | ||
switch (strings.count) { | ||
case 1: | ||
return strings[0] | ||
default: | ||
return "[" + strings.joinWithSeparator(", ") + "]" | ||
return "[" + strings.joined(separator: ", ") + "]" | ||
} | ||
} | ||
} |
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,38 +1,38 @@ | ||
public func hasEntry<K: Hashable, V>(keyMatcher: Matcher<K>, _ valueMatcher: Matcher<V>) | ||
public func hasEntry<K: Hashable, V>(_ keyMatcher: Matcher<K>, _ valueMatcher: Matcher<V>) | ||
-> Matcher<Dictionary<K, V>> { | ||
|
||
return Matcher("a dictionary containing [\(keyMatcher.description) -> \(valueMatcher.description)]") { | ||
(dictionary: Dictionary<K, V>) -> Bool in | ||
|
||
for (key, value) in dictionary { | ||
if keyMatcher.matches(key) && valueMatcher.matches(value) { | ||
if keyMatcher.matches(key).boolValue && valueMatcher.matches(value).boolValue { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
|
||
public func hasEntry<K: Equatable, V: Equatable where K: Hashable>(expectedKey: K, _ expectedValue: V) | ||
-> Matcher<Dictionary<K, V>> { | ||
public func hasEntry<K: Equatable, V: Equatable>(_ expectedKey: K, _ expectedValue: V) | ||
-> Matcher<Dictionary<K, V>> where K: Hashable { | ||
|
||
return hasEntry(equalToWithoutDescription(expectedKey), equalToWithoutDescription(expectedValue)) | ||
} | ||
|
||
public func hasKey<K: Hashable, V>(matcher: Matcher<K>) -> Matcher<Dictionary<K, V>> { | ||
public func hasKey<K: Hashable, V>(_ matcher: Matcher<K>) -> Matcher<Dictionary<K, V>> { | ||
return hasEntry(matcher, anything()) | ||
} | ||
|
||
public func hasKey<K, V where K: Equatable, K: Hashable>(expectedKey: K) | ||
-> Matcher<Dictionary<K, V>> { | ||
public func hasKey<K, V>(_ expectedKey: K) | ||
-> Matcher<Dictionary<K, V>> where K: Equatable, K: Hashable { | ||
|
||
return hasKey(equalToWithoutDescription(expectedKey)) | ||
} | ||
|
||
public func hasValue<K: Hashable, V>(matcher: Matcher<V>) -> Matcher<Dictionary<K, V>> { | ||
public func hasValue<K: Hashable, V>(_ matcher: Matcher<V>) -> Matcher<Dictionary<K, V>> { | ||
return hasEntry(anything(), matcher) | ||
} | ||
|
||
public func hasValue<K: Hashable, V: Equatable>(expectedValue: V) -> Matcher<Dictionary<K, V>> { | ||
public func hasValue<K: Hashable, V: Equatable>(_ expectedValue: V) -> Matcher<Dictionary<K, V>> { | ||
return hasValue(equalToWithoutDescription(expectedValue)) | ||
} |
Oops, something went wrong.