Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Leherbauer2006-12-04 11:17:24 -0500
committerAnton Leherbauer2006-12-04 11:17:24 -0500
commitb9391d98b150d460ee65c905bcc28c7d3d5af56b (patch)
tree97d4f17d6da085a0ae22e3cea8423f1459d2fdaa /core/org.eclipse.cdt.ui.tests
parentb9f960d0c81ecff9c5702a0547b9260d1e739832 (diff)
downloadorg.eclipse.cdt-b9391d98b150d460ee65c905bcc28c7d3d5af56b.tar.gz
org.eclipse.cdt-b9391d98b150d460ee65c905bcc28c7d3d5af56b.tar.xz
org.eclipse.cdt-b9391d98b150d460ee65c905bcc28c7d3d5af56b.zip
Create formatter junit tests
Diffstat (limited to 'core/org.eclipse.cdt.ui.tests')
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/complex/After.cpp26
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/complex/Before.cpp24
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/preview/After.cpp43
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/preview/Before.cpp5
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/sample/After.cpp55
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/sample/Before.cpp55
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp74
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/formatter/templates/Before.cpp70
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java134
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java1
10 files changed, 487 insertions, 0 deletions
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 <math.h>
+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 <math.h>
+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 <Simple.h>
+
+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 <Simple.h>
+
+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<class Bar> void fum(int i);
+};
+
+// TEMPLATE_STRUCT
+template<class Key, class Value, class SortAlgorithm=DefaultSort> struct Map {
+ Key * keys;
+ Value * values;
+ SortAlgorithm * sortAlgorithm;
+ Map();
+};
+
+// TEMPLATE_CLASS
+template<class T> class nonVector {
+private:
+ T * head;
+
+public:
+ nonVector() {
+ head =new T();
+ }
+ int length() {
+ return 1;
+ }
+ const T &first() const;
+};
+
+// TEMPLATE_UNION
+template<class X, class Y, int size=16> union ArrayOverlay {
+public:
+ X x[size];
+ Y y[size];
+
+ static int numArrays;
+};
+
+// TEMPLATE_METHODS
+class TemplateContainer {
+ // these are in an enclosing class
+ template<class Bar> void fum(int i);
+ template<int> void scrum(void) {
+ }
+ ;
+};
+
+// TEMPLATE_FUNCTION
+template<class T> const T &nonVector<T>::first() const {
+ return *head;
+}
+
+template<class X> bool IsGreaterThan(X, X);
+
+template<class Bar> void Foo::fum(int i) {
+}
+
+// TEMPLATE_VARIABLES
+template<bool threads, int inst> char
+ * default_alloc_template<threads, inst>::S_start_free = 0;
+
+// an instantiation, not a template:
+complex
+<float> cf(0,0);
+//template<class Language, class CharacterSet, class SortAlgorithm<CharacterSet> >
+//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<class Bar> void fum(int i);
+};
+
+// TEMPLATE_STRUCT
+template<class Key, class Value, class SortAlgorithm=DefaultSort>
+struct Map
+{
+ Key* keys;
+ Value* values;
+ SortAlgorithm* sortAlgorithm;
+ Map();
+};
+
+// TEMPLATE_CLASS
+template<class T> class nonVector {
+private: T* head;
+
+public:
+ nonVector() {head =new T();}
+ int length() {return 1;}
+ const T& first() const;
+};
+
+// TEMPLATE_UNION
+template<class X, class Y, int size=16>
+union ArrayOverlay {
+public:
+ X x[size]; Y y[size];
+
+ static int numArrays;
+};
+
+// TEMPLATE_METHODS
+class TemplateContainer {
+ // these are in an enclosing class
+ template<class Bar> void fum(int i);
+ template<int>
+ void scrum(void) {}
+ ;
+};
+
+// TEMPLATE_FUNCTION
+template<class T> const T& nonVector<T>::first() const
+ {
+ return *head;
+}
+
+template<class X> bool IsGreaterThan(X,X);
+
+template<class Bar> void Foo::fum(int i) {}
+
+// TEMPLATE_VARIABLES
+template <bool threads, int inst> char* default_alloc_template<threads, inst>::S_start_free = 0;
+
+// an instantiation, not a template:
+complex
+<float> cf(0,0);
+//template<class Language, class CharacterSet, class SortAlgorithm<CharacterSet> >
+//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());

Back to the top