My favorites | Sign in
Project Home Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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.junit.core;

import static java.util.Arrays.asList;
import static org.eclipse.xtext.util.Strings.isEmpty;

import java.io.File;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.*;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.XtextResource;
import org.junit.rules.MethodRule;
import org.junit.runners.model.*;

import com.google.eclipse.protobuf.protobuf.Protobuf;
import com.google.inject.*;

/**
* JUnit <code>{@link MethodRule}</code> that:
* <ol>
* <li>Performs configuration of a standalone Xtext environment</li>
* <li>Creates an <code>{@link XtextResource}</code> from method-level comments</li>
* <li>Creates .proto files in the file system based on method-level comments (if the comment starts with
* "// Create file" followed by the name of the file to create)</li>
* <li>Finds model objects and nodes in the created <code>{@link XtextResource}</code> (from #2)</li>
* </ol>
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class XtextRule implements MethodRule {
private final Injector injector;

private final CommentReader commentReader;
private final FileCreator fileCreator;
private final ProtobufInTestsParser protobufParser;

private Protobuf root;
private XtextResource resource;
private Finder finder;

public static XtextRule overrideRuntimeModuleWith(Module...testModules) {
return createWith(new OverrideRuntimeModuleSetup(testModules));
}

public static XtextRule createWith(ISetup setup) {
return createWith(setup.createInjectorAndDoEMFRegistration());
}

public static XtextRule createWith(Injector injector) {
return new XtextRule(injector);
}

private XtextRule(Injector injector) {
this.injector = injector;
commentReader = new CommentReader();
fileCreator = new FileCreator();
protobufParser = new ProtobufInTestsParser(injector);
}

@Override public Statement apply(Statement base, FrameworkMethod method, Object target) {
injector.injectMembers(target);
root = null;
String comments = commentsIn(method);
if (!isEmpty(comments)) {
parseText(comments);
finder = new Finder(resource.getParseResult().getRootNode(), comments);
}
return base;
}

private String commentsIn(FrameworkMethod method) {
for (String comment : commentReader.commentsIn(method)) {
File protoFile = fileCreator.createFileFrom(comment);
if (protoFile == null) {
return comment;
}
}
return null;
}

public void parseText(String text) {
IParseResult parseResult = protobufParser.parseText(text);
root = (Protobuf) parseResult.getRootASTElement();
if (root != null) {
resource = (XtextResource) root.eResource();
}
}

public Injector injector() {
return injector;
}

public XtextResource resource() {
return resource;
}

public Protobuf root() {
return root;
}

public <T extends EObject> T find(String name, String extra, Class<T> type, SearchOption...options) {
return find(name + extra, name.length(), type, options);
}

public <T extends EObject> T find(String name, Class<T> type, SearchOption...options) {
return find(name, name.length(), type, options);
}

public <T extends EObject> T find(String text, int count, Class<T> type, SearchOption...options) {
return finder.find(text, count, type, asList(options));
}

public ILeafNode findNode(String text) {
return finder.find(text);
}

public <T extends EObject> T findFirst(Class<T> type) {
List<T> contents = EcoreUtil2.getAllContentsOfType(root, type);
return (contents.isEmpty()) ? null : contents.get(0);
}
}

Change log

c6c01e8a0218 by Alex Ruiz <alr...@google.com> on Mar 12, 2012   Diff
Code cleanup. Added more tests.
Go to: 
Project members, sign in to write a code review

Older revisions

2a0e4f9a9c01 by Alex Ruiz <alr...@google.com> on Mar 6, 2012   Diff
Converted formatter tests from
functional tests to unit tests.
fa8d4d8c37ac by Alex Ruiz <alr...@google.com> on Mar 3, 2012   Diff
In progress: [Issue 13] Implement a
formatter

* Fixed line wrap for normal and
public imports
...
53aa3814b07d by Alex Ruiz <alr...@google.com> on Feb 29, 2012   Diff
Code cleanup. Added more tests.
All revisions of this file

File info

Size: 4062 bytes, 132 lines
Powered by Google Project Hosting