Code cleanup. Adding more tests.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/Keywords_isKeyword_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/Keywords_isKeyword_Test.java
new file mode 100644
index 0000000..dc3f22b
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/Keywords_isKeyword_Test.java
@@ -0,0 +1,47 @@
+/*
+ * 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.grammar;
+
+import static com.google.eclipse.protobuf.junit.core.Setups.unitTestSetup;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
+import static org.eclipse.xtext.GrammarUtil.getAllKeywords;
+import static org.junit.Assert.*;
+
+import org.eclipse.xtext.IGrammarAccess;
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+
+/**
+ * Tests for <code>{@link Keywords#isKeyword(String)}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Keywords_isKeyword_Test {
+
+ @Rule public XtextRule xtext = createWith(unitTestSetup());
+
+ private IGrammarAccess grammarAccess;
+ private Keywords keywords;
+
+ @Before public void setUp() {
+ grammarAccess = xtext.getInstanceOf(IGrammarAccess.class);
+ keywords = xtext.getInstanceOf(Keywords.class);
+ }
+
+ @Test public void should_return_true_if_given_String_is_keyword() {
+ for (String s : getAllKeywords(grammarAccess.getGrammar())) {
+ assertTrue(keywords.isKeyword(s));
+ }
+ }
+
+ @Test public void should_return_false_if_given_String_is_not_keyword() {
+ assertFalse(keywords.isKeyword("Google"));
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic_appendToMessage_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic_appendToMessage_Test.java
new file mode 100644
index 0000000..03778fe
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic_appendToMessage_Test.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.linking;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+import org.eclipse.xtext.nodemodel.INode;
+import org.junit.*;
+
+/**
+ * Tests for <code>{@link ProtobufDiagnostic#appendToMessage(String)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ProtobufDiagnostic_appendToMessage_Test {
+
+ private ProtobufDiagnostic diagnostic;
+
+ @Before public void setUp() {
+ diagnostic = new ProtobufDiagnostic("1000", new String[0], "Hello", mock(INode.class));
+ }
+
+ @Test public void should_append_text_to_message() {
+ diagnostic.appendToMessage(" ");
+ diagnostic.appendToMessage("World");
+ diagnostic.appendToMessage("!");
+ assertThat(diagnostic.getMessage(), equalTo("Hello World!"));
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic_constructor_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic_constructor_Test.java
new file mode 100644
index 0000000..929502b
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic_constructor_Test.java
@@ -0,0 +1,59 @@
+/*
+ * 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.linking;
+
+import static org.hamcrest.core.AllOf.allOf;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.core.IsSame.sameInstance;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+import org.eclipse.xtext.nodemodel.INode;
+import org.junit.*;
+
+/**
+ * Tests for <code>{@link ProtobufDiagnostic#ProtobufLinkingDiagnostic(String, String[], String, INode)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ProtobufDiagnostic_constructor_Test {
+
+ private static INode node;
+
+ @BeforeClass public static void setUpOnce() {
+ node = mock(INode.class);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void should_throw_exception_if_data_is_null() {
+ new ProtobufDiagnostic("1000", null, "message", node);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void should_throw_exception_if_data_contains_nulls() {
+ new ProtobufDiagnostic("1000", new String[] { null }, "message", node);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void should_throw_exception_if_node_is_null() {
+ new ProtobufDiagnostic("1000", new String[0], "message", null);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test public void should_create_new_instance() {
+ String[] data = { "abc.proto" };
+ ProtobufDiagnostic d = new ProtobufDiagnostic("1000", data, "message", node);
+ assertNotNull(d);
+ assertThat(d.getCode(), equalTo("1000"));
+ assertThat(d.getData(), allOf(equalTo(data), not(sameInstance(data))));
+ assertThat(d.getMessage(), equalTo("message"));
+ assertSame(node, d.getNode());
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufResource_createDiagnostic_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufResource_createDiagnostic_Test.java
new file mode 100644
index 0000000..3a08e0e
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/linking/ProtobufResource_createDiagnostic_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.linking;
+
+import static org.eclipse.xtext.diagnostics.Severity.WARNING;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+import org.eclipse.emf.ecore.*;
+import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
+import org.eclipse.xtext.diagnostics.DiagnosticMessage;
+import org.eclipse.xtext.nodemodel.INode;
+import org.eclipse.xtext.util.*;
+import org.junit.*;
+
+/**
+ * Tests for <code>{@link ProtobufResource#createDiagnostic(Triple, DiagnosticMessage)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ProtobufResource_createDiagnostic_Test {
+
+ private static Triple<EObject, EReference, INode> triple;
+
+ @BeforeClass public static void setUpOnce() {
+ triple = Tuples.create(mock(EObject.class), mock(EReference.class), mock(INode.class));
+ }
+
+ private DiagnosticMessage message;
+ private ProtobufResource resource;
+
+ @Before public void setUp() {
+ message = new DiagnosticMessage("message", WARNING, "1000", new String[] { "abc.proto" });
+ resource = new ProtobufResource();
+ }
+
+ @Test public void should_create_dianostic() {
+ Diagnostic diagnostic = resource.createDiagnostic(triple, message);
+ assertThat(diagnostic, instanceOf(ProtobufDiagnostic.class));
+ ProtobufDiagnostic d = (ProtobufDiagnostic) diagnostic;
+ assertThat(d.getCode(), equalTo(message.getIssueCode()));
+ assertThat(d.getData(), equalTo(message.getIssueData()));
+ assertThat(d.getMessage(), equalTo(message.getMessage()));
+ assertSame(triple.getThird(), d.getNode());
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/Keywords.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java
similarity index 95%
rename from com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/Keywords.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java
index 55a74c5..770fa44 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/Keywords.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java
@@ -6,7 +6,7 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.conversion;
+package com.google.eclipse.protobuf.grammar;
import static org.eclipse.xtext.GrammarUtil.getAllKeywords;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufLinkingDiagnostic.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic.java
similarity index 71%
rename from com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufLinkingDiagnostic.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic.java
index 57a6ce1..2a3d2d6 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufLinkingDiagnostic.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufDiagnostic.java
@@ -12,24 +12,24 @@
import static java.util.Arrays.copyOf;
import static org.eclipse.xtext.util.Arrays.contains;
-import org.eclipse.xtext.diagnostics.AbstractDiagnostic;
+import org.eclipse.xtext.diagnostics.*;
import org.eclipse.xtext.nodemodel.INode;
/**
+ * <code>{@link Diagnostic}</code> that supports appending text to its message.
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
-public class ProtobufLinkingDiagnostic extends AbstractDiagnostic {
+public class ProtobufDiagnostic extends AbstractDiagnostic {
private final String code;
private final String[] data;
private final StringBuilder message;
private final INode node;
- public ProtobufLinkingDiagnostic(String code, String[] data, String message, INode node) {
- if (contains(data, null)) {
- throw new NullPointerException("data may not contain null");
- }
- if (node == null) throw new NullPointerException("node may not be null");
+ public ProtobufDiagnostic(String code, String[] data, String message, INode node) {
+ validate(data);
+ if (node == null) throw new NullPointerException("node should not be null");
this.code = code;
this.data = copyOf(data, data.length);
this.message = new StringBuilder();
@@ -37,6 +37,15 @@
this.node = node;
}
+ private static void validate(String[] data) {
+ if (data == null) {
+ throw new NullPointerException("data should not be a null array");
+ }
+ if (contains(data, null)) {
+ throw new NullPointerException("data should not contain null");
+ }
+ }
+
@Override public String getCode() {
return code;
}
@@ -53,6 +62,10 @@
return node;
}
+ /**
+ * Appends the given text to the message of this diagnostic.
+ * @param s the text to append.
+ */
public void appendToMessage(String s) {
message.append(s);
}
@@ -69,7 +82,7 @@
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
- ProtobufLinkingDiagnostic other = (ProtobufLinkingDiagnostic) obj;
+ ProtobufDiagnostic other = (ProtobufDiagnostic) obj;
if (message == null) {
if (other.message != null) return false;
} else if (!message.equals(other.message)) return false;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufResource.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufResource.java
index 4a58eb0..f2f5d4c 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufResource.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/linking/ProtobufResource.java
@@ -21,7 +21,6 @@
public class ProtobufResource extends LazyLinkingResource {
@Override protected Diagnostic createDiagnostic(Triple<EObject, EReference, INode> t, DiagnosticMessage message) {
- INode node = t.getThird();
- return new ProtobufLinkingDiagnostic(message.getIssueCode(), message.getIssueData(), message.getMessage(), node);
+ return new ProtobufDiagnostic(message.getIssueCode(), message.getIssueData(), message.getMessage(), t.getThird());
}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
index 4c8be6d..bdf371a 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
@@ -8,7 +8,7 @@
*/
package com.google.eclipse.protobuf.model.util;
-import com.google.eclipse.protobuf.parser.NonProto2;
+import com.google.eclipse.protobuf.parser.NonProto2Protobuf;
import com.google.eclipse.protobuf.protobuf.Protobuf;
import com.google.inject.Singleton;
@@ -27,6 +27,6 @@
* {@code false} otherwise.
*/
public boolean isProto2(Protobuf protobuf) {
- return protobuf != null && !(protobuf instanceof NonProto2);
+ return protobuf != null && !(protobuf instanceof NonProto2Protobuf);
}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/NonProto2.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/NonProto2Protobuf.java
similarity index 74%
rename from com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/NonProto2.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/NonProto2Protobuf.java
index 9a18428..9b923c1 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/NonProto2.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/NonProto2Protobuf.java
@@ -12,11 +12,11 @@
import com.google.eclipse.protobuf.protobuf.impl.ProtobufImpl;
/**
- * Represents a non-proto2 protocol buffer.
+ * Represents a non-proto2 protocol buffer, which is ignored by the editor.
*
* @author alruiz@google.com (Alex Ruiz)
*/
-public class NonProto2 extends ProtobufImpl {
+public class NonProto2Protobuf extends ProtobufImpl {
- NonProto2() {}
+ NonProto2Protobuf() {}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/Proto2OnlyParser.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/Proto2OnlyParser.java
index 7a44282..8697ea9 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/Proto2OnlyParser.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/parser/Proto2OnlyParser.java
@@ -24,7 +24,7 @@
*/
public class Proto2OnlyParser extends ProtobufParser {
- private static final String[] ERRORS_TO_IGNORE = {
+ private static final String[] ERRORS_TO_LOOK_FOR = {
"missing EOF at 'c'", "missing EOF at 'parsed'", "missing EOF at 'python'", "no viable alternative at input '<'" };
@Override
@@ -32,7 +32,7 @@
IParseResult result = super.doParse(ruleName, in, builder, initialLookAhead);
// TODO ignore this check via preferences in open source version.
if (isNonProto2(result)) {
- return new ParseResult(new NonProto2(), result.getRootNode(), false);
+ return new ParseResult(new NonProto2Protobuf(), result.getRootNode(), false);
}
return result;
}
@@ -52,7 +52,7 @@
private boolean isNonProto2(SyntaxErrorMessage syntaxErrorMessage) {
if (syntaxErrorMessage == null) return false;
String message = syntaxErrorMessage.getMessage();
- for (String nonProto2Keyword : ERRORS_TO_IGNORE) {
+ for (String nonProto2Keyword : ERRORS_TO_LOOK_FOR) {
if (message.contains(nonProto2Keyword)) return true;
}
return false;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufResourceValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufResourceValidator.java
index b83e052..9704319 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufResourceValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufResourceValidator.java
@@ -29,7 +29,7 @@
import org.eclipse.xtext.util.*;
import org.eclipse.xtext.validation.*;
-import com.google.eclipse.protobuf.linking.ProtobufLinkingDiagnostic;
+import com.google.eclipse.protobuf.linking.ProtobufDiagnostic;
/**
* Adds support for converting scoping errors into warnings if non-proto2 files are imported.
@@ -70,7 +70,7 @@
Severity severity = ERROR;
if (hasNonProto2Import && isUnresolveReferenceError(error)) {
severity = WARNING;
- ProtobufLinkingDiagnostic d = (ProtobufLinkingDiagnostic) error;
+ ProtobufDiagnostic d = (ProtobufDiagnostic) error;
if (!d.getMessage().endsWith(scopingError)) {
if (!d.getMessage().endsWith(".")) d.appendToMessage(".");
d.appendToMessage(" ");
@@ -103,8 +103,8 @@
}
private boolean isUnresolveReferenceError(Resource.Diagnostic error) {
- if (!(error instanceof ProtobufLinkingDiagnostic)) return false;
- ProtobufLinkingDiagnostic d = (ProtobufLinkingDiagnostic) error;
+ if (!(error instanceof ProtobufDiagnostic)) return false;
+ ProtobufDiagnostic d = (ProtobufDiagnostic) error;
if (!"org.eclipse.xtext.diagnostics.Diagnostic.Linking".equals(d.getCode())) return false;
return error.getMessage().startsWith("Couldn't resolve");
}