Home » Modeling » TMF (Xtext) » junit test formatting removes hidden tokens (i.e. comments)
| | |
Re: junit test formatting removes hidden tokens (i.e. comments) [message #997145 is a reply to message #996907] |
Thu, 03 January 2013 23:46 |
Barrie Treloar Messages: 55 Registered: July 2009 |
Member |
|
|
Well, dont you love debuggers!
A fresh new day gives you lots of new perspective.
Break point inside MyDslFormatterconfigureFormatting(FormattingConfig c) and then running the DSL Eclipse App inside the debugger shows
ContentFormatterFactory$ContentFormatter.format(IDocument document, IRegion region) being invoked.
This is injected with a INodeModelFormatter, by default a DefaultNodeModelFormatter.
DefaultNodeModelFormatter.format(ICompositeNode root, int offset, int length) does the work with a INodeModelStreamer (default NodeModelStreamer)
ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length);
NodeModelStreamer makes sure handle the hidden tokens.
So my question is,
Can I hook into this easily in my unit tests somehow?
|
|
|
Re: junit test formatting removes hidden tokens (i.e. comments) [message #997153 is a reply to message #997145] |
Fri, 04 January 2013 02:58 |
Barrie Treloar Messages: 55 Registered: July 2009 |
Member |
|
|
Well, looks like I managed to get it working.
For those following the google trail, this is what I am doing.
Note: I've pulled out everything from ParserHelper into a new class ParserHelper2 and added the formatting into this class, since it does similar stuff and needs to parse anyway.
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.Constants;
import org.eclipse.xtext.formatting.INodeModelFormatter;
import org.eclipse.xtext.formatting.INodeModelFormatter.IFormattedRegion;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.StringInputStream;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
public class ParserHelper2<T extends EObject> {
@Inject
protected Provider<XtextResourceSet> resourceSetProvider;
@Inject
public LexerAndParserTest lexerTester;
@Inject
protected IResourceFactory resourceFactory;
@Inject
protected IResourceValidator validator;
@Inject
protected INodeModelFormatter formatter;
@Inject
@Named(Constants.FILE_EXTENSIONS)
public String fileExtension;
public String format(CharSequence text) throws Exception {
return format(text, resourceSetProvider.get());
}
public String format(CharSequence text, ResourceSet resourceSetToUse) throws Exception {
return format(getAsStream(text), computeUnusedUri(resourceSetToUse), null, resourceSetToUse);
}
public String format(CharSequence text, URI uriToUse, ResourceSet resourceSetToUse) throws Exception {
return format(getAsStream(text), uriToUse, null, resourceSetToUse);
}
public String format(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
XtextResource resource = getResource(in, uriToUse, options, resourceSet);
IParseResult parseResult = resource.getParseResult();
if (parseResult == null)
return null;
IFormattedRegion r = formatter.format(parseResult.getRootNode(), 0, parseResult.getRootNode().getText().length());
return r.getFormattedText();
}
public T parse(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
Resource resource = getResource(in, uriToUse, options, resourceSet);
validate(resource);
@SuppressWarnings("unchecked")
final T root = (T) (resource.getContents().isEmpty() ? null : resource.getContents().get(0));
return root;
}
public T parse(CharSequence text) throws Exception {
return parse(text, resourceSetProvider.get());
}
public T parse(CharSequence text, ResourceSet resourceSetToUse) throws Exception {
return parse(getAsStream(text), computeUnusedUri(resourceSetToUse), null, resourceSetToUse);
}
public T parse(CharSequence text, URI uriToUse, ResourceSet resourceSetToUse) throws Exception {
return parse(getAsStream(text), uriToUse, null, resourceSetToUse);
}
protected URI computeUnusedUri(ResourceSet resourceSet) {
String name = "__synthetic";
for (int i = 0; i < Integer.MAX_VALUE; i++) {
URI syntheticUri = URI.createURI(name + i + "." + fileExtension);
if (resourceSet.getResource(syntheticUri, false) == null)
return syntheticUri;
}
throw new IllegalStateException();
}
protected InputStream getAsStream(CharSequence text) {
return new StringInputStream(text == null ? "" : text.toString());
}
public XtextResource getResource(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
Resource resource = resourceFactory.createResource(uriToUse);
if (!(resource instanceof XtextResource)) {
throw new RuntimeException("Can only handle XtextResources and not " + resource.getClass().getCanonicalName());
}
resourceSet.getResources().add(resource);
try {
resource.load(in, options);
} catch (IOException e) {
throw new WrappedException(e);
}
return (XtextResource) resource;
}
public void validate(T model) {
validate(model.eResource());
}
public void validate(Resource resource) {
List<Issue> issues = validator.validate(resource, CheckMode.ALL, null);
if (!issues.isEmpty()) {
throw new XtextParseError(issues);
}
}
}
To use the class in your xtend junit test:
(Mytest.xtend)
@Inject extension ParserHelper2<Usrprops>
@Test
def void myFormatTest() {
val input = '''...'''.toString
val expectedFormat = '''...'''.toString
assertEquals(expectedFormat, input.format())
}
|
|
|
Goto Forum:
Current Time: Tue Sep 24 23:55:46 GMT 2024
Powered by FUDForum. Page generated in 0.04081 seconds
|