1 | package tudelft.utilities.j2p.t;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.logging.Level;
|
---|
12 |
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import tudelft.utilities.j2p.formatting.IndentBlock;
|
---|
16 | import tudelft.utilities.logging.Reporter;
|
---|
17 | import tudelft.utilities.pyrunner.PythonError;
|
---|
18 | import tudelft.utilities.pyrunner.PythonVenv;
|
---|
19 |
|
---|
20 | public 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 | }
|
---|