Added unit tests. Cleaned up code.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_enumTypeOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_enumTypeOf_Test.java new file mode 100644 index 0000000..a8ae9e5 --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_enumTypeOf_Test.java
@@ -0,0 +1,64 @@ +/* + * 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.util; + +import static com.google.eclipse.protobuf.junit.Finder.findProperty; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.*; +import com.google.eclipse.protobuf.protobuf.Enum; + +/** + * Tests for <code>{@link ProtobufElementFinder#enumTypeOf(Property)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class ProtobufElementFinder_enumTypeOf_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private ProtobufElementFinder finder; + + @Before public void setUp() { + finder = xtext.getInstanceOf(ProtobufElementFinder.class); + } + + @Test public void should_return_enum_if_property_type_is_enum() { + StringBuilder proto = new StringBuilder(); + proto.append("enum PhoneType { ") + .append(" MOBILE = 0; ") + .append(" HOME = 1; ") + .append(" WORK = 2; ") + .append("} ") + .append(" ") + .append("message PhoneNumber { ") + .append(" optional PhoneType type = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property type = findProperty("type", root); + Enum phoneType = finder.enumTypeOf(type); + assertThat(phoneType.getName(), equalTo("PhoneType")); + } + + @Test public void should_return_null_if_property_type_is_not_enum() { + StringBuilder proto = new StringBuilder(); + proto.append("message Person { ") + .append(" optional string name = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property name = findProperty("name", root); + Enum anEnum = finder.enumTypeOf(name); + assertThat(anEnum, nullValue()); + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_packageOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_packageOf_Test.java new file mode 100644 index 0000000..e1da903 --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_packageOf_Test.java
@@ -0,0 +1,61 @@ +/* + * 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.util; + +import static com.google.eclipse.protobuf.junit.Finder.findProperty; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +import org.eclipse.emf.ecore.EObject; +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.*; +import com.google.eclipse.protobuf.protobuf.Package; + +/** + * Tests for <code>{@link ProtobufElementFinder#packageOf(EObject)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class ProtobufElementFinder_packageOf_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private ProtobufElementFinder finder; + + @Before public void setUp() { + finder = xtext.getInstanceOf(ProtobufElementFinder.class); + } + + @Test public void should_return_package_if_proto_has_one() { + StringBuilder proto = new StringBuilder(); + proto.append("package person.test; ") + .append(" ") + .append("message Person { ") + .append(" optional int32 id = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property id = findProperty("id", root); + Package aPackage = finder.packageOf(id); + assertThat(aPackage.getName(), equalTo("person.test")); + } + + @Test public void should_return_null_if_proto_does_not_have_package() { + StringBuilder proto = new StringBuilder(); + proto.append("message Person { ") + .append(" optional int32 id = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property id = findProperty("id", root); + Package aPackage = finder.packageOf(id); + assertThat(aPackage, nullValue()); + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_rootOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_rootOf_Test.java new file mode 100644 index 0000000..2450b54 --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_rootOf_Test.java
@@ -0,0 +1,46 @@ +/* + * 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.util; + +import static com.google.eclipse.protobuf.junit.Finder.findProperty; +import static org.hamcrest.core.IsSame.sameInstance; +import static org.junit.Assert.assertThat; + +import org.eclipse.emf.ecore.EObject; +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.Property; +import com.google.eclipse.protobuf.protobuf.Protobuf; + +/** + * Tests for <code>{@link ProtobufElementFinder#rootOf(EObject)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class ProtobufElementFinder_rootOf_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private ProtobufElementFinder finder; + + @Before public void setUp() { + finder = xtext.getInstanceOf(ProtobufElementFinder.class); + } + + @Test public void should_return_root_of_proto() { + StringBuilder proto = new StringBuilder(); + proto.append("message Person { ") + .append(" optional string name = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property name = findProperty("name", root); + assertThat(finder.rootOf(name), sameInstance(root)); + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_scalarTypeOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_scalarTypeOf_Test.java new file mode 100644 index 0000000..7b12cc5 --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/ProtobufElementFinder_scalarTypeOf_Test.java
@@ -0,0 +1,63 @@ +/* + * 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.util; + +import static com.google.eclipse.protobuf.junit.Finder.findProperty; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.*; + +/** + * Tests for <code>{@link ProtobufElementFinder#scalarTypeOf(Property)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class ProtobufElementFinder_scalarTypeOf_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private ProtobufElementFinder finder; + + @Before public void setUp() { + finder = xtext.getInstanceOf(ProtobufElementFinder.class); + } + + @Test public void should_return_scalar_if_property_type_is_scalar() { + StringBuilder proto = new StringBuilder(); + proto.append("message Person { ") + .append(" optional int32 id = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property id = findProperty("id", root); + ScalarType int32 = finder.scalarTypeOf(id); + assertThat(int32.getName(), equalTo("int32")); + } + + @Test public void should_return_null_if_property_type_is_not_scalar() { + StringBuilder proto = new StringBuilder(); + proto.append("enum PhoneType { ") + .append(" MOBILE = 0; ") + .append(" HOME = 1; ") + .append(" WORK = 2; ") + .append("} ") + .append(" ") + .append("message PhoneNumber { ") + .append(" optional PhoneType type = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property type = findProperty("type", root); + ScalarType scalar = finder.scalarTypeOf(type); + assertThat(scalar, nullValue()); + } +}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java index 9f58cae..af4f45e 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
@@ -30,7 +30,7 @@ import com.google.eclipse.protobuf.ui.grammar.CompoundElement; import com.google.eclipse.protobuf.ui.labeling.Images; import com.google.eclipse.protobuf.ui.util.*; -import com.google.eclipse.protobuf.util.EObjectFinder; +import com.google.eclipse.protobuf.util.ProtobufElementFinder; import com.google.inject.Inject; /** @@ -40,7 +40,7 @@ */ public class ProtobufProposalProvider extends AbstractProtobufProposalProvider { - @Inject private EObjectFinder finder; + @Inject private ProtobufElementFinder finder; @Inject private Globals globals; @Inject private PluginImageHelper imageHelper; @Inject private Images imageRegistry;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider.java index 42c6f3c..4b4ee83 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider.java
@@ -21,7 +21,7 @@ import com.google.common.base.Function; import com.google.eclipse.protobuf.protobuf.Package; -import com.google.eclipse.protobuf.util.EObjectFinder; +import com.google.eclipse.protobuf.util.ProtobufElementFinder; import com.google.inject.Inject; import com.google.inject.Provider; @@ -35,7 +35,7 @@ @Inject private IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl(); @Inject private IResourceScopeCache cache = IResourceScopeCache.NullImpl.INSTANCE; - @Inject private EObjectFinder finder; + @Inject private ProtobufElementFinder finder; private Function<EObject, String> resolver = newResolver(String.class, "name");
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java index 4e77ddc..4af752b 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
@@ -38,7 +38,7 @@ import com.google.eclipse.protobuf.protobuf.Protobuf; import com.google.eclipse.protobuf.protobuf.Type; import com.google.eclipse.protobuf.protobuf.TypeReference; -import com.google.eclipse.protobuf.util.EObjectFinder; +import com.google.eclipse.protobuf.util.ProtobufElementFinder; import com.google.inject.Inject; /** @@ -52,7 +52,7 @@ private static final boolean DO_NOT_IGNORE_CASE = false; - @Inject private EObjectFinder finder; + @Inject private ProtobufElementFinder finder; @Inject private Globals globals; @Inject private IQualifiedNameProvider nameProvider; @Inject private ImportUriResolver uriResolver;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/EObjectFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/EObjectFinder.java deleted file mode 100644 index b5375c2..0000000 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/EObjectFinder.java +++ /dev/null
@@ -1,49 +0,0 @@ -/* - * 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.util; - -import org.eclipse.emf.ecore.EObject; - -import com.google.eclipse.protobuf.protobuf.*; -import com.google.eclipse.protobuf.protobuf.Enum; -import com.google.eclipse.protobuf.protobuf.Package; -import com.google.inject.Singleton; - -/** - * @author alruiz@google.com (Alex Ruiz) - */ -@Singleton -public class EObjectFinder { - - public Enum enumTypeOf(Property p) { - AbstractTypeReference aTypeRef = (p).getType(); - if (aTypeRef instanceof TypeReference) { - Type type = ((TypeReference) aTypeRef).getType(); - if (type instanceof Enum) return (Enum) type; - } - return null; - } - - public ScalarType scalarTypeOf(Property p) { - AbstractTypeReference aTypeRef = (p).getType(); - if (aTypeRef instanceof ScalarTypeReference) - return ((ScalarTypeReference) aTypeRef).getScalar(); - return null; - } - - public Package packageOf(EObject o) { - return rootOf(o).getPackage(); - } - - public Protobuf rootOf(EObject o) { - EObject current = o; - while (!(current instanceof Protobuf)) current = current.eContainer(); - return (Protobuf) current; - } -}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ProtobufElementFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ProtobufElementFinder.java new file mode 100644 index 0000000..952cdf0 --- /dev/null +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ProtobufElementFinder.java
@@ -0,0 +1,72 @@ +/* + * 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.util; + +import org.eclipse.emf.ecore.EObject; + +import com.google.eclipse.protobuf.protobuf.*; +import com.google.eclipse.protobuf.protobuf.Enum; +import com.google.eclipse.protobuf.protobuf.Package; +import com.google.inject.Singleton; + +/** + * Utility methods to find elements in a parser proto file. + * + * @author alruiz@google.com (Alex Ruiz) + */ +@Singleton +public class ProtobufElementFinder { + + /** + * Returns the enum type of the given property, only if the type of the given property is an enum. + * @param p the given property. + * @return the enum type of the given property or {@code null} if the type of the given property is not enum. + */ + public Enum enumTypeOf(Property p) { + AbstractTypeReference aTypeRef = (p).getType(); + if (aTypeRef instanceof TypeReference) { + Type type = ((TypeReference) aTypeRef).getType(); + if (type instanceof Enum) return (Enum) type; + } + return null; + } + + /** + * Returns the scalar type of the given property, only if the type of the given property is a scalar. + * @param p the given property. + * @return the scalar type of the given property or {@code null} if the type of the given property is not a scalar. + */ + public ScalarType scalarTypeOf(Property p) { + AbstractTypeReference aTypeRef = (p).getType(); + if (aTypeRef instanceof ScalarTypeReference) + return ((ScalarTypeReference) aTypeRef).getScalar(); + return null; + } + + /** + * Returns the package of the proto file containing the given object. + * @param o the given object. + * @return the package of the proto file containing the given object or {@code null} if the proto file does not have a + * package. + */ + public Package packageOf(EObject o) { + return rootOf(o).getPackage(); + } + + /** + * Returns the root element of the proto file containing the given object. + * @param o the given object. + * @return the root element of the proto file containing the given object. + */ + public Protobuf rootOf(EObject o) { + EObject current = o; + while (!(current instanceof Protobuf)) current = current.eContainer(); + return (Protobuf) current; + } +}