Skip to content

Commit b1bab69

Browse files
committed
Always ignore commented script parameters
Before this commit, commented-out new-style (#@) script parameters were ignored when in the script body, but parsed when in the script header, because they matched the pattern for old-style parameter syntax. We now explicitly omit lines with new-style syntax preceded by non-word characters from being matched, so the behavior is consistent independent of the position of the parameter in the script. Without this change in ParameterScriptProcessor, the test added in this commit would fail with: [ERROR] ParameterScriptProcessorTest.testScriptParameterParsing:49 expected null, but was:<commentedHeaderParameter: required=true, persisted=true>
1 parent 3d64e2a commit b1bab69

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/main/java/org/scijava/script/process/ParameterScriptProcessor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void begin(final ScriptInfo scriptInfo) {
129129

130130
@Override
131131
public String process(final String line) {
132-
// parse new-style parameters starting with @# anywhere in the script.
132+
// parse new-style parameters starting with #@ anywhere in the script.
133133
if (line.matches("^#@.*")) {
134134
final int at = line.indexOf('@');
135135
return process(line, line.substring(at + 1));
@@ -140,7 +140,9 @@ public String process(final String line) {
140140
// NB: Check if line contains an '@' with no prior alphameric
141141
// characters. This assumes that only non-alphanumeric characters can
142142
// be used as comment line markers.
143-
if (line.matches("^[^\\w]*@.*")) {
143+
// NB: In addition, to allow for commented-out new-style parameters, we exclude
144+
// lines that have the new-style #@ preceded by non-alphanumeric characters.
145+
if (line.matches("^[^\\w]*[^\\w#]@.*")) {
144146
final int at = line.indexOf('@');
145147
return process(line, line.substring(at + 1));
146148
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.scijava.script.process;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.StringReader;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.scijava.Context;
11+
import org.scijava.script.ScriptInfo;
12+
13+
public class ParameterScriptProcessorTest {
14+
15+
private Context context;
16+
17+
@Before
18+
public void setUp() {
19+
context = new Context();
20+
}
21+
22+
@After
23+
public void tearDown() {
24+
context.dispose();
25+
}
26+
27+
@Test
28+
public void testScriptParameterParsing() {
29+
String script = "" + //
30+
"% @String legacyStyleParameter\n" +
31+
"% #@ String commentedHeaderParameter\n" +
32+
"% ############## Some Comment ###########\n" +
33+
"#@ String implicitInputParameter\n" +
34+
"#@input String explicitInputParameter\n" +
35+
"\n" +
36+
"% @String legacyStyleBodyParameter\n" +
37+
"% #@ String commentedBodyParameter\n" +
38+
"\n" +
39+
"#@output implicitlyTypedOutputParameter\n" +
40+
"#@output String explicitlyTypedOutputParameter\n";
41+
final ScriptInfo info = new ScriptInfo(context, ".bsizes", new StringReader(script));
42+
assertEquals("legacyStyleParameter", info.getInput("legacyStyleParameter").getName());
43+
assertEquals("implicitInputParameter", info.getInput("implicitInputParameter").getName());
44+
assertEquals("explicitInputParameter", info.getInput("explicitInputParameter").getName());
45+
46+
assertEquals("implicitlyTypedOutputParameter", info.getOutput("implicitlyTypedOutputParameter").getName());
47+
assertEquals("explicitlyTypedOutputParameter", info.getOutput("explicitlyTypedOutputParameter").getName());
48+
49+
assertNull(info.getInput("commentedHeaderParameter"));
50+
assertNull(info.getInput("legacyStyleBodyParameter"));
51+
assertNull(info.getInput("commentedBodyParameter"));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)