5656 */
5757public class ScriptDocumentTest {
5858
59- /** A Python-like dialect: arrays become NumPy arrays; strings stay. */
59+ /**
60+ * A Python-like dialect: arrays become NumPy arrays (imported as a private
61+ * alias, {@code _np}); strings stay; nothing else is declared, so its header
62+ * is empty when there are no such parameters.
63+ */
6064 private static final ScriptDialect PYTHON = new ScriptDialect () {
6165
6266 @ Override
@@ -74,36 +78,13 @@ public Header header(final Map<String, Variable> variables) {
7478 final List <String > code = new ArrayList <>();
7579 final boolean numpy = variables .values ().stream ().anyMatch (v -> v .type ()
7680 .isArray ());
77- if (numpy ) code .add ("import numpy" );
81+ if (numpy ) code .add ("import numpy as _np " );
7882 variables .forEach ((name , v ) -> {
79- if (v .type ().isArray ()) code .add (name + ": numpy .ndarray" );
83+ if (v .type ().isArray ()) code .add (name + ": _np .ndarray" );
8084 else if (v .type () == String .class ) code .add (name + ": str" );
8185 });
82- code .add ("task: object" );
8386 return new Header (String .join ("; " , code ), numpy ? Collections
84- .singleton ("numpy" ) : Collections .emptySet ());
85- }
86- };
87-
88- /** A Groovy-like dialect: Java types. */
89- private static final ScriptDialect GROOVY = new ScriptDialect () {
90-
91- @ Override
92- public boolean supports (final ScriptLanguage language ) {
93- return false ;
94- }
95-
96- @ Override
97- public String outputLanguage () {
98- return "groovy" ;
99- }
100-
101- @ Override
102- public Header header (final Map <String , Variable > variables ) {
103- final List <String > code = new ArrayList <>();
104- variables .forEach ((name , v ) -> code .add (v .type ().getName () + " " +
105- name ));
106- return new Header (String .join ("; " , code ), Collections .emptySet ());
87+ .singleton ("_np" ) : Collections .emptySet ());
10788 }
10889 };
10990
@@ -120,42 +101,72 @@ private static Set<Integer> lines(final Integer... lines) {
120101 return new HashSet <>(Arrays .asList (lines ));
121102 }
122103
104+ /**
105+ * Tests how a script's parameters are declared:
106+ * <ul>
107+ * <li>By the dialect's header, on a line of its own, with the parameter
108+ * lines blanked</li>
109+ * <li>With the names the header binds only to declare them (e.g. a module)
110+ * hidden, unless the script itself mentions them</li>
111+ * </ul>
112+ */
123113 @ Test
124114 public void testDeclarations () {
125- final String script = "#@ double[] a\n #@ String s\n #@ Integer n\n a." ;
126- final ScriptDocument doc = ScriptDocument .of (script , params ("a" ,
127- double [].class , "s" , String .class , "n" , Integer .class ), lines (0 , 1 ,
128- 2 ), PYTHON );
129- // The dialect's header, on a line of its own; the parameter lines
130- // blanked.
131- assertEquals ("import numpy; a: numpy.ndarray; s: str; task: object\n " +
132- blanked (script .substring (0 , script .lastIndexOf ('\n' ))) + "\n a." , doc
133- .text ());
134- // The module is the header's alone, unless the script mentions it.
135- assertEquals (Collections .singleton ("numpy" ), doc .hiddenNames ());
136- assertTrue (ScriptDocument .of ("import numpy as np\n " + script , params ("a" ,
115+ // Create a ScriptDocument describing a 2-parameter script.
116+ final String script = "#@ double[] a\n #@ String s\n a." ;
117+ final ScriptDocument doc = ScriptDocument .of (
118+ script ,
119+ params ("a" , double [].class , "s" , String .class ),
120+ lines (0 , 1 ),
121+ PYTHON
122+ );
123+ // The converted text should be the header...
124+ final String expectedText = "import numpy as _np; a: _np.ndarray; s: str\n " +
125+ // ...plus the script where the parameters are blanked
126+ blanked (script .substring (0 , script .lastIndexOf ('\n' ))) + "\n a." ;
127+ assertEquals (expectedText , doc .text ());
128+
129+ // The script doesn't mention "_np", so it should be hidden from completions...
130+ assertEquals (Collections .singleton ("_np" ), doc .hiddenNames ());
131+ // ...even if the script imports numpy itself...
132+ assertEquals (Collections .singleton ("_np" ), ScriptDocument .of (
133+ "import numpy as np\n " + script , params ("a" , double [].class ), lines (),
134+ PYTHON ).hiddenNames ());
135+ // ...but if it did mention "_np", it should not be hidden.
136+ assertTrue (ScriptDocument .of (script + "\n _np = 1" , params ("a" ,
137137 double [].class ), lines (), PYTHON ).hiddenNames ().isEmpty ());
138- assertTrue (ScriptDocument .of (script + "\n numpyish = 1" , params ("a" ,
139- double [].class ), lines (), PYTHON ).hiddenNames ().contains ("numpy" ));
138+ // A name merely starting with "_np" is not a mention.
139+ assertEquals (Collections .singleton ("_np" ), ScriptDocument .of (script +
140+ "\n _npish = 1" , params ("a" , double [].class ), lines (), PYTHON )
141+ .hiddenNames ());
140142 }
141143
144+ /**
145+ * Tests that a dialect declaring nothing adds no header line: the text, and
146+ * so every offset, is unchanged.
147+ */
142148 @ Test
143- public void testIdentity () {
149+ public void testEmptyHeader () {
144150 final String script = "x = 1\n x." ;
145151 final ScriptDocument doc = ScriptDocument .of (script , params (), lines (),
146- GROOVY );
152+ PYTHON );
147153 assertEquals (script , doc .text ());
148154 assertEquals (5 , doc .toTransformed (5 ));
149155 assertEquals (5 , doc .toOriginal (5 ));
150156 assertEquals (new Position (1 , 1 ), doc .toTransformed (new Position (1 , 1 )));
157+ assertEquals (new Position (1 , 1 ), doc .toOriginal (new Position (1 , 1 )));
151158 }
152159
160+ /**
161+ * Tests that every character of a removed line is replaced with a space,
162+ * but for its line break (including the CR of a CRLF).
163+ */
153164 @ Test
154165 public void testBlanking () {
155166 final String script = "#@ String s\r \n println s\r \n #@output x\n # end" ;
156167 final ScriptDocument doc = ScriptDocument .of (script , params ("s" ,
157- String .class ), lines (0 , 2 ), GROOVY );
158- assertEquals ("java.lang.String s \n " +
168+ String .class ), lines (0 , 2 ), PYTHON );
169+ assertEquals ("s: str \n " +
159170 " \r \n println s\r \n \n # end" , doc .text ());
160171 // Blanking keeps every offset, and line breaks (incl. CR) in place.
161172 assertEquals (doc .text ().length () - doc .toTransformed (0 ), script
@@ -164,53 +175,55 @@ public void testBlanking() {
164175 -1 ).length );
165176 }
166177
178+ /**
179+ * Tests that offsets shift by the header's length, and positions (and
180+ * ranges) by its one line; and that those within the header map to
181+ * nothing.
182+ */
167183 @ Test
168- public void testRoundTrips () {
169- // NB: Includes a CRLF and a surrogate pair (two Java chars) .
170- final String script = "#@ String s\r \n t = \" 😀 \" \r \ n s.len" ;
184+ public void testMapping () {
185+ // NB: With a CRLF, which positions must count as one line break .
186+ final String script = "#@ String s\r \n s.len" ;
171187 final ScriptDocument doc = ScriptDocument .of (script , params ("s" ,
172188 String .class ), lines (0 ), PYTHON );
173- for (int i = 0 ; i <= script .length (); i ++) {
174- final int t = doc .toTransformed (i );
175- assertEquals (i , doc .toOriginal (t ));
176- // The text after a position is the same in both, but for the
177- // blanked parameter line.
178- final String after = doc .text ().substring (t );
179- assertEquals (script .length () - i , after .length ());
180- if (i >= script .indexOf ('\r' )) {
181- assertEquals (script .substring (i ), after );
182- }
183- }
184- for (int line = 0 ; line < 3 ; line ++) {
185- final Position p = new Position (line , 2 );
186- final Position t = doc .toTransformed (p );
187- assertEquals (new Position (line + 1 , 2 ), t );
188- assertEquals (p , doc .toOriginal (t ));
189- }
189+ final int header = "s: str\n " .length ();
190+ // Offsets: shifted by the header's length...
191+ assertEquals (header + 3 , doc .toTransformed (3 ));
192+ assertEquals (3 , doc .toOriginal (header + 3 ));
193+ // ...and positions by its line.
194+ assertEquals (new Position (2 , 1 ), doc .toTransformed (new Position (1 , 1 )));
195+ assertEquals (new Position (1 , 1 ), doc .toOriginal (new Position (2 , 1 )));
196+ assertEquals (new Range (new Position (0 , 0 ), new Position (1 , 1 )), doc
197+ .toOriginal (new Range (new Position (1 , 0 ), new Position (2 , 1 ))));
190198 // The header maps to nothing.
191- assertEquals (-1 , doc .toOriginal (0 ));
192- assertEquals (-1 , doc .toOriginal (doc .toTransformed (0 ) - 1 ));
193- assertNull (doc .toOriginal (new Position (0 , 0 )));
199+ assertEquals (-1 , doc .toOriginal (header - 1 ));
200+ assertNull (doc .toOriginal (new Position (0 , 3 )));
194201 assertNull (doc .toOriginal (new Range (new Position (0 , 0 ), new Position (1 ,
195202 1 ))));
196- assertEquals (new Range (new Position (0 , 0 ), new Position (1 , 1 )), doc
197- .toOriginal (new Range (new Position (1 , 0 ), new Position (2 , 1 ))));
198203 }
199204
205+ /**
206+ * Tests that a header with a line break (LF or CR) is rejected, as it would
207+ * disrupt translation logic (see {@link #testMapping()})
208+ */
200209 @ Test
201210 public void testOneLineHeader () {
202- // A header is one line: positions shift by just that line.
203211 assertThrows (IllegalArgumentException .class ,
204212 () -> new ScriptDialect .Header ("import numpy\n x: int" , Collections
205213 .emptySet ()));
206214 assertThrows (IllegalArgumentException .class ,
207215 () -> new ScriptDialect .Header ("x: int\r " , Collections .emptySet ()));
208216 }
209217
218+ /**
219+ * Tests that a script without a dialect is plain text, as it is: nothing
220+ * declared or blanked, not even its {@code #@} lines.
221+ */
210222 @ Test
211- public void testAsIs () {
223+ public void testPlainText () {
212224 final String script = "#@ String s\n s." ;
213225 final ScriptDocument doc = ScriptDocument .of (script );
226+ assertEquals ("plaintext" , doc .language ());
214227 assertEquals (script , doc .text ());
215228 assertEquals (0 , doc .toTransformed (0 ));
216229 assertTrue (doc .hiddenNames ().isEmpty ());
0 commit comments