-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e0ca96
commit a9bde78
Showing
22 changed files
with
771 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
162 changes: 162 additions & 0 deletions
162
modules/core/shared/src/main/scala-2/weaver/ExpectMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package weaver | ||
|
||
import scala.reflect.macros.blackbox | ||
import weaver.internals.ClueHelpers | ||
|
||
private[weaver] trait ExpectMacro { | ||
|
||
/** | ||
* Asserts that a boolean value is true. | ||
* | ||
* Use the [[Expectations.Helpers.clue]] function to investigate any failures. | ||
*/ | ||
def apply(value: Boolean): Expectations = macro ExpectMacro.applyImpl | ||
|
||
/** | ||
* Asserts that a boolean value is true and displays a failure message if not. | ||
* | ||
* Use the [[Expectations.Helpers.clue]] function to investigate any failures. | ||
*/ | ||
def apply(value: Boolean, message: => String): Expectations = | ||
macro ExpectMacro.messageImpl | ||
|
||
/** | ||
* Asserts that boolean values are all true. | ||
* | ||
* Use the [[Expectations.Helpers.clue]] function to investigate any failures. | ||
*/ | ||
def all(values: Boolean*): Expectations = macro ExpectMacro.allImpl | ||
} | ||
|
||
private[weaver] object ExpectMacro { | ||
|
||
/** | ||
* Constructs [[Expectations]] from several boolean values. | ||
* | ||
* If any value evaluates to false, all generated clues are displayed as part | ||
* of the failed expectation. | ||
*/ | ||
def allImpl(c: blackbox.Context)(values: c.Tree*): c.Tree = { | ||
import c.universe._ | ||
val sourceLoc = new weaver.macros.Macros(c).fromContext.asInstanceOf[c.Tree] | ||
val (cluesName, cluesValDef) = makeClues(c) | ||
val clueMethodSymbol = getClueMethodSymbol(c) | ||
|
||
val transformedValues = | ||
values.toList.map(replaceClueMethodCalls(c)(clueMethodSymbol, | ||
cluesName, | ||
_)) | ||
makeExpectations(c)(cluesName, | ||
cluesValDef, | ||
transformedValues, | ||
sourceLoc, | ||
q"None") | ||
} | ||
|
||
/** | ||
* Constructs [[Expectations]] from a boolean value and message. | ||
* | ||
* If the value evaluates to false, the message is displayed as part of the | ||
* failed expectation. | ||
*/ | ||
def messageImpl(c: blackbox.Context)( | ||
value: c.Tree, | ||
message: c.Tree): c.Tree = { | ||
import c.universe._ | ||
val sourceLoc = new weaver.macros.Macros(c).fromContext.asInstanceOf[c.Tree] | ||
val (cluesName, cluesValDef) = makeClues(c) | ||
val clueMethodSymbol = getClueMethodSymbol(c) | ||
|
||
val transformedValue = | ||
replaceClueMethodCalls(c)(clueMethodSymbol, cluesName, value) | ||
makeExpectations(c)(cluesName, | ||
cluesValDef, | ||
List(transformedValue), | ||
sourceLoc, | ||
q"Some($message)") | ||
} | ||
|
||
/** | ||
* Constructs [[Expectations]] from a boolean value. | ||
* | ||
* A macro is needed to support clues. The value expression may contain calls | ||
* to [[ClueHelpers.clue]], which generate clues for values under test. | ||
* | ||
* This macro constructs a local collection of [[Clues]] and adds the | ||
* generated clues to it. Calls to [[ClueHelpers.clue]] are rewritten to calls | ||
* to [[Clues.addClue]]. | ||
* | ||
* After the value is evaluated, the [[Clues]] collection is used to contruct | ||
* [[Expectations]]. | ||
*/ | ||
def applyImpl(c: blackbox.Context)(value: c.Tree): c.Tree = { | ||
|
||
import c.universe._ | ||
val sourceLoc = new weaver.macros.Macros(c).fromContext.asInstanceOf[c.Tree] | ||
val (cluesName, cluesValDef) = makeClues(c) | ||
val clueMethodSymbol = getClueMethodSymbol(c) | ||
|
||
val transformedValue = | ||
replaceClueMethodCalls(c)(clueMethodSymbol, cluesName, value) | ||
makeExpectations(c)(cluesName, | ||
cluesValDef, | ||
List(transformedValue), | ||
sourceLoc, | ||
q"None") | ||
} | ||
|
||
/** Constructs [[Expectations]] from the local [[Clues]] collection. */ | ||
private def makeExpectations(c: blackbox.Context)( | ||
cluesName: c.TermName, | ||
cluesValDef: c.Tree, | ||
values: List[c.Tree], | ||
sourceLoc: c.Tree, | ||
message: c.Tree): c.Tree = { | ||
import c.universe._ | ||
val block = | ||
q"$cluesValDef; _root_.weaver.internals.Clues.toExpectations($sourceLoc, $message, $cluesName, ..$values)" | ||
val untyped = c.untypecheck(block) | ||
val retyped = c.typecheck(untyped, pt = c.typeOf[Expectations]) | ||
retyped | ||
|
||
} | ||
|
||
/** Get the [[ClueHelpers.clue]] symbol. */ | ||
private def getClueMethodSymbol(c: blackbox.Context): c.Symbol = { | ||
import c.universe._ | ||
symbolOf[ClueHelpers].info.member(TermName("clue")) | ||
} | ||
|
||
/** Construct a [[Clues]] collection local to the `expect` call. */ | ||
private def makeClues(c: blackbox.Context): (c.TermName, c.Tree) = { | ||
import c.universe._ | ||
val cluesName = TermName(c.freshName("clues$")) | ||
val cluesValDef = | ||
q"val $cluesName: _root_.weaver.internals.Clues = new _root_.weaver.internals.Clues()" | ||
(cluesName, cluesValDef) | ||
} | ||
|
||
/** | ||
* Replaces all calls to [[ClueHelpers.clue]] with calls to [[Clues.addClue]]. | ||
*/ | ||
private def replaceClueMethodCalls(c: blackbox.Context)( | ||
clueMethodSymbol: c.Symbol, | ||
cluesName: c.TermName, | ||
value: c.Tree): c.Tree = { | ||
|
||
import c.universe._ | ||
object transformer extends Transformer { | ||
|
||
override def transform(input: Tree): Tree = input match { | ||
case c.universe.Apply(fun, List(clueValue)) | ||
if fun.symbol == clueMethodSymbol => | ||
val transformedClueValue = super.transform(clueValue) | ||
val clueName = TermName(c.freshName("clue$")) | ||
q"""{val $clueName = ${transformedClueValue}; ${cluesName}.addClue($clueName)}""" | ||
case o => super.transform(o) | ||
} | ||
} | ||
|
||
transformer.transform(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
modules/core/shared/src/main/scala-2/weaver/internals/Clue.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package weaver.internals | ||
|
||
import cats.Show | ||
|
||
/** | ||
* Captures the source code, type information, and runtime representation of a | ||
* value. | ||
* | ||
* Clues are useful for investigating failed assertions. A clue for a given | ||
* value is summoned with the [[ClueHelpers.clue]] function. This constructs a | ||
* clue for a given value using an implicit conversion. | ||
* | ||
* @param source | ||
* The source code of the value | ||
* @param value | ||
* The runtime value | ||
* @param valueType | ||
* The string representation of the type of the value | ||
* @param show | ||
* The [[cats.Show]] typeclass used to display the value. | ||
*/ | ||
private[weaver] class Clue[T]( | ||
source: String, | ||
private[internals] val value: T, | ||
valueType: String, | ||
show: Show[T] | ||
) { | ||
private[internals] def prettyPrint: String = | ||
s"${source}: ${valueType} = ${show.show(value)}" | ||
} | ||
|
||
private[internals] trait LowPriorityClueImplicits { | ||
|
||
/** | ||
* Generates a clue for a given value using the [[toString]] function to print | ||
* the value. | ||
*/ | ||
implicit def generateClueFromToString[A](value: A): Clue[A] = | ||
macro ClueMacro.showFromToStringImpl | ||
} | ||
private[weaver] object Clue extends LowPriorityClueImplicits { | ||
|
||
/** | ||
* Generates a clue for a given value using a [[Show]] instance to print the | ||
* value. | ||
*/ | ||
implicit def generateClue[A](value: A)(implicit catsShow: Show[A]): Clue[A] = | ||
macro ClueMacro.impl | ||
|
||
} |
Oops, something went wrong.