Skip to content

Commit 794e1f1

Browse files
committed
Clean up JythonDialectTest
1 parent 1ba3b03 commit 794e1f1

1 file changed

Lines changed: 42 additions & 87 deletions

File tree

‎src/test/java/org/scijava/plugins/scripting/jython/completion/JythonDialectTest.java‎

Lines changed: 42 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
import static org.junit.Assert.assertFalse;
3333
import static org.junit.Assert.assertTrue;
3434

35-
import java.util.Arrays;
36-
import java.util.LinkedHashMap;
3735
import java.util.List;
38-
import java.util.Map;
3936
import java.util.concurrent.TimeUnit;
4037
import java.util.stream.Collectors;
4138

@@ -50,7 +47,6 @@
5047
import org.scijava.lsp.LanguageServerService;
5148
import org.python.jsr223.PyScriptEngineFactory;
5249
import org.scijava.lsp.ScriptDialect.Header;
53-
import org.scijava.lsp.ScriptDialect.Variable;
5450
import org.scijava.lsp.ScriptSession;
5551
import org.scijava.script.ScriptLanguage;
5652
import org.scijava.script.ScriptService;
@@ -78,124 +74,83 @@ public void tearDown() {
7874
}
7975

8076
@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
8479
final JythonDialect dialect = new JythonDialect();
8580
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()));
8883
}
8984

9085
@Test
9186
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
9388
assertEquals("str", JythonDialect.parameterType(String.class));
9489
assertEquals("int", JythonDialect.parameterType(int.class));
9590
assertEquals("int", JythonDialect.parameterType(Long.class));
9691
assertEquals("float", JythonDialect.parameterType(double.class));
9792
assertEquals("bool", JythonDialect.parameterType(boolean.class));
9893
assertEquals("array.array", JythonDialect.parameterType(
9994
double[].class));
100-
// Or else as the Java objects themselves.
95+
// Ensure Java classes stay the same
10196
assertEquals("java.math.BigDecimal", JythonDialect.parameterType(
10297
java.math.BigDecimal.class));
10398
}
10499

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-
149100
@Test
150101
public void testScriptParameters() {
151-
final String header = "" + //
102+
final String scijavaScriptPrefix = //
152103
"#@ String (label=\"Please enter your name\") name\n" + //
153104
"#@output String greeting\n" + //
154-
"#@ int count\n" + //
105+
"#@ java.util.Map$Entry entry\n" + //
106+
"#@ double[] values\n" + //
155107
"#@ 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");
161113
assertTrue("expected upper among " + nameMembers, nameMembers.contains(
162114
"upper"));
163-
assertTrue(complete(header + "name.toUpp").isEmpty());
115+
// ...but NOT the members of their JAVA types
116+
assertTrue(complete(scijavaScriptPrefix + "name.toUpp").isEmpty());
164117
// 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+
*/
182133
@Test
183134
public void testInterpreterBindings() throws Exception {
184-
// Code typed into a running interpreter: its variables.
135+
// Derive a ScriptContext with some bindings
185136
final ScriptEngine engine = new PyScriptEngineFactory().getScriptEngine();
186137
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");
191142
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.
193145
assertTrue(complete("injected.si", live).contains("size"));
194-
assertTrue(complete("x = hel", live).contains("helper"));
195146
// An imported Java class offers its static members.
196147
assertTrue(complete("Collections.emptyL", live).contains("emptyList"));
197148
// A module bound under another name is the module itself.
198149
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"));
199154
}
200155

201156
// -- Helper methods --

0 commit comments

Comments
 (0)