Skip to content

Commit

Permalink
Merge pull request #9177 from sjrd/make-assert-util-friendly-to-scala…
Browse files Browse the repository at this point in the history
…-js-again

Move reflection-related utils from AssertUtil to new ReflectUtil.
  • Loading branch information
som-snytt committed Aug 25, 2020
2 parents 8329196 + 364b901 commit 7b85389
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
20 changes: 7 additions & 13 deletions src/testkit/scala/tools/testkit/AssertUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import java.util.IdentityHashMap

/** This module contains additional higher-level assert statements
* that are ultimately based on junit.Assert primitives.
*
* Avoid adding methods, and above all *fields* (including `lazy val`s), that
* require JVM-specific features such as run-time reflection. Otherwise, all
* tests using this object stop working in Scala.js. Put such methods in
* `ReflectUtil` instead. (`ClassTag`s are fine; they are supported in
* Scala.js and Scala Native.)
*/
object AssertUtil {

Expand Down Expand Up @@ -263,18 +269,6 @@ object AssertUtil {
def readyOrNot(awaitable: Awaitable[_]): Boolean = Try(Await.ready(awaitable, TestDuration.Standard)).isSuccess

def withoutATrace[A](body: => A) = NoTrace(body)

private lazy val modsField = classOf[Field].getDeclaredField("modifiers").tap(_.setAccessible(true))

def getFieldAccessible[T: ClassTag](n: String): Field =
implicitly[ClassTag[T]]
.runtimeClass.getDeclaredField(n)
.tap { f =>
if ((f.getModifiers & Modifier.FINAL) != 0)
modsField.setInt(f, f.getModifiers() & ~Modifier.FINAL)
if ((f.getModifiers & Modifier.PUBLIC) == 0)
f.setAccessible(true)
}
}

object TestDuration {
Expand Down Expand Up @@ -309,7 +303,7 @@ class NoTrace[A](body: => A) extends Runnable {

private final val noError = None: Option[Throwable]

def asserted: Option[Throwable] =
def asserted: Option[Throwable] =
errors.collect { case (_, e: AssertionError) => e }
.foldLeft(noError)((res, e) => res.map(suppress(_, e)).orElse(Some(e)))

Expand Down
36 changes: 36 additions & 0 deletions src/testkit/scala/tools/testkit/ReflectUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.tools.testkit

import scala.reflect.ClassTag
import scala.util.chaining._
import java.lang.reflect.{Array => _, _}

/** This module contains reflection-related utilities.
*
* This object contains methods that will not work on Scala.js nor Scala
* Native, making any test using `ReflectUtil` JVM-only.
*/
object ReflectUtil {
private lazy val modsField = classOf[Field].getDeclaredField("modifiers").tap(_.setAccessible(true))

def getFieldAccessible[T: ClassTag](n: String): Field =
implicitly[ClassTag[T]]
.runtimeClass.getDeclaredField(n)
.tap { f =>
if ((f.getModifiers & Modifier.FINAL) != 0)
modsField.setInt(f, f.getModifiers() & ~Modifier.FINAL)
if ((f.getModifiers & Modifier.PUBLIC) == 0)
f.setAccessible(true)
}
}
2 changes: 1 addition & 1 deletion test/junit/scala/collection/immutable/IntMapTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.tools.testkit.AssertUtil.{getFieldAccessible => f}
import scala.tools.testkit.ReflectUtil.{getFieldAccessible => f}

@RunWith(classOf[JUnit4])
class IntMapTest {
Expand Down
2 changes: 1 addition & 1 deletion test/junit/scala/collection/immutable/LongMapTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.tools.testkit.AssertUtil.{getFieldAccessible => f}
import scala.tools.testkit.ReflectUtil.{getFieldAccessible => f}

@RunWith(classOf[JUnit4])
class LongMapTest {
Expand Down

0 comments on commit 7b85389

Please sign in to comment.