Adding unit tests.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Literals_calculateIndexOf_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Literals_calculateIndexOf_Test.java
index 0472113..e56893c 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Literals_calculateIndexOf_Test.java
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Literals_calculateIndexOf_Test.java
@@ -8,17 +8,15 @@
*/
package com.google.eclipse.protobuf.ui.util;
-import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
+import static com.google.eclipse.protobuf.ui.util.ProtobufElements.findLiteral;
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.*;
-import com.google.eclipse.protobuf.protobuf.Enum;
+import com.google.eclipse.protobuf.protobuf.Literal;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
/**
* Tests for <code>{@link Literals#calculateIndexOf(Literal)}</code>.
@@ -37,30 +35,25 @@
@Test public void should_return_zero_for_first_and_only_literal() {
StringBuilder proto = new StringBuilder();
- proto.append("enum PhoneType {");
- proto.append(" MOBILE = 1; ");
- proto.append("} ");
+ proto.append("enum PhoneType {")
+ .append(" MOBILE = 1; ")
+ .append("} ");
Protobuf root = xtext.parse(proto.toString());
- Literal mobileLiteral = allLiteralsInFirstEnum(root).get(0);
- int index = literals.calculateIndexOf(mobileLiteral);
+ Literal mobile = findLiteral("MOBILE", root);
+ int index = literals.calculateIndexOf(mobile);
assertThat(index, equalTo(0));
}
@Test public void should_return_max_index_value_plus_one_for_new_literal() {
StringBuilder proto = new StringBuilder();
- proto.append("enum PhoneType {");
- proto.append(" MOBILE = 1; ");
- proto.append(" HOME = 5; ");
- proto.append(" WORK = 9; ");
- proto.append("} ");
+ proto.append("enum PhoneType {")
+ .append(" MOBILE = 1; ")
+ .append(" HOME = 5; ")
+ .append(" WORK = 9; ")
+ .append("} ");
Protobuf root = xtext.parse(proto.toString());
- Literal workLiteral = allLiteralsInFirstEnum(root).get(2);
- int index = literals.calculateIndexOf(workLiteral);
+ Literal work = findLiteral("WORK", root);
+ int index = literals.calculateIndexOf(work);
assertThat(index, equalTo(6));
}
-
- private List<Literal> allLiteralsInFirstEnum(Protobuf root) {
- List<Enum> allEnums = getAllContentsOfType(root, Enum.class);
- return allEnums.get(0).getLiterals();
- }
}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_calculateTagNumberOf_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_calculateTagNumberOf_Test.java
index b7ca338..6c8c05c 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_calculateTagNumberOf_Test.java
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_calculateTagNumberOf_Test.java
@@ -8,16 +8,15 @@
*/
package com.google.eclipse.protobuf.ui.util;
-import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
+import static com.google.eclipse.protobuf.ui.util.ProtobufElements.findProperty;
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.*;
+import com.google.eclipse.protobuf.protobuf.Property;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
/**
* Tests for <code>{@link Properties#calculateTagNumberOf(Property)}</code>.
@@ -36,29 +35,24 @@
@Test public void should_return_one_for_first_and_only_property() {
StringBuilder proto = new StringBuilder();
- proto.append("message Person { ");
- proto.append(" required string name = 2;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" required string name = 2;")
+ .append("} ");
Protobuf root = xtext.parse(proto.toString());
- Property nameProperty = allPropertiesInFirstMessage(root).get(0);
- int index = properties.calculateTagNumberOf(nameProperty);
+ Property name = findProperty("name", root);
+ int index = properties.calculateTagNumberOf(name);
assertThat(index, equalTo(1));
}
@Test public void should_return_max_tag_number_value_plus_one_for_new_property() {
StringBuilder proto = new StringBuilder();
- proto.append("message Person { ");
- proto.append(" required string name = 6;");
- proto.append(" required int32 id = 8; ");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" required string name = 6;")
+ .append(" required int32 id = 8; ")
+ .append("} ");
Protobuf root = xtext.parse(proto.toString());
- Property idProperty = allPropertiesInFirstMessage(root).get(1);
- int index = properties.calculateTagNumberOf(idProperty);
+ Property id = findProperty("id", root);
+ int index = properties.calculateTagNumberOf(id);
assertThat(index, equalTo(7));
}
-
- private List<Property> allPropertiesInFirstMessage(Protobuf root) {
- List<Message> messages = getAllContentsOfType(root, Message.class);
- return getAllContentsOfType(messages.get(0), Property.class);
- }
}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java
index 13d5e81..c859b75 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isBool_Test.java
@@ -6,19 +6,17 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-
package com.google.eclipse.protobuf.ui.util;
-import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
+import static com.google.eclipse.protobuf.ui.util.ProtobufElements.findProperty;
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.*;
+import com.google.eclipse.protobuf.protobuf.Property;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
/**
* Tests for <code>{@link Properties#isBool(Property)}</code>.
@@ -37,26 +35,21 @@
@Test public void should_return_true_if_property_is_bool() {
StringBuilder proto = new StringBuilder();
- proto.append("message Person { ");
- proto.append(" optional bool active = 0;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional bool active = 1;")
+ .append("} ");
Protobuf root = xtext.parse(proto.toString());
- Property activeProperty = allPropertiesInFirstMessage(root).get(0);
- assertThat(properties.isBool(activeProperty), equalTo(true));
+ Property active = findProperty("active", root);
+ assertThat(properties.isBool(active), equalTo(true));
}
@Test public void should_return_false_if_property_is_not_bool() {
StringBuilder proto = new StringBuilder();
- proto.append("message Person { ");
- proto.append(" optional string name = 0;");
- proto.append("} ");
+ proto.append("message Person { ")
+ .append(" optional string name = 1;")
+ .append("} ");
Protobuf root = xtext.parse(proto.toString());
- Property nameProperty = allPropertiesInFirstMessage(root).get(0);
- assertThat(properties.isBool(nameProperty), equalTo(false));
- }
-
- private List<Property> allPropertiesInFirstMessage(Protobuf root) {
- List<Message> messages = getAllContentsOfType(root, Message.class);
- return getAllContentsOfType(messages.get(0), Property.class);
+ Property name = findProperty("name", root);
+ assertThat(properties.isBool(name), equalTo(false));
}
}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isString_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isString_Test.java
new file mode 100644
index 0000000..dad29f4
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_isString_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.ui.util;
+
+import static com.google.eclipse.protobuf.ui.util.ProtobufElements.findProperty;
+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.Property;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
+
+/**
+ * Tests for <code>{@link Properties#isString(Property)}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Properties_isString_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_string() {
+ StringBuilder proto = new StringBuilder();
+ proto.append("message Person { ")
+ .append(" optional string name = 1;")
+ .append("} ");
+ Protobuf root = xtext.parse(proto.toString());
+ Property name = findProperty("name", root);
+ assertThat(properties.isString(name), equalTo(true));
+ }
+
+ @Test public void should_return_false_if_property_is_not_string() {
+ StringBuilder proto = new StringBuilder();
+ proto.append("message Person { ")
+ .append(" optional bool active = 1;")
+ .append("} ");
+ Protobuf root = xtext.parse(proto.toString());
+ Property active = findProperty("active", root);
+ assertThat(properties.isString(active), equalTo(false));
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProtobufElements.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProtobufElements.java
new file mode 100644
index 0000000..fc6af47
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProtobufElements.java
@@ -0,0 +1,37 @@
+/*
+ * 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 java.util.List;
+
+import com.google.eclipse.protobuf.protobuf.*;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public final class ProtobufElements {
+
+ public static Literal findLiteral(String name, Protobuf root) {
+ List<Literal> literals = getAllContentsOfType(root, Literal.class);
+ for (Literal literal : literals)
+ if (name.equals(literal.getName())) return literal;
+ return null;
+ }
+
+ public static Property findProperty(String name, Protobuf root) {
+ List<Property> proeperties = getAllContentsOfType(root, Property.class);
+ for (Property property : proeperties)
+ if (name.equals(property.getName())) return property;
+ return null;
+ }
+
+ private ProtobufElements() {}
+}