aboutsummaryrefslogtreecommitdiff
path: root/tests/pending/run/reflection-valueclasses-magic.scala
blob: ef128a82d93af8d4540ccc63adadf62cf002cd71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe.definitions._
import scala.reflect.runtime.{currentMirror => cm}
import scala.reflect.ClassTag

package scala {
  object ExceptionUtils {
    def unwrapThrowable(ex: Throwable): Throwable = scala.reflect.runtime.ReflectionUtils.unwrapThrowable(ex)
  }
}

object Test extends dotty.runtime.LegacyApp {
  def key(sym: Symbol) = {
    sym match {
      // initialize parameter symbols
      case meth: MethodSymbol => meth.paramLists.flatten.map(_.info)
    }
    sym + ": " + sym.info
  }

  def convert(value: Any, tpe: Type) = {
    import scala.runtime.BoxesRunTime._
    if (tpe =:= typeOf[Byte]) toByte(value)
    else if (tpe =:= typeOf[Short]) toShort(value)
    else if (tpe =:= typeOf[Char]) toCharacter(value)
    else if (tpe =:= typeOf[Int]) toInteger(value)
    else if (tpe =:= typeOf[Long]) toLong(value)
    else if (tpe =:= typeOf[Float]) toFloat(value)
    else if (tpe =:= typeOf[Double]) toDouble(value)
    else if (tpe =:= typeOf[String]) value.toString
    else if (tpe =:= typeOf[Boolean]) value.asInstanceOf[Boolean]
    else throw new Exception(s"not supported: value = $value, tpe = $tpe")
  }

  def test[T: ClassTag](tpe: Type, receiver: T, method: String, args: Any*): Unit = {
    def wrap[T](op: => T) =
      try {
        var result = op.asInstanceOf[AnyRef]
        if (scala.runtime.ScalaRunTime.isArray(result))
          result = scala.runtime.ScalaRunTime.toObjectArray(result).toList
        println(s"[${result.getClass}] =======> $result")
      } catch {
        case ex: Throwable =>
          val realex = scala.ExceptionUtils.unwrapThrowable(ex)
          println(realex.getClass + ": " + realex.getMessage)
      }
    val meth = tpe.decl(TermName(method).encodedName.toTermName)
    val testees = if (meth.isMethod) List(meth.asMethod) else meth.asTerm.alternatives.map(_.asMethod)
    testees foreach (testee => {
      val convertedArgs = args.zipWithIndex.map { case (arg, i) => convert(arg, testee.paramLists.flatten.apply(i).info) }
      print(s"testing ${tpe.typeSymbol.name}.$method(${testee.paramLists.flatten.map(_.info).mkString(','.toString)}) with receiver = $receiver and args = ${convertedArgs.map(arg => arg + ' '.toString + arg.getClass).toList}: ")
      wrap(cm.reflect(receiver).reflectMethod(testee)(convertedArgs: _*))
    })
  }
  def header(tpe: Type): Unit = {
    println(s"============\n$tpe")
    println("it's important to print the list of Byte's members")
    println("if some of them change (possibly, adding and/or removing magic symbols), we must update this test")
    tpe.members.toList.sortBy(key).foreach(sym => println(key(sym)))
  }

  def testNumeric[T: ClassTag](tpe: Type, value: T): Unit = {
    header(tpe)
    List("toByte", "toShort", "toChar", "toInt", "toLong", "toFloat", "toDouble") foreach (meth => test(tpe, value, meth))
    test(tpe, value, "==", 2)
    test(tpe, value, "!=", 2)
    test(tpe, value, "<", 2)
    test(tpe, value, "<=", 2)
    test(tpe, value, ">", 2)
    test(tpe, value, ">=", 2)
    test(tpe, value, "+", 2)
    test(tpe, value, "-", 2)
    test(tpe, value, "*", 2)
    test(tpe, value, "/", 2)
    test(tpe, value, "%", 2)
  }

  def testIntegral[T: ClassTag](tpe: Type, value: T): Unit = {
    testNumeric(tpe, value)
    test(tpe, value, "unary_~")
    test(tpe, value, "unary_+")
    test(tpe, value, "unary_-")
    test(tpe, value, "<<", 2)
    test(tpe, value, ">>", 2)
    test(tpe, value, ">>>", 2)
    test(tpe, value, "|", 2)
    test(tpe, value, "&", 2)
    test(tpe, value, "^", 2)
  }

  def testBoolean(): Unit = {
    header(typeOf[Boolean])
    test(typeOf[Boolean], true, "unary_!")
    test(typeOf[Boolean], true, "==", true)
    test(typeOf[Boolean], true, "!=", true)
    test(typeOf[Boolean], true, "||", true)
    test(typeOf[Boolean], true, "&&", true)
    test(typeOf[Boolean], true, "|", true)
    test(typeOf[Boolean], true, "&", true)
    test(typeOf[Boolean], true, "^", true)
  }

  def testUnit(): Unit = {
    header(typeOf[Unit])
  }

  testNumeric(typeOf[Byte], 2.toByte)
  testNumeric(typeOf[Short], 2.toShort)
  testNumeric(typeOf[Char], 2.toChar)
  testNumeric(typeOf[Int], 2.toInt)
  testNumeric(typeOf[Long], 2.toLong)
  testNumeric(typeOf[Float], 2.toFloat)
  testNumeric(typeOf[Double], 2.toDouble)
  testBoolean()
  testUnit()
}