Home » Modeling » TMF (Xtext) » Validator Testing with JUnit 5
Validator Testing with JUnit 5 [message #1838277] |
Sat, 20 February 2021 08:21  |
Oli Ana Messages: 3 Registered: February 2021 |
Junior Member |
|
|
Hi all,
I can't figure out how to test the custom validator developed for my grammar using Junit 5. At runtime the error is triggered, but when testing everything seems ok, no error is fired.
I've read the documentation (web and book), looked carefully at every example in eclipse, but I didn't have any success.
This is a simplified version of the code.
The grammar:
grammar org.xtext.example.mydsl.Mydsl with org.eclipse.xtext.common.Terminals
generate mydsl ...
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=ID '!';
The custom validator:
package org.xtext.example.mydsl.validation;
import org.eclipse.xtext.validation.Check;
import org.xtext.example.mydsl.mydsl.MydslPackage;
import org.xtext.example.mydsl.mydsl.Greeting;
public class MydslValidator extends AbstractMydslValidator {
public static final String INVALID_NAME = "invalidName";
@Check
public void checkGreetingStartsWithCapital(Greeting greeting) {
if (!Character.isUpperCase(greeting.getName().charAt(0))) {
warning("Name should start with a capital",
MydslPackage.Literals.GREETING__NAME,
INVALID_NAME);
}
}
}
and the tests
package org.xtext.example.mydsl.tests
import com.google.inject.Inject
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.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 MydslValidationTest {
@Inject extension
ParseHelper<Model>
@Inject extension ValidationTestHelper
@Test
def void greetingStartsWithCapitalTest(){
val result = '''
Hello xtext!
'''.parse
result.assertWarning(MydslPackage.Literals.GREETING, "Name should start with a capital", MydslValidator.INVALID_NAME)
}
}
The result:
Expected WARNING 'Name should start with a capital' on Greeting at [-1:-1] but got
if I replace
result.assertWarning(MydslPackage.Literals.GREETING, "Name should start with a capital", MydslValidator.INVALID_NAME)
with
The test succeeds.
If I introduce any syntax error in the model to be tested, an error is fired.
I guess my validator isn't taken into account while testing. What am I missing?
Any help would be appreciated.
Thanks in advance.
|
|
| |
Re: Validator Testing with JUnit 5 [message #1838313 is a reply to message #1838292] |
Mon, 22 February 2021 08:35   |
Oli Ana Messages: 3 Registered: February 2021 |
Junior Member |
|
|
Thanks for the quick response.
However, I can't manage to install either of the milestones...
An error occurred while collecting items to be installed
session context was:(profile=C__Users_xxx_eclipse_dsl-2020-12_eclipse, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, action=).
Artifact not found: osgi.bundle,org.eclipse.xtend.core,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.doc,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.examples,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.ide,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.ide.common,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.lib,2.25.0.v20210203-0750.
Artifact not found: osgi.bundle,org.eclipse.xtend.lib.macro,2.25.0.v20210203-0750.
Artifact not found: osgi.bundle,org.eclipse.xtend.m2e,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtend.standalone,2.25.0.v20210203-0838.
Artifact not found: osgi.bundle,org.eclipse.xtext,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.activities,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.builder,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.builder.standalone,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.buildship,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.edit,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.shared,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.shared.jdt38,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.common.types.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.doc,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ecore,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.generator,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.ide,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.java,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.junit4,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.logging,1.2.15.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.m2e,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.purexbase,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.purexbase.ide,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.purexbase.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.smap,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.testing,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codemining,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codetemplates,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codetemplates.ide,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.codetemplates.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.ecore,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.shared,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.ui.testing,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.util,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.ide,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.junit,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.lib,2.25.0.v20210203-0750.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.testing,2.25.0.v20210203-0759.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xbase.ui.testing,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.generator,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ide,2.25.0.v20210203-0753.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ui,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ui.examples,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.ui.graph,2.25.0.v20210203-0811.
Artifact not found: osgi.bundle,org.eclipse.xtext.xtext.wizard,2.25.0.v20210203-0753.
[Updated on: Mon, 22 February 2021 10:02] Report message to a moderator
|
|
| | | | |
Goto Forum:
Current Time: Tue Jun 06 14:10:19 GMT 2023
Powered by FUDForum. Page generated in 0.02201 seconds
|