-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScriptFormattingTest.groovy
More file actions
153 lines (136 loc) · 4.65 KB
/
ScriptFormattingTest.groovy
File metadata and controls
153 lines (136 loc) · 4.65 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
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2024-2025, Seqera Labs
*
* Licensed 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.
*/
package nextflow.lsp.services.script
import nextflow.script.formatter.FormattingOptions
import spock.lang.Specification
import com.github.difflib.text.*;
import java.util.stream.Collectors;
import static nextflow.lsp.TestUtils.*
/**
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
class ScriptFormattingTest extends Specification {
enum Color {
RESET("\033[0m"), // Reset
RED("\033[1;31m"), // Red
GREEN("\033[1;32m"), // Green
BLACK("\033[1;30m"); // BLACK
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
boolean checkFormat(ScriptService service, String uri, String before, String after) {
open(service, uri, before)
def textEdits = service.formatting(URI.create(uri), new FormattingOptions(4, true))
def actualText
if (textEdits.size() > 0) {
actualText = textEdits.first().getNewText()
} else {
actualText = "No text at all"
}
def expectedText = after.stripIndent()
System.err.println("Input text:\n" + before)
// Create a configured DiffRowGenerator
DiffRowGenerator generator = DiffRowGenerator.create().build();
// Compute the differences for two test texts.
List<DiffRow> rows = generator.generateDiffRows(
expectedText.lines().collect(Collectors.toList()),
actualText.lines().collect(Collectors.toList())
);
boolean success = actualText == expectedText
System.err.println("\nDiff comparision between expected and actual");
for (DiffRow row : rows) {
DiffRow.Tag tag = row.getTag();
if (tag == DiffRow.Tag.INSERT) {
System.err.println(Color.GREEN.toString() + "++: " + row.getNewLine() + Color.RESET.toString());
} else if (tag == DiffRow.Tag.DELETE) {
System.err.println(Color.RED.toString() + "--: " + row.getOldLine() + Color.RESET.toString());
} else if (tag == DiffRow.Tag.CHANGE) {
System.err.println(Color.GREEN.toString() + "++: " + row.getNewLine() + Color.RESET.toString());
System.err.println(Color.RED.toString() + "--: " + row.getOldLine() + Color.RESET.toString());
} else {
System.err.println(Color.BLACK.toString() + " : " + row.getOldLine() + Color.RESET.toString());
}
}
if (rows.stream().allMatch(r -> r.getTag() == DiffRow.Tag.EQUAL)) {
System.err.println(String.format("No diff! Comparison yields %s%b%s\n",
(success ? Color.GREEN : Color.RED), success, Color.RESET)
);
}
return actualText == expectedText
}
def 'should format a script' () {
given:
def service = getScriptService()
def uri = getUri('main.nf')
expect:
checkFormat(service, uri,
'''\
workflow { println 'Hello!' }
''',
'''\
workflow {
println('Hello!')
}
'''
)
checkFormat(service, uri,
'''\
workflow {
println('Hello!')
}
''',
'''\
workflow {
println('Hello!')
}
'''
)
}
def 'should format an include declaration' () {
given:
def service = getScriptService()
def uri = getUri('main.nf')
expect:
checkFormat(service, uri,
'''\
include{foo;bar}from'./foobar.nf'
''',
'''\
include { foo ; bar } from './foobar.nf'
'''
)
checkFormat(service, uri,
'''\
include{
foo;bar
}from'./foobar.nf'
''',
'''\
include {
foo ;
bar
} from './foobar.nf'
'''
)
}
}