Adding unit tests. Minor code cleanup.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Strings_firstCharToLowerCase_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Strings_firstCharToLowerCase_Test.java new file mode 100644 index 0000000..1b4b7a4 --- /dev/null +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Strings_firstCharToLowerCase_Test.java
@@ -0,0 +1,42 @@ +/* + * Copyright (c) 2011 Google Inc. + * + * 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 com.google.eclipse.protobuf.ui.util; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests for <code>{@link Strings#firstCharToLowerCase(String)}</code> + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class Strings_firstCharToLowerCase_Test { + + private static Strings strings; + + @BeforeClass public static void setUpOnce() { + strings = new Strings(); + } + + @Test public void should_return_null() { + assertThat(strings.firstCharToLowerCase(null), nullValue()); + } + + @Test public void should_return_empty_String() { + assertThat(strings.firstCharToLowerCase(""), equalTo("")); + } + + @Test public void should_return_String_with_first_char_lower_case() { + assertThat(strings.firstCharToLowerCase("HElLo"), equalTo("hElLo")); + } +}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Literals.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Literals.java index 2ec9a2f..b4238e9 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Literals.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Literals.java
@@ -9,8 +9,9 @@ package com.google.eclipse.protobuf.ui.util; import static java.lang.Math.max; +import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType; -import org.eclipse.emf.ecore.EObject; +import java.util.List; import com.google.eclipse.protobuf.protobuf.Literal; import com.google.inject.Singleton; @@ -43,10 +44,9 @@ */ public int calculateIndexOf(Literal l) { int index = -1; - EObject container = l.eContainer(); - for (EObject o : container.eContents()) { - if (o == l || !(o instanceof Literal)) continue; - Literal c = (Literal) o; + List<Literal> allLiterals = getAllContentsOfType(l.eContainer(), Literal.class); + for (Literal c : allLiterals) { + if (c == l) continue; index = max(index, c.getIndex()); } return ++index;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java index 6f0523c..a484024 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java
@@ -9,8 +9,10 @@ package com.google.eclipse.protobuf.ui.util; import static java.lang.Math.max; +import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType; -import org.eclipse.emf.ecore.EObject; +import java.util.List; + import org.eclipse.xtext.Keyword; import com.google.eclipse.protobuf.protobuf.*; @@ -99,9 +101,9 @@ */ public int calculateTagNumberOf(Property p) { int index = 0; - for (EObject o : p.eContainer().eContents()) { - if (o == p || !(o instanceof Property)) continue; - Property c = (Property) o; + List<Property> allProperties = getAllContentsOfType(p.eContainer(), Property.class); + for (Property c : allProperties) { + if (c == p) continue; index = max(index, c.getIndex()); } return ++index;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java index e20982d..645dee6 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java
@@ -20,6 +20,11 @@ @Singleton public class Strings { + /** + * Returns a {@code String} with the same content as the given one, but with the first character in lower case. + * @param s the original {@code String} + * @return a {@code String} with the same content as the given one, but with the first character in lower case. + */ public String firstCharToLowerCase(String s) { if (s == null) return null; if (s.length() == 0) return s;