1
1
package rx ;
2
2
3
+ import java .util .ArrayList ;
4
+
3
5
import org .junit .Test ;
4
6
5
7
import rx .util .functions .Action1 ;
12
14
*/
13
15
public class CovarianceTest {
14
16
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
+
15
37
/**
16
38
* This won't compile if super/extends isn't done correctly on generics
17
39
*/
@@ -20,51 +42,53 @@ public void testCovarianceOfZip() {
20
42
Observable <HorrorMovie > horrors = Observable .from (new HorrorMovie ());
21
43
Observable <CoolRating > ratings = Observable .from (new CoolRating ());
22
44
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
+
65
51
Observable .<Movie , CoolRating , Result > zip (horrors , ratings , combine );
66
52
}
67
53
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
+
68
92
static class Media {
69
93
}
70
94
0 commit comments