diff --git a/plugins/io.sarl.lang/src/io/sarl/lang/genmodel/SARLCodeGenerator.java b/plugins/io.sarl.lang/src/io/sarl/lang/genmodel/SARLCodeGenerator.java index dca1404646..03ee99de41 100644 --- a/plugins/io.sarl.lang/src/io/sarl/lang/genmodel/SARLCodeGenerator.java +++ b/plugins/io.sarl.lang/src/io/sarl/lang/genmodel/SARLCodeGenerator.java @@ -21,6 +21,9 @@ package io.sarl.lang.genmodel; +import io.sarl.lang.annotation.DefaultValue; +import io.sarl.lang.annotation.FiredEvent; +import io.sarl.lang.annotation.Generated; import io.sarl.lang.sarl.Action; import io.sarl.lang.sarl.ActionSignature; import io.sarl.lang.sarl.Agent; @@ -40,6 +43,7 @@ import io.sarl.lang.util.ModelUtil; import java.util.Collection; +import java.util.List; import javax.inject.Named; @@ -50,7 +54,12 @@ import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.xtext.Constants; +import org.eclipse.xtext.common.types.JvmConstructor; import org.eclipse.xtext.common.types.JvmDeclaredType; +import org.eclipse.xtext.common.types.JvmField; +import org.eclipse.xtext.common.types.JvmFormalParameter; +import org.eclipse.xtext.common.types.JvmGenericArrayTypeReference; +import org.eclipse.xtext.common.types.JvmOperation; import org.eclipse.xtext.common.types.JvmParameterizedTypeReference; import org.eclipse.xtext.common.types.JvmType; import org.eclipse.xtext.common.types.JvmTypeReference; @@ -66,6 +75,8 @@ import org.eclipse.xtext.xbase.XbaseFactory; import org.eclipse.xtext.xbase.compiler.DocumentationAdapter; import org.eclipse.xtext.xbase.compiler.ImportManager; +import org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator; +import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder; import org.eclipse.xtext.xtype.XImportDeclaration; import org.eclipse.xtext.xtype.XImportSection; import org.eclipse.xtext.xtype.XtypeFactory; @@ -90,12 +101,18 @@ public class SARLCodeGenerator { @Inject private TypeReferences typeReferences; + @Inject + private JvmTypesBuilder typesBuilder; + @Inject private IResourceFactory resourceFactory; @Inject private ActionSignatureProvider actionSignatureProvider; + @Inject + private JvmModelAssociator jvmModelAssociator; + private final String sarlFileExtension; /** @@ -130,6 +147,14 @@ public TypesFactory getTypesFactory() { return this.typeFactory; } + /** Replies the type builder. + * + * @return the type builder. + */ + public JvmTypesBuilder getTypesBuilder() { + return this.typesBuilder; + } + /** Replies the factory of resources. * * @return the factory of resources. @@ -483,18 +508,17 @@ public FormalParameter createFormalParameter(GeneratedCode code, ParameterizedFe throw new IllegalArgumentException("the parameter 'type' must contains a valid type"); //$NON-NLS-1$ } return createFormalParameter(code, container, name, type, - createXExpression(code, defaultValue, resourceSet)); + createXExpression(defaultValue, resourceSet)); } /** Create an expression. * - * @param code - the generated code in which the agent must be created. * @param expression - the texutal representation of the expression. * @param resourceSet - the set of resources in which this function could * create temporary resources for compiling the default value. * @return the SARL formal parameter. */ - public XExpression createXExpression(GeneratedCode code, String expression, ResourceSet resourceSet) { + public XExpression createXExpression(String expression, ResourceSet resourceSet) { XExpression xExpression = null; if (!Strings.isNullOrEmpty(expression)) { URI uri = computeUnusedUri(resourceSet); @@ -701,6 +725,188 @@ public JvmParameterizedTypeReference newTypeRef(GeneratedCode code, String typeN return reference; } + private JvmTypeReference cloneType(JvmTypeReference type) { + // CAUTION: The following line is needed otherwise the clone of the type will failed. + type.getIdentifier(); + return getTypesBuilder().cloneWithProxies(type); + } + + /** Replies the SARL Ecore equivalent for the givne JVM Ecore element. + * + * @param operation - the JVM Ecore element. + * @param exploreAssociatedModel - indicates if the associated model (supported + * by {@link JvmModelAssociator}) should be considered. + * @return the SARL Ecore element. + */ + public Action createAction(JvmOperation operation, boolean exploreAssociatedModel) { + if (exploreAssociatedModel) { + EObject o = this.jvmModelAssociator.getPrimarySourceElement(operation); + if (o instanceof Action) { + return (Action) o; + } + } + Action action = SarlFactory.eINSTANCE.createAction(); + // Name + action.setName(operation.getSimpleName()); + // Return types + JvmTypeReference typeReference = cloneType(operation.getReturnType()); + action.setType(typeReference); + // Parameters + action.setVarargs(operation.isVarArgs()); + List parameters = action.getParams(); + List jvmParameters = operation.getParameters(); + for (int i = 0; i < jvmParameters.size(); ++i) { + JvmFormalParameter jvmParameter = jvmParameters.get(i); + FormalParameter parameter = SarlFactory.eINSTANCE.createFormalParameter(); + parameter.setName(jvmParameter.getSimpleName()); + typeReference = jvmParameter.getParameterType(); + if (i == jvmParameters.size() - 1 && operation.isVarArgs()) { + typeReference = ((JvmGenericArrayTypeReference) typeReference).getComponentType(); + } + typeReference = cloneType(typeReference); + parameter.setParameterType(typeReference); + // Default values + String defaultValue = findDefaultValue( + operation.getDeclaringType(), + ModelUtil.annotationString(jvmParameter, DefaultValue.class)); + if (!Strings.isNullOrEmpty(defaultValue)) { + XExpression defaultValueExpr = createXExpression( + defaultValue, + operation.eResource().getResourceSet()); + parameter.setDefaultValue(defaultValueExpr); + } + parameters.add(parameter); + } + // Fired events + List firedEvents = ModelUtil.annotationClasses(operation, FiredEvent.class); + if (!firedEvents.isEmpty()) { + List events = action.getFiredEvents(); + for (JvmTypeReference type : firedEvents) { + JvmTypeReference clone = cloneType(type); + if (clone instanceof JvmParameterizedTypeReference) { + events.add((JvmParameterizedTypeReference) clone); + } + } + } + return action; + } + + /** Replies the SARL Ecore equivalent for the givne JVM Ecore element. + * + * @param operation - the JVM Ecore element. + * @param exploreAssociatedModel - indicates if the associated model (supported + * by {@link JvmModelAssociator}) should be considered. + * @return the SARL Ecore element. + */ + public ActionSignature createActionSignature(JvmOperation operation, boolean exploreAssociatedModel) { + if (exploreAssociatedModel) { + EObject o = this.jvmModelAssociator.getPrimarySourceElement(operation); + if (o instanceof ActionSignature) { + return (ActionSignature) o; + } + } + ActionSignature signature = SarlFactory.eINSTANCE.createActionSignature(); + // Name + signature.setName(operation.getSimpleName()); + // Return types + JvmTypeReference typeReference = cloneType(operation.getReturnType()); + signature.setType(typeReference); + // Parameters + signature.setVarargs(operation.isVarArgs()); + List parameters = signature.getParams(); + List jvmParameters = operation.getParameters(); + for (int i = 0; i < jvmParameters.size(); ++i) { + JvmFormalParameter jvmParameter = jvmParameters.get(i); + FormalParameter parameter = SarlFactory.eINSTANCE.createFormalParameter(); + parameter.setName(jvmParameter.getSimpleName()); + typeReference = jvmParameter.getParameterType(); + if (i == jvmParameters.size() - 1 && operation.isVarArgs()) { + typeReference = ((JvmGenericArrayTypeReference) typeReference).getComponentType(); + } + typeReference = cloneType(typeReference); + parameter.setParameterType(typeReference); + // Default values + String defaultValue = findDefaultValue( + operation.getDeclaringType(), + ModelUtil.annotationString(jvmParameter, DefaultValue.class)); + if (!Strings.isNullOrEmpty(defaultValue)) { + XExpression defaultValueExpr = createXExpression( + defaultValue, + operation.eResource().getResourceSet()); + parameter.setDefaultValue(defaultValueExpr); + } + parameters.add(parameter); + } + // Fired events + List firedEvents = ModelUtil.annotationClasses(operation, FiredEvent.class); + if (!firedEvents.isEmpty()) { + List events = signature.getFiredEvents(); + for (JvmTypeReference type : firedEvents) { + JvmTypeReference clone = cloneType(type); + if (clone instanceof JvmParameterizedTypeReference) { + events.add((JvmParameterizedTypeReference) clone); + } + } + } + return signature; + } + + /** Replies the SARL Ecore equivalent for the givne JVM Ecore element. + * + * @param constructor - the JVM Ecore element. + * @param exploreAssociatedModel - indicates if the associated model (supported + * by {@link JvmModelAssociator}) should be considered. + * @return the SARL Ecore element. + */ + public Constructor createConstructor(JvmConstructor constructor, boolean exploreAssociatedModel) { + if (exploreAssociatedModel) { + EObject o = this.jvmModelAssociator.getPrimarySourceElement(constructor); + if (o instanceof Constructor) { + return (Constructor) o; + } + } + Constructor cons = SarlFactory.eINSTANCE.createConstructor(); + // Parameters + cons.setVarargs(constructor.isVarArgs()); + List parameters = cons.getParams(); + List jvmParameters = constructor.getParameters(); + for (int i = 0; i < jvmParameters.size(); ++i) { + JvmFormalParameter jvmParameter = jvmParameters.get(i); + FormalParameter parameter = SarlFactory.eINSTANCE.createFormalParameter(); + parameter.setName(jvmParameter.getSimpleName()); + JvmTypeReference typeReference = jvmParameter.getParameterType(); + if (i == jvmParameters.size() - 1 && constructor.isVarArgs()) { + typeReference = ((JvmGenericArrayTypeReference) typeReference).getComponentType(); + } + typeReference = cloneType(typeReference); + parameter.setParameterType(typeReference); + // Default values + String defaultValue = findDefaultValue( + constructor.getDeclaringType(), + ModelUtil.annotationString(jvmParameter, DefaultValue.class)); + if (!Strings.isNullOrEmpty(defaultValue)) { + XExpression defaultValueExpr = createXExpression( + defaultValue, + constructor.eResource().getResourceSet()); + parameter.setDefaultValue(defaultValueExpr); + } + parameters.add(parameter); + } + return cons; + } + + private static String findDefaultValue(JvmDeclaredType container, String name) { + if (!Strings.isNullOrEmpty(name)) { + String dfName = ModelUtil.PREFIX_ATTRIBUTE_DEFAULT_VALUE + name; + for (JvmField field : container.getDeclaredFields()) { + if (field.getSimpleName().equals(dfName)) { + return ModelUtil.annotationString(field, Generated.class); + } + } + } + return null; + } + /** Describes a generated code. * * @author $Author: sgalland$ diff --git a/plugins/io.sarl.lang/src/io/sarl/lang/util/ModelUtil.java b/plugins/io.sarl.lang/src/io/sarl/lang/util/ModelUtil.java index 9750bbce60..8bec4518da 100644 --- a/plugins/io.sarl.lang/src/io/sarl/lang/util/ModelUtil.java +++ b/plugins/io.sarl.lang/src/io/sarl/lang/util/ModelUtil.java @@ -20,6 +20,7 @@ */ package io.sarl.lang.util; +import io.sarl.lang.genmodel.SARLCodeGenerator; import io.sarl.lang.signature.ActionKey; import io.sarl.lang.signature.ActionSignatureProvider; import io.sarl.lang.signature.SignatureKey; @@ -587,7 +588,9 @@ public static int compareVersions(String v1, String v2) { * * @param e - the executable for which a strig representation must be replied. * @return the prototype + * @deprecated use {@link SARLCodeGenerator}. */ + @Deprecated public static String toActionProtoptypeString(JvmExecutable e) { StringBuilder b = new StringBuilder(); JvmTypeReference returnType; diff --git a/plugins/io.sarl.lang/src/io/sarl/lang/validation/SARLValidator.java b/plugins/io.sarl.lang/src/io/sarl/lang/validation/SARLValidator.java index ba65dca65b..a8f21ceafc 100644 --- a/plugins/io.sarl.lang/src/io/sarl/lang/validation/SARLValidator.java +++ b/plugins/io.sarl.lang/src/io/sarl/lang/validation/SARLValidator.java @@ -130,7 +130,7 @@ public class SARLValidator extends AbstractSARLValidator { @Inject private SARLGrammarAccess grammarAccess; - + /** Replies the canonical name of the given type. * * @param typeRef - the type. diff --git a/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/genmodel/SARLCodeGeneratorTest.java b/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/genmodel/SARLCodeGeneratorTest.java index 37bf5a86d4..abd1d9c0e4 100644 --- a/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/genmodel/SARLCodeGeneratorTest.java +++ b/tests/io.sarl.lang.tests/src/io/sarl/lang/tests/genmodel/SARLCodeGeneratorTest.java @@ -15,8 +15,17 @@ */ package io.sarl.lang.tests.genmodel; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import io.sarl.lang.SARLInjectorProvider; import io.sarl.lang.genmodel.SARLCodeGenerator; import io.sarl.lang.genmodel.SARLCodeGenerator.BlockInnerDocumentationAdapter; @@ -31,6 +40,7 @@ import io.sarl.lang.sarl.Capacity; import io.sarl.lang.sarl.Constructor; import io.sarl.lang.sarl.Event; +import io.sarl.lang.sarl.FeatureContainer; import io.sarl.lang.sarl.FormalParameter; import io.sarl.lang.sarl.ParameterizedFeature; import io.sarl.lang.sarl.SarlScript; @@ -38,6 +48,7 @@ import io.sarl.lang.sarl.TopElement; import io.sarl.lang.signature.ActionSignatureProvider; import io.sarl.tests.api.AbstractSarlTest; +import io.sarl.tests.api.AbstractSarlUiTest; import io.sarl.tests.api.Nullable; import java.util.Arrays; @@ -52,9 +63,11 @@ import org.eclipse.emf.ecore.impl.EObjectImpl; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.xtext.common.types.JvmConstructor; +import org.eclipse.xtext.common.types.JvmExecutable; +import org.eclipse.xtext.common.types.JvmOperation; import org.eclipse.xtext.common.types.JvmParameterizedTypeReference; import org.eclipse.xtext.common.types.JvmType; -import org.eclipse.xtext.common.types.JvmTypeReference; import org.eclipse.xtext.common.types.TypesFactory; import org.eclipse.xtext.common.types.access.IJvmTypeProvider; import org.eclipse.xtext.common.types.util.TypeReferences; @@ -70,6 +83,7 @@ import org.eclipse.xtext.xbase.compiler.DocumentationAdapter; import org.eclipse.xtext.xbase.compiler.ImportManager; import org.eclipse.xtext.xbase.impl.XBlockExpressionImpl; +import org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -80,7 +94,6 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import com.google.common.collect.Iterables; import com.google.inject.Inject; /** @@ -107,7 +120,10 @@ SARLCodeGeneratorTest.EventFeatures.class, SARLCodeGeneratorTest.SkillFeatures.class, SARLCodeGeneratorTest.Expressions.class, - SARLCodeGeneratorTest.FormalParameters.class + SARLCodeGeneratorTest.FormalParameters.class, + SARLCodeGeneratorTest.CreateActionFromJvmElement.class, + SARLCodeGeneratorTest.CreateConstructorFromJvmElement.class, + SARLCodeGeneratorTest.CreateActionSignatureFromJvmElement.class }) @SuppressWarnings("all") public class SARLCodeGeneratorTest { @@ -1125,7 +1141,7 @@ public JvmType answer(InvocationOnMock it) throws Throwable { } @Test - public void createAction_returnNull() { + public void returnNull() { Action action = gen.createAction(code, agent, "myFct", null, block); // assertNotNull(action); @@ -1136,7 +1152,7 @@ public void createAction_returnNull() { } @Test - public void createAction_returnBoolean() { + public void returnBoolean() { Action action = gen.createAction(code, agent, "myFct", "boolean", block); // assertNotNull(action); @@ -1147,7 +1163,7 @@ public void createAction_returnBoolean() { } @Test - public void createAction_returnObject() { + public void returnObject() { Action action = gen.createAction(code, agent, "myFct", "java.lang.String", block); // assertNotNull(action); @@ -1325,7 +1341,7 @@ public JvmType answer(InvocationOnMock it) throws Throwable { } @Test - public void createAction_returnNull() { + public void returnNull() { Action action = gen.createAction(code, behavior, "myFct", null, block); // assertNotNull(action); @@ -1336,7 +1352,7 @@ public void createAction_returnNull() { } @Test - public void createAction_returnBoolean() { + public void returnBoolean() { Action action = gen.createAction(code, behavior, "myFct", "boolean", block); // assertNotNull(action); @@ -1347,7 +1363,7 @@ public void createAction_returnBoolean() { } @Test - public void createAction_returnObject() { + public void returnObject() { Action action = gen.createAction(code, behavior, "myFct", "java.lang.String", block); // assertNotNull(action); @@ -1521,7 +1537,7 @@ public JvmType answer(InvocationOnMock it) throws Throwable { } @Test - public void createActionSignature_returnNull() { + public void returnNull() { ActionSignature action = gen.createActionSignature(code, capacity, "myFct", null); // assertNotNull(action); @@ -1531,7 +1547,7 @@ public void createActionSignature_returnNull() { } @Test - public void createActionSignature_returnBoolean() { + public void returnBoolean() { ActionSignature action = gen.createActionSignature(code, capacity, "myFct", "boolean"); // assertNotNull(action); @@ -1541,7 +1557,7 @@ public void createActionSignature_returnBoolean() { } @Test - public void createActionSignature_returnObject() { + public void returnObject() { ActionSignature action = gen.createActionSignature(code, capacity, "myFct", "java.lang.String"); // assertNotNull(action); @@ -1763,7 +1779,7 @@ public JvmType answer(InvocationOnMock it) throws Throwable { } @Test - public void createAction_returnNull() { + public void returnNull() { Action action = gen.createAction(code, skill, "myFct", null, block); // assertNotNull(action); @@ -1774,7 +1790,7 @@ public void createAction_returnNull() { } @Test - public void createAction_returnBoolean() { + public void returnBoolean() { Action action = gen.createAction(code, skill, "myFct", "boolean", block); // assertNotNull(action); @@ -1785,7 +1801,7 @@ public void createAction_returnBoolean() { } @Test - public void createAction_returnObject() { + public void returnObject() { Action action = gen.createAction(code, skill, "myFct", "java.lang.String", block); // assertNotNull(action); @@ -1927,14 +1943,14 @@ public JvmType answer(InvocationOnMock it) throws Throwable { @Test public void createXExpression_null() { - XExpression expr = gen.createXExpression(code, null, eResourceSet); + XExpression expr = gen.createXExpression(null, eResourceSet); // assertNull(expr); } @Test public void createXExpression_empty() { - XExpression expr = gen.createXExpression(code, "", eResourceSet); + XExpression expr = gen.createXExpression("", eResourceSet); // assertNull(expr); } @@ -2090,4 +2106,1309 @@ public void createFormalParameterGeneratedCodeParameterizedFeatureStringStringXE } + @InjectWith(SARLInjectorProvider.class) + public static class CreateActionFromJvmElement extends AbstractSarlUiTest { + + @Inject + private SARLCodeGenerator generator; + + /** Associator of the JVM elements and the SARL elements. + */ + @Inject + protected JvmModelAssociator jvmModelAssociator; + + private JvmOperation createJvmFeature(String... sarlCode) throws Exception { + String sarlFilename = generateFilename(); + SarlScript sarlScript = this.helper.createSARLScript(sarlFilename, multilineString(sarlCode)); + this.helper.waitForAutoBuild(); + EObject feature = ((FeatureContainer) sarlScript.getElements().get(0)).getFeatures().get(0); + EObject jvmElement = this.jvmModelAssociator.getPrimaryJvmElement(feature); + return (JvmOperation) jvmElement; + } + + @Test + public void noParam_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "agent A1 {", + " def fct {}", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int*) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char*) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void noParam_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "agent A1 {", + " def fct : String { null }", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int) : String { null }", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int*) : String { null }", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char) : String { null }", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char*) : String { null }", + "}"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void noParam_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "agent A1 {", + " def fct fires MyEvent {}", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int) fires MyEvent {}", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int*) fires MyEvent {}", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char) fires MyEvent {}", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char*) fires MyEvent {}", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void noParam_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "agent A1 {", + " def fct : String fires MyEvent { null }", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int) : String fires MyEvent { null }", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int*) : String fires MyEvent { null }", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char) : String fires MyEvent { null }", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "agent A1 {", + " def fct(a : URL, b : int=4, c : char*) : String fires MyEvent { null }", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createAction(operation, false); + // + assertNotNull(feature); + Action action = (Action) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + } + + @InjectWith(SARLInjectorProvider.class) + public static class CreateConstructorFromJvmElement extends AbstractSarlUiTest { + + @Inject + private SARLCodeGenerator generator; + + /** Associator of the JVM elements and the SARL elements. + */ + @Inject + protected JvmModelAssociator jvmModelAssociator; + + private JvmConstructor createJvmFeature(String... sarlCode) throws Exception { + String sarlFilename = generateFilename(); + SarlScript sarlScript = this.helper.createSARLScript(sarlFilename, multilineString(sarlCode)); + this.helper.waitForAutoBuild(); + EObject feature = ((FeatureContainer) sarlScript.getElements().get(0)).getFeatures().get(0); + EObject jvmElement = this.jvmModelAssociator.getPrimaryJvmElement(feature); + return (JvmConstructor) jvmElement; + } + + @Test + public void noParam() throws Exception { + JvmConstructor cons = createJvmFeature( + // Code + "event E1 {", + " new() {}", + "}"); + // + ParameterizedFeature feature = this.generator.createConstructor(cons, false); + // + assertNotNull(feature); + Constructor action = (Constructor) feature; + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam() throws Exception { + JvmConstructor cons = createJvmFeature( + // Code + "import java.net.URL", + "event E1 {", + " new (a : URL, b : int) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createConstructor(cons, false); + // + assertNotNull(feature); + Constructor action = (Constructor) feature; + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam() throws Exception { + JvmConstructor cons = createJvmFeature( + // Code + "import java.net.URL", + "event E1 {", + " new (a : URL, b : int*) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createConstructor(cons, false); + // + assertNotNull(feature); + Constructor action = (Constructor) feature; + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue() throws Exception { + JvmConstructor cons = createJvmFeature( + // Code + "import java.net.URL", + "event E1 {", + " new (a : URL, b : int=4, c : char) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createConstructor(cons, false); + // + assertNotNull(feature); + Constructor action = (Constructor) feature; + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue() throws Exception { + JvmConstructor cons = createJvmFeature( + // Code + "import java.net.URL", + "event E1 {", + " new (a : URL, b : int=4, c : char*) {}", + "}"); + // + ParameterizedFeature feature = this.generator.createConstructor(cons, false); + // + assertNotNull(feature); + Constructor action = (Constructor) feature; + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + } + + @InjectWith(SARLInjectorProvider.class) + public static class CreateActionSignatureFromJvmElement extends AbstractSarlUiTest { + + @Inject + private SARLCodeGenerator generator; + + /** Associator of the JVM elements and the SARL elements. + */ + @Inject + protected JvmModelAssociator jvmModelAssociator; + + private JvmOperation createJvmFeature(String... sarlCode) throws Exception { + String sarlFilename = generateFilename(); + SarlScript sarlScript = this.helper.createSARLScript(sarlFilename, multilineString(sarlCode)); + this.helper.waitForAutoBuild(); + EObject feature = ((FeatureContainer) sarlScript.getElements().get(0)).getFeatures().get(0); + EObject jvmElement = this.jvmModelAssociator.getPrimaryJvmElement(feature); + return (JvmOperation) jvmElement; + } + + @Test + public void noParam_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "capacity C1 {", + " def fct", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int)", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int*)", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char)", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_noReturn() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char*)", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void noParam_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "capacity C1 {", + " def fct : String", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int) : String", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int*) : String", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char) : String", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_returnValue() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char*) : String", + "}"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(0, action.getFiredEvents().size()); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void noParam_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "capacity C1 {", + " def fct fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int) fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int*) fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char) fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_noReturn_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char*) fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "void"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void noParam_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "capacity C1 {", + " def fct : String fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(0, action.getParams().size()); + } + + @Test + public void stdParam_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int) : String fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void variadicParam_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int*) : String fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(2, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b"); + assertParameterTypes(action.getParams(), "java.net.URL", "int"); + assertParameterDefaultValues(action.getParams(), + null, + null); + } + + @Test + public void defaultValue_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char) : String fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertFalse(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + @Test + public void variadicParam_defaultValue_returnValue_fireEvents() throws Exception { + JvmOperation operation = createJvmFeature( + // Code + "import java.net.URL", + "capacity C1 {", + " def fct(a : URL, b : int=4, c : char*) : String fires MyEvent", + "}", + "event MyEvent"); + // + ParameterizedFeature feature = this.generator.createActionSignature(operation, false); + // + assertNotNull(feature); + ActionSignature action = (ActionSignature) feature; + assertEquals("fct", action.getName()); + assertTypeReferenceIdentifier(action.getType(), "java.lang.String"); + // + assertEquals(1, action.getFiredEvents().size()); + assertTypeReferenceIdentifiers( + action.getFiredEvents(), + "MyEvent"); + // + assertTrue(action.isVarargs()); + assertEquals(3, action.getParams().size()); + assertParameterNames(action.getParams(), "a", "b", "c"); + assertParameterTypes(action.getParams(), "java.net.URL", "int", "char"); + assertParameterDefaultValues(action.getParams(), + null, + XNumberLiteral.class, "4", + null); + } + + } + } diff --git a/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/highlighting/AbstractSARLHighlightingCalculatorTest.java b/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/highlighting/AbstractSARLHighlightingCalculatorTest.java index ab14b0d93e..f7302cb0d1 100644 --- a/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/highlighting/AbstractSARLHighlightingCalculatorTest.java +++ b/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/highlighting/AbstractSARLHighlightingCalculatorTest.java @@ -64,27 +64,6 @@ public void setUp() throws Exception { this.helper.waitForAutoBuild(); } - /** Generate a filename for a resource that does not exist yet. - * - * @return the filename. - */ - private String generateFilename() { - int filenameCounter = 0; - String oFilename = pathStr( - "io", "sarl", //$NON-NLS-1$//$NON-NLS-2$ - "lang", "ui", //$NON-NLS-1$//$NON-NLS-2$ - "tests", "highlighting", //$NON-NLS-1$//$NON-NLS-2$ - "highlighting_sarl"); //$NON-NLS-1$ - String filename = oFilename; - boolean foundFile = this.helper.isFileInSourceFolder(filename + ".sarl"); //$NON-NLS-1$ - while (foundFile) { - ++filenameCounter; - filename = oFilename + Integer.toString(filenameCounter); - foundFile = this.helper.isFileInSourceFolder(filename + ".sarl"); //$NON-NLS-1$ - } - return filename + ".sarl"; //$NON-NLS-1$ - } - /** Highlight the given text. * * @param code - the SARL script. diff --git a/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/outline/AbstractSARLOutlineTreeProviderTest.java b/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/outline/AbstractSARLOutlineTreeProviderTest.java index a8d8eb6fbd..dd532ea93b 100644 --- a/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/outline/AbstractSARLOutlineTreeProviderTest.java +++ b/tests/io.sarl.lang.ui.tests/src/io/sarl/lang/ui/tests/outline/AbstractSARLOutlineTreeProviderTest.java @@ -84,27 +84,6 @@ protected OutlineFilterAndSorter getSorter() { return this.sorter; } - /** Generate a filename for a resource that does not exist yet. - * - * @return the filename. - */ - protected String generateFilename() { - int filenameCounter = 0; - String oFilename = pathStr( - "io", "sarl", //$NON-NLS-1$//$NON-NLS-2$ - "lang", "ui", //$NON-NLS-1$//$NON-NLS-2$ - "tests", "outline", //$NON-NLS-1$//$NON-NLS-2$ - "fixing_sarl_outline"); //$NON-NLS-1$ - String filename = oFilename; - boolean foundFile = this.helper.isFileInSourceFolder(filename + ".sarl"); //$NON-NLS-1$ - while (foundFile) { - ++filenameCounter; - filename = oFilename + Integer.toString(filenameCounter); - foundFile = this.helper.isFileInSourceFolder(filename + ".sarl"); //$NON-NLS-1$ - } - return filename; - } - /** Create a new assertion tool on a specific code resource. * The code will be used for generating the outline. * diff --git a/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlTest.java b/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlTest.java index 97b5ef1e1f..a295b5d15e 100644 --- a/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlTest.java +++ b/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlTest.java @@ -32,6 +32,7 @@ import java.util.Iterator; import java.util.List; +import org.eclipse.xtext.common.types.JvmFormalParameter; import org.eclipse.xtext.common.types.JvmTypeReference; import org.eclipse.xtext.xbase.XExpression; import org.eclipse.xtext.xbase.XNullLiteral; @@ -370,8 +371,7 @@ public static void assertTypeReferenceIdentifier(JvmTypeReference actualReferenc assertEquals("void", expectedIdentifier); return; } - assertEquals("Unexpected type reference: " + actualReference.getIdentifier() + ". Expected: " + expectedIdentifier, - expectedIdentifier, actualReference.getIdentifier()); + assertEquals("Unexpected type reference", expectedIdentifier, actualReference.getIdentifier()); } /** Assert that the given actual formal parameters have the expected names. diff --git a/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlUiTest.java b/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlUiTest.java index 7bfc51f0f7..6487568d6f 100644 --- a/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlUiTest.java +++ b/tests/io.sarl.tests.api/src/io/sarl/tests/api/AbstractSarlUiTest.java @@ -197,5 +197,32 @@ protected static void assertJdtImage(ImageDescriptor expected, int expectedFlags actual.hashCode()); assertEquals(expectedFlags, ((JavaElementImageDescriptor) actual).getAdronments()); } + + /** Generate a filename for a resource that does not exist yet. + * + * @param pathElements - the elements of the path (directories and basename), without the extension. + * @return the filename. + */ + protected String generateFilename(String... pathElements) { + int filenameCounter = 0; + String oFilename = pathStr(pathElements); + String filename = oFilename; + boolean foundFile = this.helper.isFileInSourceFolder(filename + ".sarl"); //$NON-NLS-1$ + while (foundFile) { + ++filenameCounter; + filename = oFilename + Integer.toString(filenameCounter); + foundFile = this.helper.isFileInSourceFolder(filename + ".sarl"); //$NON-NLS-1$ + } + return filename; + } + + /** Generate a filename for a resource that does not exist yet. + * + * @return the filename. + */ + protected String generateFilename() { + return generateFilename( + "io", "sarl", "tests", "basename"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + } } diff --git a/tests/io.sarl.tests.api/src/io/sarl/tests/api/WorkspaceTestHelper.java b/tests/io.sarl.tests.api/src/io/sarl/tests/api/WorkspaceTestHelper.java index e35de7feb0..974fcb703a 100644 --- a/tests/io.sarl.tests.api/src/io/sarl/tests/api/WorkspaceTestHelper.java +++ b/tests/io.sarl.tests.api/src/io/sarl/tests/api/WorkspaceTestHelper.java @@ -104,7 +104,7 @@ public class WorkspaceTestHelper extends Assert { /** Relative path of the generated source folder. */ - public static String GENERATED_SOURCE_FOLDER = "src-gen"; //$NON-NLS-1$ + public static String GENERATED_SOURCE_FOLDER= "src-gen"; //$NON-NLS-1$ private static boolean IS_WAITING_FOR_BUILD_AT_FILE_CREATION = false; @@ -426,6 +426,16 @@ public boolean isFileInSourceFolder(String basename) { return this.workspace.getRoot().exists(new Path(fullFileName)); } + /** Replies if a file exists in the generated source folder. + * + * @param basename - the basename of the file in the source folder. + * @return true if the file exists, otherwise false. + */ + public boolean isFileInGeneratedSourceFolder(String basename) { + String fullFileName = convertGeneratedBasenameToWorkspace(getProject(), basename); + return this.workspace.getRoot().exists(new Path(fullFileName)); + } + /** Replies a file in the source folder. * * @param basename - the basename of the file. @@ -435,6 +445,15 @@ public IFile getFileInSourceFolder(String basename) { return this.workspace.getRoot().getFile(new Path(convertBasenameToWorkspace(getProject(), basename))); } + /** Replies a file in the generated source folder. + * + * @param basename - the basename of the file. + * @return the filename relative to the workspace directory. + */ + public IFile getFileInGeneratedSourceFolder(String basename) { + return this.workspace.getRoot().getFile(new Path(convertGeneratedBasenameToWorkspace(getProject(), basename))); + } + /** Replies a file in the workspace. * * @param filename - the filename relative to the workspace directory. @@ -450,13 +469,50 @@ public IFile getFileInWorkspace(String filename) { * @param basename - the filename without extension relative to the source folder. * @return the filename relative to the workspace. */ - protected String convertBasenameToWorkspace(IProject project, String basename) { + public String convertBasenameToWorkspace(IProject project, String basename) { String extension = (basename.indexOf(".") != -1) ? "" : "." + getFileExtension(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ String fullFileName = project.getName() + File.separator + SOURCE_FOLDER + File.separator + basename + extension; return fullFileName; } + /** Replies the basename from the given full name. + * + * @param fullName - the full name. + * @param removeExtension - indicates if the file extension must be removed from the basename. + * @return the basename. + */ + @SuppressWarnings("static-method") + public String getBasenameFrom(String fullName, boolean removeExtension) { + String basename; + int idx = fullName.lastIndexOf(File.separator); + if (idx >= 0) { + basename = fullName.substring(idx + 1); + } else { + basename = fullName; + } + if (removeExtension) { + idx = basename.indexOf('.'); + if (idx >= 0) { + basename = basename.substring(0, idx); + } + } + return basename; + } + + /** Convert a filename from the generated source folder to the workspace. + * + * @param project - project that is containing the source folder. + * @param basename - the filename without extension relative to the source folder. + * @return the filename relative to the workspace. + */ + public String convertGeneratedBasenameToWorkspace(IProject project, String basename) { + String extension = (basename.indexOf(".") != -1) ? "" : "." + getFileExtension(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + String fullFileName = project.getName() + File.separator + GENERATED_SOURCE_FOLDER + + File.separator + basename + extension; + return fullFileName; + } + /** Replies the file extension for a SARL script. * * @return the file extension for a SARL script. @@ -470,9 +526,18 @@ public String getFileExtension() { * @param file - the file * @return the URI of the file. */ - @SuppressWarnings("static-method") public URI uri(IFile file) { - return URI.createPlatformResourceURI(file.getFullPath().toString(), true); + return uri(file.getFullPath().toString()); + } + + /** Replies the URI of trhe given file. + * + * @param file - the filename from the workspace. + * @return the URI of the file. + */ + @SuppressWarnings("static-method") + public URI uri(String file) { + return URI.createPlatformResourceURI(file, true); } /** Replies the set of resources inside the current project. @@ -566,7 +631,7 @@ public Resource createSARLScriptResource(String basename, String content) throws * * @param file - the file in the source folder. * @param content - the content of the resource. - * @return the parsed SARL script. + * @return the parsed resource. * @throws Exception */ public Resource createResource(IFile file, String content) throws Exception { @@ -578,6 +643,16 @@ public Resource createResource(IFile file, String content) throws Exception { } } + /** Load an existing resource. + * + * @param file - the file in the source folder. + * @return the resource. + * @throws Exception + */ + public Resource loadResource(URI file) throws Exception { + return getResourceSet().getResource(file, true); + } + /** Create and compile a SARL script in the source folder, * and reply the top element of the given type at the given * position in the SARL script.