Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » testing custom validator - different behaviour for assertWarning() vs assertWarningContains()?
testing custom validator - different behaviour for assertWarning() vs assertWarningContains()? [message #794973] Thu, 09 February 2012 23:16 Go to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Writing unit tests for custom validation and confused by behaviour. Would appreciate any suggestions.

Custom validator as follows:

	
@Check
public void domainShouldStartWithCapital(Domain domain) {
	if (!Character.isUpperCase(domain.getName().charAt(0))) {
		warning("Name should start with a capital",
		domain,
		OoaPackage.Literals.DOMAIN__NAME,
		101);
	}
}


If I write unit test as follows it works:

public void testDomainShouldStartWithCapital(){
	Domain domain = OoaFactory.eINSTANCE.createDomain();
	domain.setName("domain");
	tester.validator().domainShouldStartWithCapital(domain);
	tester.diagnose().assertWarningContains("Name should start with a capital");
}


However if I replace the diagnose() call (last line) with the following it fails:

	tester.diagnose().assertWarning(101);


Failure:

java.lang.AssertionError: Predicate (WARNING code=101) does not match Diagnostic WARNING "Name should start with a capital" at Domain'domain'.name=="domain"
	at org.eclipse.xtext.junit.validation.AssertableDiagnostics.fail(AssertableDiagnostics.java:272)
...

Don't understand why and couldn't figure it out. Any suggestions?

Thanks.
Re: testing custom validator - different behaviour for assertWarning() vs assertWarningContains()? [message #795191 is a reply to message #794973] Fri, 10 February 2012 07:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi, 101 is not the error code of the warning it is the index in an
mon existing list. Have a look a the other overloaded signatures of
warning in your validator. Regards Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: testing custom validator - different behaviour for assertWarning() vs assertWarningContains()? [message #795736 is a reply to message #795191] Fri, 10 February 2012 21:40 Go to previous messageGo to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Thx Christian.

That explains first question but raises a second.

The method signature for the assertion is

org.eclipse.xtext.junit.validation.AssertableDiagnostics.assertError(int code)


However there's no corresponding signature in the validator that takes an int code. Closest is

org.eclipse.xtext.validation.AbstractDeclarativeValidator.error(String message, EObject source, EStructuralFeature feature, String code, String... issueData)


which takes a string. Do both 'code' params refer to the same conceptual thing - or are they different?

More generally, is there a recommended idiom for checking errors? Is the pragmatic answer just to use assertErrorContains()?

Thanks for your help.


PS: the documentation should probably be updated as the current example and subsequent text suggests the pattern in my OP. Is there somewhere I should raise that as a bug/feature req?

[Updated on: Fri, 10 February 2012 21:49]

Report message to a moderator

Re: testing custom validator - different behaviour for assertWarning() vs assertWarningContains()? [message #795763 is a reply to message #795736] Fri, 10 February 2012 22:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

int code = org.eclipse.emf.common.util.Diagnostic.getCode()
String (issue)Code = org.eclipse.xtext.validation.AbstractValidationDiagnostic.getIssueCode()

the int code is always null (if you use an Xtext Java Validator)

=> simply ignore the int stuff


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: testing custom validator - different behaviour for assertWarning() vs assertWarningContains()? [message #795788 is a reply to message #795763] Fri, 10 February 2012 23:30 Go to previous messageGo to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
OK - thx. I'll stick with assertErrorContains().
Re: testing custom validator - different behaviour for assertWarning() vs assertWarningContains()? [message #795797 is a reply to message #795788] Fri, 10 February 2012 23:50 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

why not org.eclipse.xtext.validation.AbstractDeclarativeValidator.error(String message, EObject source, EStructuralFeature feature, String code, String... issueData)
+ assertError(String issueCode)?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Use xtext-based plugin as base for another grammar
Next Topic:xText EPackages generation
Goto Forum:
  


Current Time: Thu Apr 25 09:57:45 GMT 2024

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

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

Back to the top