From b9391d98b150d460ee65c905bcc28c7d3d5af56b Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 4 Dec 2006 16:17:24 +0000 Subject: Create formatter junit tests --- .../resources/formatter/complex/After.cpp | 26 ++++ .../resources/formatter/complex/Before.cpp | 24 ++++ .../resources/formatter/preview/After.cpp | 43 +++++++ .../resources/formatter/preview/Before.cpp | 5 + .../resources/formatter/sample/After.cpp | 55 +++++++++ .../resources/formatter/sample/Before.cpp | 55 +++++++++ .../resources/formatter/templates/After.cpp | 74 ++++++++++++ .../resources/formatter/templates/Before.cpp | 70 +++++++++++ .../cdt/ui/tests/text/FormatActionTest.java | 134 +++++++++++++++++++++ .../eclipse/cdt/ui/tests/text/TextTestSuite.java | 1 + 10 files changed, 487 insertions(+) create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/complex/After.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/complex/Before.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/preview/After.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/preview/Before.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/sample/After.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/sample/Before.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/resources/formatter/templates/Before.cpp create mode 100644 core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java (limited to 'core/org.eclipse.cdt.ui.tests') diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/complex/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/complex/After.cpp new file mode 100644 index 00000000000..8c3235d3a91 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/complex/After.cpp @@ -0,0 +1,26 @@ + +/* This is sample code to test the formatter */ + +class Complex { +private: + float re; + float im; +public: + Complex(float re, float im) : + re(re), im(im) { + } + float GetRe() { + return re; + } + float GetIm() { + return im; + } + void Set(float r, float i); + /* Set real part */ + void SetRe(float r); + /* + * Set imaginary part + */ + void SetIm(float i); + void Print(); +}; diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/complex/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/complex/Before.cpp new file mode 100644 index 00000000000..7a307a77d04 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/complex/Before.cpp @@ -0,0 +1,24 @@ + + + /* This is sample code to test the formatter */ + + + + class Complex { + private : + float re ; float im; + public: + Complex(float re, float im) : + re(re), im(im) {} + float GetRe() { return re;} +float GetIm() { + return im; + } + void Set(float r, float i); + /* Set real part */ + void SetRe(float r) ; + /* + * Set imaginary part + */ + void SetIm(float i);void Print(); + }; diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/preview/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/preview/After.cpp new file mode 100644 index 00000000000..d595123b02e --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/preview/After.cpp @@ -0,0 +1,43 @@ +/* + * Indentation + */ +#include +class Point { +public: + Point(double xc, double yc) : + x(xc), y(yc) { + } + double distance(const Point& other) const; + int compareX(const Point& other) const; + double x; + double y; +}; +double Point::distance(const Point& other) const { + double dx = x - other.x; + double dy = y - other.y; + return sqrt(dx * dx + dy * dy); +} +int Point::compareX(const Point& other) const { + if (x < other.x) { + return -1; + } else if (x > other.x) { + return 1; + } else { + return 0; + } +} +namespace FOO { +int foo(int bar) const { + switch (bar) { + case 0: + ++bar; + break; + case 1: + --bar; + default: { + bar += bar; + break; + } + } +} +} // end namespace FOO diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/preview/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/preview/Before.cpp new file mode 100644 index 00000000000..a48488347f2 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/preview/Before.cpp @@ -0,0 +1,5 @@ +/* + * Indentation + */ +#include +class Point {public:Point(double xc, double yc) : x(xc), y(yc) {}double distance(const Point& other) const;int compareX(const Point& other) const;double x;double y;};double Point::distance(const Point& other) const {double dx = x - other.x;double dy = y - other.y;return sqrt(dx * dx + dy * dy);}int Point::compareX(const Point& other) const {if (x < other.x) {return -1;} else if (x > other.x) {return 1;} else {return 0;}}namespace FOO {int foo(int bar) const {switch (bar) {case 0:++bar;break;case 1:--bar;default: {bar += bar;break;}}}} // end namespace FOO diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/sample/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/sample/After.cpp new file mode 100644 index 00000000000..4fac3784e72 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/sample/After.cpp @@ -0,0 +1,55 @@ +#include + +const SimpleStruct simpleStruct = + { + 1 + , "mySimple" + , 0.1232 +}; + +#define SIZEOF( A, B ) sizeof( A.B ) + +#define FOREVER \ + for(;;)\ +{\ + \ + } + +const OtherStruct array[] = + { + { +#if FOO + "foo" +# else + "bar" +#endif + , SIZEOF( simpleStruct, num ) + , &t_int + , 0 + } + , { + "name" + , SIZEOF( simpleStruct, floatnum ) + , &t_float + , 1 + } +}; + +// single line outside scope + +void SimpleStruct_construct( + struct SimpleStruct * const this ) + { + // single line + this->num = 1; + this->name = "boo"; + this->floatNum = 1.5; +} + +int ConnectParams_doSomething(const struct SimpleStruct * const this ) + { +/* + * multiline + */ + return 1; + } diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/sample/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/sample/Before.cpp new file mode 100644 index 00000000000..e1578fe118a --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/sample/Before.cpp @@ -0,0 +1,55 @@ +#include + +const SimpleStruct simpleStruct = +{ + 1 + , "mySimple" + , 0.1232 +}; + +#define SIZEOF( A, B ) sizeof( A.B ) + + #define FOREVER \ + for(;;)\ +{\ + \ + } + +const OtherStruct array[] = +{ + { +#if FOO + "foo" + # else + "bar" +#endif + , SIZEOF( simpleStruct, num ) + , &t_int + , 0 + } + , { + "name" + , SIZEOF( simpleStruct, floatnum ) + , &t_float + , 1 + } +}; + +// single line outside scope + +void SimpleStruct_construct( +struct SimpleStruct * const this ) + { +// single line +this->num = 1; +this->name = "boo"; +this->floatNum = 1.5; + } + +int ConnectParams_doSomething( const struct SimpleStruct * const this ) + { +/* + * multiline + */ + return 1; + } diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp new file mode 100644 index 00000000000..fdfe7fe0d5a --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp @@ -0,0 +1,74 @@ +class Key; +class Value; +class SortAlgorithm; +class DefaultSort; +class T; +class X; +class Y; +class Bar; +class Foo { + template void fum(int i); +}; + +// TEMPLATE_STRUCT +template struct Map { + Key * keys; + Value * values; + SortAlgorithm * sortAlgorithm; + Map(); +}; + +// TEMPLATE_CLASS +template class nonVector { +private: + T * head; + +public: + nonVector() { + head =new T(); + } + int length() { + return 1; + } + const T &first() const; +}; + +// TEMPLATE_UNION +template union ArrayOverlay { +public: + X x[size]; + Y y[size]; + + static int numArrays; +}; + +// TEMPLATE_METHODS +class TemplateContainer { + // these are in an enclosing class + template void fum(int i); + template void scrum(void) { + } + ; +}; + +// TEMPLATE_FUNCTION +template const T &nonVector::first() const { + return *head; +} + +template bool IsGreaterThan(X, X); + +template void Foo::fum(int i) { +} + +// TEMPLATE_VARIABLES +template char + * default_alloc_template::S_start_free = 0; + +// an instantiation, not a template: +complex + cf(0,0); +//template > +//Dictionary* TheSpellCheckDictionary; + +int success; diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/Before.cpp new file mode 100644 index 00000000000..10e180dc5fd --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/Before.cpp @@ -0,0 +1,70 @@ +class Key; +class Value; +class SortAlgorithm; +class DefaultSort; +class T; +class X; +class Y; +class Bar; +class Foo { + template void fum(int i); +}; + +// TEMPLATE_STRUCT +template +struct Map +{ + Key* keys; + Value* values; + SortAlgorithm* sortAlgorithm; + Map(); +}; + +// TEMPLATE_CLASS +template class nonVector { +private: T* head; + +public: + nonVector() {head =new T();} + int length() {return 1;} + const T& first() const; +}; + +// TEMPLATE_UNION +template +union ArrayOverlay { +public: + X x[size]; Y y[size]; + + static int numArrays; +}; + +// TEMPLATE_METHODS +class TemplateContainer { + // these are in an enclosing class + template void fum(int i); + template + void scrum(void) {} + ; +}; + +// TEMPLATE_FUNCTION +template const T& nonVector::first() const + { + return *head; +} + +template bool IsGreaterThan(X,X); + +template void Foo::fum(int i) {} + +// TEMPLATE_VARIABLES +template char* default_alloc_template::S_start_free = 0; + +// an instantiation, not a template: +complex + cf(0,0); +//template > +//Dictionary* TheSpellCheckDictionary; + +int success; diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java new file mode 100644 index 00000000000..5c0732260ad --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * Copyright (c) 2005, 2006 IBM Corporation 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: + * IBM Corporation - initial API and implementation + * Anton Leherbauer (Wind River Systems) + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text; + +import java.util.ListResourceBundle; + +import junit.extensions.TestSetup; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.SourceViewer; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.testplugin.CProjectHelper; + +import org.eclipse.cdt.internal.ui.editor.CEditor; + +/** + * Test the Formatter. + */ +public class FormatActionTest extends TestCase { + private static final String PROJECT= "FormatTests"; + + private static final class EmptyBundle extends ListResourceBundle { + protected Object[][] getContents() { + return new Object[0][]; + } + } + + protected static class IndentTestSetup extends TestSetup { + + private ICProject fCProject; + + public IndentTestSetup(Test test) { + super(test); + } + + protected void setUp() throws Exception { + super.setUp(); + + fCProject= EditorTestHelper.createCProject(PROJECT, "resources/formatter"); + fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB); + } + + protected void tearDown () throws Exception { + if (fCProject != null) + CProjectHelper.delete(fCProject); + + super.tearDown(); + } + } + + private static final Class THIS= FormatActionTest.class; + public static Test suite() { + return new IndentTestSetup(new TestSuite(THIS)); + } + + private CEditor fEditor; + private SourceViewer fSourceViewer; + private IDocument fDocument; + + /* + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + String filename= createFileName("Before"); + fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true); + fSourceViewer= EditorTestHelper.getSourceViewer(fEditor); + fDocument= fSourceViewer.getDocument(); + } + + /* + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + EditorTestHelper.closeEditor(fEditor); + } + + private void assertFormatResult() throws Exception { + String afterFile= createFileName("After"); + String expected= ResourceTestHelper.read(afterFile).toString(); + + IAction formatAction= fEditor.getAction("Format"); + assertNotNull("No format action", formatAction); + formatAction.run(); + + assertEquals(expected, fDocument.get()); + } + + private String createFileName(String qualifier) { + String name= getName(); + name= name.substring(4, 5).toLowerCase() + name.substring(5); + return "/" + PROJECT + "/src/" + name + "/" + qualifier + ".cpp"; + } + + private void selectAll() { + fSourceViewer.setSelectedRange(0, fDocument.getLength()); + } + + public void testTemplates() throws Exception { + selectAll(); + assertFormatResult(); + } + + public void testPreview() throws Exception { + selectAll(); + assertFormatResult(); + } + + public void testSample() throws Exception { + selectAll(); + assertFormatResult(); + } + + public void testComplex() throws Exception { + selectAll(); + assertFormatResult(); + } + +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java index ea307d0e0f8..6bea8b483cd 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java @@ -31,6 +31,7 @@ public class TextTestSuite extends TestSuite { addTest(CHeuristicScannerTest.suite()); addTest(BracketInserterTest.suite()); addTest(IndentActionTest.suite()); + addTest(FormatActionTest.suite()); // Break iterator tests. addTest(CBreakIteratorTest.suite()); -- cgit v1.2.3