Skip to content

Commit

Permalink
Provide an abstract base class to be able to test the formatter.
Browse files Browse the repository at this point in the history
- Add the org.eclipse.xtext.testing.AbstractFormatterTest class.
- Modify the FormatterTest class of the Domainmodel example to
demonstrate the usage of the AbstractFormatterTest class.
- Modify the AbstractXtendFormatterTest base class to inherit from the
newly created AbstractFormatterTest class and adapt the Xtend formatter
test cases to use the inherited members/methods.

Signed-off-by: miklossy <miklossy@itemis.de>
  • Loading branch information
miklossy committed Apr 2, 2024
1 parent 1d83f89 commit a4d9485
Show file tree
Hide file tree
Showing 29 changed files with 1,921 additions and 1,988 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2016 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -8,131 +8,62 @@
*******************************************************************************/
package org.eclipse.xtend.core.tests.formatting

import com.google.inject.Inject
import org.eclipse.xtend.core.formatting2.XtendFormatterPreferenceKeys
import org.eclipse.xtend.core.tests.RuntimeInjectorProvider
import org.eclipse.xtext.preferences.MapBasedPreferenceValues
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.testing.formatter.FormatterTestHelper
import org.eclipse.xtext.preferences.MapBasedPreferenceValues
import org.eclipse.xtext.testing.formatter.AbstractFormatterTest
import org.eclipse.xtext.util.TextRegion
import org.junit.runner.RunWith

import static org.eclipse.xtext.formatting2.FormatterPreferenceKeys.*

@RunWith(typeof(XtextRunner))
@InjectWith(typeof(RuntimeInjectorProvider))
abstract class AbstractXtendFormatterTest {
@Inject protected FormatterTestHelper tester

def assertFormatted(CharSequence toBeFormatted) {
assertFormatted(toBeFormatted, toBeFormatted/* .parse.flattenWhitespace */)
}
@RunWith(XtextRunner)
@InjectWith(RuntimeInjectorProvider)
abstract class AbstractXtendFormatterTest extends AbstractFormatterTest {

def private toMember(CharSequence expression) '''
package foo

class bar {
«expression»
}
'''
def assertFormattedExpression((MapBasedPreferenceValues)=>void cfg, CharSequence toBeFormatted) {
assertFormattedExpression(cfg, toBeFormatted, toBeFormatted)
protected def String decode(CharSequence seq) {
seq.toString.replace("<<", "«").replace(">>", "»").replace("```", "'''")
}

def assertFormattedExpression(CharSequence toBeFormatted) {
protected def assertFormattedExpression(CharSequence toBeFormatted) {
assertFormattedExpression(null, toBeFormatted, toBeFormatted)
}

def assertFormattedExpression(String expectation, CharSequence toBeFormatted) {
protected def assertFormattedExpression(String expectation, CharSequence toBeFormatted) {
assertFormattedExpression(null, expectation, toBeFormatted)
}

def assertFormattedExpression((MapBasedPreferenceValues)=>void cfg, CharSequence expectation,
protected def assertFormattedExpression((MapBasedPreferenceValues)=>void cfg, CharSequence expectation,
CharSequence toBeFormatted) {
assertFormattedExpression(cfg, expectation, toBeFormatted, false)
}

def assertFormattedExpression((MapBasedPreferenceValues)=>void cfg, CharSequence expectation,
CharSequence toBeFormatted, boolean allowErrors) {
assertFormatted(
cfg,
expectation.toString.trim.replace("\n", "\n\t\t"),
toBeFormatted.toString.trim.replace("\n", "\n\t\t"),
"class bar {\n\tdef baz() {\n\t\t",
"\n\t}\n}",
allowErrors
)
}
def assertFormattedMember(String expectation, CharSequence toBeFormatted) {
assertFormatted(expectation.toMember, toBeFormatted.toMember)
}
def assertFormattedMember((MapBasedPreferenceValues)=>void cfg, String expectation, CharSequence toBeFormatted) {
assertFormatted(cfg, expectation.toMember, toBeFormatted.toMember)
}
def assertFormattedMember((MapBasedPreferenceValues)=>void cfg, String expectation) {
assertFormatted(cfg, expectation.toMember, expectation.toMember)
protected def assertFormattedRichStringExpression(CharSequence seq) {
assertFormattedExpression(seq.decode)
}

def assertFormattedMember(String expectation) {
assertFormatted(expectation.toMember, expectation.toMember)
protected def assertFormattedRichStringExpression(CharSequence expected, CharSequence actual) {
assertFormattedExpression(expected.decode, actual.decode)
}

def assertFormatted((MapBasedPreferenceValues)=>void cfg, CharSequence expectation) {
assertFormatted(cfg, expectation, expectation)
protected def assertFormattedRichStringExpressionWithErrors(CharSequence actual) {
assertFormattedExpression(null, actual.decode, actual.decode, true)
}

def assertFormatted(CharSequence expectation, CharSequence toBeFormatted) {
assertFormatted(null, expectation, toBeFormatted)
}
private def assertFormattedExpression((MapBasedPreferenceValues)=>void cfg, CharSequence expectation,
CharSequence toBeFormatted, boolean allowErrors) {

def assertFormatted((MapBasedPreferenceValues)=>void cfg, CharSequence expectation, CharSequence toBeFormatted) {
assertFormatted(cfg, expectation, toBeFormatted, "", "", false)
}
val prefix = "class bar {\n\tdef baz() {\n\t\t"
val postfix = "\n\t}\n}"

def assertFormatted(
(MapBasedPreferenceValues)=>void cfg,
CharSequence expectation,
CharSequence toBeFormatted,
String prefix,
String postfix,
boolean allowErrors
) {
tester.assertFormatted [
formatterTestHelper.assertFormatted [
it.preferences = [
put(maxLineWidth, 80)
put(XtendFormatterPreferenceKeys.keepOneLineMethods, false)
cfg?.apply(it)
]
it.expectation = prefix + expectation + postfix
it.toBeFormatted = prefix + toBeFormatted + postfix
it.request.regions += new TextRegion(prefix.length, toBeFormatted.length)
it.expectation = prefix + expectation.toString.trim.replace("\n", "\n\t\t") + postfix
it.toBeFormatted = prefix + toBeFormatted.toString.trim.replace("\n", "\n\t\t") + postfix
it.request.regions += new TextRegion(prefix.length, toBeFormatted.toString.trim.replace("\n", "\n\t\t").length)
it.allowSyntaxErrors = allowErrors
]
}

def protected String decode(CharSequence seq) {
seq.toString.replace("<<", "«").replace(">>", "»").replace("```", "'''")
}
def void assertFormattedRichStringExpression(CharSequence seq) {
assertFormattedExpression(seq.decode)
}
def void assertFormattedRichString(CharSequence seq) {
assertFormatted(seq.decode)
}
def void assertFormattedRichStringExpression(CharSequence expected, CharSequence actual) {
assertFormattedExpression(expected.decode, actual.decode)
}
def void assertFormattedRichStringExpressionWithErrors(CharSequence actual) {
assertFormattedExpression(null, actual.decode, actual.decode, true)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2016 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2014, 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -8,29 +8,32 @@
*******************************************************************************/
package org.eclipse.xtend.core.tests.formatting

import org.eclipse.xtend.core.tests.RuntimeInjectorProvider
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.testing.formatter.AbstractFormatterTest
import org.junit.Test
import org.junit.runner.RunWith

import static org.eclipse.xtext.xbase.formatting2.XbaseFormatterPreferenceKeys.*

class AnonymousClassFormatterTest extends AbstractXtendFormatterTest {
@RunWith(XtextRunner)
@InjectWith(RuntimeInjectorProvider)
class AnonymousClassFormatterTest extends AbstractFormatterTest {

@Test def formatSingleMember() {
assertFormatted([
put(bracesInNewLine, false)
],'''
'''
class Foo {
val foo = new Runnable() {
override run() {
}
}
}
''')
'''.assertUnformattedEqualsFormatted[put(bracesInNewLine, false)]
}

@Test def formatMultiMember() {
assertFormatted([
put(bracesInNewLine, false)
],'''
'''
class Foo {
val foo = new Runnable() {
int bar
Expand All @@ -42,27 +45,23 @@ class AnonymousClassFormatterTest extends AbstractXtendFormatterTest {
}
}
}
''')
'''.assertUnformattedEqualsFormatted[put(bracesInNewLine, false)]
}

@Test def formatTypeParam() {
assertFormatted([
put(bracesInNewLine, false)
],'''
'''
class Foo {
val foo = new Iterable<String>() {
override iterator() {
null
}
}
}
''')
'''.assertUnformattedEqualsFormatted[put(bracesInNewLine, false)]
}

@Test def formatNested() {
assertFormatted([
put(bracesInNewLine, false)
],'''
'''
import java.util.Iterator
class Foo {
Expand All @@ -72,17 +71,17 @@ class AnonymousClassFormatterTest extends AbstractXtendFormatterTest {
override hasNext() {
true
}
override next() {
null
}
override remove() {
}
}
}
}
}
''')
'''.assertUnformattedEqualsFormatted[put(bracesInNewLine, false)]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2016 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -8,30 +8,30 @@
*******************************************************************************/
package org.eclipse.xtend.core.tests.formatting

import org.eclipse.xtend.core.tests.RuntimeInjectorProvider
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.testing.formatter.AbstractFormatterTest
import org.junit.Test
import org.junit.runner.RunWith

class TypeVariableFormatterTest extends AbstractXtendFormatterTest {
def CharSequence refToFile(CharSequence string) '''
import java.util.*

class Foo {
«string» x
}
'''
def CharSequence paramToFile(CharSequence string) '''
@RunWith(XtextRunner)
@InjectWith(RuntimeInjectorProvider)
class TypeVariableFormatterTest extends AbstractFormatterTest {

private def CharSequence paramToFile(CharSequence string) '''
import java.util.*

class Foo«string» {
}
'''
def assertTypeParam(CharSequence toBeFormatted) {
assertFormatted(toBeFormatted.paramToFile)
toBeFormatted.paramToFile.assertUnformattedEqualsFormatted
}
@Test def integration() {
assertFormatted('''
assertUnformattedEqualsFormatted('''
import java.util.*

class Foo<T extends Collection<?>, K extends Collection<?>> {
Expand Down
Loading

0 comments on commit a4d9485

Please sign in to comment.