Skip to content

Commit 1620f42

Browse files
committed
Ported changes from @ohze's PR here #1824 to 3.2 branch.
1 parent f24f10e commit 1620f42

File tree

3 files changed

+109
-22
lines changed

3 files changed

+109
-22
lines changed

dotty/core/src/main/scala/org/scalatest/Assertions.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ import ArrayHelper.deep
129129
*
130130
* <pre class="stHighlight">
131131
* val attempted = 2
132-
* assert(attempted == 1, "Execution was attempted " + left + " times instead of 1 time")
132+
* assert(attempted == 1, "Execution was attempted " + attempted + " times instead of 1 time")
133133
* </pre>
134134
*
135135
* <p>
@@ -419,6 +419,8 @@ import ArrayHelper.deep
419419
* @author Bill Venners
420420
*/
421421
trait Assertions extends TripleEquals {
422+
// https://github.com/lampepfl/dotty/pull/8601#pullrequestreview-380646858
423+
implicit object UseDefaultAssertions
422424

423425
//implicit val prettifier = Prettifier.default
424426

@@ -467,8 +469,8 @@ trait Assertions extends TripleEquals {
467469
* @param condition the boolean condition to assert
468470
* @throws TestFailedException if the condition is <code>false</code>.
469471
*/
470-
inline def assert(inline condition: Boolean)(implicit prettifier: Prettifier, pos: source.Position): Assertion =
471-
${ AssertionsMacro.assert('this, 'condition, 'prettifier, 'pos, '{""}) }
472+
inline def assert(inline condition: Boolean)(implicit prettifier: Prettifier, pos: source.Position, use: UseDefaultAssertions.type): Assertion =
473+
${ AssertionsMacro.assert('{condition}, '{prettifier}, '{pos}, '{""}) }
472474

473475
private[scalatest] def newAssertionFailedException(optionalMessage: Option[String], optionalCause: Option[Throwable], pos: source.Position, analysis: scala.collection.immutable.IndexedSeq[String]): Throwable =
474476
new org.scalatest.exceptions.TestFailedException(toExceptionFunction(optionalMessage), optionalCause, Left(pos), None, analysis)
@@ -526,8 +528,8 @@ trait Assertions extends TripleEquals {
526528
* @throws TestFailedException if the condition is <code>false</code>.
527529
* @throws NullArgumentException if <code>message</code> is <code>null</code>.
528530
*/
529-
inline def assert(inline condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: source.Position): Assertion =
530-
${ AssertionsMacro.assert('this, 'condition, 'prettifier, 'pos, 'clue) }
531+
inline def assert(inline condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: source.Position, use: UseDefaultAssertions.type): Assertion =
532+
${ AssertionsMacro.assert('{condition}, '{prettifier}, '{pos}, '{clue}) }
531533

532534
/**
533535
* Assume that a boolean condition is true.
@@ -574,8 +576,8 @@ trait Assertions extends TripleEquals {
574576
* @param condition the boolean condition to assume
575577
* @throws TestCanceledException if the condition is <code>false</code>.
576578
*/
577-
inline def assume(inline condition: Boolean)(implicit prettifier: Prettifier, pos: source.Position): Assertion =
578-
${ AssertionsMacro.assume('this, 'condition, 'prettifier, 'pos, '{""}) }
579+
inline def assume(inline condition: Boolean)(implicit prettifier: Prettifier, pos: source.Position, use: UseDefaultAssertions.type): Assertion =
580+
${ AssertionsMacro.assume('{condition}, '{prettifier}, '{pos}, '{""}) }
579581

580582
/**
581583
* Assume that a boolean condition, described in <code>String</code>
@@ -627,8 +629,8 @@ trait Assertions extends TripleEquals {
627629
* @throws TestCanceledException if the condition is <code>false</code>.
628630
* @throws NullArgumentException if <code>message</code> is <code>null</code>.
629631
*/
630-
inline def assume(inline condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: source.Position): Assertion =
631-
${ AssertionsMacro.assume('this, 'condition, 'prettifier, 'pos, 'clue) }
632+
inline def assume(inline condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: source.Position, use: UseDefaultAssertions.type): Assertion =
633+
${ AssertionsMacro.assume('{condition}, '{prettifier}, '{pos}, '{clue}) }
632634

633635
/**
634636
* Asserts that a given string snippet of code does not pass the Scala type checker, failing if the given
@@ -1440,4 +1442,4 @@ object Assertions extends Assertions {
14401442
* Helper instance used by code generated by macro assertion.
14411443
*/
14421444
val assertionsHelper = new AssertionsHelper
1443-
}
1445+
}

dotty/core/src/main/scala/org/scalatest/AssertionsMacro.scala

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ object AssertionsMacro {
2929
* @param condition original condition expression
3030
* @return transformed expression that performs the assertion check and throw <code>TestFailedException</code> with rich error message if assertion failed
3131
*/
32-
def assert(ths: Expr[Assertions], condition: Expr[Boolean], prettifier: Expr[Prettifier], pos: Expr[source.Position], clue: Expr[Any])(implicit qctx: QuoteContext): Expr[Assertion] =
33-
ths match {
34-
case '{ $ths: diagrams.Diagrams } => org.scalatest.DiagrammedAssertionsMacro.assert(condition, prettifier, pos, clue)
35-
case _ => transform('{Assertions.assertionsHelper.macroAssert}, condition, prettifier, pos, clue)
36-
}
32+
def assert(condition: Expr[Boolean], prettifier: Expr[Prettifier], pos: Expr[source.Position], clue: Expr[Any])(implicit qctx: QuoteContext): Expr[Assertion] =
33+
transform('{Assertions.assertionsHelper.macroAssert}, condition, prettifier, pos, clue)
3734

3835
/**
3936
* Provides implementation for <code>Assertions.assume(booleanExpr: Boolean)</code>, with rich error message.
@@ -42,12 +39,8 @@ object AssertionsMacro {
4239
* @param condition original condition expression
4340
* @return transformed expression that performs the assumption check and throw <code>TestCanceledException</code> with rich error message if assumption failed
4441
*/
45-
def assume(ths: Expr[Assertions], condition: Expr[Boolean], prettifier: Expr[Prettifier], pos: Expr[source.Position], clue: Expr[Any])(implicit qctx: QuoteContext): Expr[Assertion] =
46-
ths match {
47-
case '{ $ths: diagrams.Diagrams } => org.scalatest.DiagrammedAssertionsMacro.assume(condition, prettifier, pos, clue)
48-
case _ => transform('{Assertions.assertionsHelper.macroAssume}, condition, prettifier, pos, clue)
49-
}
50-
42+
def assume(condition: Expr[Boolean], prettifier: Expr[Prettifier], pos: Expr[source.Position], clue: Expr[Any])(implicit qctx: QuoteContext): Expr[Assertion] =
43+
transform('{Assertions.assertionsHelper.macroAssume}, condition, prettifier, pos, clue)
5144

5245
def transform(
5346
helper:Expr[(Bool, Any, source.Position) => Assertion],

dotty/diagrams/src/main/scala/org/scalatest/diagrams/Diagrams.scala

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,99 @@ import org.scalatest.compatible.Assertion
154154
*
155155
* <p>Trait <code>DiagrammedAssertions</code> was inspired by Peter Niederwieser's work in <a href="http://code.google.com/p/spock/">Spock</a> and <a href="https://github.com/pniederw/expecty">Expecty</a>.
156156
*/
157-
trait Diagrams extends Assertions
157+
trait Diagrams extends Assertions {
158+
// https://github.com/lampepfl/dotty/pull/8601#pullrequestreview-380646858
159+
implicit object UseDiagram
160+
161+
import scala.tasty._
162+
import scala.quoted._
163+
164+
/**
165+
* Assert that a boolean condition is true.
166+
* If the condition is <code>true</code>, this method returns normally.
167+
* Else, it throws <code>TestFailedException</code>.
168+
*
169+
* <p>
170+
* This method is implemented in terms of a Scala macro that will generate a more helpful error message that includes
171+
* a diagram showing expression values.
172+
* </p>
173+
*
174+
* <p>
175+
* If multi-line <code>Boolean</code> is passed in, it will fallback to the macro implementation of <code>Assertions</code>
176+
* that does not contain diagram.
177+
* </p>
178+
*
179+
* @param condition the boolean condition to assert
180+
* @throws TestFailedException if the condition is <code>false</code>.
181+
*/
182+
inline def assert(inline condition: Boolean)(implicit prettifier: Prettifier, pos: source.Position, use: UseDiagram.type): Assertion =
183+
${ DiagrammedAssertionsMacro.assert('{condition}, '{prettifier}, '{pos}, '{""}) }
184+
185+
/**
186+
* Assert that a boolean condition, described in <code>String</code>
187+
* <code>message</code>, is true.
188+
* If the condition is <code>true</code>, this method returns normally.
189+
* Else, it throws <code>TestFailedException</code> with the
190+
* <code>String</code> obtained by invoking <code>toString</code> on the
191+
* specified <code>clue</code> as the exception's detail message and a
192+
* diagram showing expression values.
193+
*
194+
* <p>
195+
* If multi-line <code>Boolean</code> is passed in, it will fallback to the macro implementation of <code>Assertions</code>
196+
* that does not contain diagram.
197+
* </p>
198+
*
199+
* @param condition the boolean condition to assert
200+
* @param clue An objects whose <code>toString</code> method returns a message to include in a failure report.
201+
* @throws TestFailedException if the condition is <code>false</code>.
202+
* @throws NullArgumentException if <code>message</code> is <code>null</code>.
203+
*/
204+
inline def assert(inline condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: source.Position, use: UseDiagram.type): Assertion =
205+
${ DiagrammedAssertionsMacro.assert('condition, 'prettifier, 'pos, 'clue) }
206+
207+
/**
208+
* Assume that a boolean condition is true.
209+
* If the condition is <code>true</code>, this method returns normally.
210+
* Else, it throws <code>TestCanceledException</code>.
211+
*
212+
* <p>
213+
* This method is implemented in terms of a Scala macro that will generate a more helpful error message that includes
214+
* a diagram showing expression values.
215+
* </p>
216+
*
217+
* <p>
218+
* If multi-line <code>Boolean</code> is passed in, it will fallback to the macro implementation of <code>Assertions</code>
219+
* that does not contain diagram.
220+
* </p>
221+
*
222+
* @param condition the boolean condition to assume
223+
* @throws TestCanceledException if the condition is <code>false</code>.
224+
*/
225+
inline def assume(inline condition: Boolean)(implicit prettifier: Prettifier, pos: source.Position, use: UseDiagram.type): Assertion =
226+
${ DiagrammedAssertionsMacro.assume('condition, 'prettifier, 'pos, '{""}) }
227+
228+
/**
229+
* Assume that a boolean condition, described in <code>String</code>
230+
* <code>message</code>, is true.
231+
* If the condition is <code>true</code>, this method returns normally.
232+
* Else, it throws <code>TestCanceledException</code> with the
233+
* <code>String</code> obtained by invoking <code>toString</code> on the
234+
* specified <code>clue</code> as the exception's detail message and a
235+
* diagram showing expression values.
236+
*
237+
* <p>
238+
* If multi-line <code>Boolean</code> is passed in, it will fallback to the macro implementation of <code>Assertions</code>
239+
* that does not contain diagram.
240+
* </p>
241+
*
242+
* @param condition the boolean condition to assume
243+
* @param clue An objects whose <code>toString</code> method returns a message to include in a failure report.
244+
* @throws TestCanceledException if the condition is <code>false</code>.
245+
* @throws NullArgumentException if <code>message</code> is <code>null</code>.
246+
*/
247+
inline def assume(inline condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: source.Position, use: UseDiagram.type): Assertion =
248+
${ DiagrammedAssertionsMacro.assume('condition, 'prettifier, 'pos, 'clue) }
249+
}
158250

159251
/**
160252
* Companion object that facilitates the importing of <code>DiagrammedAssertions</code> members as

0 commit comments

Comments
 (0)