Skip to content

Commit

Permalink
Put closure "in" statement on same line as opening brace
Browse files Browse the repository at this point in the history
This layout is more common these days.
  • Loading branch information
jonreid committed Mar 21, 2018
1 parent 2ad0070 commit 95c1e2e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 40 deletions.
3 changes: 1 addition & 2 deletions Hamcrest/ArithmeticMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ public func equalTo<T: Equatable>(_ expectedValue: T) -> Matcher<T> {
}

public func closeTo(_ expectedValue: Double, _ delta: Double) -> Matcher<Double> {
return Matcher("within \(delta) of \(expectedValue)") {
(value: Double) -> MatchResult in
return Matcher("within \(delta) of \(expectedValue)") { (value: Double) -> MatchResult in
let actual = abs(value - expectedValue)
return MatchResult(actual < delta, "difference of \(actual)")
}
Expand Down
12 changes: 4 additions & 8 deletions Hamcrest/BasicMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ public func anything<T>() -> Matcher<T> {
}

public func sameInstance<T: AnyObject>(_ expectedValue: T) -> Matcher<T> {
return Matcher("same instance as \(describeAddress(expectedValue))") {
(value: T) -> MatchResult in
return Matcher("same instance as \(describeAddress(expectedValue))") { (value: T) -> MatchResult in
value === expectedValue ? .match : .mismatch(describeAddress(value))
}
}

// MARK: is

public func `is`<T>(_ matcher: Matcher<T>) -> Matcher<T> {
return Matcher("is " + matcher.description) {
(value: T) -> MatchResult in
return Matcher("is " + matcher.description) { (value: T) -> MatchResult in
return matcher.matches(value)
}
}
Expand All @@ -37,8 +35,7 @@ public func present<T>() -> Matcher<Optional<T>> {
}

public func presentAnd<T>(_ matcher: Matcher<T>) -> Matcher<Optional<T>> {
return Matcher("present and \(matcher.description)") {
(value: T?) -> MatchResult in
return Matcher("present and \(matcher.description)") { (value: T?) -> MatchResult in
if let unwrappedValue = value {
return matcher.matches(unwrappedValue)
} else {
Expand All @@ -59,8 +56,7 @@ public func instanceOf<T: Any>(_ expectedType: T.Type, and matcher: Matcher<T>)
}

public func instanceOfAnd<T: Any>(_ matcher: Matcher<T>) -> Matcher<Any> {
return Matcher("instance of \(T.self) and \(matcher.description)") {
(value: Any) -> MatchResult in
return Matcher("instance of \(T.self) and \(matcher.description)") { (value: Any) -> MatchResult in
if let value = value as? T {
return matcher.matches(value)
} else {
Expand Down
4 changes: 1 addition & 3 deletions Hamcrest/DictionaryMatchers.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
public func hasEntry<K, 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

return Matcher("a dictionary containing [\(keyMatcher.description) -> \(valueMatcher.description)]") { (dictionary: Dictionary<K, V>) -> Bool in
for (key, value) in dictionary {
if keyMatcher.matches(key).boolValue && valueMatcher.matches(value).boolValue {
return true
Expand Down
9 changes: 3 additions & 6 deletions Hamcrest/MetaMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ public func allOf<T>(_ matchers: Matcher<T>...) -> Matcher<T> {
}

public func allOf<S: Sequence, T>(_ matchers: S) -> Matcher<T> where S.Iterator.Element == Matcher<T> {
return Matcher(joinMatcherDescriptions(matchers)) {
(value: T) -> MatchResult in
return Matcher(joinMatcherDescriptions(matchers)) { (value: T) -> MatchResult in
var mismatchDescriptions: [String?] = []
for matcher in matchers {
switch delegateMatching(value, matcher, {
(mismatchDescription: String?) -> String? in
switch delegateMatching(value, matcher, { (mismatchDescription: String?) -> String? in
"mismatch: \(matcher.description)"
+ (mismatchDescription.map {" (\($0))"} ?? "")
}) {
Expand All @@ -43,8 +41,7 @@ public func anyOf<T>(_ matchers: Matcher<T>...) -> Matcher<T> {
}

public func anyOf<S: Sequence, T>(_ matchers: S) -> Matcher<T> where S.Iterator.Element == Matcher<T> {
return Matcher(joinMatcherDescriptions(matchers, prefix: "any of")) {
(value: T) -> MatchResult in
return Matcher(joinMatcherDescriptions(matchers, prefix: "any of")) { (value: T) -> MatchResult in
let matchedMatchers = matchers.filter {$0.matches(value).boolValue}
return MatchResult(!matchedMatchers.isEmpty)
}
Expand Down
3 changes: 1 addition & 2 deletions Hamcrest/ReflectionMatchers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
public func hasProperty<T, U>(_ propertyMatcher: Matcher<String>, _ matcher: Matcher<U>) -> Matcher<T> {
return Matcher("has property \(propertyMatcher.description) with value \(matcher.description)") {
(value: T) -> MatchResult in
return Matcher("has property \(propertyMatcher.description) with value \(matcher.description)") { (value: T) -> MatchResult in
if let propertyValue = getProperty(value, keyMatcher: propertyMatcher) {
if let propertyValue = propertyValue as? U {
return delegateMatching(propertyValue, matcher) {
Expand Down
23 changes: 8 additions & 15 deletions Hamcrest/SequenceMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ public func empty<T: Collection>() -> Matcher<T> {
}

public func hasCount<T: Collection>(_ matcher: Matcher<T.IndexDistance>) -> Matcher<T> {
return Matcher("has count " + matcher.description) {
(value: T) -> MatchResult in
return Matcher("has count " + matcher.description) { (value: T) -> MatchResult in
let n = value.count
return delegateMatching(n, matcher) {
return "count " + describeActualValue(n, $0)
Expand All @@ -17,12 +16,10 @@ public func hasCount<T: Collection>(_ expectedCount: T.IndexDistance) -> Matcher
}

public func everyItem<T, S: Sequence>(_ matcher: Matcher<T>) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence where every item \(matcher.description)") {
(values: S) -> MatchResult in
return Matcher("a sequence where every item \(matcher.description)") { (values: S) -> MatchResult in
var mismatchDescriptions: [String?] = []
for value in values {
switch delegateMatching(value, matcher, {
(mismatchDescription: String?) -> String? in
switch delegateMatching(value, matcher, { (mismatchDescription: String?) -> String? in
"mismatch: \(value)" + (mismatchDescription.map {" (\($0))"} ?? "")
}) {
case let .mismatch(mismatchDescription):
Expand All @@ -45,8 +42,8 @@ private func hasItem<T, S: Sequence>(_ matcher: Matcher<T>, _ values: S) -> Bool
}

public func hasItem<T, S: Sequence>(_ matcher: Matcher<T>) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing \(matcher.description)") {
(values: S) -> Bool in hasItem(matcher, values)
return Matcher("a sequence containing \(matcher.description)") { (values: S) -> Bool in
hasItem(matcher, values)
}
}

Expand All @@ -55,8 +52,7 @@ public func hasItem<T: Equatable, S: Sequence>(_ expectedValue: T) -> Matcher<S>
}

private func hasItems<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing \(joinMatcherDescriptions(matchers))") {
(values: S) -> MatchResult in
return Matcher("a sequence containing \(joinMatcherDescriptions(matchers))") { (values: S) -> MatchResult in
var missingItems = [] as [Matcher<T>]
for matcher in matchers {
if !hasItem(matcher, values) {
Expand All @@ -83,8 +79,7 @@ public func hasItems<T: Equatable, S: Sequence>(_ expectedValues: T...) -> Match
}

private func contains<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
return Matcher("a sequence containing " + joinDescriptions(matchers.map({$0.description}))) {
(values: S) -> MatchResult in
return Matcher("a sequence containing " + joinDescriptions(matchers.map({$0.description}))) { (values: S) -> MatchResult in
return applyMatchers(matchers, values: values)
}
}
Expand All @@ -100,9 +95,7 @@ public func contains<T: Equatable, S: Sequence>(_ expectedValues: T...) -> Match
private func containsInAnyOrder<T, S: Sequence>(_ matchers: [Matcher<T>]) -> Matcher<S> where S.Iterator.Element == T {
let descriptions = joinDescriptions(matchers.map({$0.description}))

return Matcher("a sequence containing in any order " + descriptions) {
(values: S) -> MatchResult in

return Matcher("a sequence containing in any order " + descriptions) { (values: S) -> MatchResult in
var unmatchedValues: [T] = []
var remainingMatchers = matchers

Expand Down
6 changes: 2 additions & 4 deletions Hamcrest/StringMatchers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ public func containsString(_ string: String) -> Matcher<String> {
}

public func containsStringsInOrder(_ strings: String...) -> Matcher<String> {
return Matcher("contains in order \(describe(strings))") {
(value: String) -> Bool in
return Matcher("contains in order \(describe(strings))") { (value: String) -> Bool in
var range = value.startIndex..<value.endIndex
for string in strings {
let r = value.range(of: string, options: .caseInsensitive, range: range)
Expand Down Expand Up @@ -34,8 +33,7 @@ public func matchesPattern(_ pattern: String, options: NSRegularExpression.Optio
}

public func matchesPattern(_ regularExpression: NSRegularExpression) -> Matcher<String> {
return Matcher("matches \(describe(regularExpression.pattern))") {
(value: String) -> Bool in
return Matcher("matches \(describe(regularExpression.pattern))") { (value: String) -> Bool in
let range = NSMakeRange(0, (value as NSString).length)
return regularExpression.numberOfMatches(in: value, options: [], range: range) > 0
}
Expand Down

0 comments on commit 95c1e2e

Please sign in to comment.