From b078f083dfaeea3d089530a658660bfd5d2e331f Mon Sep 17 00:00:00 2001 From: Wosin Date: Sun, 4 Sep 2022 14:32:00 +0200 Subject: [PATCH 1/3] Use `AND` PoC. --- test/shared/src/main/scala-3/zio/test/Macros.scala | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/shared/src/main/scala-3/zio/test/Macros.scala b/test/shared/src/main/scala-3/zio/test/Macros.scala index 823258bf2195..6a56171afea3 100644 --- a/test/shared/src/main/scala-3/zio/test/Macros.scala +++ b/test/shared/src/main/scala-3/zio/test/Macros.scala @@ -161,10 +161,16 @@ 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)))) => + 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) = From 6cac064283333420d57acfbfecc518eca50902cb Mon Sep 17 00:00:00 2001 From: Wosin Date: Sun, 4 Sep 2022 17:18:26 +0200 Subject: [PATCH 2/3] Modify generation of `&&` and `||` logical operations in Macros for Scala 3. --- .../src/main/scala-3/zio/test/Macros.scala | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/shared/src/main/scala-3/zio/test/Macros.scala b/test/shared/src/main/scala-3/zio/test/Macros.scala index 6a56171afea3..77bdfaf0af24 100644 --- a/test/shared/src/main/scala-3/zio/test/Macros.scala +++ b/test/shared/src/main/scala-3/zio/test/Macros.scala @@ -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 + case _ => false + } + } def getSpan(term: quotes.reflect.Term): Expr[(Int, Int)] = Expr(term.pos.start - summon[PositionContext].start, term.pos.end - summon[PositionContext].start) @@ -161,17 +167,24 @@ 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)))) => + 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) = (tpeArgs, args) match { From 4639191abe8de24d8140a388f4d5086c888f0f71 Mon Sep 17 00:00:00 2001 From: Wosin Date: Sun, 4 Sep 2022 21:12:05 +0200 Subject: [PATCH 3/3] Make `&&` and `||` only match booleans. --- test/shared/src/main/scala-2/zio/test/SmartAssertMacros.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/shared/src/main/scala-2/zio/test/SmartAssertMacros.scala b/test/shared/src/main/scala-2/zio/test/SmartAssertMacros.scala index 6641d532b499..d7344b26eb8e 100644 --- a/test/shared/src/main/scala-2/zio/test/SmartAssertMacros.scala +++ b/test/shared/src/main/scala-2/zio/test/SmartAssertMacros.scala @@ -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] => 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) =>