From 448b0253952b1dd101d0dcdc4e43754c2f4a5168 Mon Sep 17 00:00:00 2001 From: Moritz Eysholdt Date: Tue, 10 Jan 2017 14:45:39 +0100 Subject: [PATCH] see #199: added comment formatter tests Signed-off-by: Moritz Eysholdt --- .../internal/CommentFormatterTest.xtend | 147 +++++++++++ .../internal/CommentFormatterTest.java | 248 ++++++++++++++++++ 2 files changed, 395 insertions(+) create mode 100644 org.eclipse.xtext.tests/src/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.xtend create mode 100644 org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.java diff --git a/org.eclipse.xtext.tests/src/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.xtend b/org.eclipse.xtext.tests/src/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.xtend new file mode 100644 index 000000000..f4d55b10d --- /dev/null +++ b/org.eclipse.xtext.tests/src/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.xtend @@ -0,0 +1,147 @@ +/******************************************************************************* + * Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) 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 + *******************************************************************************/ +package org.eclipse.xtext.formatting2.internal + +import com.google.inject.Inject +import org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList +import org.eclipse.xtext.formatting2.internal.tests.FormatterTestLanguageInjectorProvider +import org.eclipse.xtext.testing.InjectWith +import org.eclipse.xtext.testing.XtextRunner +import org.junit.Test +import org.junit.runner.RunWith + +/** + * @author Moritz Eysholdt - Initial contribution and API + */ +@RunWith(XtextRunner) +@InjectWith(FormatterTestLanguageInjectorProvider) +class CommentFormatterTest { + @Inject extension GenericFormatterTester + //@Inject extension FormatterTestLanguageGrammarAccess + + @Test def void SL_inline() { + assertFormatted[ + toBeFormatted = ''' + idlist //x + a + ''' + formatter = [ IDList model, extension regions, extension document | + model.regionFor.keyword("idlist").append[oneSpace] + ] + expectation = ''' + idlist //x + a + ''' + ] + } + + @Test def void SL_multiline() { + assertFormatted[ + toBeFormatted = ''' + idlist + + //x + + + a + ''' + formatter = [ IDList model, extension regions, extension document | + model.regionFor.keyword("idlist").append[oneSpace] + ] + expectation = ''' + idlist //x + a + ''' + ] + } + + @Test def void MLSL_inline() { + assertFormatted[ + toBeFormatted = ''' + idlist /*x*/ a + ''' + formatter = [ IDList model, extension regions, extension document | + model.regionFor.keyword("idlist").append[oneSpace] + ] + expectation = ''' + idlist /*x*/ a + ''' + ] + } + + @Test def void MLSL_paragraph() { + assertFormatted[ + toBeFormatted = ''' + idlist + + + /*x*/ + + + a + ''' + formatter = [ IDList model, extension regions, extension document | + model.regionFor.keyword("idlist").append[oneSpace] + ] + expectation = ''' + idlist /*x*/ + a + ''' + ] + } + + @Test def void MLML_inline() { + assertFormatted[ + toBeFormatted = ''' + idlist /* + x + */ a + ''' + formatter = [ IDList model, extension regions, extension document | + model.regionFor.keyword("idlist").append[oneSpace] + ] + expectation = ''' + idlist + /* + * x + */ + a + ''' + ] + } + + @Test def void MLML_paragraph() { + assertFormatted[ + toBeFormatted = ''' + idlist + + + /* + x + */ + + + a + ''' + formatter = [ IDList model, extension regions, extension document | + model.regionFor.keyword("idlist").append[oneSpace] + ] + expectation = ''' + idlist + + + /* + * x + */ + a + ''' + ] + } + + +} diff --git a/org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.java b/org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.java new file mode 100644 index 000000000..446a4d84a --- /dev/null +++ b/org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/formatting2/internal/CommentFormatterTest.java @@ -0,0 +1,248 @@ +/** + * Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) 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 + */ +package org.eclipse.xtext.formatting2.internal; + +import com.google.inject.Inject; +import org.eclipse.xtend2.lib.StringConcatenation; +import org.eclipse.xtext.formatting2.IFormattableDocument; +import org.eclipse.xtext.formatting2.IHiddenRegionFormatter; +import org.eclipse.xtext.formatting2.internal.GenericFormatter; +import org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest; +import org.eclipse.xtext.formatting2.internal.GenericFormatterTester; +import org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList; +import org.eclipse.xtext.formatting2.internal.tests.FormatterTestLanguageInjectorProvider; +import org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.XtextRunner; +import org.eclipse.xtext.xbase.lib.Extension; +import org.eclipse.xtext.xbase.lib.Procedures.Procedure1; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Moritz Eysholdt - Initial contribution and API + */ +@RunWith(XtextRunner.class) +@InjectWith(FormatterTestLanguageInjectorProvider.class) +@SuppressWarnings("all") +public class CommentFormatterTest { + @Inject + @Extension + private GenericFormatterTester _genericFormatterTester; + + @Test + public void SL_inline() { + final Procedure1 _function = (GenericFormatterTestRequest it) -> { + StringConcatenation _builder = new StringConcatenation(); + _builder.append("idlist //x"); + _builder.newLine(); + _builder.append("a"); + _builder.newLine(); + it.setToBeFormatted(_builder); + final GenericFormatter _function_1 = new GenericFormatter() { + @Override + protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) { + final Procedure1 _function = (IHiddenRegionFormatter it_1) -> { + it_1.oneSpace(); + }; + document.append(regions.regionFor(model).keyword("idlist"), _function); + } + }; + it.setFormatter(_function_1); + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append("idlist //x"); + _builder_1.newLine(); + _builder_1.append("a"); + _builder_1.newLine(); + it.setExpectation(_builder_1); + }; + this._genericFormatterTester.assertFormatted(_function); + } + + @Test + public void SL_multiline() { + final Procedure1 _function = (GenericFormatterTestRequest it) -> { + StringConcatenation _builder = new StringConcatenation(); + _builder.append("idlist "); + _builder.newLine(); + _builder.newLine(); + _builder.append("//x"); + _builder.newLine(); + _builder.newLine(); + _builder.newLine(); + _builder.append("a"); + _builder.newLine(); + it.setToBeFormatted(_builder); + final GenericFormatter _function_1 = new GenericFormatter() { + @Override + protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) { + final Procedure1 _function = (IHiddenRegionFormatter it_1) -> { + it_1.oneSpace(); + }; + document.append(regions.regionFor(model).keyword("idlist"), _function); + } + }; + it.setFormatter(_function_1); + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append("idlist //x"); + _builder_1.newLine(); + _builder_1.append("a"); + _builder_1.newLine(); + it.setExpectation(_builder_1); + }; + this._genericFormatterTester.assertFormatted(_function); + } + + @Test + public void MLSL_inline() { + final Procedure1 _function = (GenericFormatterTestRequest it) -> { + StringConcatenation _builder = new StringConcatenation(); + _builder.append("idlist /*x*/ a"); + _builder.newLine(); + it.setToBeFormatted(_builder); + final GenericFormatter _function_1 = new GenericFormatter() { + @Override + protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) { + final Procedure1 _function = (IHiddenRegionFormatter it_1) -> { + it_1.oneSpace(); + }; + document.append(regions.regionFor(model).keyword("idlist"), _function); + } + }; + it.setFormatter(_function_1); + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append("idlist /*x*/ a"); + _builder_1.newLine(); + it.setExpectation(_builder_1); + }; + this._genericFormatterTester.assertFormatted(_function); + } + + @Test + public void MLSL_paragraph() { + final Procedure1 _function = (GenericFormatterTestRequest it) -> { + StringConcatenation _builder = new StringConcatenation(); + _builder.append("idlist"); + _builder.newLine(); + _builder.newLine(); + _builder.newLine(); + _builder.append("/*x*/"); + _builder.newLine(); + _builder.newLine(); + _builder.newLine(); + _builder.append("a"); + _builder.newLine(); + it.setToBeFormatted(_builder); + final GenericFormatter _function_1 = new GenericFormatter() { + @Override + protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) { + final Procedure1 _function = (IHiddenRegionFormatter it_1) -> { + it_1.oneSpace(); + }; + document.append(regions.regionFor(model).keyword("idlist"), _function); + } + }; + it.setFormatter(_function_1); + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append("idlist /*x*/"); + _builder_1.newLine(); + _builder_1.append("a"); + _builder_1.newLine(); + it.setExpectation(_builder_1); + }; + this._genericFormatterTester.assertFormatted(_function); + } + + @Test + public void MLML_inline() { + final Procedure1 _function = (GenericFormatterTestRequest it) -> { + StringConcatenation _builder = new StringConcatenation(); + _builder.append("idlist /*"); + _builder.newLine(); + _builder.append("x"); + _builder.newLine(); + _builder.append("*/ a"); + _builder.newLine(); + it.setToBeFormatted(_builder); + final GenericFormatter _function_1 = new GenericFormatter() { + @Override + protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) { + final Procedure1 _function = (IHiddenRegionFormatter it_1) -> { + it_1.oneSpace(); + }; + document.append(regions.regionFor(model).keyword("idlist"), _function); + } + }; + it.setFormatter(_function_1); + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append("idlist"); + _builder_1.newLine(); + _builder_1.append("/*"); + _builder_1.newLine(); + _builder_1.append(" "); + _builder_1.append("* x"); + _builder_1.newLine(); + _builder_1.append(" "); + _builder_1.append("*/"); + _builder_1.newLine(); + _builder_1.append("a"); + _builder_1.newLine(); + it.setExpectation(_builder_1); + }; + this._genericFormatterTester.assertFormatted(_function); + } + + @Test + public void MLML_paragraph() { + final Procedure1 _function = (GenericFormatterTestRequest it) -> { + StringConcatenation _builder = new StringConcatenation(); + _builder.append("idlist"); + _builder.newLine(); + _builder.newLine(); + _builder.newLine(); + _builder.append("/*"); + _builder.newLine(); + _builder.append("x"); + _builder.newLine(); + _builder.append("*/"); + _builder.newLine(); + _builder.newLine(); + _builder.newLine(); + _builder.append("a"); + _builder.newLine(); + it.setToBeFormatted(_builder); + final GenericFormatter _function_1 = new GenericFormatter() { + @Override + protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) { + final Procedure1 _function = (IHiddenRegionFormatter it_1) -> { + it_1.oneSpace(); + }; + document.append(regions.regionFor(model).keyword("idlist"), _function); + } + }; + it.setFormatter(_function_1); + StringConcatenation _builder_1 = new StringConcatenation(); + _builder_1.append("idlist"); + _builder_1.newLine(); + _builder_1.newLine(); + _builder_1.newLine(); + _builder_1.append("/*"); + _builder_1.newLine(); + _builder_1.append(" "); + _builder_1.append("* x"); + _builder_1.newLine(); + _builder_1.append(" "); + _builder_1.append("*/"); + _builder_1.newLine(); + _builder_1.append("a"); + _builder_1.newLine(); + it.setExpectation(_builder_1); + }; + this._genericFormatterTester.assertFormatted(_function); + } +}