typelevel / algebra
1
package algebra.laws
2

3
import algebra.laws.platform.Platform
4
import org.scalacheck.Prop
5
import org.scalacheck.Prop._
6
import scala.util.control.NonFatal
7
import scala.util.DynamicVariable
8

9
/**
10
 * Object with a dynamic variable that allows users to skip the
11
 * serialization tests for certain instances.
12
 */
13
private[laws] object IsSerializable {
14 3
  val runTests = new DynamicVariable[Boolean](true)
15 3
  def apply(): Boolean = (!Platform.isJs) && runTests.value
16

17
  def testSerialization[M](m: M): Prop.Result =
18 0
    if (Platform.isJs) Result(status = Proof) else {
19
      import java.io._
20 0
      val baos = new ByteArrayOutputStream()
21 0
      val oos = new ObjectOutputStream(baos)
22 0
      var ois: ObjectInputStream = null // scalastyle:ignore null
23 0
      try {
24 0
        oos.writeObject(m)
25 0
        oos.close()
26 0
        val bais = new ByteArrayInputStream(baos.toByteArray())
27 0
        ois = new ObjectInputStream(bais)
28 0
        ois.readObject() // just ensure we can read it back
29 0
        ois.close()
30 0
        Result(status = Proof)
31
      } catch { case NonFatal(t) =>
32 0
          Result(status = Exception(t))
33 0
      } finally {
34 0
        oos.close()
35 0
        if (ois != null) ois.close() // scalastyle:ignore null
36
      }
37
    }
38
}

Read our documentation on viewing source code .

Loading