Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Asserting multiple errors with ValidationTestHelper
Asserting multiple errors with ValidationTestHelper [message #1853453] Sat, 02 July 2022 17:43 Go to next message
Mirko Raner is currently offline Mirko RanerFriend
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 #1853454 is a reply to message #1853453] Sat, 02 July 2022 17:58 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 157
Registered: February 2016
Senior Member
Hello Mirko!

You can take some inspiration from the Eclipse GEF DOT project:

https://github.com/eclipse/gef/blob/master/org.eclipse.gef.dot.tests/src/org/eclipse/gef/dot/tests/DotValidatorTest.xtend

Hope that helps,
Tamás
Re: Asserting multiple errors with ValidationTestHelper [message #1853455 is a reply to message #1853453] Sat, 02 July 2022 17:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
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
Re: Asserting multiple errors with ValidationTestHelper [message #1853488 is a reply to message #1853455] Mon, 04 July 2022 23:14 Go to previous message
Mirko Raner is currently offline Mirko RanerFriend
Messages: 125
Registered: July 2009
Location: New York City, NY
Senior Member
Thanks, Tamás and Christian.
I guess I was just too singularly focused on finding an API that allowed me to assert a list of issues in a single call; your suggestion set me on the right track.
Previous Topic:Adding elements
Next Topic:I have 2 DSLs. When I launch the 2nd instance of Eclipse only 1 of them is active.
Goto Forum:
  


Current Time: Tue Apr 16 09:34:12 GMT 2024

Powered by FUDForum. Page generated in 0.72527 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top