Skip to content

Commit

Permalink
Merge pull request ReactiveX#363 from benjchristensen/reduce-covariance
Browse files Browse the repository at this point in the history
unit tests for covariance
  • Loading branch information
benjchristensen committed Sep 10, 2013
2 parents 2c56391 + 7a5184f commit 9e7c9cb
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 188 deletions.
53 changes: 53 additions & 0 deletions rxjava-core/src/test/java/rx/CombineLatestTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package rx;

import org.junit.Test;

import rx.CovarianceTest.CoolRating;
import rx.CovarianceTest.ExtendedResult;
import rx.CovarianceTest.HorrorMovie;
import rx.CovarianceTest.Media;
import rx.CovarianceTest.Movie;
import rx.CovarianceTest.Rating;
import rx.CovarianceTest.Result;
import rx.util.functions.Action1;
import rx.util.functions.Func2;

public class CombineLatestTests {
/**
* This won't compile if super/extends isn't done correctly on generics
*/
@Test
public void testCovarianceOfCombineLatest() {
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
Observable<CoolRating> ratings = Observable.from(new CoolRating());

Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Media, Rating, ExtendedResult> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(extendedAction);
Observable.<Media, Rating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Media, Rating, ExtendedResult> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);

Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine);
}

Func2<Media, Rating, ExtendedResult> combine = new Func2<Media, Rating, ExtendedResult>() {
@Override
public ExtendedResult call(Media m, Rating r) {
return new ExtendedResult();
}
};

Action1<Result> action = new Action1<Result>() {
@Override
public void call(Result t1) {
System.out.println("Result: " + t1);
}
};

Action1<ExtendedResult> extendedAction = new Action1<ExtendedResult>() {
@Override
public void call(ExtendedResult t1) {
System.out.println("Result: " + t1);
}
};
}
64 changes: 64 additions & 0 deletions rxjava-core/src/test/java/rx/ConcatTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

import org.junit.Test;

import rx.CovarianceTest.HorrorMovie;
import rx.CovarianceTest.Media;
import rx.CovarianceTest.Movie;
import rx.Observable.OnSubscribeFunc;
import rx.subscriptions.Subscriptions;

public class ConcatTests {

@Test
Expand Down Expand Up @@ -54,4 +60,62 @@ public void testConcatWithIterableOfObservable() {
assertEquals("three", values.get(2));
assertEquals("four", values.get(3));
}

@Test
public void testConcatCovariance() {
Observable<Media> o1 = Observable.<Media> from(new HorrorMovie(), new Movie());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

Observable<Observable<Media>> os = Observable.from(o1, o2);

List<Media> values = Observable.concat(os).toList().toBlockingObservable().single();
}

@Test
public void testConcatCovariance2() {
Observable<Media> o1 = Observable.from(new HorrorMovie(), new Movie(), new Media());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

Observable<Observable<Media>> os = Observable.from(o1, o2);

List<Media> values = Observable.concat(os).toList().toBlockingObservable().single();
}

@Test
public void testConcatCovariance3() {
Observable<Movie> o1 = Observable.from(new HorrorMovie(), new Movie());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

List<Media> values = Observable.concat(o1, o2).toList().toBlockingObservable().single();

assertTrue(values.get(0) instanceof HorrorMovie);
assertTrue(values.get(1) instanceof Movie);
assertTrue(values.get(2) instanceof Media);
assertTrue(values.get(3) instanceof HorrorMovie);
}

@Test
public void testConcatCovariance4() {

Observable<Movie> o1 = Observable.create(new OnSubscribeFunc<Movie>() {

@Override
public Subscription onSubscribe(Observer<? super Movie> o) {
o.onNext(new HorrorMovie());
o.onNext(new Movie());
// o.onNext(new Media()); // correctly doesn't compile
o.onCompleted();
return Subscriptions.empty();
}
});

Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

List<Media> values = Observable.concat(o1, o2).toList().toBlockingObservable().single();

assertTrue(values.get(0) instanceof HorrorMovie);
assertTrue(values.get(1) instanceof Movie);
assertTrue(values.get(2) instanceof Media);
assertTrue(values.get(3) instanceof HorrorMovie);
}
}
190 changes: 2 additions & 188 deletions rxjava-core/src/test/java/rx/CovarianceTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package rx;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import rx.Observable.OnSubscribeFunc;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action1;
import rx.util.functions.Func2;

/**
* Test super/extends of generics.
*
Expand All @@ -29,187 +21,9 @@ public void testCovarianceOfFrom() {
// Observable.<HorrorMovie>from(new Movie()); // may not compile
}

/**
* This won't compile if super/extends isn't done correctly on generics
*/
@Test
public void testCovarianceOfMerge() {
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
Observable<Observable<HorrorMovie>> metaHorrors = Observable.just(horrors);
Observable.<Media> merge(metaHorrors);
}

/**
* This won't compile if super/extends isn't done correctly on generics
*/
@Test
public void testCovarianceOfZip() {
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
Observable<CoolRating> ratings = Observable.from(new CoolRating());

Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).toBlockingObservable().forEach(extendedAction);
Observable.<Media, Rating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);

Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine);
}

/**
* This won't compile if super/extends isn't done correctly on generics
/*
* Most tests are moved into their applicable classes such as [Operator]Tests.java
*/
@Test
public void testCovarianceOfCombineLatest() {
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
Observable<CoolRating> ratings = Observable.from(new CoolRating());

Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Media, Rating, ExtendedResult> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(extendedAction);
Observable.<Media, Rating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
Observable.<Media, Rating, ExtendedResult> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);

Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine);
}

@Test
public void testConcatCovariance() {
Observable<Media> o1 = Observable.<Media> from(new HorrorMovie(), new Movie());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

Observable<Observable<Media>> os = Observable.from(o1, o2);

List<Media> values = Observable.concat(os).toList().toBlockingObservable().single();
}

@Test
public void testConcatCovariance2() {
Observable<Media> o1 = Observable.from(new HorrorMovie(), new Movie(), new Media());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

Observable<Observable<Media>> os = Observable.from(o1, o2);

List<Media> values = Observable.concat(os).toList().toBlockingObservable().single();
}

@Test
public void testConcatCovariance3() {
Observable<Movie> o1 = Observable.from(new HorrorMovie(), new Movie());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

List<Media> values = Observable.concat(o1, o2).toList().toBlockingObservable().single();

assertTrue(values.get(0) instanceof HorrorMovie);
assertTrue(values.get(1) instanceof Movie);
assertTrue(values.get(2) instanceof Media);
assertTrue(values.get(3) instanceof HorrorMovie);
}

@Test
public void testConcatCovariance4() {

Observable<Movie> o1 = Observable.create(new OnSubscribeFunc<Movie>() {

@Override
public Subscription onSubscribe(Observer<? super Movie> o) {
o.onNext(new HorrorMovie());
o.onNext(new Movie());
// o.onNext(new Media()); // correctly doesn't compile
o.onCompleted();
return Subscriptions.empty();
}
});

Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

List<Media> values = Observable.concat(o1, o2).toList().toBlockingObservable().single();

assertTrue(values.get(0) instanceof HorrorMovie);
assertTrue(values.get(1) instanceof Movie);
assertTrue(values.get(2) instanceof Media);
assertTrue(values.get(3) instanceof HorrorMovie);
}


@Test
public void testMergeCovariance() {
Observable<Media> o1 = Observable.<Media> from(new HorrorMovie(), new Movie());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

Observable<Observable<Media>> os = Observable.from(o1, o2);

List<Media> values = Observable.merge(os).toList().toBlockingObservable().single();
}

@Test
public void testMergeCovariance2() {
Observable<Media> o1 = Observable.from(new HorrorMovie(), new Movie(), new Media());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

Observable<Observable<Media>> os = Observable.from(o1, o2);

List<Media> values = Observable.merge(os).toList().toBlockingObservable().single();
}

@Test
public void testMergeCovariance3() {
Observable<Movie> o1 = Observable.from(new HorrorMovie(), new Movie());
Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

List<Media> values = Observable.merge(o1, o2).toList().toBlockingObservable().single();

assertTrue(values.get(0) instanceof HorrorMovie);
assertTrue(values.get(1) instanceof Movie);
assertTrue(values.get(2) instanceof Media);
assertTrue(values.get(3) instanceof HorrorMovie);
}

@Test
public void testMergeCovariance4() {

Observable<Movie> o1 = Observable.create(new OnSubscribeFunc<Movie>() {

@Override
public Subscription onSubscribe(Observer<? super Movie> o) {
o.onNext(new HorrorMovie());
o.onNext(new Movie());
// o.onNext(new Media()); // correctly doesn't compile
o.onCompleted();
return Subscriptions.empty();
}
});

Observable<Media> o2 = Observable.from(new Media(), new HorrorMovie());

List<Media> values = Observable.merge(o1, o2).toList().toBlockingObservable().single();

assertTrue(values.get(0) instanceof HorrorMovie);
assertTrue(values.get(1) instanceof Movie);
assertTrue(values.get(2) instanceof Media);
assertTrue(values.get(3) instanceof HorrorMovie);
}

Func2<Media, Rating, ExtendedResult> combine = new Func2<Media, Rating, ExtendedResult>() {
@Override
public ExtendedResult call(Media m, Rating r) {
return new ExtendedResult();
}
};

Action1<Result> action = new Action1<Result>() {
@Override
public void call(Result t1) {
System.out.println("Result: " + t1);
}
};

Action1<ExtendedResult> extendedAction = new Action1<ExtendedResult>() {
@Override
public void call(ExtendedResult t1) {
System.out.println("Result: " + t1);
}
};

static class Media {
}
Expand Down
Loading

0 comments on commit 9e7c9cb

Please sign in to comment.