source: java2python/tudutils-t/src/test/java/tudelft/utilities/j2p/t/ReporterTest.java

Last change on this file was 954, checked in by wouter, 4 months ago

translating to file seems working now.

File size: 2.2 KB
Line 
1package tudelft.utilities.j2p.t;
2
3import static org.junit.Assert.assertTrue;
4
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.nio.file.Paths;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.logging.Level;
14
15import org.eclipse.jdt.annotation.NonNull;
16import org.junit.Test;
17
18import tudelft.utilities.j2p.PyProgram;
19import tudelft.utilities.logging.Reporter;
20import tudelft.utilities.pyrunner.PythonError;
21import tudelft.utilities.pyrunner.PythonVenv;
22
23public class ReporterTest {
24
25 private static final String TESTDIR = "src/test/java/";
26 private static final String FULLNAME = "testcode/Reporter1.java";
27
28 @Test
29 public void test()
30 throws IOException, ClassNotFoundException, TranslationException {
31 System.out.println("TRANSLATION:");
32 final @NonNull PyProgram program = PyProgram.fromModules(
33 Paths.get(TESTDIR), Arrays.asList(new File(FULLNAME)),
34 Paths.get(TESTDIR), Collections.emptyList());
35 System.out.println(program.toString());
36 }
37
38 @Test
39 public void runFromZip() throws FileNotFoundException, IOException,
40 InterruptedException, PythonError, TranslationException {
41 final @NonNull PyProgram program = PyProgram.fromModules(
42 Paths.get(TESTDIR), Arrays.asList(new File(FULLNAME)),
43 Paths.get(TESTDIR), Collections.emptyList());
44 File zipfile = program.getZip();
45
46 System.out.println("zip file at " + zipfile);
47 Reporter reporter = new Reporter() {
48 private List<String> warnings = new LinkedList<>();
49
50 @Override
51 public void log(Level level, String msg) {
52 log(level, msg, null);
53 }
54
55 @Override
56 public void log(Level level, String msg, Throwable thrown) {
57 if (level == Level.WARNING || level == Level.SEVERE) {
58 warnings.add(msg);
59 System.err.println(msg);
60 if (thrown != null)
61 thrown.printStackTrace();
62 } else {
63 System.out.println(level + ":" + msg);
64 }
65 }
66 };
67 PythonVenv venv = new PythonVenv(zipfile, reporter);
68 String res = venv.call(Arrays.asList("-m", "testcode.Reporter1"),
69 10000);
70 System.out.println(res);
71 assertTrue(res.contains("ok"));
72 assertTrue(res.contains("INFO"));
73 }
74
75}
Note: See TracBrowser for help on using the repository browser.