Skip to content

Commit

Permalink
Do not use macros in tests of non-core modules
Browse files Browse the repository at this point in the history
This will make a future Dotty transition easier.
  • Loading branch information
fthomas committed Aug 14, 2020
1 parent f89e37f commit 91a3df2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package eu.timepit.refined.cats
import _root_.cats.data.{NonEmptyList, Validated}
import eu.timepit.refined.W
import eu.timepit.refined.api.{Refined, RefinedTypeOps}
import eu.timepit.refined.numeric.{Interval, Positive}
import eu.timepit.refined.refineMV
import eu.timepit.refined.numeric.Interval
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
Expand Down Expand Up @@ -45,12 +44,12 @@ class SyntaxSpec extends Properties("syntax") {

property("NonEmptyList refinedSize (1)") = secure {
import syntax._
NonEmptyList.of("one").refinedSize ?= refineMV[Positive](1)
NonEmptyList.of("one").refinedSize ?= PosInt.unsafeFrom(1)
}

property("NonEmptyList refinedSize (> 1)") = secure {
import syntax._
NonEmptyList.of("one", "two", "three").refinedSize ?= refineMV[Positive](3)
NonEmptyList.of("one", "two", "three").refinedSize ?= PosInt.unsafeFrom(3)
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package eu.timepit.refined

import eu.timepit.refined.TestUtils.wellTyped
import eu.timepit.refined.api.Validate
import eu.timepit.refined.eval.Eval
import org.scalacheck.Prop._
import org.scalacheck.Properties
import scala.tools.reflect.ToolBoxError
import shapeless.test.illTyped

class EvalValidateSpec extends Properties("EvalValidate") {

Expand All @@ -21,24 +19,11 @@ class EvalValidateSpec extends Properties("EvalValidate") {
Validate[Int, IsEven].showExpr(0) ?= "(x: Int) => x % 2 == 0"
}

property("Eval.refineMV") = wellTyped {
refineMV[IsEven](2)
illTyped("refineMV[IsEven](3)", "Predicate.*fail.*")
}

property("Eval.refineV.no parameter type") = {
val v = Validate[List[Int], Eval[W.`"_.headOption.fold(false)(_ > 0)"`.T]]
forAll((l: List[Int]) => v.isValid(l) ?= l.headOption.fold(false)(_ > 0))
}

property("Eval.refineMV.scope") = wellTyped {
val two = 2
illTyped(
"""refineMV[Eval[W.`"(x: Int) => x >= two"`.T]](2)""",
"exception during macro expansion.*"
)
}

property("Eval.refineV.scope") = secure {
val two = 2
throws(classOf[ToolBoxError])(refineV[Eval[W.`"(x: Int) => x >= two"`.T]](two))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package eu.timepit.refined.pureconfig

import com.typesafe.config.{ConfigOriginFactory, ConfigValueType}
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric.Positive
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
import pureconfig._
Expand All @@ -12,12 +10,11 @@ import pureconfig.generic.auto._

class RefTypeConfigConvertSpec extends Properties("RefTypeConfigConvert") {

type PosInt = Int Refined Positive
case class Config(value: PosInt)

property("load success") = secure {
loadConfigWithValue("1") ?=
Right(Config(1))
Right(Config(PosInt.unsafeFrom(1)))
}

property("load failure (predicate)") = secure {
Expand Down Expand Up @@ -75,7 +72,7 @@ class RefTypeConfigConvertSpec extends Properties("RefTypeConfigConvert") {
}

property("roundtrip success") = secure {
val config = Config(1)
val config = Config(PosInt.unsafeFrom(1))
val configValue = ConfigConvert[Config].to(config)
ConfigConvert[Config].from(configValue) ?=
Right(config)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package eu.timepit.refined.scodec

import eu.timepit.refined.TestUtils._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric.Positive
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
import scodec._
import scodec.bits._
import scodec.codecs.int8
import shapeless.test.illTyped

class RefTypeCodecSpec extends Properties("RefTypeCodec") {

type PosInt = Int Refined Positive
implicit val intCodec = int8
implicit val intCodec: Codec[Int] = int8

property("decode success") = secure {
Codec[PosInt].decode(bin"00000101") ?=
Attempt.successful(DecodeResult(5: PosInt, BitVector.empty))
Attempt.successful(DecodeResult(PosInt.unsafeFrom(5), BitVector.empty))
}

property("decode failure") = secure {
Expand All @@ -27,14 +22,10 @@ class RefTypeCodecSpec extends Properties("RefTypeCodec") {
}

property("encode success") = secure {
Codec[PosInt].encode(5) ?=
Codec[PosInt].encode(PosInt.unsafeFrom(5)) ?=
Attempt.successful(bin"00000101")
}

property("encode failure") = wellTyped {
illTyped("""Codec[PosInt].encode(-5)""", "Predicate failed.*")
}

property("sizeBound") = secure {
Codec[PosInt].sizeBound ?= intCodec.sizeBound
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package eu.timepit.refined.scopt

import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric.Positive
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
import scopt._

class RefTypeReadSpec extends Properties("RefTypeRead") {

type PosInt = Int Refined Positive
case class Config(foo: PosInt)

val parser = new OptionParser[Config]("tests") {
Expand All @@ -20,7 +17,7 @@ class RefTypeReadSpec extends Properties("RefTypeRead") {

property("load success") = secure {
loadConfigWithValue("10") =?
Some(Config(10))
Some(Config(PosInt.unsafeFrom(10)))
}

property("load failure (predicate)") = secure {
Expand All @@ -34,5 +31,5 @@ class RefTypeReadSpec extends Properties("RefTypeRead") {
}

def loadConfigWithValue(value: String): Option[Config] =
parser.parse(Array("-f", value), Config(1))
parser.parse(Array("-f", value), Config(PosInt.unsafeFrom(1)))
}

0 comments on commit 91a3df2

Please sign in to comment.