Asserting multiple errors with ValidationTestHelper [message #1853453] |
Sat, 02 July 2022 17:43 |
Mirko Raner Messages: 125 Registered: July 2009 Location: New York City, NY |
Senior Member |
|
|
I'm trying to assert the presence of multiple errors using the ValidationTestHelper, but it looks like the available methods only allow to assert a single issue, and I get errors like this:
org.junit.ComparisonFailure: expected:<[]> but was:<[Expected ERROR 'Duplicate Element 'one'' on SingleDeclaredExpression at [44:3] but got
ERROR (null) 'Duplicate Element 'one'' on SingleDeclaredExpression, offset 44, length 3
ERROR (null) 'Duplicate Element 'one'' on SingleDeclaredExpression, offset 61, length 3
]>
at org.junit.Assert.assertEquals(Assert.java:117)
at org.junit.Assert.assertEquals(Assert.java:146)
at org.eclipse.xtext.testing.validation.ValidationTestHelper.assertIssue(ValidationTestHelper.java:285)
at org.eclipse.xtext.testing.validation.ValidationTestHelper.assertIssue(ValidationTestHelper.java:138)
at org.eclipse.xtext.testing.validation.ValidationTestHelper.assertError(ValidationTestHelper.java:123)
I had a cursory look at the implementation of ValidationTestHelper and came to the conclusion that it appears to be designed for dealing with single issues only. Can someone confirm this or am I overlooking some API here?
It's easy enough to do the assertions manually, without the helper class, I just didn't want to reinvent the wheel if there is already some existing functionality.
|
|
|
|
Re: Asserting multiple errors with ValidationTestHelper [message #1853455 is a reply to message #1853453] |
Sat, 02 July 2022 17:59 |
|
you can also assert issues at specific positions
package org.xtext.example.mydsl.tests
import com.google.inject.Inject
import org.eclipse.xtext.diagnostics.Severity
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.util.ParseHelper
import org.eclipse.xtext.testing.validation.ValidationTestHelper
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.^extension.ExtendWith
import org.xtext.example.mydsl.myDsl.Model
import org.xtext.example.mydsl.myDsl.MyDslPackage
import org.xtext.example.mydsl.validation.MyDslValidator
@ExtendWith(InjectionExtension)
@InjectWith(MyDslInjectorProvider)
class MyDslParsingTest {
@Inject
ParseHelper<Model> parseHelper
@Inject
ValidationTestHelper h
@Test
def void loadModel() {
val result = parseHelper.parse('''
Hello aaaa!
Hello Xtext!
Hello bbbb!
''')
Assertions.assertNotNull(result)
val errors = result.eResource.errors
Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
h.assertIssue(result.eResource, MyDslPackage.Literals.GREETING, MyDslValidator.INVALID_NAME, 6, 4, Severity.WARNING,
"Name should start with a capital");
h.assertIssue(result.eResource, MyDslPackage.Literals.GREETING, MyDslValidator.INVALID_NAME, 31, 4, Severity.WARNING,
"Name should start with a capital");
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
|
Powered by
FUDForum. Page generated in 0.03905 seconds