Improving tests by using Xtext's injector and EMF setup.
diff --git a/com.google.eclipse.protobuf.ui.test/META-INF/MANIFEST.MF b/com.google.eclipse.protobuf.ui.test/META-INF/MANIFEST.MF
index ee6f07b..92a8b48 100644
--- a/com.google.eclipse.protobuf.ui.test/META-INF/MANIFEST.MF
+++ b/com.google.eclipse.protobuf.ui.test/META-INF/MANIFEST.MF
@@ -6,4 +6,7 @@
Bundle-Vendor: Google
Fragment-Host: com.google.eclipse.protobuf.ui;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: J2SE-1.5
-Require-Bundle: org.junit;bundle-version="4.8.1"
+Require-Bundle: org.junit;bundle-version="4.8.1",
+ org.eclipse.xtext.junit;bundle-version="2.0.0",
+ org.eclipse.xtext.junit4;bundle-version="2.0.0",
+ org.eclipse.xtext.ui.junit;bundle-version="2.0.0"
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EContainerStub.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EContainerStub.java
deleted file mode 100644
index 80b9349..0000000
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EContainerStub.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * 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.stubs;
-
-import org.eclipse.emf.ecore.InternalEObject;
-
-/**
- * @author alruiz@google.com (Alex Ruiz)
- */
-public interface EContainerStub extends InternalEObject {
-
- void add(EObjectStub...children);
-}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EObjectStub.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EObjectStub.java
deleted file mode 100644
index aa377af..0000000
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EObjectStub.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * 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.stubs;
-
-import org.eclipse.emf.ecore.EObject;
-
-/**
- * @author alruiz@google.com (Alex Ruiz)
- */
-public interface EObjectStub extends EObject {
-
- void setContainer(EContainerStub eContainer);
-}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EnumStub.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EnumStub.java
deleted file mode 100644
index 1883f46..0000000
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/EnumStub.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.stubs;
-
-import org.eclipse.emf.common.util.BasicEList;
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.ecore.EObject;
-
-import com.google.eclipse.protobuf.protobuf.impl.EnumImpl;
-
-/**
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class EnumStub extends EnumImpl implements EContainerStub {
-
- private EList<EObject> contents = new BasicEList<EObject>();
-
- public EnumStub(String name) {
- this.name = name;
- }
-
- public void add(EObjectStub...children) {
- for (EObjectStub child : children) {
- contents.add(child);
- child.setContainer(this);
- }
- }
-
- @Override public EList<EObject> eContents() {
- return contents;
- }
-}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/LiteralStub.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/LiteralStub.java
deleted file mode 100644
index 117bb70..0000000
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/stubs/LiteralStub.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * 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.stubs;
-
-import com.google.eclipse.protobuf.protobuf.impl.LiteralImpl;
-
-/**
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class LiteralStub extends LiteralImpl implements EObjectStub {
-
- public LiteralStub(String name) {
- this.name = name;
- }
-
- public void setContainer(EContainerStub container) {
- this.eContainer = container;
- }
-}
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 08eb59c..0473781 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,15 +8,23 @@
*/
package com.google.eclipse.protobuf.ui.util;
+import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
+import java.io.InputStreamReader;
+import java.util.List;
+
+import org.eclipse.xtext.parser.IParseResult;
+import org.eclipse.xtext.util.StringInputStream;
import org.junit.Before;
import org.junit.Test;
-import com.google.eclipse.protobuf.protobuf.Literal;
-import com.google.eclipse.protobuf.stubs.EnumStub;
-import com.google.eclipse.protobuf.stubs.LiteralStub;
+import com.google.eclipse.protobuf.ProtobufStandaloneSetup;
+import com.google.eclipse.protobuf.parser.antlr.ProtobufParser;
+import com.google.eclipse.protobuf.protobuf.*;
+import com.google.eclipse.protobuf.protobuf.Enum;
+import com.google.inject.Injector;
/**
* Tests for <code>{@link Literals#calculateIndexOf(Literal)}</code>.
@@ -25,29 +33,46 @@
*/
public class Literals_calculateIndexOf_Test {
- private EnumStub anEnum;
-
+ private Injector injector;
private Literals literals;
@Before public void setUp() {
- anEnum = new EnumStub("PhoneType");
- literals = new Literals();
+ injector = new ProtobufStandaloneSetup().createInjectorAndDoEMFRegistration();
+ literals = injector.getInstance(Literals.class);
}
- @Test public void should_return_zero_for_first_literal() {
- LiteralStub literal = new LiteralStub("HOME");
- anEnum.add(literal);
- int index = literals.calculateIndexOf(literal);
+ @Test public void should_return_zero_for_first_and_only_literal() {
+ StringBuilder proto = new StringBuilder();
+ proto.append("enum PhoneType {");
+ proto.append(" MOBILE = 0; ");
+ proto.append("} ");
+ Protobuf root = parse(proto.toString());
+ Literal firstLiteral = allLiteralsInFirstEnum(root).get(0);
+ int index = literals.calculateIndexOf(firstLiteral);
assertThat(index, equalTo(0));
}
@Test public void should_return_max_index_value_plus_one_for_new_literal() {
- int maxIndexValue = 1;
- LiteralStub literal1 = new LiteralStub("HOME");
- literal1.setIndex(maxIndexValue);
- LiteralStub literal2 = new LiteralStub("WORK");
- anEnum.add(literal1, literal2);
- int index = literals.calculateIndexOf(literal2);
- assertThat(index, equalTo(maxIndexValue + 1));
+ StringBuilder proto = new StringBuilder();
+ proto.append("enum PhoneType {");
+ proto.append(" MOBILE = 0; ");
+ proto.append(" HOME = 1; ");
+ proto.append(" WORK = 2; ");
+ proto.append("} ");
+ Protobuf root = parse(proto.toString());
+ Literal lastLiteral = allLiteralsInFirstEnum(root).get(2);
+ int index = literals.calculateIndexOf(lastLiteral);
+ assertThat(index, equalTo(2));
+ }
+
+ private Protobuf parse(String text) {
+ ProtobufParser parser = injector.getInstance(ProtobufParser.class);
+ IParseResult parseResult = parser.parse(new InputStreamReader(new StringInputStream(text)));
+ return (Protobuf) parseResult.getRootASTElement();
+ }
+
+ private List<Literal> allLiteralsInFirstEnum(Protobuf root) {
+ List<Enum> allEnums = getAllContentsOfType(root, Enum.class);
+ return allEnums.get(0).getLiterals();
}
}