From d6b917111b51aab127b75f2e4027a46f335bc8cc Mon Sep 17 00:00:00 2001 From: Gwendal Daniel Date: Fri, 9 Aug 2024 18:50:15 +0200 Subject: [PATCH] [wip] tests --- .../eclipse/syson/services/LabelService.java | 1 - .../DiagramDirectEditListenerTest.java | 97 +++++++++ .../view/services/ViewLabelServiceTest.java | 185 ++++++++++++++++++ 3 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 backend/services/syson-services/src/test/java/org/eclipse/syson/services/DiagramDirectEditListenerTest.java create mode 100644 backend/views/syson-diagram-common-view/src/test/java/org/eclipse/syson/diagram/common/view/services/ViewLabelServiceTest.java diff --git a/backend/services/syson-services/src/main/java/org/eclipse/syson/services/LabelService.java b/backend/services/syson-services/src/main/java/org/eclipse/syson/services/LabelService.java index 186385ec2..f4dc074a2 100644 --- a/backend/services/syson-services/src/main/java/org/eclipse/syson/services/LabelService.java +++ b/backend/services/syson-services/src/main/java/org/eclipse/syson/services/LabelService.java @@ -137,7 +137,6 @@ public Element directEdit(Element element, String newLabel, String... options) { } else { tree = parser.expression(); } - // ParseTree tree = parser.expression(); ParseTreeWalker walker = new ParseTreeWalker(); DirectEditListener listener = new DiagramDirectEditListener(element, this.getFeedbackMessageService(), options); walker.walk(listener, tree); diff --git a/backend/services/syson-services/src/test/java/org/eclipse/syson/services/DiagramDirectEditListenerTest.java b/backend/services/syson-services/src/test/java/org/eclipse/syson/services/DiagramDirectEditListenerTest.java new file mode 100644 index 000000000..4c078ce16 --- /dev/null +++ b/backend/services/syson-services/src/test/java/org/eclipse/syson/services/DiagramDirectEditListenerTest.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.syson.services; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.eclipse.sirius.components.core.api.IFeedbackMessageService; +import org.eclipse.sirius.components.representations.Message; +import org.eclipse.syson.services.grammars.DirectEditLexer; +import org.eclipse.syson.services.grammars.DirectEditListener; +import org.eclipse.syson.services.grammars.DirectEditParser; +import org.eclipse.syson.sysml.ConstraintUsage; +import org.eclipse.syson.sysml.Element; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * @author gdaniel + */ +public class DiagramDirectEditListenerTest { + + @DisplayName("Given a ConstraintUsage, when it is edited with '1 >= 2', then its expression is set") + @Test + public void testDirectEditConstraintUsageWithBooleanExpression() { + + } + + @DisplayName("Given a ConstraintUsage, when it is edited with 'feature >= 1', then its expression is set") + @Test + public void testDirectEditConstraintUsageWithFeatureReferenceExpression() { + + } + + @DisplayName("Given a ConstraintUsage, when it is edited with 'feature1.feature2 >= 1', then its expression is set") + @Test + public void testDirectEditConstraintUsageWithSingleFeatureChainingExpression() { + + } + + @DisplayName("Given a ConstraintUsage, when it is edited with 'feature1.feature2.feature3 >= 1', then its expression is set") + @Test + public void testDirectEditConstraintUsageWithMultipleFeatureChainingExpression() { + + } + + private void doDirectEdit(Element element, String input) { + DirectEditLexer lexer = new DirectEditLexer(CharStreams.fromString(input)); + CommonTokenStream tokens = new CommonTokenStream(lexer); + DirectEditParser parser = new DirectEditParser(tokens); + ParseTree tree; + if (element instanceof ConstraintUsage) { + tree = parser.constraintExpression(); + } else { + tree = parser.expression(); + } + ParseTreeWalker walker = new ParseTreeWalker(); + DirectEditListener listener = new DiagramDirectEditListener(element, new TestFeedbackMessageService()); + walker.walk(listener, tree); + } + + /** + * A test-level implementation of {@link IFeedbackMessageService}. + * + * @author jmallet + */ + public class TestFeedbackMessageService implements IFeedbackMessageService { + + private final List feedbackMessages = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void addFeedbackMessage(Message message) { + this.feedbackMessages.add(message); + } + + @Override + public List getFeedbackMessages() { + return this.feedbackMessages; + } + } + +} diff --git a/backend/views/syson-diagram-common-view/src/test/java/org/eclipse/syson/diagram/common/view/services/ViewLabelServiceTest.java b/backend/views/syson-diagram-common-view/src/test/java/org/eclipse/syson/diagram/common/view/services/ViewLabelServiceTest.java new file mode 100644 index 000000000..5dc194539 --- /dev/null +++ b/backend/views/syson-diagram-common-view/src/test/java/org/eclipse/syson/diagram/common/view/services/ViewLabelServiceTest.java @@ -0,0 +1,185 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.syson.diagram.common.view.services; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.sirius.components.core.api.IFeedbackMessageService; +import org.eclipse.sirius.components.representations.Message; +import org.eclipse.syson.sysml.ConstraintUsage; +import org.eclipse.syson.sysml.Feature; +import org.eclipse.syson.sysml.FeatureDirectionKind; +import org.eclipse.syson.sysml.FeatureValue; +import org.eclipse.syson.sysml.LiteralInteger; +import org.eclipse.syson.sysml.OperatorExpression; +import org.eclipse.syson.sysml.ParameterMembership; +import org.eclipse.syson.sysml.ResultExpressionMembership; +import org.eclipse.syson.sysml.SysmlFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * View Label services tests. + * + * @author jmallet + */ +public class ViewLabelServiceTest { + + private static final String ATTRIBUTE_USAGE_NAME = "myAttributeUsage"; + + private ViewLabelService viewLabelService; + + @BeforeEach + void beforeEach() { + this.viewLabelService = new ViewLabelService(new TestFeedbackMessageService(), new ShowDiagramsIconsService()); + } + + // @DisplayName("Chek Attribute Usage item label with default properties") + // @Test + // void testItemCompartmentLabelWithDefaultProperties() { + // AttributeUsage attributeUsage = SysmlFactory.eINSTANCE.createAttributeUsage(); + // attributeUsage.setDeclaredName(ATTRIBUTE_USAGE_NAME); + // assertEquals(ATTRIBUTE_USAGE_NAME, this.viewLabelService.getCompartmentItemLabel(attributeUsage)); + // } + // + // @DisplayName("Chek Attribute Usage item label with no name") + // @Test + // void testItemCompartmentLabelWithoutName() { + // AttributeUsage attributeUsage = SysmlFactory.eINSTANCE.createAttributeUsage(); + // assertEquals("", this.viewLabelService.getCompartmentItemLabel(attributeUsage)); + // } + // + // @DisplayName("Chek Attribute Usage item label with prefix") + // @Test + // void testItemCompartmentLabelWithPrefix() { + // AttributeUsage attributeUsage = SysmlFactory.eINSTANCE.createAttributeUsage(); + // attributeUsage.setDeclaredName(ATTRIBUTE_USAGE_NAME); + // + // attributeUsage.setIsAbstract(true); + // assertEquals(LabelConstants.ABSTRACT + LabelConstants.SPACE + ATTRIBUTE_USAGE_NAME, + // this.viewLabelService.getCompartmentItemLabel(attributeUsage)); + // } + // + // @DisplayName("Chek Attribute Usage item label with multiplicity") + // @Test + // void testItemCompartmentLabelWithMultiplicity() { + // AttributeUsage attributeUsage = SysmlFactory.eINSTANCE.createAttributeUsage(); + // attributeUsage.setDeclaredName(ATTRIBUTE_USAGE_NAME); + // + // attributeUsage.setIsOrdered(true); + // assertEquals(ATTRIBUTE_USAGE_NAME + LabelConstants.SPACE + LabelConstants.ORDERED, + // this.viewLabelService.getCompartmentItemLabel(attributeUsage)); + // } + // + // @DisplayName("Chek Attribute Usage item label with prefix and multiplicity") + // @Test + // void testItemCompartmentLabelWithPrefixAndMultiplicity() { + // AttributeUsage attributeUsage = SysmlFactory.eINSTANCE.createAttributeUsage(); + // attributeUsage.setDeclaredName(ATTRIBUTE_USAGE_NAME); + // + // attributeUsage.setIsOrdered(true); + // attributeUsage.setIsAbstract(true); + // assertEquals(LabelConstants.ABSTRACT + LabelConstants.SPACE + ATTRIBUTE_USAGE_NAME + LabelConstants.SPACE + + // LabelConstants.ORDERED, + // this.viewLabelService.getCompartmentItemLabel(attributeUsage)); + // } + + @DisplayName("Given a ConstraintUsage with no expression, when its label is computed, then the label contains the name of the constraint") + @Test + public void testGetCompartmentItemLabelOfConstraintWithNoExpression() { + ConstraintUsage constraintUsage = SysmlFactory.eINSTANCE.createConstraintUsage(); + constraintUsage.setDeclaredName("myConstraint"); + assertThat(this.viewLabelService.getCompartmentItemLabel(constraintUsage)).isEqualTo("myConstraint"); + } + + @DisplayName("Given a ConstraintUsage with a boolean expression, when its label is computed, then the label represents the expression") + @Test + public void testGetCompartmentItemLabelOfConstraintWithBooleanExpression() { + ConstraintUsage constraintUsage = SysmlFactory.eINSTANCE.createConstraintUsage(); + constraintUsage.setDeclaredName("myConstraint"); + ResultExpressionMembership resultExpressionMembership = SysmlFactory.eINSTANCE.createResultExpressionMembership(); + constraintUsage.getOwnedRelationship().add(resultExpressionMembership); + OperatorExpression operatorExpression = SysmlFactory.eINSTANCE.createOperatorExpression(); + resultExpressionMembership.getOwnedRelatedElement().add(operatorExpression); + operatorExpression.setOperator(">="); + + ParameterMembership p1 = SysmlFactory.eINSTANCE.createParameterMembership(); + operatorExpression.getOwnedRelationship().add(p1); + Feature x = SysmlFactory.eINSTANCE.createFeature(); + p1.getOwnedRelatedElement().add(x); + x.setDeclaredName("x"); + x.setDirection(FeatureDirectionKind.IN); + FeatureValue xValue = SysmlFactory.eINSTANCE.createFeatureValue(); + x.getOwnedRelationship().add(xValue); + LiteralInteger literalInteger1 = SysmlFactory.eINSTANCE.createLiteralInteger(); + literalInteger1.setValue(1); + xValue.getOwnedRelatedElement().add(literalInteger1); + + ParameterMembership p2 = SysmlFactory.eINSTANCE.createParameterMembership(); + operatorExpression.getOwnedRelationship().add(p2); + Feature y = SysmlFactory.eINSTANCE.createFeature(); + p2.getOwnedRelatedElement().add(y); + y.setDeclaredName("y"); + y.setDirection(FeatureDirectionKind.IN); + FeatureValue yValue = SysmlFactory.eINSTANCE.createFeatureValue(); + y.getOwnedRelationship().add(yValue); + LiteralInteger literalInteger2 = SysmlFactory.eINSTANCE.createLiteralInteger(); + literalInteger2.setValue(2); + yValue.getOwnedRelatedElement().add(literalInteger2); + + assertThat(this.viewLabelService.getCompartmentItemLabel(constraintUsage)).isEqualTo("1 >= 2"); + } + + @DisplayName("Given a ConstraintUsage with an expression containing a feature reference, when its label is computed, then the label represents the expression") + @Test + public void testGetCompartmentItemLabelOfConstraintWithFeatureReferenceExpression() { + + } + + @DisplayName("Given a ConstraintUsage with an expression containing a single feature chaining, when its label is computed, then the label represents the expression") + @Test + public void testGetCompartmentItemLabelOfConstraintWithSingleFeatureChainingExpression() { + + } + + @DisplayName("Given a ConstraintUsage with an expression containing multiple feature chainings, when its label is computed, then the label represents the expression") + @Test + public void testGetCompartmentItemLabelOfConstraintWithMultipleFeatureChainingExpression() { + + } + + /** + * A test-level implementation of {@link IFeedbackMessageService}. + * + * @author jmallet + */ + public class TestFeedbackMessageService implements IFeedbackMessageService { + + private final List feedbackMessages = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void addFeedbackMessage(Message message) { + this.feedbackMessages.add(message); + } + + @Override + public List getFeedbackMessages() { + return this.feedbackMessages; + } + } +} \ No newline at end of file