Added more unit tests. Fixed parsing of proto content in XtextRule. Fixed implementation of Globals so it doesn't require a full-blown Eclipse environment.
diff --git a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/XtextRule.java b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/XtextRule.java index 2c6e584..222353f 100644 --- a/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/XtextRule.java +++ b/com.google.eclipse.protobuf.junit/src/com/google/eclipse/protobuf/junit/XtextRule.java
@@ -8,21 +8,27 @@ */ package com.google.eclipse.protobuf.junit; -import java.io.InputStreamReader; +import static org.eclipse.emf.common.util.URI.createURI; +import static org.eclipse.emf.ecore.util.EcoreUtil.resolveAll; +import static org.eclipse.xtext.util.CancelIndicator.NullImpl; -import org.eclipse.xtext.parser.IParseResult; +import java.io.IOException; +import java.io.InputStream; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.xtext.linking.lazy.LazyLinkingResource; +import org.eclipse.xtext.resource.*; import org.eclipse.xtext.util.StringInputStream; import org.junit.rules.MethodRule; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; import com.google.eclipse.protobuf.ProtobufStandaloneSetup; -import com.google.eclipse.protobuf.parser.antlr.ProtobufParser; import com.google.eclipse.protobuf.protobuf.Protobuf; import com.google.inject.Injector; /** - * Xtext rule that performs basic configuration of an Xtext environment. + * Rule that performs configuration of a standalone Xtext environment. * * @author alruiz@google.com (Alex Ruiz) */ @@ -38,18 +44,39 @@ return injector; } - public <T> T getInstanceOf(Class<T> type) { - return injector.getInstance(type); - } - public Protobuf parse(StringBuilder text) { return parse(text.toString()); } - + public Protobuf parse(String text) { - ProtobufParser parser = injector.getInstance(ProtobufParser.class); - IParseResult parseResult = parser.parse(new InputStreamReader(new StringInputStream(text))); - return (Protobuf) parseResult.getRootASTElement(); + XtextResource resource = resourceFrom(new StringInputStream(text)); + return (Protobuf) resource.getParseResult().getRootASTElement(); + } + + private XtextResource resourceFrom(InputStream input) { + return resourceFrom(input, createURI("mytestmodel.proto")); + } + + private XtextResource resourceFrom(InputStream input, URI uri) { + XtextResourceSet set = getInstanceOf(XtextResourceSet.class); + set.setClasspathURIContext(getClass()); + XtextResource resource = (XtextResource) getInstanceOf(IResourceFactory.class).createResource(uri); + set.getResources().add(resource); + try { + resource.load(input, null); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (resource instanceof LazyLinkingResource) { + ((LazyLinkingResource) resource).resolveLazyCrossReferences(NullImpl); + return resource; + } + resolveAll(resource); + return resource; + } + + public <T> T getInstanceOf(Class<T> type) { + return injector.getInstance(type); } private class XtextStatement extends Statement {
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_typeNameOf_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_typeNameOf_Test.java new file mode 100644 index 0000000..890c118 --- /dev/null +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Properties_typeNameOf_Test.java
@@ -0,0 +1,61 @@ +/* + * 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.junit.Finder.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#typeNameOf(Property)}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +public class Properties_typeNameOf_Test { + + @Rule public XtextRule xtext = new XtextRule(); + + private Properties properties; + + @Before public void setUp() { + properties = xtext.getInstanceOf(Properties.class); + } + + @Test public void should_return_name_of_scalar() { + StringBuilder proto = new StringBuilder(); + proto.append("message Person { ") + .append(" optional string name = 1;") + .append("} "); + Protobuf root = xtext.parse(proto); + Property name = findProperty("name", root); + assertThat(properties.typeNameOf(name), equalTo("string")); + } + + @Test public void should_return_name_of_type() { + StringBuilder proto = new StringBuilder(); + proto.append("message Person { ") + .append(" optional string name = 1; ") + .append(" optional PhoneNumber number = 2;") + .append(" ") + .append(" message PhoneNumber { ") + .append(" optional string value = 1; ") + .append(" } ") + .append("} "); + Protobuf root = xtext.parse(proto); + Property number = findProperty("number", root); + assertThat(properties.typeNameOf(number), equalTo("PhoneNumber")); + } + +}
diff --git a/com.google.eclipse.protobuf/global.proto b/com.google.eclipse.protobuf/global.proto deleted file mode 100644 index 294cd0e..0000000 --- a/com.google.eclipse.protobuf/global.proto +++ /dev/null
@@ -1,22 +0,0 @@ -message FileOptions { - - optional string java_package = 1; - optional string java_outer_classname = 8; - optional bool java_multiple_files = 10 [default=false]; - optional bool java_generate_equals_and_hash = 20 [default=false]; - - enum OptimizeMode { - SPEED = 1; // Generate complete code for parsing, serialization, etc. - CODE_SIZE = 2; // Use ReflectionOps to implement these methods. - LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime. - } - - optional OptimizeMode optimize_for = 9 [default=SPEED]; - optional bool cc_generic_services = 16 [default=false]; - optional bool java_generic_services = 17 [default=false]; - optional bool py_generic_services = 18 [default=false]; - - repeated UninterpretedOption uninterpreted_option = 999; - - extensions 1000 to max; -}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java index f7f36e2..e4194a8 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Globals.java
@@ -9,19 +9,15 @@ package com.google.eclipse.protobuf.scoping; import static java.util.Collections.unmodifiableCollection; -import static org.eclipse.core.runtime.FileLocator.*; import java.io.*; -import java.net.URL; import java.util.*; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; import org.eclipse.emf.common.util.URI; import org.eclipse.xtext.parser.IParseResult; import org.eclipse.xtext.parser.IParser; import org.eclipse.xtext.resource.XtextResource; -import org.osgi.framework.Bundle; +import org.eclipse.xtext.util.StringInputStream; import com.google.eclipse.protobuf.protobuf.*; import com.google.eclipse.protobuf.protobuf.Enum; @@ -30,7 +26,7 @@ import com.google.inject.Singleton; /** - * Protobuf elements accesible to any .proto file. + * Protobuf elements accessible to any .proto file. * * @author alruiz@google.com (Alex Ruiz) */ @@ -56,11 +52,30 @@ } } - private static InputStream globalScopeContents() throws IOException { - Bundle bundle = Platform.getBundle("com.google.eclipse.protobuf"); - Path originPath = new Path("global.proto"); - URL bundledFileURL = find(bundle, originPath, null); - return (InputStream) resolve(bundledFileURL).getContent(); + private static InputStream globalScopeContents() { + return new StringInputStream(globalProto()); + } + + private static String globalProto() { + StringBuilder proto = new StringBuilder(); + proto.append("message FileOptions {") + .append(" optional string java_package = 1;") + .append(" optional string java_outer_classname = 8;") + .append(" optional bool java_multiple_files = 10 [default=false];") + .append(" optional bool java_generate_equals_and_hash = 20 [default=false];") + .append(" enum OptimizeMode {") + .append(" SPEED = 1;") + .append(" CODE_SIZE = 2;") + .append(" LITE_RUNTIME = 3;") + .append(" }") + .append(" optional OptimizeMode optimize_for = 9 [default=SPEED];") + .append(" optional bool cc_generic_services = 16 [default=false];") + .append(" optional bool java_generic_services = 17 [default=false];") + .append(" optional bool py_generic_services = 18 [default=false];") + .append(" repeated UninterpretedOption uninterpreted_option = 999;") + .append(" extensions 1000 to max;") + .append("}"); + return proto.toString(); } private void init() {