|
32 | 32 | import static org.junit.Assert.assertFalse; |
33 | 33 | import static org.junit.Assert.assertTrue; |
34 | 34 |
|
35 | | -import java.util.Arrays; |
36 | | -import java.util.LinkedHashMap; |
37 | 35 | import java.util.List; |
38 | | -import java.util.Map; |
39 | 36 | import java.util.concurrent.TimeUnit; |
40 | 37 | import java.util.stream.Collectors; |
41 | 38 |
|
|
50 | 47 | import org.scijava.lsp.LanguageServerService; |
51 | 48 | import org.python.jsr223.PyScriptEngineFactory; |
52 | 49 | import org.scijava.lsp.ScriptDialect.Header; |
53 | | -import org.scijava.lsp.ScriptDialect.Variable; |
54 | 50 | import org.scijava.lsp.ScriptSession; |
55 | 51 | import org.scijava.script.ScriptLanguage; |
56 | 52 | import org.scijava.script.ScriptService; |
@@ -78,124 +74,83 @@ public void tearDown() { |
78 | 74 | } |
79 | 75 |
|
80 | 76 | @Test |
81 | | - public void testScriptsAreServed() { |
82 | | - // Jython scripts become plain Jython, which scijava-lsp-jython |
83 | | - // serves. |
| 77 | + public void testSupports() { |
| 78 | + // JythonDialect supports JythonScriptLanguage |
84 | 79 | final JythonDialect dialect = new JythonDialect(); |
85 | 80 | assertTrue(dialect.supports(jython)); |
86 | | - assertEquals("jython", dialect.outputLanguage()); |
87 | | - assertTrue(context.service(LanguageServerService.class).supports(jython)); |
| 81 | + // JythonLanguageServer supports JythonDialect's output |
| 82 | + assertTrue(context.service(LanguageServerService.class).supports(dialect.outputLanguage())); |
88 | 83 | } |
89 | 84 |
|
90 | 85 | @Test |
91 | 86 | public void testParameterType() { |
92 | | - // As Jython converts them: e.g. strings and numbers to Python's. |
| 87 | + // Ensure Java primitives are converted to Python primitives |
93 | 88 | assertEquals("str", JythonDialect.parameterType(String.class)); |
94 | 89 | assertEquals("int", JythonDialect.parameterType(int.class)); |
95 | 90 | assertEquals("int", JythonDialect.parameterType(Long.class)); |
96 | 91 | assertEquals("float", JythonDialect.parameterType(double.class)); |
97 | 92 | assertEquals("bool", JythonDialect.parameterType(boolean.class)); |
98 | 93 | assertEquals("array.array", JythonDialect.parameterType( |
99 | 94 | double[].class)); |
100 | | - // Or else as the Java objects themselves. |
| 95 | + // Ensure Java classes stay the same |
101 | 96 | assertEquals("java.math.BigDecimal", JythonDialect.parameterType( |
102 | 97 | java.math.BigDecimal.class)); |
103 | 98 | } |
104 | 99 |
|
105 | | - @Test |
106 | | - public void testHeader() { |
107 | | - // Each parameter is declared as an instance of its type, whose package |
108 | | - // is imported. |
109 | | - final Map<String, Variable> parameters = new LinkedHashMap<>(); |
110 | | - parameters.put("d", Variable.ofType(java.math.BigDecimal.class)); |
111 | | - parameters.put("n", Variable.ofType(int.class)); |
112 | | - parameters.put("e", Variable.ofType(Map.Entry.class)); |
113 | | - parameters.put("a", Variable.ofType(double[].class)); |
114 | | - final Header header = new JythonDialect().header(parameters); |
115 | | - assertEquals("import array; import java.math; import java.util; " + |
116 | | - "d = java.math.BigDecimal(); n = int(); " + |
117 | | - "e = java.util.Map.Entry(); a = array.array()", header.code()); |
118 | | - // The names the imports bind ("import a.b" binds a) are the header's |
119 | | - // alone. |
120 | | - assertEquals(Arrays.asList("array", "java"), Arrays.asList(header |
121 | | - .hiddenNames().toArray())); |
122 | | - } |
123 | | - |
124 | | - @Test |
125 | | - public void testLiveHeader() throws Exception { |
126 | | - // A live interpreter's variables, by their values. |
127 | | - final ScriptEngine engine = new PyScriptEngineFactory().getScriptEngine(); |
128 | | - engine.put("injected", new java.util.ArrayList<String>()); |
129 | | - engine.eval("import os\n" + // |
130 | | - "from java.util import HashMap\n" + // |
131 | | - "def helper(): pass\n" + // |
132 | | - "pairs = {1: 2}\n"); |
133 | | - final Map<String, Variable> live = new LinkedHashMap<>(); |
134 | | - for (final String name : Arrays.asList("injected", "os", "HashMap", |
135 | | - "helper", "pairs")) |
136 | | - { |
137 | | - final Object value = engine.get(name); |
138 | | - live.put(name, Variable.ofValue(value, value.getClass())); |
139 | | - } |
140 | | - final Header header = new JythonDialect().header(live); |
141 | | - assertEquals("import java.util; " + // |
142 | | - "injected = java.util.ArrayList(); " + // |
143 | | - "import os as os; " + // |
144 | | - "HashMap = java.util.HashMap; " + // |
145 | | - "helper = lambda *args, **kwargs: None; " + // |
146 | | - "pairs = dict()", header.code()); |
147 | | - } |
148 | | - |
149 | 100 | @Test |
150 | 101 | public void testScriptParameters() { |
151 | | - final String header = "" + // |
| 102 | + final String scijavaScriptPrefix = // |
152 | 103 | "#@ String (label=\"Please enter your name\") name\n" + // |
153 | 104 | "#@output String greeting\n" + // |
154 | | - "#@ int count\n" + // |
| 105 | + "#@ java.util.Map$Entry entry\n" + // |
| 106 | + "#@ double[] values\n" + // |
155 | 107 | "#@ NoSuchType mystery\n"; |
156 | | - assertTrue(complete(header + "x = na").contains("name")); |
157 | | - assertTrue(complete(header + "myst").contains("mystery")); |
158 | | - // As the Python values Jython converts them to: e.g. strings are str, |
159 | | - // whose members are Python's, not java.lang.String's. |
160 | | - final List<String> nameMembers = complete(header + "name.upp"); |
| 108 | + // Ensure script parameter names can be autocompleted |
| 109 | + assertTrue(complete(scijavaScriptPrefix + "x = na").contains("name")); |
| 110 | + assertTrue(complete(scijavaScriptPrefix + "myst").contains("mystery")); |
| 111 | + // Primitive script parameters have the members of their JYTHON types... |
| 112 | + final List<String> nameMembers = complete(scijavaScriptPrefix + "name.upp"); |
161 | 113 | assertTrue("expected upper among " + nameMembers, nameMembers.contains( |
162 | 114 | "upper")); |
163 | | - assertTrue(complete(header + "name.toUpp").isEmpty()); |
| 115 | + // ...but NOT the members of their JAVA types |
| 116 | + assertTrue(complete(scijavaScriptPrefix + "name.toUpp").isEmpty()); |
164 | 117 | // Outputs are known too, even before the script assigns them. |
165 | | - assertTrue(complete(header + "greeting.st").contains("strip")); |
166 | | - // Numbers are int, not java.lang.Integer. |
167 | | - assertFalse(complete(header + "count.intV").contains("intValue")); |
168 | | - // Packages imported only to declare those are not completed. |
169 | | - assertFalse(complete(header + "j").contains("java")); |
170 | | - } |
171 | | - |
172 | | - |
173 | | - @Test |
174 | | - public void testAssignmentShadowsScriptParameter() { |
175 | | - final List<String> texts = complete( |
176 | | - "#@ String name\nfrom java.util import ArrayList\nname = ArrayList()\nname.ensureCap"); |
177 | | - assertTrue("expected ArrayList member among " + texts, texts.contains( |
178 | | - "ensureCapacity")); |
179 | | - } |
180 | | - |
181 | | - |
| 118 | + assertTrue(complete(scijavaScriptPrefix + "greeting.st").contains("strip")); |
| 119 | + // Other Java objects are instances of their classes, nested ones included. |
| 120 | + assertTrue(complete(scijavaScriptPrefix + "entry.getK").contains("getKey")); |
| 121 | + // Java arrays are Python arrays. |
| 122 | + assertTrue(complete(scijavaScriptPrefix + "values.app").contains("append")); |
| 123 | + // Modules imported only to declare those are not completed. |
| 124 | + // TODO: Also check "j" (for java), once scijava-lsp ignores #@ lines |
| 125 | + // when deciding whether the script mentions a name. |
| 126 | + assertFalse(complete(scijavaScriptPrefix + "arr").contains("array")); } |
| 127 | + |
| 128 | + |
| 129 | + /** |
| 130 | + * Tests that bindings in an {@link org.scijava.lsp.Environment} make it into the |
| 131 | + * {@link Header}. |
| 132 | + */ |
182 | 133 | @Test |
183 | 134 | public void testInterpreterBindings() throws Exception { |
184 | | - // Code typed into a running interpreter: its variables. |
| 135 | + // Derive a ScriptContext with some bindings |
185 | 136 | final ScriptEngine engine = new PyScriptEngineFactory().getScriptEngine(); |
186 | 137 | engine.put("injected", new java.util.ArrayList<String>()); |
187 | | - engine.eval("from java.util import Collections, HashMap\n" + |
188 | | - "m = HashMap()\n" + |
189 | | - "def helper(): pass\n" + |
190 | | - "import os.path as osp\n"); |
| 138 | + engine.eval("from java.util import Collections\n" + // |
| 139 | + "import os.path as osp\n" + // |
| 140 | + "def helper(): pass\n" + // |
| 141 | + "pairs = {1: 2}\n"); |
191 | 142 | final ScriptContext live = engine.getContext(); |
192 | | - assertTrue(complete("m.putIfAb", live).contains("putIfAbsent")); |
| 143 | + // Pass that ScriptContext to completion and ensure it can be used. |
| 144 | + // A Java object offers its instance members. |
193 | 145 | assertTrue(complete("injected.si", live).contains("size")); |
194 | | - assertTrue(complete("x = hel", live).contains("helper")); |
195 | 146 | // An imported Java class offers its static members. |
196 | 147 | assertTrue(complete("Collections.emptyL", live).contains("emptyList")); |
197 | 148 | // A module bound under another name is the module itself. |
198 | 149 | assertTrue(complete("osp.joi", live).contains("join")); |
| 150 | + // A function is known by name. |
| 151 | + assertTrue(complete("x = hel", live).contains("helper")); |
| 152 | + // A Python value is an instance of its builtin type. |
| 153 | + assertTrue(complete("pairs.ke", live).contains("keys")); |
199 | 154 | } |
200 | 155 |
|
201 | 156 | // -- Helper methods -- |
|
0 commit comments