Better error messages

This commit is contained in:
Clément Fournier
2018-08-22 18:15:28 +02:00
parent 1703dde463
commit 84ac91b373
2 changed files with 44 additions and 3 deletions

View File

@ -138,10 +138,20 @@ class NWrapper<N : Node> private constructor(val it: N,
childType.isInstance(toWrap)
}
@Suppress("UNCHECKED_CAST")
val wrapper = NWrapper(toWrap as M, matcherPath + childType, ignoreChildrenMatchers)
val childPath = matcherPath + childType
wrapper.spec()
@Suppress("UNCHECKED_CAST")
val wrapper = NWrapper(toWrap as M, childPath, ignoreChildrenMatchers)
try {
wrapper.spec()
} catch (e: AssertionError) {
if (e.message?.matches("At (/.*?|<root>):.*".toRegex()) == false) {
// the exception has no path, let's add one
throw AssertionError(formatErrorMessage(childPath, e.message ?: "No explanation provided"), e)
}
throw e
}
assertFalse(formatErrorMessage(matcherPath + childType, "Wrong number of children, expected ${wrapper.nextChildMatcherIdx}, actual ${wrapper.it.numChildren}")) {
!ignoreChildrenMatchers && wrapper.nextChildMatcherIdx != wrapper.it.numChildren

View File

@ -90,6 +90,37 @@ class DslTest : FunSpec({
}
}
failureTest("All assertions should have a node path",
messageContains = setOf("At /LocalVariableDeclaration/Type:", "expected: \"bratwurst\"")) {
parseStatement("int[] i = 0;") should matchNode<ASTLocalVariableDeclaration> {
child<ASTType> {
// this fails
it.typeImage shouldBe "bratwurst"
}
unspecifiedChild()
}
}
failureTest("Child assertions should have a node path",
messageContains = setOf("At /LocalVariableDeclaration/Type:", "expected", "type", "LambdaExpression")) {
parseStatement("int[] i = 0;") should matchNode<ASTLocalVariableDeclaration> {
child<ASTType> {
// this fails
child<ASTLambdaExpression> { }
}
unspecifiedChild()
}
}
failureTest("Leaf nodes should assert that they have no children",
messageContains = setOf("number", "children", "expected 0")) {