diff options
author | Lukas Rytz <lukas.rytz@epfl.ch> | 2008-10-16 10:45:15 +0000 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@epfl.ch> | 2008-10-16 10:45:15 +0000 |
commit | 6372a8b619768365a036f7955f8edaa46f5960f0 (patch) | |
tree | 9d498f5d26469c8a62974907c92d6c6b2b50286d /src/library | |
parent | a44eeedd3c6c9792dea33ed579be1276474f5c4f (diff) | |
download | scala-6372a8b619768365a036f7955f8edaa46f5960f0.tar.gz scala-6372a8b619768365a036f7955f8edaa46f5960f0.tar.bz2 scala-6372a8b619768365a036f7955f8edaa46f5960f0.zip |
copying 'scala.testing.Show' from 2.7.2 branch ...
copying 'scala.testing.Show' from 2.7.2 branch to trunk
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/testing/Show.scala | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/library/scala/testing/Show.scala b/src/library/scala/testing/Show.scala new file mode 100644 index 0000000000..32d32d4a0c --- /dev/null +++ b/src/library/scala/testing/Show.scala @@ -0,0 +1,60 @@ +package scala.testing + +/** Classes inheriting trait `Show` can test their member methods using the notattion + * 'meth(arg_1, ..., arg_n), where `meth' is the name of the method and `arg_1,...,arg_n' are + * the arguments. The only difference to a normal method call is the leading quote character ('). + * A quoted method call like the one above will produces a legible diagnostic to be printed on Console. + * It is of the form + * + * meth(arg_1, ..., arg_n) gives <result> + * + * where <result> is the result of evaluating the call. + */ +trait Show { + + /** The result class of wrapper `symApply`. + * Prints out diagnostics of method applications. + */ + class SymApply(f: Symbol) { + def apply[A](args: A*) { + println(test(f, args: _*)) + } + } + + /** An implicit definition that adds an apply method to Symbol which forwards to `test`. + */ + implicit def symApply(sym: Symbol) = new SymApply(sym) + + /** Apply method with name of given symbol `f` to given arguments and return + * a result diagnostics. + */ + def test[A](f: Symbol, args: A*): String = { + val args1 = args map (_.asInstanceOf[AnyRef]) + def testMethod(meth: java.lang.reflect.Method): String = + f.name+"("+(args mkString ",")+") gives "+ + { + try { + meth.invoke(this, args1: _*) + } catch { + case ex: IllegalAccessException => ex + case ex: IllegalArgumentException => ex + case ex: java.lang.reflect.InvocationTargetException => ex + } + } + getClass.getMethods.toList filter (_.getName == f.name) match { + case List() => + f.name+" is not defined" + case List(m) => + testMethod(m) + case ms => // multiple methods, disambiguate by number of arguments + ms filter (_.getParameterTypes.length == args.length) match { + case List() => + testMethod(ms.head) // go ahead anyway, to get an exception + case List(m) => + testMethod(m) + case ms => + "cannot disambiguate between multiple implementations of "+f.name + } + } + } +} |