Adding unit tests.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isPrimitive_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isPrimitive_Test.java new file mode 100644 index 0000000..63c843e --- /dev/null +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isPrimitive_Test.java
@@ -0,0 +1,74 @@ +/* + * 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.eclipse.xtext.EcoreUtil2.getAllContentsOfType; +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.XtextRule; +import com.google.eclipse.protobuf.protobuf.Property; +import com.google.eclipse.protobuf.protobuf.Protobuf; + +/** + * Tests for <code>{@link Properties#isPrimitive(Property)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class Properties_isPrimitive_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private Properties properties; + + @Before public void setUp() { + properties = xtext.getInstanceOf(Properties.class); + } + + @Test public void should_return_true_if_property_is_primitive() { + StringBuilder proto = new StringBuilder(); + proto.append("message Primitives { ") + .append(" optional float float_1 = 1; ") + .append(" optional int32 int32_1 = 2; ") + .append(" optional int64 int64_1 = 3; ") + .append(" optional uint32 uint32_1 = 4; ") + .append(" optional uint64 uint64_1 = 5; ") + .append(" optional sint32 sint32_1 = 6; ") + .append(" optional sint64 sint64_1 = 7; ") + .append(" optional fixed32 fixed32_1 = 8;") + .append(" optional fixed64 fixed64_1 = 9;") + .append(" optional bool bool_1 = 10; ") + .append("} "); + Protobuf root = xtext.parse(proto); + List<Property> allProperties = getAllContentsOfType(root, Property.class); + for (Property p : allProperties) + assertThat(properties.isPrimitive(p), equalTo(true)); + } + + @Test public void should_return_false_if_property_is_not_primitive() { + StringBuilder proto = new StringBuilder(); + proto.append("message Types { ") + .append(" optional string string_1 = 1; ") + .append(" optional bytes bytes_1 = 2; ") + .append(" optional Person person = 3; ") + .append("} ") + .append(" ") + .append("message Person { ") + .append(" optional string name = 1 ") + .append("} "); + Protobuf root = xtext.parse(proto); + List<Property> allProperties = getAllContentsOfType(root, Property.class); + for (Property p : allProperties) + assertThat(properties.isPrimitive(p), equalTo(false)); + } +}
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 5e20379..6f0523c 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
@@ -28,6 +28,13 @@ @Inject private Keywords keywords; + /** + * Indicates whether the type of the given property is primitive. Primitive types include: {@code double}, + * {@code float}, {@code int32}, {@code int64}, {@code uint32}, {@code uint64}, {@code sint32}, {@code sint64}, + * {@code fixed32}, {@code fixed64}, {@code sfixed32}, {@code sfixed64} and {@code bool}. + * @param p the given property. + * @return {@code true} if the type of the given property is primitive, {@code false} otherwise. + */ public boolean isPrimitive(Property p) { AbstractTypeReference r = p.getType(); if (!(r instanceof ScalarTypeReference)) return false; @@ -41,7 +48,7 @@ * @return {@code true} if the given property is of type {@code bool}, {@code false} otherwise. */ public boolean isBool(Property p) { - return isOfScalarType(p, keywords.bool()); + return isScalarType(p, keywords.bool()); } /** @@ -50,10 +57,10 @@ * @return {@code true} if the given property is of type {@code string}, {@code false} otherwise. */ public boolean isString(Property p) { - return isOfScalarType(p, keywords.string()); + return isScalarType(p, keywords.string()); } - private boolean isOfScalarType(Property p, Keyword typeKeyword) { + private boolean isScalarType(Property p, Keyword typeKeyword) { return typeKeyword.getValue().equals(typeNameOf(p)); }