1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 442754 - Refactor the test harness for semantic highlightings

Change-Id: I649b80e96f87481802c6b2f29029ed42bdb491a8
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
Reviewed-on: https://git.eclipse.org/r/35518
Tested-by: Hudson CI
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
Nathan Ridge 2014-10-26 02:04:14 -04:00 committed by Sergey Prigogin
parent 40cdeb4102
commit 356478286c
5 changed files with 384 additions and 785 deletions

View file

@ -1,161 +0,0 @@
#define INT int
#define FUNCTION_MACRO(arg) globalFunc(arg)
#define EMPTY_MACRO(arg)
enum Enumeration {
enumerator
};
const int globalConstant = 0;
int globalVariable = 0;
static int globalStaticVariable = 0;
void globalFunc(int a);
static void globalStaticFunc() {
EMPTY_MACRO(n);
};
class Base1 {};
class Base2 {};
class ClassContainer : Base1, Base2 {
friend void friendFunc();
friend class FriendClass;
public:
static int staticPubField;
const int constPubField;
const static int constStaticPubField;
int pubField;
static int staticPubMethod(int arg) {
FUNCTION_MACRO(arg);
globalFunc(arg);
return globalStaticVariable;
}
int pubMethod();
enum pubEnumeration {pubEnumerator};
class pubClass{};
class pubStruct{};
class pubUnion{};
typedef pubClass pubTypedef;
protected:
static const int constStaticProtField = 12;
static int staticProtField;
const int constProtField;
int protField;
static int staticProtMethod();
int protMethod();
enum protEnumeration {protEnumerator};
class protClass{};
class protStruct{};
class protUnion{};
typedef protClass protTypedef;
private:
static const int constStaticPrivField = 12;
static int staticPrivField;
const int constPrivField;
int privField;
static int staticPrivMethod();
int privMethod();
enum privEnumeration {privEnumerator};
class privClass{};
class privStruct{};
class privUnion{};
typedef privClass privTypedef;
};
template<class T1, class T2> class TemplateClass {
T1 tArg1;
T2 tArg2;
TemplateClass(T1 arg1, T2 arg2) {
tArg1 = arg1;
tArg2 = arg2;
}
};
template<class T1> class PartialInstantiatedClass : TemplateClass<T1, Base1> {};
struct CppStruct {
int structField;
};
union CppUnion {
int unionField;
CppUnion operator+(CppUnion);
CppUnion operator[](int);
};
typedef CppUnion TUnion;
namespace ns {
int namespaceVar = 0;
int namespaceFunc() {
globalStaticFunc();
return namespaceVar;
}
}
INT ClassContainer::protMethod() {
return protField;
}
INT ClassContainer::pubMethod() {
int localVar = 0;
return pubField + localVar;
}
INT ClassContainer::staticPrivMethod() {
CppStruct* st= new CppStruct();
st->structField= 1;
CppUnion un;
un.unionField= 2;
staticPubMethod(staticPrivField);
un + un[6];
label:
FUNCTION_MACRO(0);
if (un.unionField < st->structField) goto label;
problemMethod();
// external SDK
SDKClass sdkClass;
sdkClass.SDKMethod();
SDKFunction();
return 0;
}
//http://bugs.eclipse.org/209203
template <int n>
int f()
{
return n;
}
//http://bugs.eclipse.org/220392
#define EMPTY
EMPTY int f();
//http://bugs.eclipse.org/340492
template< template<class> class U > class myClass {};
//http://bugs.eclipse.org/372004
void g() {
extern int globalVariable; // declared as global near top
}
//http://bugs.eclipse.org/399149
class C final {
void finalMethod() final;
void overrideMethod() override;
int final; // ordinary field, happens to be named 'final'
};

View file

@ -1,215 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000, 2012 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.io.File;
import java.io.FileOutputStream;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.TestScannerProvider;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.testplugin.Accessor;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.testplugin.ResourceTestHelper;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlighting;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingPresenter;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightings;
/**
* Derived from JDT.
*
* @since 4.0
*/
public class AbstractSemanticHighlightingTest extends TestCase {
protected static class SemanticHighlightingTestSetup extends TestSetup {
private ICProject fCProject;
private final String fTestFilename;
private File fExternalFile;
public SemanticHighlightingTestSetup(Test test, String testFilename) {
super(test);
fTestFilename= testFilename;
}
@Override
protected void setUp() throws Exception {
super.setUp();
String sdkCode=
"void SDKFunction();\n"+
"class SDKClass { public: void SDKMethod(); };\n\n";
fExternalFile= createExternalFile(sdkCode);
assertNotNull(fExternalFile);
// Load the file using option -include to make it part of the index.
TestScannerProvider.sIncludeFiles= new String[] {fExternalFile.getAbsolutePath()};
fCProject= EditorTestHelper.createCProject(PROJECT, LINKED_FOLDER, false, true);
BaseTestCase.waitForIndexer(fCProject);
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(fTestFilename), true);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 500, 10000, 100));
EditorTestHelper.joinBackgroundActivities();
}
private static File createExternalFile(final String code) throws Exception {
File dest = File.createTempFile("external", ".h");
FileOutputStream fos = new FileOutputStream(dest);
fos.write(code.getBytes());
fos.close();
return dest;
}
protected String getTestFilename() {
return fTestFilename;
}
@Override
protected void tearDown () throws Exception {
EditorTestHelper.closeEditor(fEditor);
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setToDefault(PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED);
SemanticHighlighting[] semanticHighlightings= SemanticHighlightings.getSemanticHighlightings();
for (SemanticHighlighting semanticHighlighting : semanticHighlightings) {
String enabledPreferenceKey= SemanticHighlightings.getEnabledPreferenceKey(semanticHighlighting);
if (!store.isDefault(enabledPreferenceKey))
store.setToDefault(enabledPreferenceKey);
}
if (fCProject != null)
CProjectHelper.delete(fCProject);
if (fExternalFile != null) {
fExternalFile.delete();
}
TestScannerProvider.sIncludeFiles= null;
super.tearDown();
}
}
public static final String LINKED_FOLDER= "resources/semanticHighlighting";
public static final String PROJECT= "SHTest";
public static final String TESTFILE= "/SHTest/src/SHTest.cpp";
private static CEditor fEditor;
private static SourceViewer fSourceViewer;
private String fCurrentHighlighting;
private SemanticHighlightingTestSetup fProjectSetup;
@Override
protected void setUp() throws Exception {
super.setUp();
if (!ResourcesPlugin.getWorkspace().getRoot().exists(new Path(PROJECT))) {
fProjectSetup= new SemanticHighlightingTestSetup(this, TESTFILE);
fProjectSetup.setUp();
}
disableAllSemanticHighlightings();
EditorTestHelper.runEventQueue(500);
}
@Override
protected void tearDown() throws Exception {
if (fProjectSetup != null) {
fProjectSetup.tearDown();
fProjectSetup= null;
}
super.tearDown();
}
protected void assertEqualPositions(Position[] expected, Position[] actual) {
assertEquals(expected.length, actual.length);
for (int i= 0, n= expected.length; i < n; i++) {
assertEquals(expected[i].isDeleted(), actual[i].isDeleted());
assertEquals(expected[i].getOffset(), actual[i].getOffset());
assertEquals(expected[i].getLength(), actual[i].getLength());
}
}
protected Position createPosition(int line, int column, int length) throws BadLocationException {
IDocument document= fSourceViewer.getDocument();
return new Position(document.getLineOffset(line) + column, length);
}
String toString(Position[] positions) throws BadLocationException {
StringBuffer buf= new StringBuffer();
buf.append("// "+fCurrentHighlighting+'\n');
IDocument document= fSourceViewer.getDocument();
buf.append("Position[] expected= new Position[] {\n");
for (Position position : positions) {
int line= document.getLineOfOffset(position.getOffset());
int column= position.getOffset() - document.getLineOffset(line);
buf.append("\tcreatePosition(" + line + ", " + column + ", " + position.getLength() + "),\n");
}
buf.append("};\n");
return buf.toString();
}
protected Position[] getSemanticHighlightingPositions() throws BadPositionCategoryException {
SemanticHighlightingManager manager= (SemanticHighlightingManager) new Accessor(fEditor, CEditor.class).get("fSemanticManager");
SemanticHighlightingPresenter presenter= (SemanticHighlightingPresenter) new Accessor(manager, manager.getClass()).get("fPresenter");
String positionCategory= (String) new Accessor(presenter, presenter.getClass()).invoke("getPositionCategory", new Object[0]);
IDocument document= fSourceViewer.getDocument();
return document.getPositions(positionCategory);
}
protected void setUpSemanticHighlighting(String semanticHighlighting) {
fCurrentHighlighting= semanticHighlighting;
enableSemanticHighlighting(semanticHighlighting);
// give enough time to finish updating the highlighting positions
EditorTestHelper.runEventQueue(1000);
}
private void enableSemanticHighlighting(String preferenceKey) {
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setValue(getEnabledPreferenceKey(preferenceKey), true);
}
private String getEnabledPreferenceKey(String preferenceKey) {
return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + preferenceKey + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED_SUFFIX;
}
private static void disableAllSemanticHighlightings() {
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setValue(PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED, true);
SemanticHighlighting[] semanticHilightings= SemanticHighlightings.getSemanticHighlightings();
for (SemanticHighlighting semanticHilighting : semanticHilightings) {
if (store.getBoolean(SemanticHighlightings.getEnabledPreferenceKey(semanticHilighting)))
store.setValue(SemanticHighlightings.getEnabledPreferenceKey(semanticHilighting), false);
}
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2013 IBM Corporation and others.
* Copyright (c) 2000, 2014 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
@ -8,16 +8,51 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Anton Leherbauer (Wind River Systems) - Adapted for CDT
* Nathan Ridge - refactoring
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.SourceViewer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.jface.text.Position;
import org.eclipse.cdt.ui.text.ICColorConstants;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.TestScannerProvider;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.testplugin.Accessor;
import org.eclipse.cdt.ui.testplugin.CTestPlugin;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.testplugin.ResourceTestHelper;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlighting;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingPresenter;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingPresenter.TestHighlightedPosition;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightings;
/**
@ -27,419 +62,323 @@ import org.eclipse.cdt.internal.ui.editor.SemanticHighlightings;
*
* @since 4.0
*/
public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
private static final boolean PRINT_POSITIONS= false;
private static final Class<?> THIS= SemanticHighlightingTest.class;
public class SemanticHighlightingTest extends TestCase {
public static Test suite() {
return new SemanticHighlightingTestSetup(new TestSuite(THIS), TESTFILE);
return new TestSuite(SemanticHighlightingTest.class);
}
public void testStaticFieldHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.STATIC_FIELD);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(25, 15, 14),
createPosition(27, 21, 19),
createPosition(44, 21, 20),
createPosition(45, 15, 15),
createPosition(59, 21, 20),
createPosition(60, 15, 15),
createPosition(122, 20, 15),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
private File fExternalFile;
private ICProject fCProject;
private CEditor fEditor;
private SourceViewer fSourceViewer;
private IIndex fIndex;
private IASTTranslationUnit fAST;
private static File createExternalFile(final String code) throws Exception {
File dest = File.createTempFile("external", ".h");
FileOutputStream fos = new FileOutputStream(dest);
fos.write(code.getBytes());
fos.close();
return dest;
}
public void testFieldHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.FIELD);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(25, 15, 14),
createPosition(26, 14, 13),
createPosition(27, 21, 19),
createPosition(28, 8, 8),
createPosition(44, 21, 20),
createPosition(45, 15, 15),
createPosition(46, 15, 14),
createPosition(47, 8, 9),
createPosition(59, 21, 20),
createPosition(60, 15, 15),
createPosition(61, 15, 14),
createPosition(62, 8, 9),
createPosition(77, 7, 5),
createPosition(78, 7, 5),
createPosition(80, 8, 5),
createPosition(81, 8, 5),
createPosition(89, 8, 11),
createPosition(93, 8, 10),
createPosition(109, 11, 9),
createPosition(114, 11, 8),
createPosition(119, 8, 11),
createPosition(121, 7, 10),
createPosition(122, 20, 15),
createPosition(126, 11, 10),
createPosition(126, 28, 11),
createPosition(159, 8, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
private static void enableAllSemanticHighlightings() {
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setValue(PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED, true);
SemanticHighlighting[] semanticHilightings= SemanticHighlightings.getSemanticHighlightings();
for (SemanticHighlighting semanticHilighting : semanticHilightings) {
String enabledPreferenceKey = SemanticHighlightings.getEnabledPreferenceKey(semanticHilighting);
if (!store.getBoolean(enabledPreferenceKey))
store.setValue(enabledPreferenceKey, true);
}
}
public void testMethodDeclarationHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.METHOD_DECLARATION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(30, 15, 15),
createPosition(35, 8, 9),
createPosition(49, 15, 16),
createPosition(50, 8, 10),
createPosition(64, 15, 16),
createPosition(65, 8, 10),
createPosition(79, 4, 13),
createPosition(94, 13, 9),
createPosition(95, 13, 10),
createPosition(108, 4, 26),
createPosition(112, 4, 25),
createPosition(117, 4, 32),
createPosition(156, 9, 11),
createPosition(157, 9, 14),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
private static void restoreAllSemanticHighlightingToDefaults() {
IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore();
store.setToDefault(PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED);
SemanticHighlighting[] semanticHighlightings= SemanticHighlightings.getSemanticHighlightings();
for (SemanticHighlighting semanticHighlighting : semanticHighlightings) {
String enabledPreferenceKey= SemanticHighlightings.getEnabledPreferenceKey(semanticHighlighting);
if (!store.isDefault(enabledPreferenceKey))
store.setToDefault(enabledPreferenceKey);
}
}
public void testMethodHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.METHOD);
Position[] expected= new Position[] {
createPosition(30, 15, 15),
createPosition(35, 8, 9),
createPosition(49, 15, 16),
createPosition(50, 8, 10),
createPosition(64, 15, 16),
createPosition(65, 8, 10),
createPosition(79, 4, 13),
createPosition(94, 13, 9),
createPosition(95, 13, 10),
createPosition(108, 4, 26),
createPosition(112, 4, 25),
createPosition(117, 4, 32),
createPosition(122, 4, 15),
createPosition(130, 13, 9),
createPosition(156, 9, 11),
createPosition(157, 9, 14),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@Override
protected void setUp() throws Exception {
super.setUp();
enableAllSemanticHighlightings();
SemanticHighlightingPresenter.sIsForTest = true;
StringBuilder[] testData = TestSourceReader.getContentsForTest(CTestPlugin.getDefault().getBundle(), "ui", getClass(), getName(), 0);
if (testData.length == 2) {
fExternalFile= createExternalFile(testData[0].toString());
assertNotNull(fExternalFile);
// Load the file using option -include to make it part of the index.
TestScannerProvider.sIncludeFiles= new String[] {fExternalFile.getAbsolutePath()};
}
public void testStaticMethodInvocationHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.STATIC_METHOD_INVOCATION);
Position[] expected= new Position[] {
createPosition(122, 4, 15),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
fCProject= CProjectHelper.createCCProject("SHTest", "bin", IPDOMManager.ID_FAST_INDEXER);
IFile cppfile = TestSourceReader.createFile(fCProject.getProject(), new Path("SHTest.cpp"),
testData.length == 2 ? testData[1].toString() : testData[0].toString());
IIndexManager indexManager= CCorePlugin.getIndexManager();
indexManager.joinIndexer(5000, new NullProgressMonitor());
BaseTestCase.waitForIndexer(fCProject);
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile("/SHTest/SHTest.cpp"), true);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 500, 10000, 100));
EditorTestHelper.joinBackgroundActivities();
fIndex = CCorePlugin.getIndexManager().getIndex(fCProject);
fIndex.acquireReadLock();
fAST = TestSourceReader.createIndexBasedAST(fIndex, fCProject, cppfile);
}
public void testLocalVariableDeclarationHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE_DECLARATION);
Position[] expected= new Position[] {
createPosition(113, 8, 8),
createPosition(118, 15, 2),
createPosition(120, 13, 2),
createPosition(129, 13, 8),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@Override
protected void tearDown() throws Exception {
fIndex.releaseReadLock();
EditorTestHelper.closeEditor(fEditor);
if (fCProject != null)
CProjectHelper.delete(fCProject);
if (fExternalFile != null) {
fExternalFile.delete();
}
public void testLocalVariableReferencesHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE);
Position[] expected= new Position[] {
createPosition(114, 22, 8),
createPosition(119, 4, 2),
createPosition(121, 4, 2),
createPosition(123, 4, 2),
createPosition(123, 9, 2),
createPosition(126, 8, 2),
createPosition(126, 24, 2),
createPosition(130, 4, 8),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
TestScannerProvider.sIncludeFiles= null;
SemanticHighlightingPresenter.sIsForTest = false;
restoreAllSemanticHighlightingToDefaults();
super.tearDown();
}
public void testParameterVariableHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.PARAMETER_VARIABLE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(12, 20, 1),
createPosition(30, 35, 3),
createPosition(31, 23, 3),
createPosition(32, 19, 3),
createPosition(79, 21, 4),
createPosition(79, 30, 4),
createPosition(80, 16, 4),
createPosition(81, 16, 4),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
private Position[] getSemanticHighlightingPositions() throws BadPositionCategoryException {
SemanticHighlightingManager manager= (SemanticHighlightingManager) new Accessor(fEditor, CEditor.class).get("fSemanticManager");
SemanticHighlightingPresenter presenter= (SemanticHighlightingPresenter) new Accessor(manager, manager.getClass()).get("fPresenter");
String positionCategory= (String) new Accessor(presenter, presenter.getClass()).invoke("getPositionCategory", new Object[0]);
IDocument document= fSourceViewer.getDocument();
return document.getPositions(positionCategory);
}
public void testTemplateParameterHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.TEMPLATE_PARAMETER);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(76, 15, 2),
createPosition(76, 25, 2),
createPosition(77, 4, 2),
createPosition(78, 4, 2),
createPosition(79, 18, 2),
createPosition(79, 27, 2),
createPosition(85, 15, 2),
createPosition(85, 66, 2),
createPosition(136, 14, 1),
createPosition(139, 9, 1),
createPosition(147, 32, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
private void makeAssertions() throws Exception {
IDocument document = fSourceViewer.getDocument();
int lines = document.getNumberOfLines();
List<String>[] expected = new List[lines];
for (int i = 0; i < lines; ++i) {
expected[i] = new ArrayList<String>();
}
for (IASTComment comment : fAST.getComments()) {
String contents = new String(comment.getComment());
if (contents.length() > 2 && contents.substring(0, 3).equals("//$")) {
for (String component : contents.substring(3).split(",")) {
// subtract 1 to make it into a 0-based line number
expected[comment.getFileLocation().getStartingLineNumber() - 1].add(component);
}
}
}
public void testEnumHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.ENUM);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(4, 5, 11),
createPosition(37, 9, 14),
createPosition(52, 9, 15),
createPosition(67, 9, 15),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
List<String>[] actual = new List[lines];
for (int i = 0; i < lines; ++i) {
actual[i] = new ArrayList<String>();
}
for (Position p : getSemanticHighlightingPositions()) {
assertTrue(p instanceof TestHighlightedPosition);
int line = document.getLineOfOffset(p.getOffset());
actual[line].add(((TestHighlightedPosition) p).getPreferenceKey());
}
public void testClassHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.CLASS);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(17, 6, 5),
createPosition(18, 6, 5),
createPosition(20, 6, 14),
createPosition(20, 23, 5),
createPosition(20, 30, 5),
createPosition(22, 17, 11),
createPosition(38, 10, 8),
createPosition(39, 10, 9),
createPosition(40, 10, 8),
createPosition(41, 12, 8),
createPosition(53, 10, 9),
createPosition(54, 10, 10),
createPosition(55, 10, 9),
createPosition(56, 12, 9),
createPosition(68, 10, 9),
createPosition(69, 10, 10),
createPosition(70, 10, 9),
createPosition(71, 12, 9),
createPosition(76, 35, 13),
createPosition(85, 25, 24),
createPosition(85, 52, 13),
createPosition(85, 70, 5),
createPosition(88, 7, 9),
createPosition(92, 6, 8),
createPosition(94, 4, 8),
createPosition(94, 23, 8),
createPosition(95, 4, 8),
createPosition(98, 8, 8),
createPosition(108, 4, 14),
createPosition(112, 4, 14),
createPosition(117, 4, 14),
createPosition(118, 4, 9),
createPosition(118, 23, 9),
createPosition(120, 4, 8),
createPosition(129, 4, 8),
createPosition(147, 42, 7),
createPosition(155, 6, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
assertEqualMaps(actual, expected);
}
public void testFunctionDeclarationHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.FUNCTION_DECLARATION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(12, 5, 10),
createPosition(13, 12, 16),
createPosition(21, 16, 10),
createPosition(102, 8, 13),
createPosition(137, 4, 1),
createPosition(144, 10, 1),
createPosition(150, 5, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
private void assertEqualMaps(List<String>[] actual, List<String>[] expected) {
assertEquals(expected.length, actual.length);
for (int i = 0; i < actual.length; ++i) {
assertEquals("Expected " + expected[i].size() + " positions on line " + i + ", got " + actual[i].size(),
expected[i].size(), actual[i].size());
for (int j = 0; j < actual[i].size(); ++j) {
assertEquals(expected[i].get(j), actual[i].get(j));
}
}
}
public void testFunctionHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.FUNCTION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(12, 5, 10),
createPosition(13, 12, 16),
createPosition(21, 16, 10),
createPosition(32, 8, 10),
createPosition(102, 8, 13),
createPosition(103, 1, 16),
createPosition(131, 4, 11),
createPosition(137, 4, 1),
createPosition(144, 10, 1),
createPosition(150, 5, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
// void SDKFunction();
// class SDKClass { public: void SDKMethod(); };\n\n";
public void testGlobalVariableHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.GLOBAL_VARIABLE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(8, 10, 14),
createPosition(9, 4, 14),
createPosition(10, 11, 20),
createPosition(33, 15, 20),
createPosition(101, 8, 12),
createPosition(104, 8, 12),
createPosition(151, 15, 14),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testMacroDefinitionHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.MACRO_DEFINITION);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(0, 8, 3),
createPosition(1, 8, 14),
createPosition(2, 8, 11),
createPosition(143, 8, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testMacroReferencesHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.MACRO_REFERENCE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(14, 4, 11),
createPosition(31, 8, 14),
createPosition(108, 0, 3),
createPosition(112, 0, 3),
createPosition(117, 0, 3),
createPosition(125, 4, 14),
createPosition(144, 0, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testTypedefHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.TYPEDEF);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(41, 21, 10),
createPosition(56, 22, 11),
createPosition(71, 22, 11),
createPosition(98, 17, 6),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testNamespaceHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.NAMESPACE);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(100, 10, 2),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testLabelHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.LABEL);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(124, 0, 5),
createPosition(126, 46, 5),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testEnumeratorHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.ENUMERATOR);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(5, 4, 10),
createPosition(37, 25, 13),
createPosition(52, 26, 14),
createPosition(67, 26, 14),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testProblemHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.PROBLEM);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(127, 4, 13),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testExternalSDKHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.EXTERNAL_SDK);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(130, 13, 9),
createPosition(131, 4, 11),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testOverloadedOperatorHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.OVERLOADED_OPERATOR);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(123, 7, 1),
createPosition(123, 11, 1),
createPosition(123, 13, 1),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
public void testContextSensitiveKeywordHighlighting() throws Exception {
setUpSemanticHighlighting(ICColorConstants.C_KEYWORD);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(155, 8, 5),
createPosition(156, 23, 5),
createPosition(157, 26, 8),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
//#define INT int //$macroDefinition
//#define FUNCTION_MACRO(arg) globalFunc(arg) //$macroDefinition
//#define EMPTY_MACRO(arg) //$macroDefinition
//#include "SHTest.h"
//enum Enumeration { //$enum
// enumerator //$enumerator
//};
//
//const int globalConstant = 0; //$globalVariable
//int globalVariable = 0; //$globalVariable
//static int globalStaticVariable = 0; //$globalVariable
//
//void globalFunc(int a); //$functionDeclaration,parameterVariable
//static void globalStaticFunc() { //$functionDeclaration
// EMPTY_MACRO(n); //$macroSubstitution
//};
//
//class Base1 {}; //$class
//class Base2 {}; //$class
//
//class ClassContainer : Base1, Base2 { //$class,class,class
// friend void friendFunc(); //$functionDeclaration
// friend class FriendClass; //$class
//
//public:
// static int staticPubField; //$staticField
// const int constPubField; //$field
// const static int constStaticPubField; //$staticField
// int pubField; //$field
//
// static int staticPubMethod(int arg) { //$methodDeclaration,parameterVariable
// FUNCTION_MACRO(arg); //$macroSubstitution,parameterVariable
// globalFunc(arg); //$function,parameterVariable
// return globalStaticVariable; //$globalVariable
// }
// int pubMethod(); //$methodDeclaration
//
// enum pubEnumeration {pubEnumerator}; //$enum,enumerator
// class pubClass{}; //$class
// class pubStruct{}; //$class
// class pubUnion{}; //$class
// typedef pubClass pubTypedef; //$class,typedef
//
//protected:
// static const int constStaticProtField = 12; //$staticField
// static int staticProtField; //$staticField
// const int constProtField; //$field
// int protField; //$field
//
// static int staticProtMethod(); //$methodDeclaration
// int protMethod(); //$methodDeclaration
//
// enum protEnumeration {protEnumerator}; //$enum,enumerator
// class protClass{}; //$class
// class protStruct{}; //$class
// class protUnion{}; //$class
// typedef protClass protTypedef; //$class,typedef
//
//private:
// static const int constStaticPrivField = 12; //$staticField
// static int staticPrivField; //$staticField
// const int constPrivField; //$field
// int privField; //$field
//
// static int staticPrivMethod(); //$methodDeclaration
// int privMethod(); //$methodDeclaration
//
// enum privEnumeration {privEnumerator}; //$enum,enumerator
// class privClass{}; //$class
// class privStruct{}; //$class
// class privUnion{}; //$class
// typedef privClass privTypedef; //$class,typedef
//
//
//};
//
//template<class T1, class T2> class TemplateClass { //$templateParameter,templateParameter,class
// T1 tArg1; //$templateParameter,field
// T2 tArg2; //$templateParameter,field
// TemplateClass(T1 arg1, T2 arg2) { //$methodDeclaration,templateParameter,parameterVariable,templateParameter,parameterVariable
// tArg1 = arg1; //$field,parameterVariable
// tArg2 = arg2; //$field,parameterVariable
// }
//};
//
//template<class T1> class PartialInstantiatedClass //$templateParameter,class
// : TemplateClass<T1, Base1> {}; //$class,templateParameter,class
//
//
//struct CppStruct { //$class
// int structField; //$field
//};
//
//union CppUnion { //$class
// int unionField; //$field
// CppUnion operator+(CppUnion); //$class,methodDeclaration,class
// CppUnion operator[](int); //$class,methodDeclaration
//};
//
//typedef CppUnion TUnion; //$class,typedef
//
//namespace ns { //$namespace
// int namespaceVar = 0; //$globalVariable
// int namespaceFunc() { //$functionDeclaration
// globalStaticFunc(); //$function
// return namespaceVar; //$globalVariable
// }
//}
//
//INT ClassContainer::protMethod() { //$macroSubstitution,methodDeclaration
// return protField; //$field
//}
//
//INT ClassContainer::pubMethod() { //$macroSubstitution,methodDeclaration
// int localVar = 0; //$localVariableDeclaration
// return pubField + localVar; //$field,localVariable
//}
//
//INT ClassContainer::staticPrivMethod() { //$macroSubstitution,methodDeclaration
// CppStruct* st= new CppStruct(); //$class,localVariableDeclaration,class
// st->structField= 1; //$localVariable,field
// CppUnion un; //$class,localVariableDeclaration
// un.unionField= 2; //$localVariable,field
// staticPubMethod(staticPrivField); //$staticMethod,staticField
// un + un[6]; //$localVariable,overloadedOperator,localVariable,overloadedOperator,overloadedOperator
//label: //$label
// FUNCTION_MACRO(0); //$macroSubstitution
// if (un.unionField < st->structField) //$localVariable,field,localVariable,field
// goto label; //$label
// problemMethod(); //$problem
// // external SDK
// SDKClass sdkClass; //$class,localVariableDeclaration
// sdkClass.SDKMethod(); //$localVariable,externalSDK
// SDKFunction(); //$externalSDK
// return 0;
//}
//
////http://bugs.eclipse.org/209203
//template <int n> //$templateParameter
//int f() //$functionDeclaration
//{
// return n; //$templateParameter
//}
//
////http://bugs.eclipse.org/220392
//#define EMPTY //$macroDefinition
//EMPTY int f(); //$macroSubstitution,functionDeclaration
//
////http://bugs.eclipse.org/340492
//template< template<class> class U > //$templateParameter
//class myClass {}; //$class
//
////http://bugs.eclipse.org/372004
//void g() { //$functionDeclaration
// // declared as global near top
// extern int globalVariable; //$globalVariable
//}
//
////http://bugs.eclipse.org/399149
//class C final { //$class,c_keyword
// void finalMethod() final; //$methodDeclaration,c_keyword
// void overrideMethod() override; //$methodDeclaration,c_keyword
//
// // ordinary field, happens to be named 'final'
// int final; //$field
//};
public void testVariousHighlightings() throws Exception {
makeAssertions();
}
}

View file

@ -239,6 +239,32 @@ public class SemanticHighlightingPresenter implements ITextPresentationListener,
/** <code>true</code> iff the current reconcile is canceled. */
private boolean fIsCanceled= false;
/**
* A subclass of HighlightedPosition that records which semantic highlighting
* the position is for. Used for testing.
*
*/
public static class TestHighlightedPosition extends HighlightedPosition {
private String fPreferenceKey;
public String getPreferenceKey() {
return fPreferenceKey;
}
public TestHighlightedPosition(int offset, int length, HighlightingStyle highlighting,
Object lock, String preferenceKey) {
super(offset, length, highlighting, lock);
fPreferenceKey = preferenceKey;
}
}
/**
* A flag that is set when this class is exercised by a test.
* When the flag is set, the positions added to the document are of type
* TestHighlightedPosition rather than HighlightedPosition.
*/
public static boolean sIsForTest = false;
/**
* Creates and returns a new highlighted position with the given offset, length and highlighting.
* <p>
@ -247,12 +273,20 @@ public class SemanticHighlightingPresenter implements ITextPresentationListener,
*
* @param offset The offset
* @param length The length
* @param highlighting The highlighting
* @param style The highlighting
* @return The new highlighted position
*/
public HighlightedPosition createHighlightedPosition(int offset, int length, HighlightingStyle highlighting) {
public HighlightedPosition createHighlightedPosition(int offset, int length, HighlightingStyle style) {
// TODO: reuse deleted positions
return new HighlightedPosition(offset, length, highlighting, fPositionUpdater);
return createHighlightedPosition(offset, length, style, null);
}
public HighlightedPosition createHighlightedPosition(int offset, int length, HighlightingStyle style,
String preferenceKey) {
if (sIsForTest) {
return new TestHighlightedPosition(offset, length, style, fPositionUpdater, preferenceKey);
}
return new HighlightedPosition(offset, length, style, fPositionUpdater);
}
/**

View file

@ -184,9 +184,9 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
SemanticHighlighting semanticHighlighting= fJobSemanticHighlightings[i];
if (fJobHighlightings[i].isEnabled() && semanticHighlighting.consumes(fToken)) {
if (node instanceof IASTName) {
addNameLocation((IASTName) node, fJobHighlightings[i]);
addNameLocation((IASTName) node, i);
} else {
addNodeLocation(node.getFileLocation(), fJobHighlightings[i]);
addNodeLocation(node.getFileLocation(), i);
}
consumed= true;
break;
@ -202,7 +202,7 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
* @param name The name
* @param highlighting The highlighting
*/
private void addNameLocation(IASTName name, HighlightingStyle highlightingStyle) {
private void addNameLocation(IASTName name, int highlighting) {
IASTImageLocation imageLocation= name.getImageLocation();
if (imageLocation != null) {
if (imageLocation.getLocationKind() != IASTImageLocation.MACRO_DEFINITION) {
@ -211,7 +211,7 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
int length= imageLocation.getNodeLength();
if (offset >= 0 && length > 0) {
fMinLocation= offset + length;
addPosition(offset, length, highlightingStyle);
addPosition(offset, length, highlighting);
}
}
}
@ -219,7 +219,7 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
// Fallback in case no image location available.
IASTNodeLocation[] nodeLocations= name.getNodeLocations();
if (nodeLocations.length == 1 && !(nodeLocations[0] instanceof IASTMacroExpansionLocation)) {
addNodeLocation(nodeLocations[0], highlightingStyle);
addNodeLocation(nodeLocations[0], highlighting);
}
}
}
@ -230,7 +230,7 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
* @param nodeLocation The node location
* @param highlighting The highlighting
*/
private void addNodeLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlighting) {
private void addNodeLocation(IASTNodeLocation nodeLocation, int highlighting) {
if (nodeLocation == null) {
return;
}
@ -251,14 +251,15 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
* @param length The range length
* @param highlighting The highlighting
*/
private void addPosition(int offset, int length, HighlightingStyle highlighting) {
private void addPosition(int offset, int length, int highlighting) {
boolean isExisting= false;
// TODO: use binary search
HighlightingStyle style = fJobHighlightings[highlighting];
for (int i= 0, n= fRemovedPositions.size(); i < n; i++) {
HighlightedPosition position= fRemovedPositions.get(i);
if (position == null)
continue;
if (position.isEqual(offset, length, highlighting)) {
if (position.isEqual(offset, length, style)) {
isExisting= true;
fRemovedPositions.set(i, null);
fNOfRemovedPositions--;
@ -267,7 +268,8 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
}
if (!isExisting) {
HighlightedPosition position= fJobPresenter.createHighlightedPosition(offset, length, highlighting);
HighlightedPosition position= fJobPresenter.createHighlightedPosition(offset, length, style,
fJobSemanticHighlightings[highlighting].getPreferenceKey());
fAddedPositions.add(position);
}
}