Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxTest: Adding XCTAssertEqual methods for Void streams #2485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions RxTest/Event+Equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ internal func equals<Element: Equatable>(lhs: Event<Element?>, rhs: Event<Elemen
}
}

internal func equals(lhs: Event<Void>, rhs: Event<Void>) -> Bool {
switch (lhs, rhs) {
case (.completed, .completed): return true
case let (.error(e1), .error(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 (.next, .next): return true
default: return false
}
}

internal func equals<Element: Equatable>(lhs: SingleEvent<Element>, rhs: SingleEvent<Element>) -> Bool {
switch (lhs, rhs) {
case let (.failure(e1), .failure(e2)):
Expand Down Expand Up @@ -84,6 +103,25 @@ internal func equals<Element: Equatable>(lhs: MaybeEvent<Element>, rhs: MaybeEve
}
}

internal func equals(lhs: MaybeEvent<Void>, rhs: MaybeEvent<Void>) -> Bool {
switch (lhs, rhs) {
case (.completed, .completed): return true
case let (.error(e1), .error(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, .success): return true
default: return false
}
}

/// Compares two `CompletableEvent` events.
///
/// In case `Error` events are being compared, they are equal in case their `NSError` representations are equal (domain and code)
Expand Down Expand Up @@ -114,8 +152,20 @@ extension Event: Equatable where Element: Equatable {
}
}

public extension Event where Element == Void {
static func == (lhs: Event<Element>, rhs: Event<Element>) -> Bool {
equals(lhs: lhs, rhs: rhs)
}
}

extension MaybeEvent: Equatable where Element: Equatable {
public static func == (lhs: MaybeEvent<Element>, rhs: MaybeEvent<Element>) -> Bool {
equals(lhs: lhs, rhs: rhs)
}
}

public extension MaybeEvent where Element == Void {
static func == (lhs: MaybeEvent<Element>, rhs: MaybeEvent<Element>) -> Bool {
equals(lhs: lhs, rhs: rhs)
}
}
9 changes: 9 additions & 0 deletions RxTest/Recorded+Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ extension Recorded {
public static func next<T>(_ time: TestTime, _ element: T) -> Recorded<Event<T>> where Value == Event<T> {
Recorded(time: time, value: .next(element))
}

/**
Factory method for a `.next` event recorded at a given time.
- parameter time: Recorded virtual time the `.next` event occurs.
- returns: Recorded event in time.
*/
public static func next(_ time: TestTime) -> Recorded<Value> where Value == Event<Void> {
return Recorded<Value>(time: time, value: .next(()))
}

/**
Factory method for an `.completed` event recorded at a given time.
Expand Down
12 changes: 12 additions & 0 deletions RxTest/Recorded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ extension Recorded: Equatable where Value: Equatable {
lhs.time == rhs.time && lhs.value == rhs.value
}
}

public extension Recorded where Value == Event<Void> {
static func == (lhs: Recorded<Value>, rhs: Recorded<Value>) -> Bool {
lhs.time == rhs.time && lhs.value == rhs.value
}
}

public extension Recorded where Value == MaybeEvent<Void> {
static func == (lhs: Recorded<Value>, rhs: Recorded<Value>) -> Bool {
lhs.time == rhs.time && lhs.value == rhs.value
}
}
126 changes: 126 additions & 0 deletions RxTest/XCTest+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ public func XCTAssertEqual<Element: Equatable>(_ lhs: [Event<Element>], _ rhs: [
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 rhs: second set of events.
- parameter file: The path to the file in which it appears.
- parameter line: The line number on which it appears.
*/
public func XCTAssertEqual(_ lhs: [Event<Void>], _ rhs: [Event<Void>], 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.

Expand Down Expand Up @@ -65,6 +93,34 @@ public func XCTAssertEqual<Element: Equatable>(_ lhs: [SingleEvent<Element>], _
printSequenceDifferences(lhs.map { try? $0.get() }, rhs.map { try? $0.get() }, ==)
}

/**
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 rhs: second set of events.
- parameter file: The path to the file in which it appears.
- parameter line: The line number on which it appears.
*/
public func XCTAssertEqual(_ lhs: [SingleEvent<Void>], _ rhs: [SingleEvent<Void>], file: StaticString = #file, line: UInt = #line) {
let leftEquatable = lhs.map { AnyEquatable(target: try? $0.get(), comparer: compareVoidOptionals) }
let rightEquatable = rhs.map { AnyEquatable(target: try? $0.get(), comparer: compareVoidOptionals) }
#if os(Linux)
XCTAssertEqual(leftEquatable, rightEquatable)
#else
XCTAssertEqual(leftEquatable, rightEquatable, file: file, line: line)
#endif
if leftEquatable == rightEquatable {
return
}

printSequenceDifferences(lhs.map { try? $0.get() }, rhs.map { try? $0.get() }, ==)
}

/**
Asserts two lists of events are equal.

Expand Down Expand Up @@ -93,6 +149,34 @@ public func XCTAssertEqual<Element: Equatable>(_ lhs: [MaybeEvent<Element>], _ r
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 rhs: second set of events.
- parameter file: The path to the file in which it appears.
- parameter line: The line number on which it appears.
*/
public func XCTAssertEqual(_ lhs: [MaybeEvent<Void>], _ rhs: [MaybeEvent<Void>], 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.

Expand Down Expand Up @@ -183,6 +267,37 @@ public func XCTAssertEqual<Element: Equatable>(_ lhs: [Recorded<Event<Element?>>
printSequenceDifferences(lhs, rhs, ==)
}

/**
Asserts two lists of Recorded events with optional elements are equal.

Recorded events are equal if times are equal and recorded 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 rhs: second set of events.
- parameter file: The path to the file in which it appears.
- parameter line: The line number on which it appears.
*/
public func XCTAssertEqual(_ lhs: [Recorded<Event<Void>>], _ rhs: [Recorded<Event<Void>>], 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, ==)
}

/**
Assert a list of Recorded events has emitted the provided elements.
This method does not take event times into consideration.
Expand Down Expand Up @@ -243,4 +358,15 @@ func printSequenceDifferences<Element>(_ lhs: [Element], _ rhs: [Element], _ equ
print("rhs[\(index + shortest)]:\n \(element)")
}
}

private func compareVoidOptionals(_ lhs: Optional<Void>, _ rhs: Optional<Void>) -> Bool {
switch (lhs, rhs) {
case (.none, .none),
(.some, .some):
return true
case (.none, .some),
(.some, .none):
return false
}
}
#endif