Skip to content

Commit

Permalink
[wip] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdaniel committed Aug 9, 2024
1 parent e4f737f commit d6b9171
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Message> feedbackMessages = Collections.synchronizedList(new ArrayList<>());

@Override
public void addFeedbackMessage(Message message) {
this.feedbackMessages.add(message);
}

@Override
public List<Message> getFeedbackMessages() {
return this.feedbackMessages;
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Message> feedbackMessages = Collections.synchronizedList(new ArrayList<>());

@Override
public void addFeedbackMessage(Message message) {
this.feedbackMessages.add(message);
}

@Override
public List<Message> getFeedbackMessages() {
return this.feedbackMessages;
}
}
}

0 comments on commit d6b9171

Please sign in to comment.