Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/shared/src/main/scala-2/zio/test/SmartAssertMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ class SmartAssertMacros(val c: blackbox.Context) {
case q"!($inner)" =>
AST.Not(parseExpr(inner), pos.getPos(tree), pos.getPos(inner))

case q"$lhs && $rhs" =>
case q"$lhs && $rhs" if lhs.tpe == typeOf[Boolean] =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sure that if we have a && function defined for some type we gonna still handle that in assert true:

object AssertSpec extends ZIOSpecDefault {
  override def spec: Spec[Any, Any] =
    test("") {

      val first = TestClazz(10)
      val second = TestClazz(20)
      zio.test.assertTrue(first && second)
    }
}
case class TestClazz(int: Int){
  def &&(anotherClz: TestClazz): Boolean = {
    false
  }
}

Currently, we are just going to fail compilation for that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, open to propositions if there is a simpler/prettier way to write this.

AST.And(parseExpr(lhs), parseExpr(rhs), pos.getPos(tree), pos.getPos(lhs), pos.getPos(rhs))

case q"$lhs || $rhs" =>
case q"$lhs || $rhs" if lhs.tpe == typeOf[Boolean] =>
AST.Or(parseExpr(lhs), parseExpr(rhs), pos.getPos(tree), pos.getPos(lhs), pos.getPos(rhs))

case MethodCall(lhs, name, tpes, args) =>
Expand Down
21 changes: 20 additions & 1 deletion test/shared/src/main/scala-3/zio/test/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ object SmartAssertMacros {

def transform[A: Type](expr: Expr[A])(using PositionContext, Quotes) : Expr[TestArrow[Any, A]] = {
import quotes.reflect._
def isBool(term: quotes.reflect.Term): Boolean = {
term.tpe.widen.asType match {
case '[Boolean] => true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this function, but I have no idea if we can match just particular types in :

      case Unseal(MethodCall(lhs, "&&", tpes, Some(List(rhs)))) if isBool(lhs) =>

case _ => false
}
}

def getSpan(term: quotes.reflect.Term): Expr[(Int, Int)] =
Expr(term.pos.start - summon[PositionContext].start, term.pos.end - summon[PositionContext].start)
Expand Down Expand Up @@ -161,10 +167,23 @@ object SmartAssertMacros {
case Unseal(MethodCall(lhs, "==", tpes, Some(List(rhs)))) =>
val span = getSpan(rhs)
lhs.tpe.widen.asType match {
case '[l] =>
case '[l] =>
'{${transform(lhs.asExprOf[l])} >>> SmartAssertions.equalTo(${rhs.asExprOf[l]}).span($span)}.asExprOf[TestArrow[Any, A]]
}

case Unseal(MethodCall(lhs, "&&", tpes, Some(List(rhs)))) if isBool(lhs) =>
val span = getSpan(rhs)
lhs.tpe.widen.asType match {
case '[l] =>
'{${transform(lhs.asExprOf[Boolean])} && {${transform(rhs.asExprOf[Boolean])}}}.asExprOf[TestArrow[Any, A]]
}

case Unseal(MethodCall(lhs, "||", tpes, Some(List(rhs)))) if isBool(lhs) =>
val span = getSpan(rhs)
lhs.tpe.widen.asType match {
case '[l] =>
'{${transform(lhs.asExprOf[Boolean])} || {${transform(rhs.asExprOf[Boolean])}}}.asExprOf[TestArrow[Any, A]]
}

case Unseal(method @ MethodCall(lhs, name, tpeArgs, args)) =>
def body(param: Term) =
Expand Down