Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit-testing custom validators?
Unit-testing custom validators? [message #1751929] Mon, 16 January 2017 20:41 Go to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 174
Registered: July 2009
Senior Member
Hello,

I've succeeded in creating a set of basic unit tests that check for parsing errors. Now I would like to extend these unit tests to also cover some additional validators - and I'm stuck. The validators are called from the editor, but they aren't used during the unit tests. How can I enable the validation in this scenario?

(Perhaps an improvement suggestion for the generated code - the pre-generated unit test will accept anything and always pass, even if the input passed to the parseHelper is not valid wrt to the language definition. It would be nice to at least have a comment that shows new users how to have the unit test fail on parsing errors.)

Thanks
Volker
Re: Unit-testing custom validators? [message #1751930 is a reply to message #1751929] Mon, 16 January 2017 21:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10551
Registered: July 2009
Senior Member
You may follow this discussion at https://github.com/eclipse/xtext-core/issues/118
It addresses both

Parse errors
(Fixed in 2.11)
And validation errors
(Postponed to a better documentation to be written)

[Updated on: Mon, 16 January 2017 21:09]

Report message to a moderator

Re: Unit-testing custom validators? [message #1751932 is a reply to message #1751930] Mon, 16 January 2017 21:19 Go to previous messageGo to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 174
Registered: July 2009
Senior Member
Christian,

indeed, thank you!

Best regards
Volker
Re: Unit-testing custom validators? [message #1751933 is a reply to message #1751932] Mon, 16 January 2017 21:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10551
Registered: July 2009
Senior Member
Created https://github.com/eclipse/xtext-core/issues/253
Re: Unit-testing custom validators? [message #1751935 is a reply to message #1751933] Mon, 16 January 2017 22:06 Go to previous messageGo to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 174
Registered: July 2009
Senior Member
Hmm - apparently, there's still something missing. The tests do look much nicer now, but the validators are still not executed.
Re: Unit-testing custom validators? [message #1751944 is a reply to message #1751935] Tue, 17 January 2017 04:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10551
Registered: July 2009
Senior Member
how does your validator look like? (how is it registered)
how does your test look like?

package org.xtext.example.mydsl1.tests

import com.google.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.xtext.example.mydsl1.myDsl.Model
import org.eclipse.xtext.junit4.validation.ValidationTestHelper
import org.xtext.example.mydsl1.myDsl.MyDslPackage
import org.xtext.example.mydsl1.validation.MyDslValidator

@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslParsingTest{

	@Inject
	ParseHelper<Model> parseHelper
	
	@Inject extension
	ValidationTestHelper

	@Test 
	def void loadModel() {
		val result = parseHelper.parse('''
			Hello xtext!
		''')
		Assert.assertNotNull(result)
		result.assertWarning(MyDslPackage.Literals.GREETING, MyDslValidator.INVALID_NAME)
	}

}
Re: Unit-testing custom validators? [message #1751949 is a reply to message #1751944] Tue, 17 January 2017 06:43 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1729
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

In the second edition of the Xtext book, https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition , there's an entire chapter (7) on testing (both runtime concepts and UI concepts).

cheers
Lorenzo
Re: Unit-testing custom validators? [message #1751955 is a reply to message #1751944] Tue, 17 January 2017 07:57 Go to previous messageGo to next message
Volker Wegert is currently offline Volker WegertFriend
Messages: 174
Registered: July 2009
Senior Member
Christian Dietrich wrote on Mon, 16 January 2017 23:06
how does your validator look like? (how is it registered)


In the file GenerateMyModel.mwe2, I added
validator = {
	composedCheck = "foo.bar.baz.MyValidator"
}


What baffles me is that the validator is called from the UI, but it is not called when executing the unit tests (either as plug-in test or "normally"). (I used a break-point to verify this.)

Christian Dietrich wrote on Mon, 16 January 2017 23:06
how does your how does your test look like?


I've changed it to match your example exactly (except for the language specifics, and I'm testing for an Error, so I used assertError instead of assertWarning. Still, no luck.
Re: Unit-testing custom validators? [message #1751956 is a reply to message #1751955] Tue, 17 January 2017 08:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10551
Registered: July 2009
Senior Member
how does your validator impl look like?

i still cannot reproduce this

package foo.bar.baz

import org.eclipse.xtext.validation.AbstractDeclarativeValidator
import org.eclipse.xtext.validation.Check
import org.xtext.example.mydsl4.myDsl.Greeting
import org.xtext.example.mydsl4.myDsl.MyDslPackage
import org.eclipse.xtext.validation.EValidatorRegistrar

class MyValidator extends AbstractDeclarativeValidator {
	
		public static val INVALID_NAME = 'invalidName'

	@Check
	def checkGreetingStartsWithCapital(Greeting greeting) {
		if (!Character.isUpperCase(greeting.name.charAt(0))) {
			warning('Name should start with a capital', 
					MyDslPackage.Literals.GREETING__NAME,
					INVALID_NAME)
		}
	}
	
	override register(EValidatorRegistrar registrar) {
	}
	
}
Re: Unit-testing custom validators? [message #1751957 is a reply to message #1751956] Tue, 17 January 2017 08:28 Go to previous message
Volker Wegert is currently offline Volker WegertFriend
Messages: 174
Registered: July 2009
Senior Member
Never mind. After re-running the MWE2 workflow, I can't reproduce it either. I'm not sure what might have happened - possibly an uncaught UserTooTiredException last night...
Previous Topic:Quickfix Tooltip Description Formatting
Next Topic:Ecore and GenModel annotation not found in xcore files
Goto Forum:
  


Current Time: Sat May 27 04:24:30 GMT 2017

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

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