tkawachi / sbt-doctest

Doctest for scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compile error in a Scala.js project

fthomas opened this issue · comments

I'm trying to use sbt-doctest in refined which is a JVM/JS cross project. Compiling the test sources for Scala.js (sbt refinedJS/test) yields the following error:

[error] refined/js/target/scala-2.11/src_managed/test/eu/timepit/refined/numericDoctest.scala:16: You may not export a local class
[error] include(new org.scalacheck.Properties("numeric") {
[error] ^
[error] one error found

I should mention that this can be reproduced by enabling sbt-doctest here: https://github.com/fthomas/refined/blob/d200633c714de3dcb980f55a35520129b1bc91c8/build.sbt#L178

The above error can be fixed with

--- a/src/main/scala/com/github/tkawachi/doctest/ScalaCheckGen.scala
+++ b/src/main/scala/com/github/tkawachi/doctest/ScalaCheckGen.scala
@@ -22,9 +22,9 @@ object ScalaCheckGen extends TestGen {
   }

   def generateExample(basename: String, parsed: ParsedDoctest): String = {
-    s"""  include(new org.scalacheck.Properties("${parsed.symbol}") {
+    s"""  {
        |${parsed.components.map(gen(parsed.lineNo, _)).mkString("\n\n")}
-       |  })""".stripMargin
+       |  }""".stripMargin
   }

But now I'm getting runtime errors because of the usage of scala.runtime.ScalaRunTime in

  def sbtDoctestReplString(any: Any): String = {
    val s = scala.runtime.ScalaRunTime.replStringOf(any, 1000).init
    if (s.headOption == Some('\n')) s.tail else s
  }

The errors look like this:

[error] Referring to non-existent method jl_Class.getPackage__jl_Package
[error]   called from sr_ScalaRunTime$.packageOf$1__p1__O__T
[error]   called from sr_ScalaRunTime$.isScalaClass$1__p1__O__Z
[error]   called from sr_ScalaRunTime$.useOwnToString$1__p1__O__Z
[error]   called from sr_ScalaRunTime$.inner$1__p1__O__I__T
[error]   called from sr_ScalaRunTime$.$$anonfun$7__p1__I__O__T
[error]   called from sr_ScalaRunTime$.inner$1__p1__O__I__T
[error]     (already seen, not repeating call stack)
[error] involving instantiated classes:
[error]   sr_ScalaRunTime$

I think this is related: typelevel/scalacheck#208

scala.runtime.ScalaRunTime.replStringOf() is used to get the same string representation as in REPL.
It uses methods which is not implemented by Scala.js.
https://github.com/scala/scala/blob/v2.11.7/src/library/scala/runtime/ScalaRunTime.scala#L264-L340

I'm afraid it's hard to port replStringOf() to scala.js.

The next version will require JvmPlugin.
08473ab#diff-80f647238c7cff18df190250d41c16eeR35

You guys may eventually be interested on #78, #80, #84, #85

The compilation error reported initially is over.
But there's still some remaining leftovers.
Evidences of that at fthomas/refined#306