Code cleanup. Added more tests.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/CommentReaderRule.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/CommentReaderRule.java
index c1ed17f..683122b 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/CommentReaderRule.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/CommentReaderRule.java
@@ -27,7 +27,7 @@
private final Injector injector;
private final CommentReader commentReader;
- private final ProtoParser protoParser;
+ private final ProtobufInTestsParser protobufParser;
private ICompositeNode rootNode;
private String expectedText;
@@ -41,7 +41,7 @@
private CommentReaderRule(Injector injector) {
this.injector = injector;
commentReader = new CommentReader();
- protoParser = new ProtoParser(injector);
+ protobufParser = new ProtobufInTestsParser(injector);
}
@Override public Statement apply(Statement base, FrameworkMethod method, Object target) {
@@ -56,7 +56,7 @@
}
void parseText(String text) {
- IParseResult parseResult = protoParser.parseText(text);
+ IParseResult parseResult = protobufParser.parseText(text);
rootNode = parseResult.getRootNode();
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter_Test.java
index 0ceeb19..99f3990 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter_Test.java
@@ -18,6 +18,7 @@
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.junit.*;
+import com.google.eclipse.protobuf.junit.core.AbstractTestModule;
import com.google.inject.Inject;
/**
@@ -26,7 +27,7 @@
* @author alruiz@google.com (Alex Ruiz)
*/
public class ProtobufFormatter_Test {
- @Rule public CommentReaderRule commentReader = overrideRuntimeModuleWith(unitTestModule());
+ @Rule public CommentReaderRule commentReader = overrideRuntimeModuleWith(unitTestModule(), new TestModule());
@Inject private INodeModelFormatter formatter;
@@ -80,10 +81,31 @@
assertThatFormattingWorksCorrectly();
}
+ // message Person { optional string name = 1; optional bool active = 2
+ // [default = true]; }
+
+ // message Person {
+ // optional string name = 1;
+ // optional bool active = 2 [default = true];
+ // }
+ @Test public void should_format_message_fields() {
+ assertThatFormattingWorksCorrectly();
+ }
+
private void assertThatFormattingWorksCorrectly() {
ICompositeNode rootNode = commentReader.rootNode();
IFormattedRegion region = formatter.format(rootNode, 0, rootNode.getText().length());
String formatted = region.getFormattedText();
assertThat(formatted, equalTo(commentReader.expectedText()));
}
+
+ private static class TestModule extends AbstractTestModule {
+ @Override protected void configure() {
+ binder().bind(IIndentationInformation.class).toInstance(new IIndentationInformation() {
+ @Override public String getIndentString() {
+ return " ";
+ }
+ });
+ }
+ }
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/CommentReader.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/CommentReader.java
index 282fa48..d80b28f 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/CommentReader.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/CommentReader.java
@@ -66,7 +66,11 @@
while (scanner.hasNextLine()) {
line = scanner.nextLine().replaceFirst("^\\s*", "");
if (line.startsWith(COMMENT_START)) {
- comment.append(line.substring(COMMENT_START.length()).trim());
+ String text = line.substring(COMMENT_START.length());
+ if (text.startsWith(" ")) {
+ text = text.substring(1);
+ }
+ comment.append(text);
continue;
}
if (comment.isEmpty()) {
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ProtoParser.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ProtobufInTestsParser.java
similarity index 96%
rename from com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ProtoParser.java
rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ProtobufInTestsParser.java
index 9bb9904..8865179 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ProtoParser.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/ProtobufInTestsParser.java
@@ -27,10 +27,10 @@
/**
* @author alruiz@google.com (Alex Ruiz)
*/
-public class ProtoParser {
+public class ProtobufInTestsParser {
private final Injector injector;
- public ProtoParser(Injector injector) {
+ public ProtobufInTestsParser(Injector injector) {
this.injector = injector;
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/XtextRule.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/XtextRule.java
index d296162..16c4b83 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/XtextRule.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/junit/core/XtextRule.java
@@ -43,7 +43,7 @@
private final CommentReader commentReader;
private final FileCreator fileCreator;
- private final ProtoParser protoParser;
+ private final ProtobufInTestsParser protobufParser;
private Protobuf root;
private XtextResource resource;
@@ -65,7 +65,7 @@
this.injector = injector;
commentReader = new CommentReader();
fileCreator = new FileCreator();
- protoParser = new ProtoParser(injector);
+ protobufParser = new ProtobufInTestsParser(injector);
}
@Override public Statement apply(Statement base, FrameworkMethod method, Object target) {
@@ -90,12 +90,11 @@
}
public void parseText(String text) {
- IParseResult parseResult = protoParser.parseText(text);
+ IParseResult parseResult = protobufParser.parseText(text);
root = (Protobuf) parseResult.getRootASTElement();
- if (root.getSyntax() == null) {
- throw new IllegalStateException("Please specify 'proto2' syntax");
+ if (root != null) {
+ resource = (XtextResource) root.eResource();
}
- resource = (XtextResource) root.eResource();
}
public Injector injector() {