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

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

#115 working at translators for annotations

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