Skip to content

Commit

Permalink
Reformat.
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Sep 16, 2024
1 parent ba96e75 commit 396247b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
45 changes: 22 additions & 23 deletions modules/core/shared/src/main/scala-2/weaver/ExpectMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import weaver.internals.ClueHelpers

private[weaver] trait ExpectMacro {
def apply(value: Boolean): Expectations = macro ExpectMacro.applyImpl
def apply(value: Boolean, message: => String): Expectations = macro ExpectMacro.messageImpl
def apply(value: Boolean, message: => String): Expectations =
macro ExpectMacro.messageImpl
def all(values: Boolean*): Expectations = macro ExpectMacro.allImpl
}
private[weaver] object ExpectMacro {
def allImpl(c: blackbox.Context)(values: c.Tree*): c.Tree = {
import c.universe._
val loc = new weaver.macros.Macros(c).fromContext.asInstanceOf[c.Tree]
val (cluesName, cluesValDef) = makeClues(c)
val clueMethodSymbol = getClueMethodSymbol(c)
val clueMethodSymbol = getClueMethodSymbol(c)

val outputTrees = values.toList.map(transform(c)(clueMethodSymbol,
cluesName,
_))
val outputTrees =
values.toList.map(transform(c)(clueMethodSymbol, cluesName, _))
makeBlock(c)(cluesName, cluesValDef, outputTrees, loc, q"None")
}
def messageImpl(c: blackbox.Context)(
Expand All @@ -26,7 +26,7 @@ private[weaver] object ExpectMacro {
import c.universe._
val loc = new weaver.macros.Macros(c).fromContext.asInstanceOf[c.Tree]
val (cluesName, cluesValDef) = makeClues(c)
val clueMethodSymbol = getClueMethodSymbol(c)
val clueMethodSymbol = getClueMethodSymbol(c)

val outputTree =
transform(c)(clueMethodSymbol, cluesName, value)
Expand All @@ -42,15 +42,11 @@ private[weaver] object ExpectMacro {
import c.universe._
val loc = new weaver.macros.Macros(c).fromContext.asInstanceOf[c.Tree]
val (cluesName, cluesValDef) = makeClues(c)
val clueMethodSymbol = getClueMethodSymbol(c)
val clueMethodSymbol = getClueMethodSymbol(c)

val outputTree =
transform(c)(clueMethodSymbol, cluesName, value)
makeBlock(c)(cluesName,
cluesValDef,
List(outputTree),
loc,
q"None")
makeBlock(c)(cluesName, cluesValDef, List(outputTree), loc, q"None")
}

private def makeBlock(c: blackbox.Context)(
Expand All @@ -72,13 +68,15 @@ private[weaver] object ExpectMacro {
private def getClueMethodSymbol(c: blackbox.Context): c.Symbol = {
import c.universe._
val clueFunctionsBaseClass = symbolOf[ClueHelpers]
def go(s: c.Symbol, depth: Int): c.Symbol = if(s.isClass && s.asClass.baseClasses.contains(clueFunctionsBaseClass)) {
s.info.member(TermName("clue"))
} else {
if (depth < 100) {
go(s.owner, depth + 1)
} else throw new Error(s"Failed to find module! ${c.internal.enclosingOwner}")
}
def go(s: c.Symbol, depth: Int): c.Symbol =
if (s.isClass && s.asClass.baseClasses.contains(clueFunctionsBaseClass)) {
s.info.member(TermName("clue"))
} else {
if (depth < 100) {
go(s.owner, depth + 1)
} else throw new Error(
s"Failed to find module! ${c.internal.enclosingOwner}")
}
val parent = go(c.internal.enclosingOwner, 0)
parent
}
Expand All @@ -100,12 +98,13 @@ private[weaver] object ExpectMacro {
object mytransformer extends Transformer {

override def transform(input: Tree): Tree = input match {
case c.universe.Apply(fun, List(clueSource)) if fun.symbol == clueMethodSymbol =>
case c.universe.Apply(fun, List(clueSource))
if fun.symbol == clueMethodSymbol =>
val transformedValue = super.transform(clueSource)
val clueValueName = TermName(c.freshName("clueValue$"))
val clueValueTree = q"$clueValueName"
val clueValueName = TermName(c.freshName("clueValue$"))
val clueValueTree = q"$clueValueName"
val transformedTree =
q"""{val $clueValueName = ${transformedValue}; ${cluesName}.addClue($clueValueTree)}"""
q"""{val $clueValueName = ${transformedValue}; ${cluesName}.addClue($clueValueTree)}"""
transformedTree
case o => super.transform(o)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ private[weaver] class Clue[T](
}

trait LowPriorityClueImplicits {
implicit def generateClueFromToString[A](value: A): Clue[A] = macro ClueMacro.showFromToStringImpl
implicit def generateClueFromToString[A](value: A): Clue[A] =
macro ClueMacro.showFromToStringImpl
}
private[weaver] object Clue extends LowPriorityClueImplicits {
implicit def generateClue[A](value: A)(implicit catsShow: Show[A]): Clue[A] = macro ClueMacro.impl
implicit def generateClue[A](value: A)(implicit catsShow: Show[A]): Clue[A] =
macro ClueMacro.impl

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ private[weaver] object ClueMacro {
if (value.pos != null && value.pos.isRange) {
val chars = value.pos.source.content
val start = value.pos.start
val end = value.pos.end
if (
end > start &&
val end = value.pos.end
if (end > start &&
start >= 0 && start < chars.length &&
end >= 0 && end < chars.length
) {
end >= 0 && end < chars.length) {
new String(chars, start, end - start)
} else {
""
Expand All @@ -35,12 +33,12 @@ private[weaver] object ClueMacro {
// Printers.log(t)(Location.empty)
t.widen
}
val source = Literal(Constant(text.trim))
val source = Literal(Constant(text.trim))
val valueType = Literal(Constant(simplifyType(value.tpe).toString()))
val clueTpe = c.internal.typeRef(
NoPrefix,
c.mirror.staticClass(classOf[Clue[_]].getName()),
List(value.tpe.widen)
NoPrefix,
c.mirror.staticClass(classOf[Clue[_]].getName()),
List(value.tpe.widen)
)
q"new $clueTpe(..$source, $value, $valueType, $catsShow)"
}
Expand Down

0 comments on commit 396247b

Please sign in to comment.