Java Code Examples for org.eclipse.xtext.testing.util.ParseHelper

The following examples show how to use org.eclipse.xtext.testing.util.ParseHelper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sarl   Source File: Bug776Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void parsing_08() throws Exception {
	ParseHelper<SarlScript> parser = getParseHelper();
	SarlScript mas1 = parser.parse(multilineString(
			"package io.sarl.lang.tests.bug776.b",
			"class Toto{}"));
	SarlScript mas2 = parser.parse(multilineString(
			"package io.sarl.lang.tests.bug776",
			"import io.sarl.lang.tests.bug776.b.Toto",
			"class X {",
			"}",
			"class Y extends Z {",
			" static class YX {",
			"    var a : Toto",
			" }",
			"}",
			"class Z {",
			"}",
			""),
			mas1.eResource().getResourceSet());
	XtendClass container = ((XtendClass) mas2.getXtendTypes().get(1).getMembers().get(0));
	JvmTypeReference typeRef = ((SarlField) container.getMembers().get(0)).getType();
	assertEquals("Toto", this.strings.referenceToString(typeRef, "Object"));
}
 
Example 2
Source Project: sarl   Source File: Bug661Test.java    License: Apache License 2.0 6 votes vote down vote up
@RepeatedTest(5)
public void isSubtypeOf_01() throws Exception {
	ParseHelper<SarlScript> helper = getParseHelper();
	
	SarlScript mas0 = helper.parse(SNIPSET1);
	ResourceSet resourceSet = mas0.eResource().getResourceSet();
	SarlScript mas1 = helper.parse(SNIPSET1, resourceSet);

	assertSame(mas0.eResource().getResourceSet(), mas1.eResource().getResourceSet());
	
	JvmTypeReference reference0 = this.typeReferences.getTypeForName("boolean", mas0);
	
	StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, mas1);
	LightweightTypeReference reference1 = owner.newParameterizedTypeReference(this.typeReferences.findDeclaredType("boolean", mas1));
	
	assertTrue(reference1.isSubtypeOf(reference0.getType()));
}
 
Example 3
Source Project: sarl   Source File: Bug661Test.java    License: Apache License 2.0 6 votes vote down vote up
@RepeatedTest(5)
public void isAssignableFrom_01() throws Exception {
	ParseHelper<SarlScript> helper = getParseHelper();
	
	SarlScript mas0 = helper.parse(SNIPSET1);
	ResourceSet resourceSet = mas0.eResource().getResourceSet();
	SarlScript mas1 = helper.parse(SNIPSET1, resourceSet);

	assertSame(mas0.eResource().getResourceSet(), mas1.eResource().getResourceSet());

	JvmTypeReference reference0 = this.typeReferences.getTypeForName("boolean", mas0);
	
	StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, mas1);
	LightweightTypeReference reference1 = owner.newParameterizedTypeReference(this.typeReferences.findDeclaredType("boolean", mas1));
	
	assertTrue(reference1.isAssignableFrom(reference0.getType()));
}
 
Example 4
Source Project: xtext-core   Source File: FormatterTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected void serializeToTokenBuffer(String model, ITokenStream out) throws Exception {
	EObject semanticObject = get(ParseHelper.class).parse(model);
	ISerializationDiagnostic.Acceptor errors = ISerializationDiagnostic.EXCEPTION_THROWING_ACCEPTOR;
	ISemanticSequencer semantic = get(ISemanticSequencer.class);
	ISyntacticSequencer syntactic = get(ISyntacticSequencer.class);
	IHiddenTokenSequencer hidden = get(IHiddenTokenSequencer.class);
	TokenStreamSequenceAdapter tokenstream = new TokenStreamSequenceAdapter(out, getGrammarAccess().getGrammar(), errors);
	semantic.init((ISemanticSequenceAcceptor) syntactic, errors);
	ISerializationContext context = new SerializationContext.RuleContext(null, (ParserRule) get(IGrammarAccess.class).getGrammar().getRules().get(0));
	syntactic.init(context, semanticObject, (ISyntacticSequenceAcceptor) hidden, errors);
	hidden.init(context, semanticObject, tokenstream, errors);
	tokenstream.init(context);
	semantic.createSequence(context, semanticObject);
}
 
Example 5
Source Project: xtext-core   Source File: AnotherFormatterTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void loadModel() {
  try {
    final ParseHelper<Root> parseHelper = this.getInjector().<ParseHelper<Root>>getInstance(Key.<ParseHelper<Root>>get(new TypeLiteral<ParseHelper<Root>>() {
    }));
    final INodeModelFormatter formatter = this.getInjector().<INodeModelFormatter>getInstance(INodeModelFormatter.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("test sample {");
    _builder.newLine();
    _builder.append("// just a comment");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Root root = parseHelper.parse(_builder);
    Assert.assertNotNull(root);
    final EList<Resource.Diagnostic> errors = root.eResource().getErrors();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("Unexpected errors: ");
    String _join = IterableExtensions.join(errors, ", ");
    _builder_1.append(_join);
    Assert.assertTrue(_builder_1.toString(), errors.isEmpty());
    Resource _eResource = root.eResource();
    ICompositeNode rootNode = ((XtextResource) _eResource).getParseResult().getRootNode();
    StringConcatenation _builder_2 = new StringConcatenation();
    _builder_2.append("test sample {");
    _builder_2.newLine();
    _builder_2.newLine();
    _builder_2.append("// just a comment");
    _builder_2.newLine();
    _builder_2.append("}");
    final String expected = _builder_2.toString();
    INodeModelFormatter.IFormattedRegion formattedRegion = formatter.format(rootNode, rootNode.getTotalOffset(), rootNode.getTotalLength());
    Assert.assertEquals(expected, formattedRegion.getFormattedText());
    formattedRegion = formatter.format(rootNode, rootNode.getTotalOffset(), rootNode.getTotalLength());
    Assert.assertEquals(expected, formattedRegion.getFormattedText());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 6
Source Project: sarl   Source File: OperatorExtensions.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
private static ParseHelper<SarlScript> getParseHelper() {
	if (parser == null) {
		parser = (Provider) getInjector().getProvider(ParseHelper.class);
		loggerOff(ProcessorInstanceForJvmTypeProvider.class, "logger"); //$NON-NLS-1$
	}
	return parser.get();
}
 
Example 7
Source Project: sarl   Source File: ResourceSetGlobalCompilationContext.java    License: Apache License 2.0 5 votes vote down vote up
/** Constructor.
 *
 * @param basePackage the name of the package that is the root for all the resources.
 * @param injector the injector.
 * @param parser the SARL parser.
 * @param validator the validator.
 */
ResourceSetGlobalCompilationContext(String basePackage, Injector injector, ParseHelper<SarlScript> parser,
		SarlValidationTestHelper validator) {
	assert basePackage != null;
	assert injector != null;
	assert parser != null;
	assert validator != null;
	this.basePackage = basePackage;
	this.injector = injector;
	this.parser = parser;
	this.validator = validator;
}
 
Example 8
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 5 votes vote down vote up
/** Create an instance of class.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the content of the file.
 * @param resourceSet the set of resources in which the file is created.
 * @return the SARL script extracted from the file content.
 * @since 0.9
 */
public static SarlScript file(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper,
		String string, ResourceSet resourceSet) throws Exception {
	assert parser != null;
	SarlScript script;
	if (resourceSet == null) {
		script = parser.parse(string);
	} else {
		script = parser.parse(string, resourceSet);
	}
	if (validationHelper != null) {
		Resource resource = script.eResource();
		ResourceSet resourceSet0 = resource.getResourceSet();
		if (resourceSet0 instanceof XtextResourceSet) {
			((XtextResourceSet) resourceSet0).setClasspathURIContext(TestEObjects.class);
		}
		assertEquals(0, resource.getErrors().size(), () -> resource.getErrors().toString());
		Collection<Issue> issues = Collections2.filter(issues(validationHelper, resource), new Predicate<Issue>() {
			@Override
			public boolean apply(Issue input) {
				return input.getSeverity() == Severity.ERROR;
			}
		});
		assertTrue(issues.isEmpty(), () -> "Resource contained errors : " + issues.toString());
	}
	return script;
}
 
Example 9
Source Project: n4js   Source File: N4JSStandaloneTestsModule.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/** */
public Class<? extends ParseHelper<Script>> bindParseHelperScript() {
	return SmokeTestWriter.class;
}
 
Example 10
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of function.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the SARL function.
 */
public static SarlAction function(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper, String string, String... prefix) throws Exception {
	SarlClass clazz = clazz(parser, validationHelper,
			IterableExtensions.join(Arrays.asList(prefix), TestUtils.getLineSeparator())
			+ TestUtils.getLineSeparator() + "class Foo { " + string + "}");
	return (SarlAction) clazz.getMembers().get(0);
}
 
Example 11
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of JVM function.
 *
 * @param parser the SARL parser.
 * @param associations the provider of associations between the JVM and the SARL Ecores.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the JVM operation.
 */
public static JvmOperation jvmOperation(ParseHelper<SarlScript> parser, Provider<SarlJvmModelAssociations> associations,
		ValidationTestHelper validationHelper, String string, String... prefix) throws Exception {
	assert associations != null;
	SarlAction action = function(parser, validationHelper, string, prefix);
	return (JvmOperation) associations.get().getPrimaryJvmElement(action);
}
 
Example 12
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of function signature.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the SARL action.
 */
public static SarlAction functionSignature(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper,
		String string, String... prefix) throws Exception {
	SarlInterface interfaze = interfaze(parser, validationHelper,
			IterableExtensions.join(Arrays.asList(prefix), TestUtils.getLineSeparator())
			+ TestUtils.getLineSeparator() + "interface Foo { " + string + "}");
	return (SarlAction) interfaze.getMembers().get(0);
}
 
Example 13
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of JVM function.
 *
 * @param parser the SARL parser.
 * @param associations the provider of associations between the JVM and the SARL Ecores.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the JVM operation signature.
 */
public static JvmOperation jvmOperationSignature(ParseHelper<SarlScript> parser, Provider<SarlJvmModelAssociations> associations,
		ValidationTestHelper validationHelper, String string, String... prefix) throws Exception {
	assert associations != null;
	SarlAction action = functionSignature(parser, validationHelper, string, prefix);
	return (JvmOperation) associations.get().getPrimaryJvmElement(action);
}
 
Example 14
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of constructor.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the SARL constructor.
 */
public static SarlConstructor constructor(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper,
		String string, String... prefix) throws Exception {
	SarlClass clazz = clazz(parser, validationHelper,
			IterableExtensions.join(Arrays.asList(prefix), TestUtils.getLineSeparator())
			+ TestUtils.getLineSeparator() + "class Foo { " + string + "}");
	return (SarlConstructor) clazz.getMembers().get(0);
}
 
Example 15
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of JVM constructor.
 *
 * @param parser the SARL parser.
 * @param associations the provider of associations between the JVM and the SARL Ecores.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the JVM constructor.
 */
public static JvmConstructor jvmConstructor(ParseHelper<SarlScript> parser, Provider<SarlJvmModelAssociations> associations,
		ValidationTestHelper validationHelper, String string, String... prefix) throws Exception {
	assert associations != null;
	SarlConstructor constructor = constructor(parser, validationHelper, string, prefix);
	return (JvmConstructor) associations.get().getPrimaryJvmElement(constructor);
}
 
Example 16
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of field.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the SARL field.
 */
public static SarlField field(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper,
		String string, String... prefix) throws Exception {
	SarlClass clazz = clazz(parser, validationHelper,
			IterableExtensions.join(Arrays.asList(prefix), TestUtils.getLineSeparator())
			+ TestUtils.getLineSeparator() + "class Foo { " + string + "}");
	return (SarlField) clazz.getMembers().get(0);
}
 
Example 17
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create an instance of behavior unit.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the SARL behavior unit.
 */
public static SarlBehaviorUnit behaviorUnit(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper,
		String string, String... prefix) throws Exception {
	SarlAgent agent = agent(parser, validationHelper,
			IterableExtensions.join(Arrays.asList(prefix), TestUtils.getLineSeparator())
			+ TestUtils.getLineSeparator() + "agent Foo { " + string + "}");
	return (SarlBehaviorUnit) agent.getMembers().get(0);
}
 
Example 18
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 3 votes vote down vote up
/** Create a type reference with the SARL parser.
 *
 * @param parser the SARL parser.
 * @param typeName the fully qualified name of the type.
 * @param prefix the set of lines to put before the class declaration of the function.
 * @return the type reference.
 */
public static JvmTypeReference getType(ParseHelper<SarlScript> parser, String typeName, String... prefix) throws Exception {
	SarlAgent agent = agent(parser,
			IterableExtensions.join(Arrays.asList(prefix), TestUtils.getLineSeparator())
			+ TestUtils.getLineSeparator() + "agent Foo { var fooAttr : " + typeName + " }");
	return ((SarlField) agent.getMembers().get(0)).getType();
}
 
Example 19
Source Project: sarl   Source File: OperatorExtensions.java    License: Apache License 2.0 2 votes vote down vote up
/** Parse the given SARL code and replies the EMF tree.
 *
 * @param code the SARL code to parse and convert to EMF.
 * @return the EMF tree.
 * @throws Exception if the code cannot be parsed.
 */
private static SarlScript scriptToEMF(String code) throws Exception {
	final ParseHelper<SarlScript> parser = getParseHelper();
	return parser.parse(code);
}
 
Example 20
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of class.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the content of the file.
 * @return the SARL script extracted from the file content.
 */
public static SarlScript file(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper, String string) throws Exception {
	return file(parser, validationHelper, string, null);
}
 
Example 21
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create a SARL script.
 *
 * @param parser the SARL parser.
 * @param string the content of the file.
 * @return the SARL script extracted from the file content.
 */
public static SarlScript file(ParseHelper<SarlScript> parser, String string) throws Exception {
	return file(parser, null, string);
}
 
Example 22
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of agent.
 *
 * @param parser the SARL parser.
 * @param string the file content to parse.
 * @return the SARL agent.
 */
public static SarlAgent agent(ParseHelper<SarlScript> parser, String string) throws Exception {
	return agent(parser, null, string);
}
 
Example 23
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of agent.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @return the SARL agent.
 */
public static SarlAgent agent(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper, String string) throws Exception {
	List<XtendTypeDeclaration> decls = file(parser, validationHelper, string).getXtendTypes();
	return (SarlAgent) decls.get(decls.size() - 1);
}
 
Example 24
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of capacity.
 *
 * @param parser the SARL parser.
 * @param string the file content to parse.
 * @return the SARL capacity.
 */
public static SarlCapacity capacity(ParseHelper<SarlScript> parser, String string) throws Exception {
	return capacity(parser, null, string);
}
 
Example 25
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of capacity.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @return the SARL capacity.
 */
public static SarlCapacity capacity(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper, String string) throws Exception {
	List<XtendTypeDeclaration> decls = file(parser, validationHelper, string).getXtendTypes();
	return (SarlCapacity) decls.get(decls.size() - 1);
}
 
Example 26
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of event.
 *
 * @param parser the SARL parser.
 * @param string the file content to parse.
 * @return the SARL event.
 */
public static SarlEvent event(ParseHelper<SarlScript> parser, String string) throws Exception {
	return event(parser, null, string);
}
 
Example 27
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of event.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @return the SARL event.
 */
public static SarlEvent event(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper, String string) throws Exception {
	List<XtendTypeDeclaration> decls = file(parser, validationHelper, string).getXtendTypes();
	return (SarlEvent) decls.get(decls.size() - 1);
}
 
Example 28
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of skill.
 *
 * @param parser the SARL parser.
 * @param string the file content to parse.
 * @return the SARL skill.
 */
public static SarlSkill skill(ParseHelper<SarlScript> parser, String string) throws Exception {
	return skill(parser, null, string);
}
 
Example 29
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of skill.
 *
 * @param parser the SARL parser.
 * @param validationHelper the validation test helper. If it is {@code null}, no validation.
 * @param string the file content to parse.
 * @return the SARL skill.
 */
public static SarlSkill skill(ParseHelper<SarlScript> parser, ValidationTestHelper validationHelper, String string) throws Exception {
	List<XtendTypeDeclaration> decls = file(parser, validationHelper, string).getXtendTypes();
	return (SarlSkill) decls.get(decls.size() - 1);
}
 
Example 30
Source Project: sarl   Source File: TestEObjects.java    License: Apache License 2.0 2 votes vote down vote up
/** Create an instance of behavior.
 *
 * @param parser the SARL parser.
 * @param string the file content to parse.
 * @return the SARL behavior.
 */
public static SarlBehavior behavior(ParseHelper<SarlScript> parser, String string) throws Exception {
	return behavior(parser, null, string);
}