Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions com.avaloq.tools.ddk.xtext.export.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ Fragment-Host: com.avaloq.tools.ddk.xtext.export.ui
Require-Bundle: com.avaloq.tools.ddk.xtext.export,
com.google.inject,
com.avaloq.tools.ddk.xtext.test.core,
org.junit,
com.avaloq.tools.ddk.xtext,
junit-jupiter-api,
junit-jupiter-engine,
junit-vintage-engine
junit-platform-suite-api
Export-Package: com.avaloq.tools.ddk.xtext.test.export
Automatic-Module-Name: com.avaloq.tools.ddk.xtext.export.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.export.exporting;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.avaloq.tools.ddk.xtext.test.AbstractXtextTest;
import com.avaloq.tools.ddk.xtext.test.export.util.ExportTestUtil;
import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractXtextTest;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import com.avaloq.tools.ddk.xtext.test.TestSource;
import com.avaloq.tools.ddk.xtext.test.export.util.ExportTestUtil;
import com.avaloq.tools.ddk.xtext.test.formatting.AbstractFormattingTest;
import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractFormattingTest;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.export.scoping;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;

import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.scoping.IScope;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.avaloq.tools.ddk.xtext.export.export.ExportModel;
import com.avaloq.tools.ddk.xtext.export.export.ExportPackage;
import com.avaloq.tools.ddk.xtext.scoping.IDomain.NullMapper;
import com.avaloq.tools.ddk.xtext.test.export.util.ExportTestUtil;
import com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest;
import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractScopingTest;


/**
Expand All @@ -46,25 +46,25 @@ protected ExportTestUtil getXtextTestUtil() {
public void testImportPackageScope() throws IOException {
ExportModel model = (ExportModel) getTestSource().getModel();
IScope scope = scopeProvider.scope_Import_package(model.getImports().get(0), ExportPackage.Literals.IMPORT__PACKAGE);
assertNotNull("Could not locate Import.", scope.getSingleElement(QualifiedName.create("http://www.avaloq.com/tools/ddk/xtext/export/Export")));
assertNull("Located non-existent Import.", scope.getSingleElement(QualifiedName.create("http://www.avaloq.com/tools/ddk/xtext/export/ExportX")));
assertNotNull(scope.getSingleElement(QualifiedName.create("http://www.avaloq.com/tools/ddk/xtext/export/Export")), "Could not locate Import.");
assertNull(scope.getSingleElement(QualifiedName.create("http://www.avaloq.com/tools/ddk/xtext/export/ExportX")), "Located non-existent Import.");
}

@Test
public void testEclassScope() throws IOException {
ExportModel model = (ExportModel) getTestSource().getModel();
IScope scope = scopeProvider.scope_EClass(model, null);
assertNotNull("Could not locate EClass.", scope.getSingleElement(QualifiedName.create("InterfaceExpression")));
assertNull("Located non-existent EClass.", scope.getSingleElement(QualifiedName.create("InterfaceExpressionX")));
assertNotNull(scope.getSingleElement(QualifiedName.create("InterfaceExpression")), "Could not locate EClass.");
assertNull(scope.getSingleElement(QualifiedName.create("InterfaceExpressionX")), "Located non-existent EClass.");
}

@Test
public void testEStructuralFeatureScope() throws IOException {
ExportModel model = (ExportModel) getTestSource().getModel();
IScope scope = scopeProvider.scope_EStructuralFeature(model.getInterfaces().get(0), null);
// CHECKSTYLE:OFF (DuplicateString)
assertNotNull("Could not locate EStructuralFeature.", scope.getSingleElement(QualifiedName.create("unordered")));
assertNull("Located non-existent EStructuralFeature.", scope.getSingleElement(QualifiedName.create("unorderedX")));
assertNotNull(scope.getSingleElement(QualifiedName.create("unordered")), "Could not locate EStructuralFeature.");
assertNull(scope.getSingleElement(QualifiedName.create("unorderedX")), "Located non-existent EStructuralFeature.");
// CHECKSTYLE:ON
}

Expand All @@ -73,8 +73,8 @@ public void testEAttributeScope() throws IOException {
ExportModel model = (ExportModel) getTestSource().getModel();
IScope scope = scopeProvider.scope_EAttribute(model.getInterfaces().get(0), null);
// CHECKSTYLE:OFF (DuplicateString)
assertNotNull("Could not locate EStructuralFeature.", scope.getSingleElement(QualifiedName.create("unordered")));
assertNull("Located non-existent EStructuralFeature.", scope.getSingleElement(QualifiedName.create("expr")));
assertNotNull(scope.getSingleElement(QualifiedName.create("unordered")), "Could not locate EStructuralFeature.");
assertNull(scope.getSingleElement(QualifiedName.create("expr")), "Located non-existent EStructuralFeature.");
// CHECKSTYLE:ON
}

Expand All @@ -83,8 +83,8 @@ public void testInterfaceNavigationRefScope() throws IOException {
ExportModel model = (ExportModel) getTestSource().getModel();
IScope scope = scopeProvider.scope_InterfaceNavigation_ref(model.getInterfaces().get(0), null);
// CHECKSTYLE:OFF (DuplicateString)
assertNotNull("Could not locate InterfaceNavigationRef.", scope.getSingleElement(QualifiedName.create("expr")));
assertNull("Located non-existent InterfaceNavigationRef.", scope.getSingleElement(QualifiedName.create("unordered")));
assertNotNull(scope.getSingleElement(QualifiedName.create("expr")), "Could not locate InterfaceNavigationRef.");
assertNull(scope.getSingleElement(QualifiedName.create("unordered")), "Located non-existent InterfaceNavigationRef.");
// CHECKSTYLE:ON
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.export.validation;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.avaloq.tools.ddk.xtext.test.export.util.ExportTestUtil;
import com.avaloq.tools.ddk.xtext.test.validation.AbstractValidationTest;
import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractValidationTest;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.export.validation;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import com.avaloq.tools.ddk.xtext.test.export.util.ExportTestUtil;
import com.avaloq.tools.ddk.xtext.test.validation.AbstractValidationTest;
import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractValidationTest;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.test.export;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

import com.avaloq.tools.ddk.xtext.export.exporting.ExportExportingTest;
import com.avaloq.tools.ddk.xtext.export.formatting.ExportFormattingTest;
Expand All @@ -22,7 +22,7 @@
/**
* Empty class serving only as holder for JUnit4 annotations.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ExportFormattingTest.class, ExportValidationTest.class, ExportScopingTest.class, ExportExportingTest.class})
@Suite
@SelectClasses({ExportFormattingTest.class, ExportValidationTest.class, ExportScopingTest.class, ExportExportingTest.class})
public class ExportTestSuite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*******************************************************************************
* Copyright (c) 2025 Avaloq Group AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Avaloq Group AG - initial API and implementation
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.test.jupiter;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.formatting.INodeModelFormatter;
import org.eclipse.xtext.formatting.INodeModelFormatter.IFormattedRegion;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.SaveOptions;
import org.junit.jupiter.api.Test;


/**
* Base class for formatting tests.
*/
@SuppressWarnings("nls")
public abstract class AbstractFormattingTest extends AbstractXtextTest {

private static final String CR_LF = "\r\n";
private static final String LF = "\n";

@Override
protected List<String> getRequiredSourceFileNames() {
List<String> result = super.getRequiredSourceFileNames();
result.add(getExpectedTestSourceFileName());
return result;
}

@Override
protected String getTestSourceModelName() {
return super.getTestSourceModelName() + "Input";
}

/**
* The default implementation returns the default source model name for the test class and adds 'Expected'.
* this test. A test class needs to override this, if the name of the expected formatting test source model differs from the default.
*
* @return the name of the expected formatting test source model
*/
private String getExpectedTestSourceModelName() {
return super.getTestSourceModelName() + "Expected";
}

/**
* The default implementation returns the default source model name for the test class and adds 'Expected' and the default file extension for the grammar of
* this test. A test class needs to override this, if the name of the expected formatting test source file differs from the default.
*
* @return the name of the expected formatting test source file
*/
protected String getExpectedTestSourceFileName() {
return getExpectedTestSourceModelName() + "." + getXtextTestUtil().getFileExtension();
}

/**
* Test formatting based on the NodeModel.
*/
@Test
public void formattedNodeModel() {
assertFormattedNodeModel();
}

/**
* Test formatting based on the ParseTreeConstructor.
*/
public void formattedParseTreeConstructor() {
assertFormattedParseTreeConstructor();
}

/**
* Test preservation of formatting using ParseTreeConstructor.
*/
public void preservedParseTreeConstructor() {
assertPreservedParseTreeConstructor();
}

/**
* Test preservation of formatting using NodeModelFormatter.
*/
@Test
public void preservedNodeModel() {
assertPreservedNodeModel();
}

/**
* Test formatting based on the ParseTreeConstructor.
*/
protected final void assertFormattedParseTreeConstructor() {
assertFormattedParseTreeConstructor(getSemanticModel(), getTestSource(getExpectedTestSourceFileName()).getContent());
}

/**
* Test formatting based on the NodeModel.
*
* @param offset
* Offset from which to start formatting
* @param length
* Length of region to format
*/
private void assertFormattedNodeModel(final int offset, final int length) {
assertFormattedNodeModel(getSemanticModel(), getTestSource().getContent(), getTestSource(getExpectedTestSourceFileName()).getContent(), offset, length);
}

/**
* Test formatting based on the NodeModel.
*/
private void assertFormattedNodeModel() {
assertFormattedNodeModel(0, getTestSource().getContent().length());
}

/**
* Test preservation of formatting.
*/
private void assertPreservedNodeModel() {
String expectedContent = getTestSource(getExpectedTestSourceFileName()).getContent();
assertFormattedNodeModel(getTestSource(getExpectedTestSourceFileName()).getModel(), expectedContent, expectedContent, 0, expectedContent.length());
}

/**
* Test preservation of formatting.
*/
protected final void assertPreservedParseTreeConstructor() {
assertFormattedParseTreeConstructor(getTestSource(getExpectedTestSourceFileName()).getModel(), getTestSource(getExpectedTestSourceFileName()).getContent());
}

/**
* Test formatting based on the ParseTreeConstructor.
*
* @param model
* the model to be serialized and compared with expected string
* @param expected
* Expected formatted String
*/
private void assertFormattedParseTreeConstructor(final EObject model, final String expected) {
String actual = getXtextTestUtil().getSerializer().serialize(model, SaveOptions.newBuilder().format().getOptions());
assertEquals(expected.replaceAll(CR_LF, LF), actual.replaceAll(CR_LF, LF), "Formatted ParseTree");
}

/**
* Test formatting based on the NodeModel.
*
* @param model
* the model to check
* @param input
* String representing a serialized model
* @param expected
* Expected formatted String
* @param offset
* Offset from which to start formatting
* @param length
* Length of region to format
*/
private void assertFormattedNodeModel(final EObject model, final String input, final String expected, final int offset, final int length) {
ICompositeNode node = NodeModelUtils.getNode(model).getRootNode();
IFormattedRegion region = getXtextTestUtil().get(INodeModelFormatter.class).format(node, offset, length);
String actual = input.substring(0, offset) + region.getFormattedText() + input.substring(length + offset);
assertEquals(expected.replaceAll(CR_LF, LF), actual.replaceAll(CR_LF, LF), "Formatted NodeModel");
}

}
Loading