Code cleanup. Added more tests.
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/MessageMatcherStrategy.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/MessageMatcherStrategy.java
index 695c885..1f9fe3c 100644
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/MessageMatcherStrategy.java
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/MessageMatcherStrategy.java
@@ -41,7 +41,7 @@
matches.add(uriOf(message));
break;
}
- // go one level deeper.
+ // perfect segment match - go one level deeper.
contents = contentsOf(message);
continue;
}
@@ -53,6 +53,7 @@
return unmodifiableList(matches);
}
+ // TODO move to ModelObjects
private URI uriOf(EObject e) {
Resource resource = e.eResource();
URI uri = resource.getURI();
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_fieldsOf_CustomFieldOption_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_fieldsOf_CustomFieldOption_Test.java
new file mode 100644
index 0000000..598e753
--- /dev/null
+++ b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_fieldsOf_CustomFieldOption_Test.java
@@ -0,0 +1,54 @@
+/*
+ * 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.model.util;
+
+import static com.google.eclipse.protobuf.junit.core.IntegrationTestModule.integrationTestModule;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.overrideRuntimeModuleWith;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.protobuf.*;
+import com.google.inject.Inject;
+
+/**
+ * Tests for <code>{@link Options#fieldsOf(AbstractCustomOption)}</code>.
+ *
+ * alruiz@google.com (Alex Ruiz)
+ */
+public class Options_fieldsOf_CustomFieldOption_Test {
+ @Rule public XtextRule xtext = overrideRuntimeModuleWith(integrationTestModule());
+
+ @Inject private Options options;
+
+ // syntax = "proto2";
+ //
+ // import 'google/protobuf/descriptor.proto';
+ //
+ // message Custom {
+ // optional int32 count = 1;
+ // }
+ //
+ // extend google.protobuf.FieldOptions {
+ // optional Custom custom = 1000;
+ // }
+ //
+ // message Person {
+ // optional boolean active = 1 [(custom).count = 6];
+ // }
+ @Test public void should_return_option_field() {
+ CustomFieldOption option = xtext.find("custom", ").", CustomFieldOption.class);
+ List<OptionField> fields = option.getFields();
+ assertThat(options.fieldsOf(option), equalTo(fields));
+ }
+}
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_fieldsOf_CustomOption_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_fieldsOf_CustomOption_Test.java
new file mode 100644
index 0000000..d76df26
--- /dev/null
+++ b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_fieldsOf_CustomOption_Test.java
@@ -0,0 +1,52 @@
+/*
+ * 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.model.util;
+
+import static com.google.eclipse.protobuf.junit.core.IntegrationTestModule.integrationTestModule;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.overrideRuntimeModuleWith;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.protobuf.*;
+import com.google.inject.Inject;
+
+/**
+ * Tests for <code>{@link Options#fieldsOf(AbstractCustomOption)}</code>.
+ *
+ * alruiz@google.com (Alex Ruiz)
+ */
+public class Options_fieldsOf_CustomOption_Test {
+ @Rule public XtextRule xtext = overrideRuntimeModuleWith(integrationTestModule());
+
+ @Inject private Options options;
+
+ // syntax = "proto2";
+ //
+ // import 'google/protobuf/descriptor.proto';
+ //
+ // message Custom {
+ // optional int32 count = 1;
+ // }
+ //
+ // extend google.protobuf.FileOptions {
+ // optional Custom custom = 1000;
+ // }
+ //
+ // option (custom).count = 6;
+ @Test public void should_return_option_field() {
+ CustomOption option = xtext.find("custom", ")", CustomOption.class);
+ List<OptionField> fields = option.getFields();
+ assertThat(options.fieldsOf(option), equalTo(fields));
+ }
+}
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_sourceOfLastFieldIn_CustomFieldOption_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_sourceOfLastFieldIn_CustomFieldOption_Test.java
index 69c785f..210e8dd 100644
--- a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_sourceOfLastFieldIn_CustomFieldOption_Test.java
+++ b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Options_sourceOfLastFieldIn_CustomFieldOption_Test.java
@@ -27,7 +27,7 @@
public class Options_sourceOfLastFieldIn_CustomFieldOption_Test {
@Rule public XtextRule xtext = overrideRuntimeModuleWith(integrationTestModule());
- @Inject private Options fieldOptions;
+ @Inject private Options options;
// syntax = "proto2";
//
@@ -46,7 +46,7 @@
// }
@Test public void should_return_option_field() {
CustomFieldOption option = xtext.find("custom", ").", CustomFieldOption.class);
- MessageField field = (MessageField) fieldOptions.sourceOfLastFieldIn(option);
+ MessageField field = (MessageField) options.sourceOfLastFieldIn(option);
assertThat(field.getName(), equalTo("count"));
}
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/Packages_addPackageNameSegments_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/Packages_addPackageNameSegments_Test.java
new file mode 100644
index 0000000..0c3c294
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/Packages_addPackageNameSegments_Test.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2012 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.model.util;
+
+import static com.google.eclipse.protobuf.junit.core.UnitTestModule.unitTestModule;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.overrideRuntimeModuleWith;
+import static com.google.eclipse.protobuf.model.util.QualifiedNameCollectionContains.contains;
+import static org.junit.Assert.*;
+
+import java.util.Collection;
+
+import org.eclipse.xtext.naming.QualifiedName;
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.protobuf.Package;
+import com.google.inject.Inject;
+
+/**
+ * Tests for <code>{@link Packages#addPackageNameSegments(Package, QualifiedName)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Packages_addPackageNameSegments_Test {
+ @Rule public XtextRule xtext = overrideRuntimeModuleWith(unitTestModule());
+
+ @Inject private Packages packages;
+
+
+ // syntax = "proto2";
+ //
+ // package com.google.test;
+ @Test public void should_create_a_qualified_name_per_segment_in_package_name() {
+ Package aPackage = xtext.find("com.google.test", Package.class);
+ Collection<QualifiedName> names = packages.addPackageNameSegments(aPackage, QualifiedName.create("Person"));
+ assertThat(names, contains("test.Person", "google.test.Person"));
+ }
+
+ // syntax = "proto2";
+ //
+ // package google;
+ @Test public void should_return_empty_list_if_package_has_only_one_segment() {
+ Package aPackage = xtext.find("google", Package.class);
+ Collection<QualifiedName> names = packages.addPackageNameSegments(aPackage, QualifiedName.create("Person"));
+ assertTrue(names.isEmpty());
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/QualifiedNameCollectionContains.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/QualifiedNameCollectionContains.java
new file mode 100644
index 0000000..4ba52db
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/QualifiedNameCollectionContains.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2012 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.model.util;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+import java.util.*;
+
+import org.eclipse.xtext.naming.QualifiedName;
+import org.hamcrest.*;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+class QualifiedNameCollectionContains extends TypeSafeMatcher<Collection<QualifiedName>> {
+ private final List<String> qualifiedNames;
+
+ static QualifiedNameCollectionContains contains(String...qualifiedNames) {
+ return new QualifiedNameCollectionContains(qualifiedNames);
+ }
+
+ private QualifiedNameCollectionContains(String[] qualifiedNames) {
+ this.qualifiedNames = newArrayList(qualifiedNames);
+ }
+
+ @Override public boolean matchesSafely(Collection<QualifiedName> item) {
+ if (item.size() != qualifiedNames.size()) {
+ return false;
+ }
+ List<String> copy = newArrayList(qualifiedNames);
+ for (QualifiedName name : item) {
+ copy.remove(name.toString());
+ }
+ return copy.isEmpty();
+ }
+
+ @Override public void describeTo(Description description) {
+ description.appendValue(qualifiedNames);
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Packages.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Packages.java
index 52f37f4..0963f43 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Packages.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Packages.java
@@ -64,7 +64,38 @@
return true;
}
- // TODO test and document
+ /**
+ * Adds the segments in the name of the given package to the given qualified name, creating a new qualified name per
+ * segment.
+ * <p>
+ * For example, given the following proto element:
+ *
+ * <pre>
+ * package test.alternative.names;
+ *
+ * message Person {
+ * optional string name = 1;
+ *
+ * enum PhoneType {
+ * HOME = 0;
+ * WORK = 1;
+ * }
+ * }
+ * </pre>
+ *
+ * This method will create the following qualified names for {@code PhoneType}:
+ * <ul>
+ * <li>{@code PhoneType}</li>
+ * <li>{@code Person.PhoneType}</li>
+ * <li>{@code names.Person.PhoneType}</li>
+ * <li>{@code test.names.Person.PhoneType}</li>
+ * </ul>
+ * </p>
+ * @param p the given package.
+ * @param name the base name.
+ * @return a collection containing the created qualified names, or an empty list if the name of the given package
+ * contains zero or one segments.
+ */
public Collection<QualifiedName> addPackageNameSegments(Package p, QualifiedName name) {
QualifiedName current = name;
List<String> segments = segmentsOf(p);
@@ -80,6 +111,11 @@
return unmodifiableList(allNames);
}
+ /**
+ * Returns the segments in the name of the given package.
+ * @param p the given package.
+ * @return the segments in the name of the given package.
+ */
public List<String> segmentsOf(Package p) {
QualifiedName name = (p == null) ? null : nameOf(p);
if (name == null) {
@@ -90,9 +126,6 @@
private QualifiedName nameOf(Package p) {
String name = p.getName();
- if (isEmpty(name)) {
- return null;
- }
- return qualifiedNameConverter.toQualifiedName(name);
+ return (isEmpty(name)) ? null : qualifiedNameConverter.toQualifiedName(name);
}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/LocalNamesProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/LocalNamesProvider.java
index 5d7b9bf..0bb3563 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/LocalNamesProvider.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/LocalNamesProvider.java
@@ -14,14 +14,14 @@
import static org.eclipse.xtext.util.Strings.isEmpty;
import static org.eclipse.xtext.util.Tuples.pair;
-import com.google.eclipse.protobuf.model.util.*;
-import com.google.inject.*;
+import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.naming.*;
import org.eclipse.xtext.util.*;
-import java.util.List;
+import com.google.eclipse.protobuf.model.util.*;
+import com.google.inject.*;
/**
* Provides alternative qualified names for protobuf elements.
@@ -50,9 +50,6 @@
* <li>{@code test.names.Person.PhoneType}</li>
* </ul>
* </p>
- * <p>
- * This class provides the non-default qualified names recognized by protoc.
- * </p>
*
* @author alruiz@google.com (Alex Ruiz)
*/