Adding unit tests. Cleaning up code. Adding/updating Javadocs.
diff --git a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/EnumHasLiterals.java b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/EnumHasLiterals.java new file mode 100644 index 0000000..df5a346 --- /dev/null +++ b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/EnumHasLiterals.java
@@ -0,0 +1,53 @@ +/* + * 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.junit; + +import java.util.*; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +import com.google.eclipse.protobuf.protobuf.Enum; +import com.google.eclipse.protobuf.protobuf.Literal; + +/** + * @author alruiz@google.com (Alex Ruiz) + */ +public class EnumHasLiterals extends BaseMatcher<Enum> { + + private final String[] literalNames; + + public static EnumHasLiterals hasLiterals(String... literalNames) { + return new EnumHasLiterals(literalNames); + } + + private EnumHasLiterals(String... literalNames) { + this.literalNames = literalNames; + } + + /** {@inheritDoc} */ + public boolean matches(Object arg) { + if (!(arg instanceof Enum)) return false; + Enum anEnum = (Enum) arg; + List<String> actualNames = literalNames(anEnum); + for (String name : literalNames) actualNames.remove(name); + return actualNames.isEmpty(); + } + + private List<String> literalNames(Enum anEnum) { + List<String> names = new ArrayList<String>(); + for (Literal literal : anEnum.getLiterals()) names.add(literal.getName()); + return names; + } + + /** {@inheritDoc} */ + public void describeTo(Description description) { + description.appendValue(Arrays.toString(literalNames)); + } +}
diff --git a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/Finder.java b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/Finder.java index 50fddba..9f0affd 100644 --- a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/Finder.java +++ b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/Finder.java
@@ -24,7 +24,13 @@ if (name.equals(message.getName())) return message; return null; } - + + public static Option findOption(String name, Protobuf root) { + for (Option option : root.getOptions()) + if (name.equals(option.getName())) return option; + return null; + } + public static Property findProperty(String name, Protobuf root) { for (Property property : allProperties(root)) if (name.equals(property.getName())) return property;
diff --git a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/PropertyHasType.java b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/PropertyHasType.java new file mode 100644 index 0000000..9f53cba --- /dev/null +++ b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/PropertyHasType.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.junit; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +import com.google.eclipse.protobuf.protobuf.*; + +/** + * @author alruiz@google.com (Alex Ruiz) + */ +public class PropertyHasType extends BaseMatcher<Property> { + + private final String typeName; + + public static PropertyHasType hasType(String typeName) { + return new PropertyHasType(typeName); + } + + private PropertyHasType(String typeName) { + this.typeName = typeName; + } + + /** {@inheritDoc} */ + public boolean matches(Object arg) { + if (!(arg instanceof Property)) return false; + Property property = (Property) arg; + return typeName.equals(typeNameOf(property)); + } + + private String typeNameOf(Property property) { + AbstractTypeReference r = property.getType(); + if (r instanceof ScalarTypeReference) return ((ScalarTypeReference) r).getScalar().getName(); + if (r instanceof TypeReference) { + Type type = ((TypeReference) r).getType(); + return type == null ? null : type.getName(); + } + return r.toString(); + } + + /** {@inheritDoc} */ + public void describeTo(Description description) { + description.appendValue(typeName); + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider_getFullyQualifiedName_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider_getFullyQualifiedName_Test.java index facba74..0307b19 100644 --- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider_getFullyQualifiedName_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/ProtobufQualifiedNameProvider_getFullyQualifiedName_Test.java
@@ -60,7 +60,6 @@ assertThat(fqn.toString(), equalTo("fqn.test.Person.name")); } - @Test public void should_not_include_package_name_as_part_of_message_FQN_if_package_is_not_specified() { StringBuilder proto = new StringBuilder(); proto.append("message Person { ")
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/GlobalsTest_fileOptions_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/GlobalsTest_fileOptions_Test.java new file mode 100644 index 0000000..ec8acab --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/GlobalsTest_fileOptions_Test.java
@@ -0,0 +1,55 @@ +/* + * 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.scoping; + +import static com.google.eclipse.protobuf.junit.PropertyHasType.hasType; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.*; + +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.Property; + +/** + * Tests for <code>{@link Globals#fileOptions()}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class GlobalsTest_fileOptions_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private Globals globals; + + @Before public void setUp() { + globals = xtext.getInstanceOf(Globals.class); + } + + @Test public void should_return_all_file_options() { + Map<String, Property> fileOptions = mapByName(globals.fileOptions()); + assertThat(fileOptions.get("java_package"), hasType("string")); + assertThat(fileOptions.get("java_outer_classname"), hasType("string")); + assertThat(fileOptions.get("java_multiple_files"), hasType("bool")); + assertThat(fileOptions.get("java_generate_equals_and_hash"), hasType("bool")); + assertThat(fileOptions.containsKey("optimize_for"), equalTo(true)); + assertThat(fileOptions.get("cc_generic_services"), hasType("bool")); + assertThat(fileOptions.get("java_generic_services"), hasType("bool")); + assertThat(fileOptions.get("py_generic_services"), hasType("bool")); + } + + private static Map<String, Property> mapByName(Collection<Property> properties) { + Map<String, Property> mapByName = new HashMap<String, Property>(); + for (Property property : properties) + mapByName.put(property.getName(), property); + return mapByName; + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_isOptimizeForOption_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_isOptimizeForOption_Test.java new file mode 100644 index 0000000..fef5cb3 --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_isOptimizeForOption_Test.java
@@ -0,0 +1,57 @@ +/* + * 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.scoping; + +import static com.google.eclipse.protobuf.junit.Finder.findOption; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.Option; +import com.google.eclipse.protobuf.protobuf.Protobuf; + +/** + * Tests for <code>{@link Globals#isOptimizeForOption(Option)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class Globals_isOptimizeForOption_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private Globals globals; + + @Before public void setUp() { + globals = xtext.getInstanceOf(Globals.class); + } + + @Test public void should_true_if_option_is_OptimizeForOption() { + StringBuilder proto = new StringBuilder(); + proto.append("option java_generate_equals_and_hash = false;") + .append("option optimize_for = CODE_SIZE; "); + Protobuf root = xtext.parse(proto); + Option option = findOption("optimize_for", root); + assertThat(globals.isOptimizeForOption(option), equalTo(true)); + } + + @Test public void should_false_if_option_is_not_OptimizeForOption() { + StringBuilder proto = new StringBuilder(); + proto.append("option java_generate_equals_and_hash = false;") + .append("option optimize_for = CODE_SIZE; "); + Protobuf root = xtext.parse(proto); + Option option = findOption("java_generate_equals_and_hash", root); + assertThat(globals.isOptimizeForOption(option), equalTo(false)); + } + + @Test public void should_false_if_option_is_null() { + assertThat(globals.isOptimizeForOption(null), equalTo(false)); + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_lookupFileOption_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_lookupFileOption_Test.java new file mode 100644 index 0000000..8923010 --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_lookupFileOption_Test.java
@@ -0,0 +1,43 @@ +/* + * 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.scoping; + +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.Property; + +/** + * Tests for <code>{@link Globals#lookupFileOption(String)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class Globals_lookupFileOption_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private Globals globals; + + @Before public void setUp() { + globals = xtext.getInstanceOf(Globals.class); + } + + @Test public void should_look_up_file_option() { + Property option = globals.lookupFileOption("java_multiple_files"); + assertThat(option.getName(), equalTo("java_multiple_files")); + } + + @Test public void should_return_null_if_option_not_found() { + assertThat(globals.lookupFileOption("hello"), nullValue()); + } +}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_optimizedMode_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_optimizedMode_Test.java new file mode 100644 index 0000000..8f1ad9b --- /dev/null +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Globals_optimizedMode_Test.java
@@ -0,0 +1,40 @@ +/* + * 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.scoping; + +import static com.google.eclipse.protobuf.junit.EnumHasLiterals.hasLiterals; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.*; + +import com.google.eclipse.protobuf.junit.XtextRule; +import com.google.eclipse.protobuf.protobuf.Enum; + +/** + * Tests for <code>{@link Globals#optimizedMode()}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class Globals_optimizedMode_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private Globals globals; + + @Before public void setUp() { + globals = xtext.getInstanceOf(Globals.class); + } + + @Test public void should_return_enum_OptimizeMode() { + Enum optimizedMode = globals.optimizedMode(); + assertThat(optimizedMode.getName(), equalTo("OptimizeMode")); + assertThat(optimizedMode, hasLiterals("SPEED", "CODE_SIZE", "LITE_RUNTIME")); + } +}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java index 9e1030c..e8e8b26 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java
@@ -22,7 +22,6 @@ import com.google.eclipse.protobuf.protobuf.*; import com.google.eclipse.protobuf.protobuf.Enum; -import com.google.eclipse.protobuf.util.EObjectFinder; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -36,18 +35,20 @@ private Protobuf root; - private boolean initialized; private final Map<String, Property> fileOptions = new LinkedHashMap<String, Property>(); private Enum optimizedMode; - @Inject EObjectFinder finder; - + /** + * Creates a new </code>{@link Globals}</code>. + * @param parser the grammar parser. + */ @Inject public Globals(IParser parser) { try { XtextResource resource = new XtextResource(createURI("globals.proto")); IParseResult result = parser.parse(new InputStreamReader(globalScopeContents(), "UTF-8")); root = (Protobuf) result.getRootASTElement(); resource.getContents().add(root); + initContents(); } catch (IOException e) { throw new IllegalStateException("Unable to parse global scope", e); } @@ -79,9 +80,7 @@ return proto.toString(); } - private void init() { - if (initialized) return; - initialized = true; + private void initContents() { Message m = fileOptionsMessage(); for (MessageElement e : m.getElements()) { if (e instanceof Property) { @@ -105,23 +104,45 @@ fileOptions.put(p.getName(), p); } + /** + * Returns all the file-level options available. These are the options defined in + * {@code google/protobuf/descriptor.proto} (more details can be found + * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @return all the file-level options available. + */ public Collection<Property> fileOptions() { - init(); return unmodifiableCollection(fileOptions.values()); } + /** + * Returns the {@code enum} "OptimizeMode" (defined in {@code google/protobuf/descriptor.proto}. More details can be + * found <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @return the {@code enum} "OptimizeMode." + */ public Enum optimizedMode() { - init(); return optimizedMode; } + /** + * Indicates whether the given option is the "OptimizeMode" one (defined in {@code google/protobuf/descriptor.proto}. + * More details can be found + * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @param option the given option. + * @return {@code true} if the given option is the "OptimizeMode" one, {@code false} otherwise. + */ public boolean isOptimizeForOption(Option option) { - init(); + if (option == null) return false; return "optimize_for".equals(option.getName()); } + /** + * Looks up a file-level option per name. File-level options are defined in {@code google/protobuf/descriptor.proto} + * (more details can be found <a + * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @param name the name of the option to look for. + * @return the option whose name matches the given one or {@code null} if a matching option is not found. + */ public Property lookupFileOption(String name) { - init(); return fileOptions.get(name); } }