|
1 | 1 | package rx.lang.scala |
2 | 2 |
|
3 | | -import scala.reflect.runtime.universe._ |
4 | | -import org.scalatest.junit.JUnitSuite |
5 | | -import org.junit.Test |
6 | | -import rx.util.functions._ |
7 | | -import scala.collection.SortedSet |
| 3 | +import java.util.Calendar |
| 4 | + |
8 | 5 | import scala.collection.SortedMap |
| 6 | +import scala.reflect.runtime.universe |
| 7 | +import scala.reflect.runtime.universe.Symbol |
| 8 | +import scala.reflect.runtime.universe.Type |
| 9 | +import scala.reflect.runtime.universe.typeOf |
| 10 | + |
9 | 11 | import org.junit.Ignore |
10 | | -import java.lang.reflect.Modifier |
11 | | -import java.util.Date |
12 | | -import java.util.Calendar |
| 12 | +import org.junit.Test |
| 13 | +import org.scalatest.junit.JUnitSuite |
13 | 14 |
|
| 15 | +/** |
| 16 | + * These tests can be used to check if all methods of the Java Observable have a corresponding |
| 17 | + * method in the Scala Observable. |
| 18 | + * |
| 19 | + * These tests don't contain any assertions, so they will always succeed, but they print their |
| 20 | + * results to stdout. |
| 21 | + */ |
14 | 22 | class CompletenessTest extends JUnitSuite { |
15 | 23 |
|
| 24 | + // some frequently used comments: |
16 | 25 | val unnecessary = "[considered unnecessary in Scala land]" |
17 | | - |
18 | 26 | val deprecated = "[deprecated in RxJava]" |
19 | | - |
20 | 27 | val averageProblem = "[We can't have a general average method because Scala's `Numeric` does not have " + |
21 | 28 | "scalar multiplication (we would need to calculate `(1.0/numberOfElements)*sum`). " + |
22 | 29 | "You can use `fold` instead to accumulate `sum` and `numberOfElements` and divide at the end.]" |
23 | | - |
24 | 30 | val commentForFirstWithPredicate = "[use `.filter(condition).first`]" |
25 | | - |
26 | 31 | val fromFuture = "[TODO: Decide how Scala Futures should relate to Observables. Should there be a " + |
27 | 32 | "common base interface for Future and Observable? And should Futures also have an unsubscribe method?]" |
28 | 33 |
|
29 | | - val correspondence = defaultMethodCorrespondence ++ Map( |
| 34 | + /** |
| 35 | + * Maps each method from the Java Observable to its corresponding method in the Scala Observable |
| 36 | + */ |
| 37 | + val correspondence = defaultMethodCorrespondence ++ correspondenceChanges // ++ overrides LHS with RHS |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates default method correspondence mappings, assuming that Scala methods have the same |
| 41 | + * name and the same argument types as in Java |
| 42 | + */ |
| 43 | + def defaultMethodCorrespondence: Map[String, String] = { |
| 44 | + val allMethods = getPublicInstanceAndCompanionMethods(typeOf[rx.Observable[_]]) |
| 45 | + val tuples = for (javaM <- allMethods) yield (javaM, javaMethodSignatureToScala(javaM)) |
| 46 | + tuples.toMap |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Manually added mappings from Java Observable methods to Scala Observable methods |
| 51 | + */ |
| 52 | + def correspondenceChanges = Map( |
30 | 53 | // manually added entries for Java instance methods |
31 | 54 | "aggregate(Func2[T, T, T])" -> "reduce((U, U) => U)", |
32 | 55 | "aggregate(R, Func2[R, _ >: T, R])" -> "foldLeft(R)((R, T) => R)", |
@@ -158,8 +181,7 @@ class CompletenessTest extends JUnitSuite { |
158 | 181 | def getPublicInstanceAndCompanionMethods(tp: Type): Iterable[String] = |
159 | 182 | getPublicInstanceMethods(tp) ++ |
160 | 183 | getPublicInstanceMethods(tp.typeSymbol.companionSymbol.typeSignature) |
161 | | - |
162 | | - |
| 184 | + |
163 | 185 | def printMethodSet(title: String, tp: Type) { |
164 | 186 | println("\n" + title) |
165 | 187 | println(title.map(_ => '-') + "\n") |
@@ -204,12 +226,6 @@ class CompletenessTest extends JUnitSuite { |
204 | 226 | .replaceAll("(\\w+)\\(\\)", "$1") |
205 | 227 | } |
206 | 228 |
|
207 | | - def defaultMethodCorrespondence: Map[String, String] = { |
208 | | - val allMethods = getPublicInstanceAndCompanionMethods(typeOf[rx.Observable[_]]) |
209 | | - val tuples = for (javaM <- allMethods) yield (javaM, javaMethodSignatureToScala(javaM)) |
210 | | - tuples.toMap |
211 | | - } |
212 | | - |
213 | 229 | @Ignore // because spams output |
214 | 230 | @Test def printDefaultMethodCorrespondence: Unit = { |
215 | 231 | println("\nDefault Method Correspondence") |
|
0 commit comments