lundi 21 septembre 2020

java.lang.NoSuchFieldError when using ScalaTest

I'm getting a quite-hard-to-debug error when using ScalaTest. Oddly this seems to occur when my program has no Main object, but not when it does have a Main object. My code is really just using a typeclass with a polymorphic method to get a slice from a list, and looks like this (apologies for the slightly verbose example, I've reduced it down as much as I can):

package sportarray

object ArrayDefs {
  abstract class IsArr[A, I0, DT] {
    def getElem(self: A, i: Int): DT
    def getSlice[R](self: A, r: R)(implicit sliceTc: IsSlice[R]): sliceTc.Out = sliceTc.getSlice(self, r)

    trait IsSlice[R] {
      type Out
      def getSlice(self: A, ref: R): Out 
    }
    object IsSlice {
      implicit val iLocTCForInt = new IsSlice[Int] { 
        type Out = DT
        def getSlice(self: A, ref: Int): Out = getElem(self, ref)
      }
      implicit val iLocTCForList = new IsSlice[List[Int]] { 
        type Out = List[DT]
        def getSlice(self: A, ref: List[Int]): Out = ref.map(getElem(self, _))
      }
    }
  }

  object IsArrSyntax {
    implicit class IsArrOps[A, I0, T](self: A)(implicit 
      tc1d: IsArr[A, I0, T]
    ) {
      def getElem(i: Int) = tc1d.getElem(self, i)
      def getSlice[R](r: R)(implicit sliceTc: tc1d.IsSlice[R]) = tc1d.getSlice(self, r)
    }
  }
}

object ListOfListsObj {
  import ArrayDefs._
  case class List1d[I0, T] (
    indices: List[I0],
    data: List[T],
  )
  implicit def list1dIs1dSpArr[A, I0, T] = 
    new IsArr[List1d[I0, T], I0, T] {
      def getElem(self: List1d[I0, T], i: Int) = self.data(i)
    }
}

My test is simple and looks like this, in its own file in the test directory:

package sportarray

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ArraySpec extends AnyFlatSpec with Matchers {
  "List" should "act as an array" in {
    import ArrayDefs._
    import ArrayDefs.IsArrSyntax._
    import ListOfListsObj._
    val list1d = List1d[Int, Double](List(1,2,3), List(1.0, 2.0, 3.0))
    assert(list1d.getSlice(1) == 2.0)
  }
}

If I sbt test this code as it is, I get the following error:

java.lang.NoSuchFieldError: sportarray$ArrayDefs$IsArrSyntax$IsArrOps$$tc1d
at sportarray.ArraySpec.$anonfun$new$1(HelloSpec.scala:12)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.flatspec.AnyFlatSpecLike$$anon$5.apply(AnyFlatSpecLike.scala:1683)

I then tried adding a Main class to the program to see if this was a problem with the test, or a problem with the code itself:

object Main extends App {
  import ArrayDefs._
  import ArrayDefs.IsArrSyntax._
  import ListOfListsObj._
  val list1d = List1d[Int, Double](List(1,2,3), List(1.0, 2.0, 3.0))
  assert(list1d.getSlice(1) == 2.0)
}

This works fine if I sbt run, and, oddly enough, also allows me to run sbt test without any problems. If I then delete this main class, sbt test once again fails. Is anybody able to shed any light on what is going on here?

Thanks for any help!





Aucun commentaire:

Enregistrer un commentaire