source: java2python/src/test/java/tudelft/utilities/j2p/PersonJsonTest.java@ 359

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

#110 working on new example translating code that uses ImmutableList. This part does not yet work.

File size: 2.0 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.IOException;
8import java.util.Arrays;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.logging.Level;
12
13import org.junit.Ignore;
14import org.junit.Test;
15
16import tudelft.utilities.logging.Reporter;
17import tudelft.utilities.pyrunner.PythonError;
18import tudelft.utilities.pyrunner.PythonVenv;
19
20public class PersonJsonTest extends PyTest {
21
22 private static final String TESTDIR = "src/test/java/";
23 private static final String FULLNAME = "testcode/PersonJson.java";
24
25 @Test
26 public void test() throws IOException, ClassNotFoundException {
27 System.out.println("TRANSLATION:");
28 System.out.println(translate(TESTDIR + FULLNAME));
29 }
30
31 @Ignore // this fails because we dont have pyson
32 @Test
33 public void runInPython() throws Exception {
34 List<String> res = runPython(translate(TESTDIR + FULLNAME),
35 FULLNAME.replace(".java", ".py"));
36
37 assertEquals(Arrays.asList("Person[Kees,25]\n"
38 + "{\"PersonJson\":{\"name\":\"Kees\",\"age\":25}}\n" + "true"),
39 res);
40 }
41
42 @Test
43 public void runFromZip() throws FileNotFoundException, IOException,
44 InterruptedException, PythonError {
45 File zipfile = zip(translate(TESTDIR + FULLNAME),
46 FULLNAME.split("/")[0]);
47 System.out.println("zip file at " + zipfile);
48 Reporter reporter = new Reporter() {
49 private List<String> warnings = new LinkedList<>();
50
51 @Override
52 public void log(Level level, String msg) {
53 log(level, msg, null);
54 }
55
56 @Override
57 public void log(Level level, String msg, Throwable thrown) {
58 if (level == Level.WARNING || level == Level.SEVERE) {
59 warnings.add(msg);
60 System.err.println(msg);
61 if (thrown != null)
62 thrown.printStackTrace();
63 } else {
64 System.out.println(level + ":" + msg);
65 }
66 }
67 };
68 PythonVenv venv = new PythonVenv(zipfile, reporter);
69 venv.call(Arrays.asList("-c", "from testcode import PersonJson"),
70 10000);
71 }
72
73}
Note: See TracBrowser for help on using the repository browser.