-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmfParsleyDslExecutionTest.xtend
173 lines (152 loc) · 5.6 KB
/
EmfParsleyDslExecutionTest.xtend
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
* Copyright (c) 2016 RCP Vision (http://www.rcp-vision.com) and others.
* 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
*
* Contributors:
* Lorenzo Bettini - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.parsley.dsl.tests
import com.google.common.base.Joiner
import com.google.inject.Guice
import com.google.inject.Inject
import com.google.inject.Module
import org.eclipse.emf.ecore.resource.impl.ResourceImpl
import org.eclipse.emf.parsley.examples.library.EXTLibraryFactory
import org.eclipse.emf.parsley.junit4.AbstractEmfParsleyShellBasedTest
import org.eclipse.emf.parsley.viewers.ViewerFactory
import org.eclipse.jface.viewers.ILabelProvider
import org.eclipse.jface.viewers.TreeViewer
import org.eclipse.ui.plugin.AbstractUIPlugin
import org.eclipse.xtext.diagnostics.Severity
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.TemporaryFolder
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.util.Wrapper
import org.eclipse.xtext.xbase.compiler.CompilationTestHelper
import org.eclipse.xtext.xbase.compiler.CompilationTestHelper.Result
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import static extension org.junit.Assert.*
@RunWith(XtextRunner)
@InjectWith(EmfParsleyDslInjectorProvider)
class EmfParsleyDslExecutionTest extends AbstractEmfParsleyShellBasedTest {
@Rule
@Inject public TemporaryFolder temporaryFolder
@Inject extension CompilationTestHelper
val libraryFactory = EXTLibraryFactory.eINSTANCE
@Test def void testGeneratedLabelProvider() {
val labelProvider = '''
import org.eclipse.emf.parsley.examples.library.Book
import org.eclipse.emf.parsley.examples.library.BookOnTape
import org.eclipse.emf.parsley.examples.library.Borrower
import org.eclipse.emf.parsley.examples.library.Lendable
import org.eclipse.emf.parsley.examples.library.Library
import org.eclipse.emf.parsley.examples.library.Writer
import org.eclipse.swt.graphics.ImageData
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
module my.empty extends org.eclipse.emf.parsley.EmfParsleyJavaGuiceModule {
labelProvider {
text {
Library -> 'foo' // constant
Writer writer -> writer.getName() // explicit param
Book -> "Title: " + (title ?: "no title") // implit 'it' param
}
image {
Library -> 'library.jpeg' // constant
Writer writer ->
if (writer.name.nullOrEmpty)
"noname.gif"
else
new ImageData("writer.jpeg")
}
font {
Library -> JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT)
}
foreground {
Library -> Display.getCurrent().getSystemColor(SWT.COLOR_BLUE)
}
background {
Library -> Display.getCurrent().getSystemColor(SWT.COLOR_GREEN)
}
}
}
'''.compileInstantiate(ILabelProvider)
"foo".assertEquals(labelProvider.getText(libraryFactory.createLibrary))
"Title: no title".assertEquals(labelProvider.getText(libraryFactory.createBook))
"Title: Foo".assertEquals(labelProvider.getText(libraryFactory.createBook => [title = "Foo"]))
}
@Test def void testGeneratedContentProvider() {
val viewerFactory = '''
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.parsley.examples.library.Book
import org.eclipse.emf.parsley.examples.library.Library
import org.eclipse.emf.parsley.examples.library.Writer
module my.empty extends org.eclipse.emf.parsley.EmfParsleyJavaGuiceModule {
viewerContentProvider {
elements {
Resource -> allContents.filter(Library)
}
children {
Library -> books + writers
Writer writer -> writer.books
Book -> author // implit 'it' param
}
}
}
'''.compileInstantiate(ViewerFactory)
val tree = new TreeViewer(shell)
val resource = new ResourceImpl => [ res |
res.contents += libraryFactory.createLibrary => [
books += libraryFactory.createBook => [
title = "a book"
]
books += libraryFactory.createBook => [
title = "another book"
]
writers += libraryFactory.createWriter => [
name = "an author"
]
// this won't be shown by the content provider
borrowers += libraryFactory.createBorrower
]
]
viewerFactory.initialize(tree, resource)
tree.expandAll
assertAllLabels(tree,
'''
Library
Book a book
Book another book
Writer an author
'''
)
}
def private <T> T compileInstantiate(CharSequence input, Class<T> clazz) {
val instantiated = Wrapper.forType(clazz)
input.compile [
assertNoValidationErrors
val moduleClassName = "my.empty.EmptyEmfParsleyGuiceModule"
val moduleClass = getCompiledClass(moduleClassName)
val cons = moduleClass.getConstructor(AbstractUIPlugin)
val module = cons.newInstance(#[null]) as Module
val injector = Guice.createInjector(module)
val instance = injector.getInstance(clazz)
instantiated.set(instance)
]
return instantiated.get
}
private def assertNoValidationErrors(Result it) {
val allErrors = getErrorsAndWarnings.filter[severity == Severity.ERROR]
if (!allErrors.empty) {
throw new IllegalStateException(
"One or more resources contained errors : " + Joiner.on(',').join(allErrors)
);
}
}
}