-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseScriptSpecTest.groovy
More file actions
141 lines (123 loc) · 4.81 KB
/
BaseScriptSpecTest.groovy
File metadata and controls
141 lines (123 loc) · 4.81 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import groovy.transform.CompileStatic
@CompileStatic
class BaseScriptSpecTest extends GroovyTestCase {
void testSimpleScript() {
def script = '''
// tag::simple_script[]
println 'Hello from Groovy'
// end::simple_script[]
assert (this instanceof Script)
'''
def shell = new GroovyShell()
def o = shell.parse(script)
assert o instanceof Script
}
void testScriptWithBinding() {
// tag::integ_binding[]
def binding = new Binding() // <1>
def shell = new GroovyShell(binding) // <2>
binding.setVariable('x',1) // <3>
binding.setVariable('y',3)
shell.evaluate 'z=2*x+y' // <4>
assert binding.getVariable('z') == 5 // <5>
// end::integ_binding[]
}
void testBaseClassThroughConfig() {
assertScript '''import org.codehaus.groovy.control.CompilerConfiguration
// tag::baseclass_def[]
abstract class MyBaseClass extends Script {
String name
public void greet() { println "Hello, $name!" }
}
// end::baseclass_def[]
// tag::use_custom_conf[]
def config = new CompilerConfiguration() // <1>
config.scriptBaseClass = 'MyBaseClass' // <2>
def shell = new GroovyShell(this.class.classLoader, config) // <3>
shell.evaluate """
setName 'Judith' // <4>
greet()
"""
// end::use_custom_conf[]
'''
}
void testBaseClassThroughBaseScript() {
assertScript '''
abstract class MyBaseClass extends Script {
String name
public void greet() { println "Hello, $name!" }
}
def shell = new GroovyShell(this.class.classLoader)
shell.evaluate """
// tag::use_basescript[]
import groovy.transform.BaseScript
@BaseScript MyBaseClass baseScript
setName 'Judith'
greet()
// end::use_basescript[]
"""
'''
assertScript '''
abstract class MyBaseClass extends Script {
String name
public void greet() { println "Hello, $name!" }
}
def shell = new GroovyShell(this.class.classLoader)
shell.evaluate """
// tag::use_basescript_alt[]
@BaseScript(MyBaseClass)
import groovy.transform.BaseScript
setName 'Judith'
greet()
// end::use_basescript_alt[]
"""
'''
}
void testBaseClassCustomRunMethod() {
assertScript '''import org.codehaus.groovy.control.CompilerConfiguration
// tag::custom_run_method[]
abstract class MyBaseClass extends Script {
int count
abstract void scriptBody() // <1>
def run() {
count++ // <2>
scriptBody() // <3>
count // <4>
}
}
// end::custom_run_method[]
def conf = new CompilerConfiguration()
conf.scriptBaseClass = 'MyBaseClass'
def shell = new GroovyShell(this.class.classLoader, conf)
// tag::custom_run_eval[]
def result = shell.evaluate """
println 'Ok'
"""
assert result == 1
// end::custom_run_eval[]
// tag::custom_run_parse[]
def script = shell.parse("println 'Ok'")
assert script.run() == 1
assert script.run() == 2
// end::custom_run_parse[]
'''
}
}