source: java2python/src/test/java/tudelft/utilities/j2p/t/PersonJsonTest.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.0 KB
Line 
1package tudelft.utilities.j2p.t;
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.Test;
14
15import tudelft.utilities.j2p.formatting.IndentBlock;
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(((IndentBlock) translate(TESTDIR + FULLNAME))
29 .toCode("from testcode.PersonJson import PersonJson"));
30 }
31
32 @Test
33 public void runFromZip() throws FileNotFoundException, IOException,
34 InterruptedException, PythonError {
35 File zipfile = zip(translate(TESTDIR + FULLNAME), new File(FULLNAME));
36 System.out.println("zip file at " + zipfile);
37 Reporter reporter = new Reporter() {
38 private List<String> warnings = new LinkedList<>();
39
40 @Override
41 public void log(Level level, String msg) {
42 log(level, msg, null);
43 }
44
45 @Override
46 public void log(Level level, String msg, Throwable thrown) {
47 if (level == Level.WARNING || level == Level.SEVERE) {
48 warnings.add(msg);
49 System.err.println(msg);
50 if (thrown != null)
51 thrown.printStackTrace();
52 } else {
53 System.out.println(level + ":" + msg);
54 }
55 }
56 };
57 PythonVenv venv = new PythonVenv(zipfile, reporter);
58 String res = venv.call(Arrays.asList("-m", "testcode.PersonJson"),
59 10000);
60 System.out.println(res);
61 assertEquals("Person[Kees,25]\n"
62 + "{\"PersonJson\":{\"name\":\"Kees\",\"age\":25}}\n"
63 + "True\n", res.replace(" ", ""));
64 // as in Java but true->True and pyson inserts some extra whitespace
65 }
66
67}
Note: See TracBrowser for help on using the repository browser.