2929
3030package org .scijava .plugins .scripting .appose .python .completion ;
3131
32- import static org .junit .Assert .assertEquals ;
3332import static org .junit .Assert .assertFalse ;
3433import static org .junit .Assert .assertTrue ;
3534import static org .junit .Assert .fail ;
4544import java .util .concurrent .TimeUnit ;
4645
4746import org .eclipse .lsp4j .CompletionItem ;
48- import org .eclipse .lsp4j .CompletionList ;
4947import org .eclipse .lsp4j .Diagnostic ;
50- import org .eclipse .lsp4j .Hover ;
51- import org .eclipse .lsp4j .SignatureHelp ;
5248import org .junit .AfterClass ;
5349import org .junit .BeforeClass ;
5450import org .junit .Test ;
5551import org .scijava .Context ;
5652import org .scijava .code .lsp .LanguageServerService ;
57- import org .scijava .code .lsp .Positions ;
5853import org .scijava .code .lsp .ScriptSession ;
5954import org .scijava .module .ModuleService ;
6055import org .scijava .script .ScriptInfo ;
6358import org .scijava .script .ScriptService ;
6459
6560/**
66- * End-to-end tests of the language servers of Python scripts run via Appose
67- * (see {@link ApposePythonDialect}, and scijava-code-lsp-jedi), with a real
68- * jedi-language-server, through the {@link LanguageServerService}, as editors
69- * use it.
61+ * End-to-end tests of completing Python scripts run via Appose (see
62+ * {@link ApposePythonDialect}), with a real jedi-language-server (from
63+ * scijava-code-lsp-jedi, whose own tests test it on plain Python), through the
64+ * {@link LanguageServerService}, as editors use it: what is particular to
65+ * these scripts.
7066 * <p>
71- * The first completion builds jedi-language-server's environment (see
72- * code-lsp-jedi), a new dependency the tooling environment that installs
73- * overlays, and a script run the small uv environment of
74- * {@code appose-test/requirements.txt}; all require network access.
67+ * The first completion builds jedi-language-server's environment, and a
68+ * script run the small uv environment of {@code appose-test/requirements.txt};
69+ * both require network access.
7570 * </p>
7671 *
7772 * @author Gabriel Selzer
@@ -105,22 +100,6 @@ public static void tearDown() {
105100 if (context != null ) context .dispose ();
106101 }
107102
108- @ Test
109- public void testStandardLibrary () throws Exception {
110- // Before any script environment is built, the server knows the standard
111- // library, with signatures and documentation.
112- final List <CompletionItem > completions = completeUntil (
113- "import json\n json.du" , null , "dumps" );
114- final CompletionItem dumps = completions .stream ().filter (c -> c .getLabel ()
115- .equals ("dumps" ) && c .getLabelDetails () != null ).findFirst ().get ();
116- assertTrue (dumps .getLabelDetails ().getDetail (), dumps .getLabelDetails ()
117- .getDetail ().startsWith ("(Any obj, bool skipkeys" ));
118- assertTrue (dumps .getDocumentation () != null || session (null ).resolve (
119- dumps ).get (1 , TimeUnit .MINUTES )
120- .getDocumentation () != null );
121- report ("import json\n json.l" , null , "loads" , "o" );
122- }
123-
124103 @ Test
125104 public void testScriptEnvironment () throws Exception {
126105 // Run a script, building its environment (which includes numpy).
@@ -139,46 +118,12 @@ public void testScriptEnvironment() throws Exception {
139118 // known, and so is the type of the image parameter.
140119 completeUntil (header + "np.ze" , script .getPath (), "zeros" );
141120 completeUntil (header + "image.sha" , script .getPath (), "shape" );
142- report (header + "np.random.r" , script .getPath (), "randn" , "a" );
143- }
144-
145- @ Test
146- public void testSignatureHelp () throws Exception {
147- final String code = "import json\n json.dumps(obj, " ;
148- final ScriptSession session = session (null );
149- final long deadline = System .currentTimeMillis () + BUILD_TIMEOUT ;
150- SignatureHelp help = null ;
151- while (help == null && System .currentTimeMillis () < deadline ) {
152- help = session .signatureHelp (code , code .length ()).get (1 ,
153- TimeUnit .MINUTES );
154- }
155- assertTrue (help .getSignatures ().get (0 ).getLabel ().contains ("dumps(" ));
156- assertFalse (help .getSignatures ().get (0 ).getParameters ().isEmpty ());
157- assertEquals (Integer .valueOf (1 ), help .getActiveParameter ());
158- }
159-
160- @ Test
161- public void testHover () throws Exception {
162- final String code = "import json\n json.dumps(1)" ;
163- final ScriptSession session = session (null );
164- final long deadline = System .currentTimeMillis () + BUILD_TIMEOUT ;
165- Hover hover = null ;
166- while (hover == null && System .currentTimeMillis () < deadline ) {
167- hover = session .hover (code , code .indexOf ("dumps" ) + 2 ).get (1 ,
168- TimeUnit .MINUTES );
169- }
170- assertTrue (String .valueOf (hover ), hover .getContents ().getRight ().getValue ()
171- .contains ("JSON" ));
172- // The word's range, in the script.
173- assertEquals (Positions .position (code , code .indexOf ("dumps" )), hover
174- .getRange ().getStart ());
175- assertEquals (Positions .position (code , code .indexOf ("dumps" ) + 5 ), hover
176- .getRange ().getEnd ());
177121 }
178122
179123 @ Test
180124 public void testDiagnostics () throws Exception {
181- // A syntax error, on the script's third line.
125+ // A syntax error, on the script's third line: after its parameter
126+ // lines.
182127 final String code = "#@ double sigma\n x = sigma\n y = (1 +\n " ;
183128 final long deadline = System .currentTimeMillis () + BUILD_TIMEOUT ;
184129 List <Diagnostic > problems = Collections .emptyList ();
@@ -200,21 +145,6 @@ public void testParameterBeforeEnvironment() throws Exception {
200145 "shape" );
201146 }
202147
203- @ Test
204- public void testNewDependency () throws Exception {
205- // A script declaring a dependency that is installed nowhere: its
206- // environment has not even been built.
207- final File script = new File (scriptDir , "overlaid.py" );
208- final String header = "#!appose-python\n # /// script\n " +
209- "# dependencies = [\" tifffile\" ]\n # ///\n import tifffile\n " ;
210- Files .write (script .toPath (), (header + "x = 1\n " ).getBytes (
211- StandardCharsets .UTF_8 ));
212- // Opening it prepares completion, including the dependency's overlay.
213- final String code = header + "tifffile.imr" ;
214- sync (code , script .getPath ());
215- completeUntil (code , script .getPath (), "imread" );
216- }
217-
218148 // -- Helper methods --
219149
220150 /** The scripts' sessions, by path ("" for an untitled one). */
@@ -233,11 +163,6 @@ private static List<Diagnostic> problems(final String code) {
233163 }
234164 }
235165
236- /** Tells the servers a script's text. */
237- private static ScriptSession sync (final String code , final String path ) {
238- return session (path ).update (code );
239- }
240-
241166 /** Completes until the expected completion appears (environments build). */
242167 private static List <CompletionItem > completeUntil (final String code ,
243168 final String path , final String expected ) throws Exception
@@ -261,26 +186,4 @@ private static List<CompletionItem> complete(final String code,
261186 return session (path ).completion (code , code .length ()).get (1 ,
262187 TimeUnit .MINUTES ).getItems ();
263188 }
264-
265- /**
266- * Prints how long completions take, once warm: for another keystroke of a
267- * word the server has answered (cached); and for a new word.
268- */
269- private static void report (final String code , final String path ,
270- final String expected , final String more ) throws Exception
271- {
272- completeUntil (code , path , expected );
273- long t0 = System .nanoTime ();
274- final int n = complete (code + more , path ).size ();
275- final long cached = System .nanoTime () - t0 ;
276- final String other = code .substring (0 , code .lastIndexOf ('.' ) + 1 ) + "s" ;
277- final ScriptSession session = session (path );
278- t0 = System .nanoTime ();
279- final CompletionList all = session .completion (other , other .length ()).get (
280- 1 , TimeUnit .MINUTES );
281- final long arrived = System .nanoTime () - t0 ;
282- System .out .println ("[completion timing] same word (cached): " + cached /
283- 1000000 + " ms, " + n + " completions; new word: " + all .getItems ()
284- .size () + " completions after " + arrived / 1000000 + " ms" );
285- }
286189}
0 commit comments