@@ -2,61 +2,93 @@ package rx.lang.scalatests
2
2
3
3
import org .scalatest .junit .JUnitSuite
4
4
5
+ import scala .language .postfixOps
5
6
import rx .lang .scala ._
6
7
import scala .concurrent .duration ._
7
8
import org .junit .{Before , Test , Ignore }
8
9
9
10
import org .junit .Assert ._
10
11
12
+ @ Ignore // Since this doesn't do automatic testing.
11
13
class RxScalaDemo extends JUnitSuite {
12
-
13
- @ Test def testSwitchNoTiming () {
14
- val o1 : Observable [Observable [Int ]] = Observable (Observable (1 , 2 ), Observable (3 , 4 ))
15
- val o2 : Observable [Int ] = o1.switch
16
14
15
+ def output (s : String ): Unit = println(s)
16
+
17
+ def sleep (ms : Long ): Unit = Thread .sleep(ms)
18
+
19
+ @ Test def intervalExample () {
20
+ println(" hello" )
21
+ Observable .interval(200 millis).take(5 ).subscribe((n : Long ) => println(" n = " + n))
22
+ // need to sleep here because otherwise JUnit kills the thread created by interval()
23
+ sleep(1200 )
24
+ }
25
+
26
+ def msTicks (start : Long , step : Long ): Observable [Long ] = {
27
+ // will be easier once we have Observable.generate method
28
+ Observable .interval(step millis) map (_ * step + start)
29
+ }
30
+
31
+ def prefixedTicks (start : Long , step : Long , prefix : String ): Observable [String ] = {
32
+ msTicks(start, step).map(prefix + _)
33
+ }
34
+
35
+ @ Test def testTicks () {
36
+ prefixedTicks(5000 , 500 , " t = " ).take(5 ).subscribe(output(_))
37
+ sleep(3000 )
38
+ }
39
+
40
+ @ Test def testSwitch () {
41
+ // We do not have ultimate precision: Sometimes, 747 gets through, sometimes not
42
+ val o = Observable .interval(1000 millis).map(n => prefixedTicks(0 , 249 , s " Observable# $n: " ))
43
+ o.switch.take(16 ).subscribe(output(_))
44
+ sleep(5000 )
45
+ }
46
+
47
+ @ Test def testSwitchOnObservableOfInt () {
17
48
// Correctly rejected with error
18
49
// "Cannot prove that Observable[Int] <:< Observable[Observable[U]]"
19
- // val o3 = Observable(1, 2).switch
50
+ // val o = Observable(1, 2).switch
51
+ }
52
+
53
+ @ Test def testMyOwnSequenceEqual () {
54
+ // the sequenceEqual operation can be obtained like this:
20
55
21
- println(o2.toBlockingObservable.toIterable.toList)
56
+ val first = Observable (10 , 11 , 12 )
57
+ val second = Observable (10 , 11 , 12 )
58
+
59
+ val b1 = (first zip second) map (p => p._1 == p._2) forall (b => b)
60
+
61
+ val equality = (a : Any , b : Any ) => a == b
62
+ val b2 = (first zip second) map (p => equality(p._1, p._2)) forall (b => b)
63
+
64
+ assertTrue(b1.toBlockingObservable.single)
65
+ assertTrue(b2.toBlockingObservable.single)
22
66
}
23
67
24
- @ Test def testSequenceEqualIsUnnecessary () {
25
- // the sequenceEqual is unnecessary
68
+ @ Test def testMyOwnSequenceEqualWithForComprehension () {
69
+ // the sequenceEqual operation can be obtained like this:
70
+
26
71
val first = Observable (10 , 11 , 12 )
27
72
val second = Observable (10 , 11 , 12 )
28
73
29
- val b1 = ( first zip second) map (p => p._1 == p._2 )
74
+ val booleans = for ((n1, n2) <- ( first zip second)) yield (n1 == n2 )
30
75
31
- val equality = ( a : Any , b : Any ) => a == b
76
+ val b1 = booleans.forall(_ == true ) // without ` == true`, b1 is assigned the forall function
32
77
33
- val b2 = (first zip second) map (p => equality(p._1, p._2) )
78
+ assertTrue(b1.toBlockingObservable.single )
34
79
}
35
80
36
81
@ Test def testStartWithIsUnnecessary () {
37
82
val before = Observable (- 2 , - 1 , 0 )
38
83
val source = Observable (1 , 2 , 3 )
39
- before ++ source
40
- }
41
-
42
- @ Test def intervalExample () {
43
- println(" hello" )
44
- Observable .interval(200 millis).take(5 ).subscribe((n : Long ) => println(" n = " + n))
45
- // need to sleep here because otherwise JUnit kills the thread created by interval()
46
- Thread .sleep(1200 )
84
+ println((before ++ source).toBlockingObservable.toList)
47
85
}
48
-
49
- @ Test def forComprehensionExample () {
86
+
87
+ @ Test def mergeExample () {
50
88
val slowNumbers = Observable .interval(400 millis).take(5 ).map(" slow " + _)
51
89
val fastNumbers = Observable .interval(200 millis).take(10 ).map(" fast " + _)
52
90
53
- // it would actually make more sense to use merge instead of for comprehension...
54
- val o1 = Observable (slowNumbers, fastNumbers)
55
- val o2 = {for (o <- o1; str <- o) yield str}
56
-
57
- o2.subscribe((s : String ) => println(s))
58
-
59
- Thread .sleep(2300 )
91
+ // TODO
60
92
}
61
93
62
- }
94
+ }
0 commit comments