Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2015-01-06 03:09:44 +0000
committerSergey Prigogin2015-01-06 03:09:44 +0000
commitbcc3aad8409b9cf37c76c0c01b320243d9ff4d1d (patch)
treecbaaefee0cbc2c9c08876ec297928fb5a0b8ef5d
parent75900af3aee09a6a11a8c62b1a11006968dbe285 (diff)
downloadorg.eclipse.cdt-bcc3aad8409b9cf37c76c0c01b320243d9ff4d1d.tar.gz
org.eclipse.cdt-bcc3aad8409b9cf37c76c0c01b320243d9ff4d1d.tar.xz
org.eclipse.cdt-bcc3aad8409b9cf37c76c0c01b320243d9ff4d1d.zip
Bug 456099. Added a test for NameComposer.createByExample method.
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java89
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java14
2 files changed, 63 insertions, 40 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java
index afaec7ed7b..c230bf771a 100644
--- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java
+++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/NameComposerTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011 Google, Inc and others.
+ * Copyright (c) 2011, 2015 Google, Inc 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
@@ -10,12 +10,12 @@
*******************************************************************************/
package org.eclipse.cdt.ui.tests.refactoring.utils;
-import org.eclipse.cdt.ui.PreferenceConstants;
-
-import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility;
-
+import static org.eclipse.cdt.internal.corext.codemanipulation.StubUtility.trimFieldName;
+import static org.eclipse.cdt.internal.ui.util.NameComposer.createByExample;
import junit.framework.TestCase;
+import org.eclipse.cdt.ui.PreferenceConstants;
+
import org.eclipse.cdt.internal.ui.util.NameComposer;
public class NameComposerTest extends TestCase {
@@ -25,6 +25,16 @@ public class NameComposerTest extends TestCase {
private static final int CAPITALIZATION_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_CAMEL_CASE;
private static final int CAPITALIZATION_LOWER_CAMEL_CASE = PreferenceConstants.NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE;
+ private NameComposer assertCreatedByExample(String seedName, String composedName,
+ int defaultCapitalization, String defaultDelimiter) {
+ NameComposer composer =
+ createByExample(seedName, composedName, defaultCapitalization, defaultDelimiter);
+ assertNotNull("Failed to create a name composer for \"" + seedName + "\", \"" + composedName + "\".",
+ composer);
+ assertEquals(composedName, composer.compose(seedName));
+ return composer;
+ }
+
public void testCompose() {
NameComposer composer = new NameComposer(CAPITALIZATION_ORIGINAL, "", "", ".h");
assertEquals("MyClass.h", composer.compose("MyClass"));
@@ -46,44 +56,51 @@ public class NameComposerTest extends TestCase {
composer = new NameComposer(CAPITALIZATION_ORIGINAL, "_", "", "");
assertEquals("RGB_Value", composer.compose("RGBValue"));
}
-
+
+ public void testCreateByExample() {
+ NameComposer composer = assertCreatedByExample("MyName", "prefix_my-name_suffix", CAPITALIZATION_ORIGINAL, "");
+ assertEquals("prefix_einstein-podolsky-rosen_suffix", composer.compose("EinsteinPodolskyRosen"));
+ assertCreatedByExample("MyName", "PREFIX-MYNAME-SUFFIX", CAPITALIZATION_ORIGINAL, "_");
+ assertNull(createByExample("Class", "Classic", CAPITALIZATION_ORIGINAL, ""));
+ }
+
public void testTrimFieldName() {
- assertEquals("f", StubUtility.trimFieldName("f_"));
- assertEquals("F", StubUtility.trimFieldName("F_"));
- assertEquals("oo", StubUtility.trimFieldName("F_oo"));
- assertEquals("o", StubUtility.trimFieldName("f_o"));
+ assertEquals("f", trimFieldName("f_"));
+ assertEquals("F", trimFieldName("F_"));
+ assertEquals("oo", trimFieldName("F_oo"));
+ assertEquals("o", trimFieldName("f_o"));
- assertEquals("M", StubUtility.trimFieldName("a_M_"));
- assertEquals("bs", StubUtility.trimFieldName("a_bs_"));
- assertEquals("foo_bar", StubUtility.trimFieldName("foo_bar"));
- assertEquals("foo_bar", StubUtility.trimFieldName("foo_bar_"));
+ assertEquals("M", trimFieldName("a_M_"));
+ assertEquals("bs", trimFieldName("a_bs_"));
+ assertEquals("foo_bar", trimFieldName("foo_bar"));
+ assertEquals("foo_bar", trimFieldName("foo_bar_"));
- assertEquals("foo_b", StubUtility.trimFieldName("foo_b_"));
+ assertEquals("foo_b", trimFieldName("foo_b_"));
- assertEquals("foo", StubUtility.trimFieldName("foo"));
- assertEquals("foo", StubUtility.trimFieldName("_foo"));
- assertEquals("bar", StubUtility.trimFieldName("_f_bar"));
+ assertEquals("foo", trimFieldName("foo"));
+ assertEquals("foo", trimFieldName("_foo"));
+ assertEquals("bar", trimFieldName("_f_bar"));
- assertEquals("f", StubUtility.trimFieldName("f__"));
- assertEquals("f", StubUtility.trimFieldName("__f"));
- assertEquals("O__b", StubUtility.trimFieldName("fO__b"));
- assertEquals("Oo", StubUtility.trimFieldName("fOo"));
- assertEquals("O", StubUtility.trimFieldName("fO"));
- assertEquals("MyStatic", StubUtility.trimFieldName("sMyStatic"));
- assertEquals("MyMember", StubUtility.trimFieldName("mMyMember"));
+ assertEquals("f", trimFieldName("f__"));
+ assertEquals("f", trimFieldName("__f"));
+ assertEquals("O__b", trimFieldName("fO__b"));
+ assertEquals("Oo", trimFieldName("fOo"));
+ assertEquals("O", trimFieldName("fO"));
+ assertEquals("MyStatic", trimFieldName("sMyStatic"));
+ assertEquals("MyMember", trimFieldName("mMyMember"));
- assertEquals("8", StubUtility.trimFieldName("_8"));
+ assertEquals("8", trimFieldName("_8"));
- assertEquals("8bar", StubUtility.trimFieldName("_8bar_"));
- assertEquals("8bar_8", StubUtility.trimFieldName("_8bar_8"));
- assertEquals("8bAr", StubUtility.trimFieldName("_8bAr"));
- assertEquals("b8Ar", StubUtility.trimFieldName("_b8Ar"));
+ assertEquals("8bar", trimFieldName("_8bar_"));
+ assertEquals("8bar_8", trimFieldName("_8bar_8"));
+ assertEquals("8bAr", trimFieldName("_8bAr"));
+ assertEquals("b8Ar", trimFieldName("_b8Ar"));
- assertEquals("Id", StubUtility.trimFieldName("Id"));
- assertEquals("ID", StubUtility.trimFieldName("ID"));
- assertEquals("IDS", StubUtility.trimFieldName("IDS"));
- assertEquals("ID", StubUtility.trimFieldName("bID"));
- assertEquals("Id", StubUtility.trimFieldName("MId"));
- assertEquals("IdA", StubUtility.trimFieldName("IdA"));
+ assertEquals("Id", trimFieldName("Id"));
+ assertEquals("ID", trimFieldName("ID"));
+ assertEquals("IDS", trimFieldName("IDS"));
+ assertEquals("ID", trimFieldName("bID"));
+ assertEquals("Id", trimFieldName("MId"));
+ assertEquals("IdA", trimFieldName("IdA"));
}
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java
index 394ab247a2..0f8888a74f 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/NameComposer.java
@@ -11,6 +11,7 @@
package org.eclipse.cdt.internal.ui.util;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import com.ibm.icu.text.BreakIterator;
@@ -173,14 +174,19 @@ public class NameComposer {
if (seedWords.isEmpty())
return null;
List<CharSequence> composedWords = splitIntoWords(composedName);
+ String delimiter = defaultWordDelimiter;
int numPrefixWords = indexOfSublistIgnoreCase(composedWords, seedWords);
- if (numPrefixWords < 0)
- return null;
+ if (numPrefixWords < 0) {
+ delimiter = ""; //$NON-NLS-1$
+ seedWords = Collections.<CharSequence>singletonList(seedName);
+ numPrefixWords = indexOfSublistIgnoreCase(composedWords, seedWords);
+ if (numPrefixWords < 0)
+ return null;
+ }
String prefix = deducePrefix(composedName, numPrefixWords);
- String delimiter = defaultWordDelimiter;
if (seedWords.size() > 1) {
delimiter = ""; //$NON-NLS-1$
- int start = prefix.length() + composedWords.get(0).length();
+ int start = prefix.length() + composedWords.get(numPrefixWords).length();
for (int i = start; i < composedName.length(); i++) {
if (Character.isLetterOrDigit(composedName.charAt(i))) {
delimiter = composedName.substring(start, i);

Back to the top