Adding more unit tests.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_charCount_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_charCount_Test.java new file mode 100644 index 0000000..5a7f449 --- /dev/null +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_charCount_Test.java
@@ -0,0 +1,29 @@ +/* + * 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.grammar; + +import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.*; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +/** + * Tests for <code>{@link CompoundElement#charCount()}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class CompoundElement_charCount_Test { + + @Test public void should_return_number_of_characters_in_value() { + assertThat(DEFAULT_EQUAL.charCount(), equalTo(DEFAULT_EQUAL.toString().length())); + assertThat(DEFAULT_EQUAL_IN_BRACKETS.charCount(), equalTo(DEFAULT_EQUAL_IN_BRACKETS.toString().length())); + } + +}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java new file mode 100644 index 0000000..15ef28d --- /dev/null +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement_indexOf_Test.java
@@ -0,0 +1,32 @@ +/* + * 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.grammar; + +import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; +import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.DEFAULT_EQUAL; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +/** + * Tests for <code>{@link CompoundElement#indexOf(CommonKeyword)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class CompoundElement_indexOf_Test { + + @Test public void should_return_index_of_keyword_as_substring() { + assertThat(DEFAULT_EQUAL.indexOf(EQUAL), equalTo(DEFAULT_EQUAL.toString().indexOf(EQUAL.toString()))); + } + + @Test public void should_return_negative_one_if_keyword_not_found() { + assertThat(DEFAULT_EQUAL.indexOf(TRUE), equalTo(-1)); + } +}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java index 18612a5..030ea0f 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java
@@ -11,7 +11,7 @@ import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; /** - * Element composed of one or more keywords. + * Element composed of more than one keyword. * * @author alruiz@google.com (Alex Ruiz) */ @@ -27,28 +27,18 @@ private final String value; - private static String join(CommonKeyword...keywords) { + private static String join(Object...objects) { StringBuilder buffer = new StringBuilder(); - int count = keywords.length; + int count = objects.length; for (int i = 0; i < count; i++) { - buffer.append(keywords[i].toString()); - if (i < count - 1) buffer.append(" "); - } - return buffer.toString(); - } - - private static String join(CompoundElement...elements) { - StringBuilder buffer = new StringBuilder(); - int count = elements.length; - for (int i = 0; i < count; i++) { - buffer.append(elements[i].value); + buffer.append(objects[i].toString()); if (i < count - 1) buffer.append(" "); } return buffer.toString(); } private static String inBrackets(CompoundElement element) { - return String.format("[%s]", element.value); + return OPENING_BRACKET + element.value + CLOSING_BRACKET; } private CompoundElement(String value) { @@ -66,7 +56,7 @@ } /** - * Returns the number of characters in this compound element. + * Returns the number of characters in this compound element (similar to calling {@code toString().length()}.) * @return the number of characters in this compound element. */ public int charCount() { @@ -74,7 +64,8 @@ } /** - * Returns the index within this compound element of the first occurrence of the value of the specified keyword. + * Returns the index within this compound element of the first occurrence of the value of the specified keyword + * (similar to calling {@code toString().indexOf(keyword.toString())}. * @param keyword the given keyword. * @return the index within this compound element of the first occurrence of the value of the specified keyword; -1 * if the value of the given keyword is not found.