Skip to content

Commit 9830290

Browse files
gselzerclaude
andcommitted
code-lsp: split ScriptProcessingTest by what is removed
Test new-style (#@) lines, old-style header parameters and shebangs separately, checking the variables they declare by name and type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 142274f commit 9830290

1 file changed

Lines changed: 62 additions & 77 deletions

File tree

‎scijava-code-lsp/src/test/java/org/scijava/code/lsp/ScriptProcessingTest.java‎

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -69,38 +69,63 @@ public void tearDown() {
6969
context.dispose();
7070
}
7171

72+
/**
73+
* Tests that new ({@code #@}) script declarations are removed when they
74+
* start a line in the script.
75+
*/
7276
@Test
73-
public void testRemovedLines() {
74-
// The lines SciJava removes before running a script: its shebang, its
75-
// #@ lines (parameters and directives), and old-style parameters in its
76-
// header.
77+
public void testNewStyleParameters() {
78+
final ScriptProcessing.Processed p = processing.of("" + //
79+
"#@ String name\n" + // 0
80+
"#@script(name=\"x\")\n" + // 1: directives too
81+
"x = 1\n" + // 2
82+
"#@output int n\n" + // 3: even after code
83+
" #@ int indented"); // 4: but only at a line's start
84+
assertEquals(Arrays.asList(0, 1, 3), removedLines(p));
85+
assertEquals(Arrays.asList("name: String", "n: int"), declarations(p));
86+
}
87+
88+
/**
89+
* Tests that old ({@code // @}) parameter declarations are removed, in the
90+
* script's header only.
91+
*/
92+
@Test
93+
public void testOldStyleParameters() {
94+
final ScriptProcessing.Processed p = processing.of("" + //
95+
"// @String name\n" + // 0
96+
"\n" + // 1: blank lines keep the header going
97+
"# @int n\n" + // 2: in any language's comments
98+
"// Greets n\n" + // 3: but words end it, even in comments
99+
"// @String late"); // 4
100+
assertEquals(Arrays.asList(0, 2), removedLines(p));
101+
assertEquals(Arrays.asList("name: String", "n: int"), declarations(p));
102+
}
103+
104+
/** Tests that shebangs are removed, on the first line only. */
105+
@Test
106+
public void testShebang() {
77107
final ScriptProcessing.Processed p = processing.of("" + //
78108
"#!/usr/bin/env groovy\n" + // 0
79-
"// @String old\n" + // 1
80-
"#@script(name=\"x\")\n" + // 2
81-
"#@ int n\n" + // 3
82-
"println n // @not\n" + // 4: not these, which are the script's
83-
" #@ indented\n" + // 5
84-
"# comment\n" + // 6
85-
"// @late\n" + // 7
86-
"#!late"); // 8
87-
assertEquals(Arrays.asList(0, 1, 2, 3), new ArrayList<>(new TreeSet<>(
88-
p.removedLines)));
89-
assertEquals(Arrays.asList("old", "n"), new ArrayList<>(p.parameters
90-
.keySet()));
109+
"#!late"); // 1
110+
assertEquals(List.of(0), removedLines(p));
111+
assertTrue(p.parameters.isEmpty());
91112
}
92113

114+
/**
115+
* Tests script parameter lines are removed even when incomplete
116+
*/
93117
@Test
94118
public void testRejectedParameters() {
95-
// Parameters SciJava rejects are likely being typed: their lines are
96-
// removed all the same, and the variables they declare known.
97119
final ScriptProcessing.Processed p = processing.of("" + //
98-
"#@ NoSuchType thing\n" + //
99-
"#@ Str\n" + //
120+
"#@ NoSuchType thing\n" + // 0: unknown type, so Object
121+
"#@ Str\n" + // 1: no name yet
122+
"#@ String (label=\"Unfinished\n" + // 2: unclosed
100123
"x = 1");
101-
assertEquals(Arrays.asList(0, 1), new ArrayList<>(new TreeSet<>(
102-
p.removedLines)));
103-
assertSame(Object.class, p.parameters.get("thing"));
124+
// All three parameter lines are removed
125+
assertEquals(Arrays.asList(0, 1, 2), removedLines(p));
126+
// even though only one is complete
127+
assertEquals(Collections.singletonMap("thing", Object.class),
128+
p.parameters);
104129
}
105130

106131
@Test
@@ -110,65 +135,25 @@ public void testNothingToProcess() {
110135
assertEquals(Collections.emptyMap(), p.parameters);
111136
}
112137

113-
@Test
114-
public void testInputsAndOutputs() {
115-
final Map<String, Class<?>> params = parameters("" + //
116-
"#@ String (label=\"Please enter your name\") name\n" + //
117-
"#@output String greeting\n" + //
118-
"#@ int count\n" + //
119-
"\n" + //
120-
"greeting = \"Hello, \" + name + \"!\"\n");
121-
assertEquals(Arrays.asList("name", "count", "greeting"), //
122-
new ArrayList<>(params.keySet()));
123-
assertSame(String.class, params.get("name"));
124-
assertSame(int.class, params.get("count"));
125-
assertSame(String.class, params.get("greeting"));
126-
}
127-
128-
@Test
129-
public void testNoParameters() {
130-
assertTrue(parameters("x = 1\n").isEmpty());
131-
// The implicit return value output is not a declared variable.
132-
assertTrue(parameters("#@script(name=\"x\")\n").isEmpty());
133-
}
134-
135-
@Test
136-
public void testHalfTypedAndUnresolvable() {
137-
final Map<String, Class<?>> params = parameters("" + //
138-
"#@ String name\n" + //
139-
"#@ NoSuchType thing\n" + //
140-
"#@ Str\n" + //
141-
"#@ String (label=\"Unfinished\n");
142-
assertEquals(Arrays.asList("name", "thing"), //
143-
new ArrayList<>(params.keySet()));
144-
assertSame(String.class, params.get("name"));
145-
assertSame(Object.class, params.get("thing"));
146-
}
138+
// -- Helper methods --
147139

148-
@Test
149-
public void testScriptDirective() {
150-
final List<String> warnings = new ArrayList<>();
151-
context.service(LogService.class).addLogListener(message -> {
152-
if (message.level() == LogLevel.WARN) warnings.add(message.text());
153-
});
154-
final Map<String, Class<?>> params = parameters("" + //
155-
"#@script(env=\"env.toml\", scheme=\"pixi.toml\")\n" + //
156-
"#@ String name\n");
157-
assertEquals(Arrays.asList("name"), new ArrayList<>(params.keySet()));
158-
// The directive is not a parameter, so it is not warned about.
159-
assertTrue(warnings.isEmpty(), warnings.toString());
140+
/** The lines removed from a processed script, in order. */
141+
private static List<Integer> removedLines(
142+
final ScriptProcessing.Processed p)
143+
{
144+
return new ArrayList<>(new TreeSet<>(p.removedLines));
160145
}
161146

162-
@Test
163-
public void testCached() {
164-
final Map<String, Class<?>> first = parameters(
165-
"#@ String name\nx = 1\n");
166-
// Non-declaration edits reuse the parse.
167-
assertSame(first, parameters("#@ String name\nx = 12\n"));
147+
/** The variables declared by a processed script, in order: "name: Type". */
148+
private static List<String> declarations(
149+
final ScriptProcessing.Processed p)
150+
{
151+
final List<String> result = new ArrayList<>();
152+
p.parameters.forEach((name, type) -> result.add(name + ": " + type
153+
.getSimpleName()));
154+
return result;
168155
}
169156

170-
// -- Helper methods --
171-
172157
/** The variables a script's parameters define, as processed. */
173158
private Map<String, Class<?>> parameters(final String text) {
174159
return processing.of(text).parameters;

0 commit comments

Comments
 (0)