org.eclipse.xtext.xbase.XStringLiteral Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XStringLiteral. 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 File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_0_1_2_3() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String) {",
			"super(null) // must be never null during runtime",
			"System.out.println(arg0)",
			"}",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", null);
}
 
Example #2
Source File: SARLValidator.java    From sarl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"checkstyle:cyclomaticcomplexity"})
private static String parseIssueOnCallAnnotation(List<JvmAnnotationValue> values) {
	final StringBuilder message = new StringBuilder();
	for (final JvmAnnotationValue value : values) {
		if (value instanceof JvmStringAnnotationValue) {
			message.append(((JvmStringAnnotationValue) value).getValues());
		} else if (value instanceof JvmCustomAnnotationValue) {
			for (final Object obj : ((JvmCustomAnnotationValue) value).getValues()) {
				if (obj instanceof XStringLiteral) {
					message.append(((XStringLiteral) obj).getValue());
				}
			}
		}
	}
	return message.toString();
}
 
Example #3
Source File: LiteralsCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
	if (obj instanceof XStringLiteral) {
		_toJavaStatement((XStringLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XNumberLiteral) {
		_toJavaStatement((XNumberLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XNullLiteral) {
		_toJavaStatement((XNullLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XBooleanLiteral) {
		_toJavaStatement((XBooleanLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XTypeLiteral) {
		_toJavaStatement((XTypeLiteral) obj, appendable, isReferenced);
	} else {
		super.doInternalToJavaStatement(obj, appendable, isReferenced);
	}
}
 
Example #4
Source File: VarArgFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_04() {
  final IFeatureCallArguments arguments = this.toArgumentsWithoutReceiver("String s, int i", "\"\", 1");
  Assert.assertEquals(2, arguments.getArgumentCount());
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot firstSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(firstSlot.isVarArg());
  Assert.assertFalse(firstSlot.isSuperfluous());
  XExpression _argumentExpression = firstSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", firstSlot.getDeclaredType().getSimpleName());
  Assert.assertFalse(arguments.isProcessed(0));
  firstSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(0));
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot secondSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertTrue(secondSlot.isVarArg());
  Assert.assertFalse(secondSlot.isSuperfluous());
  XExpression _argumentExpression_1 = secondSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression_1 instanceof XNumberLiteral));
  Assert.assertEquals("int", secondSlot.getDeclaredType().getSimpleName());
  Assert.assertFalse(arguments.isProcessed(1));
  secondSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example #5
Source File: ConstantExpressionValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public boolean isConstant(final XExpression expression) {
  if (expression instanceof XAbstractFeatureCall) {
    return _isConstant((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBooleanLiteral) {
    return _isConstant((XBooleanLiteral)expression);
  } else if (expression instanceof XCastedExpression) {
    return _isConstant((XCastedExpression)expression);
  } else if (expression instanceof XNumberLiteral) {
    return _isConstant((XNumberLiteral)expression);
  } else if (expression instanceof XStringLiteral) {
    return _isConstant((XStringLiteral)expression);
  } else if (expression instanceof XTypeLiteral) {
    return _isConstant((XTypeLiteral)expression);
  } else if (expression != null) {
    return _isConstant(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #6
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_0_1_2_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String=\"klm\") {",
			"super(null) // must be never null during runtime",
			"System.out.println(arg0)",
			"}",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", XStringLiteral.class, "klm");
}
 
Example #7
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_1_2_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String=\"klm\")",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", XStringLiteral.class, "klm");
}
 
Example #8
Source File: AbstractConstantExpressionsInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #9
Source File: StandardFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_09() {
  final IFeatureCallArguments arguments = this.toArgumentsWithoutReceiver("String s", "\"\", 1");
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertEquals(2, arguments.getArgumentCount());
  final IFeatureCallArgumentSlot valid = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(valid.isVarArg());
  Assert.assertFalse(valid.isSuperfluous());
  XExpression _argumentExpression = valid.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", valid.getDeclaredType().getSimpleName());
  valid.markProcessed();
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot superfluous = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(superfluous.isVarArg());
  Assert.assertTrue(superfluous.isSuperfluous());
  XExpression _argumentExpression_1 = superfluous.getArgumentExpression();
  Assert.assertTrue((_argumentExpression_1 instanceof XNumberLiteral));
  Assert.assertFalse(arguments.isProcessed(1));
  superfluous.markProcessed();
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example #10
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int, arg1 : String, arg2 : int, arg3 : int, arg4 : String=\"xyz\")",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(), null, null, null, null, XStringLiteral.class, "xyz");
}
 
Example #11
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int, arg1 : String, arg2 : int, arg3 : int, arg4 : String=\"xyz\") {",
			"super(null) // must be never null during runtime",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(), null, null, null, null, XStringLiteral.class, "xyz");
}
 
Example #12
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_1() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int, arg1 : String=\"abc\", arg2 : int, arg3 : int, arg4 : String)",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(), null, XStringLiteral.class, "abc", null, null, null);
}
 
Example #13
Source File: SerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_01() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XClosure closure = factory.createXClosure();
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	XBlockExpression blockExpression = factory.createXBlockExpression();
	blockExpression.getExpressions().add(stringLiteral);
	closure.setExpression(blockExpression);
	closure.setExplicitSyntax(true);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(closure);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	assertEquals("[|\"value\"] instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example #14
Source File: SerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_02() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XIfExpression ifExpression = factory.createXIfExpression();
	ifExpression.setIf(factory.createXBooleanLiteral());
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	ifExpression.setThen(stringLiteral);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(ifExpression);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=464846
	assertEquals("( if(false) \"value\" ) instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example #15
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #16
Source File: VarArgFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_08() {
  final IFeatureCallArguments arguments = this.toArgumentsWithReceiver("String s, int i", "\"\", 1");
  Assert.assertEquals(3, arguments.getArgumentCount());
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot firstSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(firstSlot.isVarArg());
  Assert.assertFalse(firstSlot.isSuperfluous());
  XExpression _argumentExpression = firstSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", firstSlot.getDeclaredType().getSimpleName());
  Assert.assertTrue(arguments.isProcessed(0));
  Assert.assertFalse(arguments.isProcessed(1));
  firstSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot secondSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertTrue(secondSlot.isVarArg());
  Assert.assertFalse(secondSlot.isSuperfluous());
  XExpression _argumentExpression_1 = secondSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression_1 instanceof XNumberLiteral));
  Assert.assertEquals("int", secondSlot.getDeclaredType().getSimpleName());
  Assert.assertFalse(arguments.isProcessed(2));
  secondSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(2));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example #17
Source File: AgentParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
public void variableUse() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"var name : String = \"Hello\"",
			"def myfct() { println(name) }",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals("A1", agent.getName());
	assertNull(agent.getExtends());
	assertEquals(2, agent.getMembers().size());
	//
	SarlField attr1 = (SarlField) agent.getMembers().get(0);
	assertEquals("name", attr1.getName());
	assertTypeReferenceIdentifier(attr1.getType(), "java.lang.String");
	assertTrue(attr1.getInitialValue() instanceof XStringLiteral);
	assertEquals("Hello", ((XStringLiteral) attr1.getInitialValue()).getValue());
	assertFalse(attr1.isFinal());
	//
	SarlAction act = (SarlAction) agent.getMembers().get(1);
	assertEquals("myfct", act.getName());
}
 
Example #18
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_1_2_3() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"def myaction(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String) {",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals("A1", agent.getName());
	assertNull(agent.getExtends());
	assertEquals(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("myaction", action.getName());
	assertTypeReferenceIdentifiers(action.getFiredEvents());
	assertParameterNames(action.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(action.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(action.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", null);
	assertTypeReferenceIdentifier(action.getReturnType(), "void");
}
 
Example #19
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRichStringFOR_02() throws Exception {
	XtendFunction function = function("def withForLoop() '''�FOR i: 1..10 BEFORE 'a' SEPARATOR '\t' AFTER i��ENDFOR�'''");
	final RichString richString = (RichString) function.getExpression();
	final RichStringForLoop rsFor = (RichStringForLoop) richString.getExpressions().get(1);
	assertTrue(rsFor.getForExpression() instanceof XBinaryOperation);
	assertEquals("i", rsFor.getDeclaredParam().getName());
	assertTrue(rsFor.getSeparator() instanceof XStringLiteral);
	assertTrue(rsFor.getBefore() instanceof XStringLiteral);
	assertTrue(rsFor.getAfter() instanceof XFeatureCall);
	assertTrue(((XAbstractFeatureCall) rsFor.getAfter()).getFeature().eIsProxy());
}
 
Example #20
Source File: VarArgFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_03() {
  final IFeatureCallArguments arguments = this.toArgumentsWithoutReceiver("String s", "\"\"");
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertEquals(1, arguments.getArgumentCount());
  final IFeatureCallArgumentSlot singleSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertTrue(singleSlot.isVarArg());
  Assert.assertFalse(singleSlot.isSuperfluous());
  XExpression _argumentExpression = singleSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", singleSlot.getDeclaredType().getSimpleName());
  singleSlot.markProcessed();
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example #21
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRichString_01() throws Exception {
	XtendFunction function = function("def foo() ''' foo �'holla'� bar '''");
	final RichString richString = (RichString) function.getExpression();
	assertTrue(richString.getExpressions().get(0) instanceof RichStringLiteral); 
	assertTrue(richString.getExpressions().get(1) instanceof XStringLiteral); 
	assertTrue(richString.getExpressions().get(2) instanceof RichStringLiteral); 
}
 
Example #22
Source File: TestAssertions.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Assert that the given actual formal parameters have the expected default values.
 *
 * The order of the parameters and the expected types is significant.
 *
 * The parameter <code>expectedDefaultValues</code> is a sequence of pairs, where
 * the first element is the type of the default value and the second element is
 * the representation of the default value.
 * If the first element of the pair is null (meaning no default value), then
 * the second element must be missed.
 *
 * @param actualFormalParameters the list of the formal parameters.
 * @param expectedDefaultValues the expected default values.
 */
@SuppressWarnings("rawtypes")
public static void assertParameterDefaultValues(Iterable<? extends XtendParameter> actualFormalParameters, Object... expectedDefaultValues) {
	int i = 0;
	for (XtendParameter parameter : actualFormalParameters) {
		if (expectedDefaultValues[i] == null) {
			if (parameter instanceof SarlFormalParameter) {
				assertNull(((SarlFormalParameter) parameter).getDefaultValue(), () -> "No default value expected");
			}
		} else {
			assertTrue(parameter instanceof SarlFormalParameter);
			final int ii = i;
			assertTrue(expectedDefaultValues[i] instanceof Class, () -> "The #" + ii + " in expectedDefaultValues is not a Class");
			Class type = (Class) expectedDefaultValues[i];
			assertTrue(type.isInstance(((SarlFormalParameter) parameter).getDefaultValue()), () -> "Unexpected type for the default value.");
			if (XNumberLiteral.class.isAssignableFrom(type)) {
				++i;
				assertEquals(expectedDefaultValues[i], ((XNumberLiteral) ((SarlFormalParameter) parameter).getDefaultValue()).getValue());
			} else if (XStringLiteral.class.isAssignableFrom(type)) {
				++i;
				assertEquals(expectedDefaultValues[i], ((XStringLiteral) ((SarlFormalParameter) parameter).getDefaultValue()).getValue());
			} else if (XNullLiteral.class.isAssignableFrom(type)) {
				//
			} else {
				throw new RuntimeException("Unsupported type of literal for this assertion function");
			}
		}
		++i;
	}
	if (i < expectedDefaultValues.length) {
		fail("Not enough default values. Expected: " + Arrays.toString(expectedDefaultValues)
			+ "Actual: " + Iterables.toString(actualFormalParameters));
	}
}
 
Example #23
Source File: RichStringEvaluationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void acceptExpression(/* @NonNull */ XExpression expression, /* @NonNull */ CharSequence indentation, boolean immediate) {
	XStringLiteral literal = (XStringLiteral) expression;
	String value = literal.getValue();
	value = value.replaceAll("\\n", "\n" + indentation);
	if (immediate) {
		if (currentLine.toString().matches("^\\s*$")) {
			for(int at = builder.length() - 1; at >= 0; at++) {
				if (builder.charAt(at) == '\n') {
					builder.insert(at, value);
					return;
				}
			}
		}
	}
	for(int i = value.length() - 1; i >= 0; i--) {
		if (value.charAt(i) == '\n') {
			builder.append(currentLine);
			builder.append(value.substring(0, i + 1));
			if (i != value.length() - 1) {
				currentLine = new StringBuilder(value.substring(i + 1));
			} else {
				currentLine = new StringBuilder();
			}
			return;
		}
	}
	currentLine.append(value);
}
 
Example #24
Source File: RichStringEvaluationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void acceptForLoop(/* @NonNull */ JvmFormalParameter parameter, /* @NonNull */ XExpression expression) {
	if (!ignore()) {
		XMemberFeatureCall featureCall = (XMemberFeatureCall) expression;
		XStringLiteral receiver = (XStringLiteral) featureCall.getMemberCallTarget();
		int length = receiver.getValue().length();
		if (length != 0)
			forLoopStack.push(length * -1);
		else
			forLoopStack.push(null);
	}
}
 
Example #25
Source File: JvmAnnotationReferencePrinter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String internalToString(Object obj) {
	if (obj instanceof XBinaryOperation) {
		return _internalToString((XBinaryOperation) obj);
	} else if (obj instanceof XFeatureCall) {
		return _internalToString((XFeatureCall) obj);
	} else if (obj instanceof XListLiteral) {
		return _internalToString((XListLiteral) obj);
	} else if (obj instanceof XMemberFeatureCall) {
		return _internalToString((XMemberFeatureCall) obj);
	} else if (obj instanceof XBooleanLiteral) {
		return _internalToString((XBooleanLiteral) obj);
	} else if (obj instanceof XNumberLiteral) {
		return _internalToString((XNumberLiteral) obj);
	} else if (obj instanceof XStringLiteral) {
		return _internalToString((XStringLiteral) obj);
	} else if (obj instanceof XTypeLiteral) {
		return _internalToString((XTypeLiteral) obj);
	} else if (obj instanceof XAnnotation) {
		return _internalToString((XAnnotation) obj);
	} else if (obj instanceof JvmAnnotationReference) {
		return _internalToString((JvmAnnotationReference) obj);
	} else if (obj instanceof JvmAnnotationValue) {
		return _internalToString((JvmAnnotationValue) obj);
	} else {
		return _internalToString(obj);
	}
}
 
Example #26
Source File: EventParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void completeFinalFieldInitialization() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"event E1 {",
			"val field1 : int = 5",
			"val field2 : String = \"\"",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlEvent event = (SarlEvent) mas.getXtendTypes().get(0);
	assertEquals("E1", event.getName());
	assertNull(event.getExtends());
	assertEquals(2, event.getMembers().size());
	//
	SarlField attr1 = (SarlField) event.getMembers().get(0);
	assertEquals("field1", attr1.getName());
	assertTypeReferenceIdentifier(attr1.getType(), "int");
	assertXExpression(attr1.getInitialValue(), XNumberLiteral.class, "5");
	//
	SarlField attr2 = (SarlField) event.getMembers().get(1);
	assertEquals("field2", attr2.getName());
	assertTypeReferenceIdentifier(attr2.getType(), "java.lang.String");
	assertXExpression(attr2.getInitialValue(), XStringLiteral.class, "");
}
 
Example #27
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"def myaction(arg0 : int, arg1 : String, arg2 : int, arg3 : int, arg4 : String=\"xyz\") {",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals("A1", agent.getName());
	assertNull(agent.getExtends());
	assertEquals(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("myaction", action.getName());
	assertTypeReferenceIdentifiers(action.getFiredEvents());
	assertParameterNames(action.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(action.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(action.getParameters(), null, null, null, null, XStringLiteral.class, "xyz");
	assertTypeReferenceIdentifier(action.getReturnType(), "void");
}
 
Example #28
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"def myaction(arg0 : int=4, arg1 : String, arg2 : int, arg3 : int=56, arg4 : String=\"def\") {",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals("A1", agent.getName());
	assertNull(agent.getExtends());
	assertEquals(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("myaction", action.getName());
	assertTypeReferenceIdentifiers(action.getFiredEvents());
	assertParameterNames(action.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(action.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(action.getParameters(), XNumberLiteral.class, "4", null, null, XNumberLiteral.class, "56", XStringLiteral.class, "def");
	assertTypeReferenceIdentifier(action.getReturnType(), "void");
}
 
Example #29
Source File: ArgDefaultValueParsingTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_2_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"def myaction(arg0 : int=4, arg1 : String, arg2 : int=18, arg3 : int, arg4 : String=\"def\") {",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals("A1", agent.getName());
	assertNull(agent.getExtends());
	assertEquals(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("myaction", action.getName());
	assertTypeReferenceIdentifiers(action.getFiredEvents());
	assertParameterNames(action.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(action.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(action.getParameters(), XNumberLiteral.class, "4", null, XNumberLiteral.class, "18", null, XStringLiteral.class, "def");
	assertTypeReferenceIdentifier(action.getReturnType(), "void");
}
 
Example #30
Source File: StandardFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_03() {
  final IFeatureCallArguments arguments = this.toArgumentsWithoutReceiver("String s", "\"\"");
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertEquals(1, arguments.getArgumentCount());
  final IFeatureCallArgumentSlot singleSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(singleSlot.isVarArg());
  Assert.assertFalse(singleSlot.isSuperfluous());
  XExpression _argumentExpression = singleSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", singleSlot.getDeclaredType().getSimpleName());
  singleSlot.markProcessed();
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}