Test with a cross-reference model [message #1806274] |
Tue, 07 May 2019 10:02 |
Marshall Charron Messages: 72 Registered: August 2018 |
Member |
|
|
Hello,
I am making unit tests in my DSL named Karbura. Karbura imports another model from another a DSL named MyDSL. Below is the test I am making. To resolve the model of MyDSL I use the function beneath. When I launch an eclipse instance to test this everything works fine and it resolve "portA" in the test below. However, in my test I place the object1.mydsl at the same directory than my test but it could not resolve it. I got this error for resolving :
The ERROR :
URI : object1.mydsl
URI Resolve : __synthetic0.cy
0 [main] ERROR base.resource.BatchLinkableResource - resolution of uriFragment '|3' failed.
java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base
The TEST :
@Test
def void TestMyDSLImport() {
val result = parseHelper.parse('''
object object1 import "object1.mydsl"
object1.portA // It is a port from object1.mydsl
''')
result.assertNoErrors
Assert.assertNotNull(result)
Assert.assertTrue(result.eResource.errors.isEmpty)
}
The Resolving function :
public static MyDSLModel getMyDSLObject(Object object) {
URI new_uri = URI.createFileURI(object.getImportPath());
MyDSLStandaloneSetup.doSetup();
System.out.println("URI : " + new_uri);
System.out.println("URI Resolve : " + object.eResource().getURI());
if (new_uri.isRelative()) {
new_uri = new_uri.resolve(object.eResource().getURI());
}
Resource r = object.eResource().getResourceSet().getResource(new_uri, true);
if (r != null && r.getContents().size() > 0 && r.getContents().get(0) instanceof MyDSLModel) {
return (MyDSLModel) r.getContents().get(0);
} else {
try {
throw new Exception("No valid model found for resource " + object.getImportPath());
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
How can I achieve this test ?
Thanks
[Updated on: Wed, 08 May 2019 07:18] Report message to a moderator
|
|
|
|
|
|
Re: Test with a cross-reference model [message #1806299 is a reply to message #1806295] |
Tue, 07 May 2019 13:25 |
|
@ExtendWith(InjectionExtension)
@InjectWith(MyDslInjectorProvider)
class MyDslParsingTest {
@Inject
ParseHelper<Model> parseHelper
@Inject
extension ValidationTestHelper
@Inject
Provider<ResourceSet> rsp
@Test
def void loadModel() {
val result = parseHelper.parse('''
Hello Xtext from Christian!
''', URI.createURI("Xtext.mydsl1"),
rsp.get => [
createResource(URI.createURI("Other.mydsl1")) => [
load(new StringInputStream('''Hello Christian!''', "UTF-8"), null)
]
])
Assertions.assertNotNull(result)
val errors = result.eResource.errors
Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
result.assertNoIssues
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Test with a cross-reference model [message #1806343 is a reply to message #1806314] |
Wed, 08 May 2019 00:34 |
Marshall Charron Messages: 72 Registered: August 2018 |
Member |
|
|
Thank you for your response.
Never mind for the classes.
I tried your new snipped, but "Other.mydsl1" is another DSL not the same. I got a java.lang.NullPointerException on load().
I think this is because the DSL is not the same for the second file. How can I achieve something like that (notice that now the second file is mydsl2) :
val result = parseHelper.parse('''
Hello Xtext from Christian!
''', URI.createURI("Xtext.mydsl1"),
rsp.get => [
createResource(URI.createURI("Other.mydsl2")) => [
load(new StringInputStream('''Another DSL!''', "UTF-8"), null)
]
])
[Updated on: Wed, 08 May 2019 00:39] Report message to a moderator
|
|
|
Re: Test with a cross-reference model [message #1806349 is a reply to message #1806343] |
Wed, 08 May 2019 04:09 |
|
So you have two dsls?
Then you have to make sure the standalonesetup is calling the one for the other too
Eg
XxxStandaloneasetup.doSetup()
Or create a custom injecorptovider that does that
e.g.
// use this one in the test
public class MyDslTestInjectorProvider extends MyDslInjectorProvider { // this is the dsl that has the reference
@Override
protected Injector internalCreateInjector() {
MyDsl2StandaloneSetup.doSetup(); // this is the dsl that is refferred to
return super.internalCreateInjector();
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Wed, 08 May 2019 04:47] Report message to a moderator
|
|
|
Re: Test with a cross-reference model [message #1806355 is a reply to message #1806349] |
Wed, 08 May 2019 05:38 |
Marshall Charron Messages: 72 Registered: August 2018 |
Member |
|
|
Thank you for your help.
Here is my Test now (below), but I still get this Error. It seems that the test recognizes other.mydsl2 and load it, but it has a problem to resolve. What could be the origin of this problem ?
I use the same function above to resolve it and it works with the UI. And btw I added the XXXStandaloneSetup.doSetup(); and it seems to help loading the model of the other DSL.
URI : other.mydsl2
URI Resolve : test.mydsl
1 [main] ERROR base.resource.BatchLinkableResource - resolution of uriFragment '|3' failed.
java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base
This error is thrown by this line in the previous function
new_uri = new_uri.resolve(object.eResource().getURI());
@Test
def void TestMyDSLImport() {
val result = parseHelper.parse('''
object object1 import "other.mydsl2"
object1.portA // It is a port from object1.mydsl
''', URI.createURI("test.mydsl"),resourcesetProvider.get => [
createResource(URI.createURI("other.mydsl2")) => [
load(new StringInputStream('''
Another DSL !
''', "UTF-8"), null)
]
])
result.assertNoErrors
Assert.assertNotNull(result)
Assert.assertTrue(result.eResource.errors.isEmpty)
}
[Updated on: Wed, 08 May 2019 07:18] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
Re: Test with a cross-reference model [message #1806390 is a reply to message #1806383] |
Wed, 08 May 2019 10:55 |
|
there is no import path in your model.
its null.
there is a parse error.
should be
myobject object1 import "other.thingml"
then you should have a look what
URI : other.thingml
URI Resolve : test.mydsl
0 [main] ERROR xt.linking.lazy.LazyLinkingResource - resolution of uriFragment '|1' failed.
java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base
at org.eclipse.emf.common.util.URI$Hierarchical.resolve(URI.java:3548)
at org.eclipse.emf.common.util.URI.resolve(URI.java:5579)
at org.xtext.example.mydsl.scoping.Helpers.getThingInThingML(Helpers.java:30)
at org.xtext.example.mydsl.scoping.Helpers.getAllPortsThing(Helpers.java:18)
at org.xtext.example.mydsl.scoping.Helpers.allPorts(Helpers.java:47)
logic makes with the uris you have
thats a emf/uri problem.
you could use file uris e.g.
''', URI.createFileURI("/test.mydsl"),resourcesetProvider.get => [
createResource(URI.createFileURI("/other.thingml"))
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04572 seconds