-
Notifications
You must be signed in to change notification settings - Fork 595
/
ScriptMetaTest.groovy
150 lines (121 loc) · 4.73 KB
/
ScriptMetaTest.groovy
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
package nextflow.script
import spock.lang.Specification
import groovy.transform.InheritConstructors
import nextflow.exception.DuplicateModuleIncludeException
/**
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
class ScriptMetaTest extends Specification {
@InheritConstructors
static class FooScript extends BaseScript {
@Override
protected Object runScript() { null }
}
def 'should return all defined names' () {
given:
def script = new FooScript(new ScriptBinding())
def proc1 = new ProcessDef(script, 'proc1', Mock(ProcessConfig), Mock(TaskBody))
def proc2 = new ProcessDef(script, 'proc2', Mock(ProcessConfig), Mock(TaskBody))
def func1 = new FunctionDef(name: 'func1')
def work1 = new WorkflowDef(Mock(BaseScript), Mock(TaskBody), 'work1')
def meta = new ScriptMeta(script)
when:
meta.addDefinition(func1)
meta.addDefinition(work1)
meta.addDefinition(proc1)
meta.addDefinition(proc2)
then:
meta.getComponent('work1') == work1
meta.getComponent('func1') == func1
meta.getComponent('proc1') == proc1
meta.getComponent('proc2') == proc2
meta.getComponent('xxx') == null
meta.getComponent('yyy') == null
// then:
// meta.getProcessNames() as Set == ['proc1','proc2'] as Set
//
}
def 'should add imports' () {
given:
def script1 = new FooScript(new ScriptBinding())
def script2 = new FooScript(new ScriptBinding())
def script3 = new FooScript(new ScriptBinding())
def meta1 = new ScriptMeta(script1)
def meta2 = new ScriptMeta(script2)
def meta3 = new ScriptMeta(script3)
// defs in the root script
def func1 = new FunctionDef(name: 'func1')
def proc1 = new ProcessDef(script1, 'proc1', Mock(ProcessConfig), Mock(TaskBody))
def work1 = new WorkflowDef(script1, Mock(TaskBody), 'work1')
meta1.addDefinition(proc1, func1, work1)
// defs in the second script imported in the root namespace
def func2 = new FunctionDef(name: 'func2')
def proc2 = new ProcessDef(script2, 'proc2', Mock(ProcessConfig), Mock(TaskBody))
def work2 = new WorkflowDef(script2, Mock(TaskBody), 'work2')
meta2.addDefinition(proc2, func2, work2)
// defs in the third script imported in a separate namespace
def func3 = new FunctionDef(name: 'func3')
def proc3 = new ProcessDef(script2, 'proc3', Mock(ProcessConfig), Mock(TaskBody))
def work3 = new WorkflowDef(script3, Mock(TaskBody), 'work3')
meta3.addDefinition(proc3, func3, work3)
when:
meta1.addModule(meta2, null, null)
meta1.addModule(meta3, 'proc3', 'my_process')
meta1.addModule(meta3, 'work3', null)
then:
meta1.getDefinitions() as Set == [proc1, func1, work1] as Set
meta1.getComponent('proc1') == proc1
meta1.getComponent('func1') == func1
meta1.getComponent('work1') == work1
then:
// from root namespace
meta1.getComponent('proc2') == proc2
meta1.getComponent('func2') == func2
meta1.getComponent('work2') == work2
then:
// other namespace are not reachable
meta1.getComponent('xxx') == null
meta1.getComponent('func3') == null
meta1.getComponent('proc3') == null
meta1.getComponent('work3') == work3
meta1.getComponent('my_process') instanceof ProcessDef
meta1.getComponent('my_process').name == 'my_process'
// then:
// meta1.getProcessNames() == ['proc1','proc2','my_process'] as Set
}
def 'should add a module component' () {
given:
def meta = Spy(ScriptMeta)
def comp1 = Mock(ComponentDef)
def comp2 = Mock(ComponentDef)
// should a component to imports
when:
meta.addModule0(comp1)
then:
2 * comp1.getName() >> 'foo'
1 * meta.getComponent('foo') >> null
meta.@imports.get('foo') == comp1
// should a component to imports with alias name
when:
meta.@imports.clear()
meta.addModule0(comp1, 'bar')
then:
1 * comp1.getName() >> 'foo'
1 * meta.getComponent('bar') >> null
1 * comp1.withName('bar') >> comp2
meta.@imports.get('bar') == comp2
}
def 'should throw a duplicate process name exception' () {
given:
def meta = Spy(ScriptMeta)
def comp1 = Mock(ComponentDef)
when:
meta.@imports.clear()
meta.addModule0(comp1)
then:
1 * comp1.getName() >> 'foo'
1 * meta.getComponent('foo') >> comp1
thrown(DuplicateModuleIncludeException)
}
}