-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace expecty with clue #64
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
170 changes: 170 additions & 0 deletions
170
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,170 @@ | ||
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._ | ||
|
||
// This transformation outputs code that adds clues to a local | ||
// clues collection `cluesName`. It recurses over the input code and replaces | ||
// all calls of `ClueHelpers.clue` with `cluesName.addClue`. | ||
object transformer extends Transformer { | ||
|
||
override def transform(input: Tree): Tree = input match { | ||
case c.universe.Apply(fun, List(clueValue)) | ||
if fun.symbol == clueMethodSymbol => | ||
// The input tree corresponds to `ClueHelpers.clue(clueValue)` . | ||
// Transform it into `clueName.addClue(clueValue)` | ||
// Apply the transformation recursively to `clueValue` to support nested clues. | ||
val transformedClueValue = super.transform(clueValue) | ||
q"""${cluesName}.addClue($transformedClueValue)""" | ||
case o => | ||
// Otherwise, recurse over the input. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ package weaver | |
|
||
// kudos to https://github.com/monix/minitest | ||
// format: off | ||
import scala.reflect.macros.whitebox | ||
import scala.reflect.macros.blackbox | ||
|
||
trait SourceLocationMacro { | ||
|
||
|
@@ -22,10 +22,10 @@ trait SourceLocationMacro { | |
} | ||
|
||
object macros { | ||
class Macros(val c: whitebox.Context) { | ||
class Macros(val c: blackbox.Context) { | ||
import c.universe._ | ||
|
||
def fromContext: Tree = { | ||
def fromContext: c.Tree = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
val (pathExpr, relPathExpr, lineExpr) = getSourceLocation | ||
val SourceLocationSym = symbolOf[SourceLocation].companion | ||
q"""$SourceLocationSym($pathExpr, $relPathExpr, $lineExpr)""" | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about this transformation here. Can you explain ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course!
For context, the user writes
clue(x)
whereclue
refers toClueHelpers.clue
. There's an implicit conversion fromx
to aClue
. Explicitly, this is written asclue(Clue.generateClue(x))
.The transformation replaces
ClueHelpers.clue
withaddClue
on a clues collection. In other words, it replaces:clue(Clue.generateClue(x))
withclues.addClue(Clue.generateClue(x))
.The clues collection is generated as part of the overall
expect
macro. For example:expect(clue(List(x, clue(y))))
becomes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh gotcha, hadn't thought of nested clues, but it makes sense 👍