Skip to content

Commit e962d00

Browse files
author
jmhofer
committed
added a few 'compiler' tests
1 parent 04f35cd commit e962d00

File tree

2 files changed

+68
-44
lines changed

2 files changed

+68
-44
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public static <T> Observable<T> error(Throwable exception) {
515515
* emitted by the resulting Observable
516516
* @return an Observable that emits each item in the source {@link Iterable} sequence
517517
*/
518-
public static <T> Observable<T> from(Iterable<T> iterable) {
518+
public static <T> Observable<T> from(Iterable<? extends T> iterable) {
519519
return create(OperationToObservableIterable.toObservableIterable(iterable));
520520
}
521521

@@ -579,7 +579,7 @@ public static Observable<Integer> range(int start, int count) {
579579
* @return an Observable whose {@link Observer}s trigger an invocation of the given Observable
580580
* factory function
581581
*/
582-
public static <T> Observable<T> defer(Func0<? extends Observable<T>> observableFactory) {
582+
public static <T> Observable<T> defer(Func0<? extends Observable<? extends T>> observableFactory) {
583583
return create(OperationDefer.defer(observableFactory));
584584
}
585585

rxjava-core/src/test/java/rx/CovarianceTest.java

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package rx;
22

3+
import java.util.ArrayList;
4+
35
import org.junit.Test;
46

57
import rx.util.functions.Action1;
@@ -12,6 +14,26 @@
1214
*/
1315
public class CovarianceTest {
1416

17+
/**
18+
* This won't compile if super/extends isn't done correctly on generics
19+
*/
20+
@Test
21+
public void testCovarianceOfFrom() {
22+
Observable.<Movie>from(new HorrorMovie());
23+
Observable.<Movie>from(new ArrayList<HorrorMovie>());
24+
// Observable.<HorrorMovie>from(new Movie()); // may not compile
25+
}
26+
27+
/**
28+
* This won't compile if super/extends isn't done correctly on generics
29+
*/
30+
@Test
31+
public void testCovarianceOfMerge() {
32+
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
33+
Observable<Observable<HorrorMovie>> metaHorrors = Observable.just(horrors);
34+
Observable.<Media>merge(metaHorrors);
35+
}
36+
1537
/**
1638
* This won't compile if super/extends isn't done correctly on generics
1739
*/
@@ -20,51 +42,53 @@ public void testCovarianceOfZip() {
2042
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
2143
Observable<CoolRating> ratings = Observable.from(new CoolRating());
2244

23-
Func2<Media, Rating, ExtendedResult> combine = new Func2<Media, Rating, ExtendedResult>() {
24-
@Override
25-
public ExtendedResult call(Media m, Rating r) {
26-
return new ExtendedResult();
27-
}
28-
};
29-
30-
Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() {
31-
@Override
32-
public void call(Result t1) {
33-
System.out.println("Result: " + t1);
34-
}
35-
});
36-
37-
Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() {
38-
@Override
39-
public void call(Result t1) {
40-
System.out.println("Result: " + t1);
41-
}
42-
});
43-
44-
Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<ExtendedResult>() {
45-
@Override
46-
public void call(ExtendedResult t1) {
47-
System.out.println("Result: " + t1);
48-
}
49-
});
50-
51-
Observable.<Media, Rating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() {
52-
@Override
53-
public void call(Result t1) {
54-
System.out.println("Result: " + t1);
55-
}
56-
});
57-
58-
Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() {
59-
@Override
60-
public void call(Result t1) {
61-
System.out.println("Result: " + t1);
62-
}
63-
});
64-
45+
Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
46+
Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
47+
Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).toBlockingObservable().forEach(extendedAction);
48+
Observable.<Media, Rating, Result> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
49+
Observable.<Media, Rating, ExtendedResult> zip(horrors, ratings, combine).toBlockingObservable().forEach(action);
50+
6551
Observable.<Movie, CoolRating, Result> zip(horrors, ratings, combine);
6652
}
6753

54+
/**
55+
* This won't compile if super/extends isn't done correctly on generics
56+
*/
57+
@Test
58+
public void testCovarianceOfCombineLatest() {
59+
Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie());
60+
Observable<CoolRating> ratings = Observable.from(new CoolRating());
61+
62+
Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
63+
Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
64+
Observable.<Media, Rating, ExtendedResult> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(extendedAction);
65+
Observable.<Media, Rating, Result> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
66+
Observable.<Media, Rating, ExtendedResult> combineLatest(horrors, ratings, combine).toBlockingObservable().forEach(action);
67+
68+
Observable.<Movie, CoolRating, Result> combineLatest(horrors, ratings, combine);
69+
}
70+
71+
Func2<Media, Rating, ExtendedResult> combine = new Func2<Media, Rating, ExtendedResult>() {
72+
@Override
73+
public ExtendedResult call(Media m, Rating r) {
74+
return new ExtendedResult();
75+
}
76+
};
77+
78+
Action1<Result> action = new Action1<Result>() {
79+
@Override
80+
public void call(Result t1) {
81+
System.out.println("Result: " + t1);
82+
}
83+
};
84+
85+
Action1<ExtendedResult> extendedAction = new Action1<ExtendedResult>() {
86+
@Override
87+
public void call(ExtendedResult t1) {
88+
System.out.println("Result: " + t1);
89+
}
90+
};
91+
6892
static class Media {
6993
}
7094

0 commit comments

Comments
 (0)