This commit is contained in:
Claudio Maggioni 2024-12-16 23:57:38 +01:00
parent 9c812f7158
commit e13b40fd3d
2 changed files with 7 additions and 8 deletions

View file

@ -72,7 +72,7 @@ object GenerativeFluentAssertionsDSL:
// and the type of the property (actually... not exactly that. Why?) // and the type of the property (actually... not exactly that. Why?)
case '{ case '{
(${ typeProvider }: HavePropertyTypeProvider[subjectType]) (${ typeProvider }: HavePropertyTypeProvider[subjectType])
.applyDynamic($propertyNameExpr)($valueExpr: valueType) .applyDynamic($propertyNameExpr)($valueExpr: valueType => Boolean)
.$asInstanceOf$[AssertionProperty] .$asInstanceOf$[AssertionProperty]
} => } =>
val subjectExpr = '{ $typeProvider.subject } val subjectExpr = '{ $typeProvider.subject }
@ -141,7 +141,7 @@ object GenerativeFluentAssertionsDSL:
private def generateShouldHaveAssertion[T, R]( private def generateShouldHaveAssertion[T, R](
subjectExpr: Expr[T], subjectExpr: Expr[T],
propertyNameExpr: Expr[String], propertyNameExpr: Expr[String],
propertyValueExpr: Expr[R] propertyValueExpr: Expr[R => Boolean]
)(using Type[T], Type[R])(using Quotes): Expr[Unit] = )(using Type[T], Type[R])(using Quotes): Expr[Unit] =
import quotes.reflect.* import quotes.reflect.*
@ -174,12 +174,11 @@ object GenerativeFluentAssertionsDSL:
*/ */
val assertion = '{ val assertion = '{
val subject = $subjectExpr val subject = $subjectExpr
val expectedValue = $propertyValueExpr val tester = $propertyValueExpr
lazy val result = ${ Select(('subject).asTerm, candidateMethod).asExpr } lazy val value = ${ Select(('subject).asTerm, candidateMethod).asExprOf[R] }
assert( assert(
result == expectedValue, tester.apply(value),
s"${subject} did not have ${$propertyNameExpr} equal" + s"assertion failed for ${subject}.${$propertyNameExpr}"
s" to ${expectedValue}, but it was equal to ${result}"
) )
} }
report.info(assertion.show, subjectExpr.asTerm.pos) report.info(assertion.show, subjectExpr.asTerm.pos)

View file

@ -40,7 +40,7 @@ case class Box(label: String, weight: Mass, price: Money)
// assert(List(1,2,3).nonEmpty) // assert(List(1,2,3).nonEmpty)
List(1, 2, 3) should be.nonEmpty List(1, 2, 3) should be.nonEmpty
(person should have) age ((x: Int) => x == 10) person should have age satisfying >= 10
// New syntax for `be` with type provider // New syntax for `be` with type provider
// either adult is a method with a dummy Unit parameter // either adult is a method with a dummy Unit parameter