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?)
case '{
(${ typeProvider }: HavePropertyTypeProvider[subjectType])
.applyDynamic($propertyNameExpr)($valueExpr: valueType)
.applyDynamic($propertyNameExpr)($valueExpr: valueType => Boolean)
.$asInstanceOf$[AssertionProperty]
} =>
val subjectExpr = '{ $typeProvider.subject }
@ -141,7 +141,7 @@ object GenerativeFluentAssertionsDSL:
private def generateShouldHaveAssertion[T, R](
subjectExpr: Expr[T],
propertyNameExpr: Expr[String],
propertyValueExpr: Expr[R]
propertyValueExpr: Expr[R => Boolean]
)(using Type[T], Type[R])(using Quotes): Expr[Unit] =
import quotes.reflect.*
@ -174,12 +174,11 @@ object GenerativeFluentAssertionsDSL:
*/
val assertion = '{
val subject = $subjectExpr
val expectedValue = $propertyValueExpr
lazy val result = ${ Select(('subject).asTerm, candidateMethod).asExpr }
val tester = $propertyValueExpr
lazy val value = ${ Select(('subject).asTerm, candidateMethod).asExprOf[R] }
assert(
result == expectedValue,
s"${subject} did not have ${$propertyNameExpr} equal" +
s" to ${expectedValue}, but it was equal to ${result}"
tester.apply(value),
s"assertion failed for ${subject}.${$propertyNameExpr}"
)
}
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)
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
// either adult is a method with a dummy Unit parameter