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

Add assertValueAt(int, value) to TestObserver #5529

Merged
merged 5 commits into from
Aug 2, 2017

Conversation

NickFirmani
Copy link
Contributor

I found myself frequently writing tests that want to check that an observable emitted a certain value second or third, but I don't care what the first or other values one, so assertValues(Object...)) is more brittle.

This PR implements an assertValueAt(index, value) method, similar to assertValue(value). Tests are also added.

@akarnokd akarnokd added the 2.x label Jul 31, 2017
@akarnokd
Copy link
Member

There is an assertValueAt(int, Predicate) just below your changes, what's wrong with that? Also there could be an overload problem because of a T - Predicate conflict in Java 8+.

@NickFirmani
Copy link
Contributor Author

I personally make heavy use of the assertValue(T) method, BaseConsumer#324, which also has a similar paired method assertValue(Predicate<T>). Using that method keeps my tests cleaner, when I know the exact value I expect. It seems pretty verbose to me to implement a Predicate when the body will just be value.equals(expected).

If this causes an overload problem, we'll have the same problem with assertValue(T) and assertValue(Predicate<T>) as well

@codecov
Copy link

codecov bot commented Jul 31, 2017

Codecov Report

Merging #5529 into 2.x will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##                2.x   #5529      +/-   ##
===========================================
+ Coverage     96.14%   96.2%   +0.05%     
- Complexity     5806    5821      +15     
===========================================
  Files           631     631              
  Lines         41285   41414     +129     
  Branches       5732    5740       +8     
===========================================
+ Hits          39693   39841     +148     
+ Misses          629     621       -8     
+ Partials        963     952      -11
Impacted Files Coverage Δ Complexity Δ
.../java/io/reactivex/observers/BaseTestConsumer.java 99.65% <100%> (+0.04%) 112 <4> (+4) ⬆️
...l/operators/observable/ObservableFlatMapMaybe.java 84.96% <0%> (-10.46%) 2% <0%> (ø)
...tivex/internal/schedulers/InstantPeriodicTask.java 61.11% <0%> (-5.56%) 7% <0%> (-2%)
.../operators/observable/ObservableFlatMapSingle.java 91.79% <0%> (-4.48%) 2% <0%> (ø)
...rnal/operators/flowable/FlowableFlatMapSingle.java 94.02% <0%> (-2.72%) 2% <0%> (ø)
...ternal/operators/completable/CompletableMerge.java 96.42% <0%> (-2.39%) 2% <0%> (ø)
...rnal/operators/flowable/FlowableSkipLastTimed.java 95.91% <0%> (-2.05%) 2% <0%> (ø)
...java/io/reactivex/processors/PublishProcessor.java 98.29% <0%> (-1.71%) 45% <0%> (-1%)
...internal/operators/completable/CompletableAmb.java 96.61% <0%> (-1.7%) 10% <0%> (-1%)
...rnal/subscriptions/DeferredScalarSubscription.java 98.46% <0%> (-1.54%) 28% <0%> (-1%)
... and 75 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ac06c24...bfbd517. Read the comment docs.

@akarnokd
Copy link
Member

akarnokd commented Aug 1, 2017

/cc @vanniktech, @artem-zinnatullin

Copy link
Contributor

@artem-zinnatullin artem-zinnatullin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I would use that!


thrown.expect(AssertionError.class);
thrown.expectMessage("Expected: 2 (class: Integer), Actual: 3 (class: Integer) (latch = 0, values = 3, errors = 0, completions = 1)");
ts.assertValueAt(2, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please compare something else than integers (String will be ok)?

Silly code change in assert method can break its logic but pass this test because both index and value are 2 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

@vanniktech
Copy link
Collaborator

We need to find some kind of policy for adding new API methods or not.

I'd say let's not get this in. Write a Kotlin extension functions 🙃 or use the assertValueAt with a Predicate and then define a test class that implements the interface and has a static factory method Predicate<T> just(T).

If this gets in I also would like to have mergeWith, concatWith, startWith variants for Single, Completable and Maybe on Observable & Flowable.

@artem-zinnatullin
Copy link
Contributor

I'm pretty sure this method will be much more popular than assertValueAt(int, Predicate) but that's just my guess

Mr Negotiator @vanniktech 😸

For simple equals comparisons (which I believe are 95+% of asserts on values) this variant is definitely better than assertValueAt(int, Predicate):

  • It has nicer message and prints both expected and actual values including their classes (sometimes that saves tons of time)
  • It requires less symbols to type assertValueAt(3, expected) even comparing with Kotlin assertValueAt(3) { it == expected }

@akarnokd
Copy link
Member

akarnokd commented Aug 1, 2017

Independent additions are individually evaluated based on the merits, value, quality and maintenance requirements they pose.

This PR is self contained and adds a convenience pair to an existing method. Plus, once merged in, there will be no need for further development on it.

In contrast, all those mergeWith(Single) and co are just shorthands for mergeWith(Single.toObservable())s and alike and the premise of "optimize later" actually adds burden to the maintainer(s) of RxJava.

@vanniktech
Copy link
Collaborator

Alright, you got me convinced now. If those shorthands were to be implemented one by one and also in an optimized way, would you consider PRs for that?

@akarnokd
Copy link
Member

akarnokd commented Aug 1, 2017

Yes.

@@ -403,6 +403,31 @@ public final U assertNever(Predicate<? super T> valuePredicate) {

/**
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
* which is equal to the given value with respect to Objects.equals.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects is a Java 8 component but ObjectHelper is internal, thus maybe let's say "with respect to null-safe Object.equals()".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the other javadoc I copied that from too :)

* @param index the position to assert on
* @param value the value to expect
* @return this
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@since 2.1.3 - experimental

* @return this
*/
@SuppressWarnings("unchecked")
public final U assertValueAt(int index, T value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Experimental

throw fail("No values");
}

if (index >= values.size()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use s instead of values.size() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

@NickFirmani
Copy link
Contributor Author

Thanks for the reviews!

@NickFirmani
Copy link
Contributor Author

CI is failing due to
ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-7-oracle

@NickFirmani NickFirmani closed this Aug 1, 2017
@NickFirmani NickFirmani reopened this Aug 1, 2017
@akarnokd
Copy link
Member

akarnokd commented Aug 1, 2017

There has been an image change on Travis CI and JDK 7 is no longer supported. See #5531.

@akarnokd
Copy link
Member

akarnokd commented Aug 1, 2017

Could you rebase the PR so the build settings can take effect?

* 2.x: Try fixing Travis CI lack of java

* Force dist: precise

* Oracle JDK 8 is then
@akarnokd akarnokd merged commit 5242cf3 into ReactiveX:2.x Aug 2, 2017
@NickFirmani
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants