/
EdeltaInterpreterHelperTest.xtend
112 lines (94 loc) · 2.98 KB
/
EdeltaInterpreterHelperTest.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
package edelta.tests
import com.google.inject.Inject
import edelta.interpreter.EdeltaInterpreterHelper
import edelta.interpreter.EdeltaSafeInterpreter
import edelta.lib.AbstractEdelta
import edelta.tests.additional.MyCustomEdelta
import edelta.tests.additional.MyCustomEdeltaThatCannotBeLoadedAtRuntime
import org.eclipse.xtext.common.types.util.JavaReflectAccess
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static org.assertj.core.api.Assertions.*
import static org.junit.Assert.*
@RunWith(XtextRunner)
@InjectWith(EdeltaInjectorProviderForJavaReflectAccess)
class EdeltaInterpreterHelperTest extends EdeltaAbstractTest {
@Inject EdeltaInterpreterHelper interpreterHelper
@Inject JavaReflectAccess javaReflectAccess;
var AbstractEdelta other
static class InstantiateExceptionClass {
new() {
throw new InstantiationException
}
}
@Before
def void setup() {
other = new AbstractEdelta() {
}
}
@Test
def void testSafeInstantiateOfValidUseAs() {
'''
import edelta.tests.additional.MyCustomEdelta
use MyCustomEdelta as my
'''.parse.useAsClauses.head => [
assertEquals(
MyCustomEdelta,
interpreterHelper.safeInstantiate(javaReflectAccess, it, other).class
)
]
}
@Test
def void testSafeInstantiateOfUseAsWithoutType() {
'''
use as my
'''.parse.useAsClauses.head => [
assertEquals(
Object,
interpreterHelper.safeInstantiate(javaReflectAccess, it, other).class
)
]
}
@Test
def void testSafeInstantiateOfValidUseAsWithoutType() {
'''
import edelta.tests.EdeltaInterpreterHelperTest.InstantiateExceptionClass
use InstantiateExceptionClass as my
'''.parse.useAsClauses.head => [
assertEquals(
Object,
interpreterHelper.safeInstantiate(javaReflectAccess, it, other).class
)
]
}
@Test
def void testSafeInstantiateOfUnresolvedUseAsType() {
assertThatThrownBy[
'''
use NonExistent as my
'''.parse.useAsClauses.head => [
interpreterHelper.safeInstantiate(javaReflectAccess, it, other).class
]
].isInstanceOf(IllegalStateException)
.hasMessageContaining("Cannot resolve proxy")
}
@Test
def void testSafeInstantiateOfValidUseAsButNotFoundAtRuntime() {
// this is a simulation of what would happen if a type is resolved
// but the interpreter cannot load it with Class.forName
// because the ClassLoader cannot find it
// https://github.com/LorenzoBettini/edelta/issues/69
assertThatThrownBy[
'''
import edelta.tests.additional.MyCustomEdeltaThatCannotBeLoadedAtRuntime
use MyCustomEdeltaThatCannotBeLoadedAtRuntime as my
'''.parse.useAsClauses.head => [
interpreterHelper.safeInstantiate(javaReflectAccess, it, other).class
]
].isInstanceOf(EdeltaSafeInterpreter.EdeltaInterpreterRuntimeException)
.hasMessageContaining('''The type '«MyCustomEdeltaThatCannotBeLoadedAtRuntime.name»' has been resolved but cannot be loaded by the interpreter''')
}
}