source: java2python/src/test/java/tudelft/utilities/j2p/ActionsTestTest.java@ 383

Last change on this file since 383 was 383, checked in by wouter, 2 years ago

#115 numerous fixes: UniaryExpression, String.matches, throws, etc. ActionTest now compiles

File size: 2.6 KB
Line 
1package tudelft.utilities.j2p;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.FilenameFilter;
8import java.io.IOException;
9import java.nio.file.Path;
10import java.util.Arrays;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.logging.Level;
14
15import org.junit.Test;
16
17import tudelft.utilities.j2p.t.PyTest;
18import tudelft.utilities.logging.Reporter;
19import tudelft.utilities.pyrunner.PythonError;
20import tudelft.utilities.pyrunner.PythonVenv;
21
22/**
23 * A more complex package involving multiple files.
24 */
25public class ActionsTestTest extends PyTest {
26
27 private static final String TESTDIR = "src/test/java/";
28 private static final Path TESTPATH = Path.of(TESTDIR);
29 private static final String FULLNAME = "testcode/actions/ActionsTest.java";
30
31 @Test
32 public void test() throws IOException, ClassNotFoundException {
33 System.out.println(getProgram());
34 }
35
36 private PyProgram getProgram() throws FileNotFoundException {
37 // get all java files under TESTDIR/testcode/actions
38 File dir = TESTPATH.resolve("testcode/actions").toFile();
39 File[] javafiles = dir.listFiles(new FilenameFilter() {
40 @Override
41 public boolean accept(File dir, String name) {
42 return name.endsWith(".java");
43 }
44 });
45
46 List<PyModule> modules = new LinkedList<>();
47 for (File file : javafiles) {
48 // re-relativize
49 file = TESTPATH.relativize(file.toPath()).toFile();
50 modules.add(PyModule.fromJavaFile(TESTPATH, file));
51 }
52
53 return new PyProgram(modules);
54 }
55
56 @Test
57 public void runFromZip() throws FileNotFoundException, IOException,
58 InterruptedException, PythonError {
59 File zipfile = zip(translate(TESTDIR + FULLNAME), new File(FULLNAME));
60 System.out.println("zip file at " + zipfile);
61 Reporter reporter = new Reporter() {
62 private List<String> warnings = new LinkedList<>();
63
64 @Override
65 public void log(Level level, String msg) {
66 log(level, msg, null);
67 }
68
69 @Override
70 public void log(Level level, String msg, Throwable thrown) {
71 if (level == Level.WARNING || level == Level.SEVERE) {
72 warnings.add(msg);
73 System.err.println(msg);
74 if (thrown != null)
75 thrown.printStackTrace();
76 } else {
77 System.out.println(level + ":" + msg);
78 }
79 }
80 };
81 PythonVenv venv = new PythonVenv(zipfile, reporter);
82 String res = venv.call(
83 Arrays.asList("-m", "testcode.actions.ActionsTest"), 10000);
84 System.out.println(res);
85 assertEquals("Person[Kees,25]\n"
86 + "{\"PersonJson\":{\"name\":\"Kees\",\"age\":25}}\n"
87 + "True\n", res.replace(" ", ""));
88 // as in Java but true->True and pyson inserts some extra whitespace
89 }
90
91}
Note: See TracBrowser for help on using the repository browser.