Skip to content

Commit

Permalink
Adds PrimitiveSequence events to RxTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzaher committed Mar 12, 2017
1 parent 11e25f6 commit a26b9ac
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
67 changes: 67 additions & 0 deletions RxTest/Event+Equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,70 @@ public func == <Element: Equatable>(lhs: Event<Element>, rhs: Event<Element>) ->
default: return false
}
}

/// Compares two events. They are equal if they are both the same member of `Event` enumeration.
///
/// In case `Error` events are being compared, they are equal in case their `NSError` representations are equal (domain and code)
/// and their string representations are equal.
public func == <Element: Equatable>(lhs: SingleEvent<Element>, rhs: SingleEvent<Element>) -> Bool {
switch (lhs, rhs) {
case (.error(let e1), .error(let e2)):
#if os(Linux)
return "\(e1)" == "\(e2)"
#else
let error1 = e1 as NSError
let error2 = e2 as NSError

return error1.domain == error2.domain
&& error1.code == error2.code
&& "\(e1)" == "\(e2)"
#endif
case (.success(let v1), .success(let v2)): return v1 == v2
default: return false
}
}

/// Compares two events. They are equal if they are both the same member of `Event` enumeration.
///
/// In case `Error` events are being compared, they are equal in case their `NSError` representations are equal (domain and code)
/// and their string representations are equal.
public func == <Element: Equatable>(lhs: MaybeEvent<Element>, rhs: MaybeEvent<Element>) -> Bool {
switch (lhs, rhs) {
case (.completed, .completed): return true
case (.error(let e1), .error(let e2)):
#if os(Linux)
return "\(e1)" == "\(e2)"
#else
let error1 = e1 as NSError
let error2 = e2 as NSError

return error1.domain == error2.domain
&& error1.code == error2.code
&& "\(e1)" == "\(e2)"
#endif
case (.success(let v1), .success(let v2)): return v1 == v2
default: return false
}
}

/// Compares two events. They are equal if they are both the same member of `Event` enumeration.
///
/// In case `Error` events are being compared, they are equal in case their `NSError` representations are equal (domain and code)
/// and their string representations are equal.
public func == (lhs: CompleteableEvent, rhs: CompleteableEvent) -> Bool {
switch (lhs, rhs) {
case (.completed, .completed): return true
case (.error(let e1), .error(let e2)):
#if os(Linux)
return "\(e1)" == "\(e2)"
#else
let error1 = e1 as NSError
let error2 = e2 as NSError

return error1.domain == error2.domain
&& error1.code == error2.code
&& "\(e1)" == "\(e2)"
#endif
default: return false
}
}
78 changes: 78 additions & 0 deletions RxTest/XCTest+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,84 @@ public func XCTAssertEqual<T: Equatable>(_ lhs: [Event<T>], _ rhs: [Event<T>], f
printSequenceDifferences(lhs, rhs, ==)
}

/**
Asserts two lists of events are equal.
Event is considered equal if:
* `Next` events are equal if they have equal corresponding elements.
* `Error` events are equal if errors have same domain and code for `NSError` representation and have equal descriptions.
* `Completed` events are always equal.
- parameter lhs: first set of events.
- parameter lhs: second set of events.
*/
public func XCTAssertEqual<T: Equatable>(_ lhs: [SingleEvent<T>], _ rhs: [SingleEvent<T>], file: StaticString = #file, line: UInt = #line) {
let leftEquatable = lhs.map { AnyEquatable(target: $0, comparer: ==) }
let rightEquatable = rhs.map { AnyEquatable(target: $0, comparer: ==) }
#if os(Linux)
XCTAssertEqual(leftEquatable, rightEquatable)
#else
XCTAssertEqual(leftEquatable, rightEquatable, file: file, line: line)
#endif
if leftEquatable == rightEquatable {
return
}

printSequenceDifferences(lhs, rhs, ==)
}

/**
Asserts two lists of events are equal.
Event is considered equal if:
* `Next` events are equal if they have equal corresponding elements.
* `Error` events are equal if errors have same domain and code for `NSError` representation and have equal descriptions.
* `Completed` events are always equal.
- parameter lhs: first set of events.
- parameter lhs: second set of events.
*/
public func XCTAssertEqual<T: Equatable>(_ lhs: [MaybeEvent<T>], _ rhs: [MaybeEvent<T>], file: StaticString = #file, line: UInt = #line) {
let leftEquatable = lhs.map { AnyEquatable(target: $0, comparer: ==) }
let rightEquatable = rhs.map { AnyEquatable(target: $0, comparer: ==) }
#if os(Linux)
XCTAssertEqual(leftEquatable, rightEquatable)
#else
XCTAssertEqual(leftEquatable, rightEquatable, file: file, line: line)
#endif
if leftEquatable == rightEquatable {
return
}

printSequenceDifferences(lhs, rhs, ==)
}

/**
Asserts two lists of events are equal.
Event is considered equal if:
* `Next` events are equal if they have equal corresponding elements.
* `Error` events are equal if errors have same domain and code for `NSError` representation and have equal descriptions.
* `Completed` events are always equal.
- parameter lhs: first set of events.
- parameter lhs: second set of events.
*/
public func XCTAssertEqual(_ lhs: [CompleteableEvent], _ rhs: [CompleteableEvent], file: StaticString = #file, line: UInt = #line) {
let leftEquatable = lhs.map { AnyEquatable(target: $0, comparer: ==) }
let rightEquatable = rhs.map { AnyEquatable(target: $0, comparer: ==) }
#if os(Linux)
XCTAssertEqual(leftEquatable, rightEquatable)
#else
XCTAssertEqual(leftEquatable, rightEquatable, file: file, line: line)
#endif
if leftEquatable == rightEquatable {
return
}

printSequenceDifferences(lhs, rhs, ==)
}

/**
Asserts two lists of Recorded events are equal.
Expand Down

0 comments on commit a26b9ac

Please sign in to comment.