1 | package tudelft.utilities.j2p;
|
---|
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.nio.file.Paths;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.logging.Level;
|
---|
13 |
|
---|
14 | import org.junit.Test;
|
---|
15 |
|
---|
16 | import tudelft.utilities.logging.Reporter;
|
---|
17 | import tudelft.utilities.pyrunner.PythonError;
|
---|
18 | import tudelft.utilities.pyrunner.PythonVenv;
|
---|
19 |
|
---|
20 | public class ValueTest {
|
---|
21 |
|
---|
22 | PyProgram program = PyProgram.fromDirectory(Paths.get("src/test/value"));
|
---|
23 |
|
---|
24 | @Test
|
---|
25 | public void test() throws IOException, ClassNotFoundException {
|
---|
26 | System.out.println(program);
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Test
|
---|
30 | public void runFromZip() throws FileNotFoundException, IOException,
|
---|
31 | InterruptedException, PythonError {
|
---|
32 | File zipfile = program.getZip();
|
---|
33 | System.out.println("zip file at " + zipfile);
|
---|
34 | Reporter reporter = new Reporter() {
|
---|
35 | private List<String> warnings = new LinkedList<>();
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void log(Level level, String msg) {
|
---|
39 | log(level, msg, null);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void log(Level level, String msg, Throwable thrown) {
|
---|
44 | if (level == Level.WARNING || level == Level.SEVERE) {
|
---|
45 | warnings.add(msg);
|
---|
46 | System.err.println(msg);
|
---|
47 | if (thrown != null)
|
---|
48 | thrown.printStackTrace();
|
---|
49 | } else {
|
---|
50 | System.out.println(level + ":" + msg);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | };
|
---|
54 | PythonVenv venv = new PythonVenv(zipfile, reporter);
|
---|
55 | String res = venv.call(Arrays.asList("-m", "testcode.Value"), 10000);
|
---|
56 | System.out.println(res);
|
---|
57 | assertEquals(Arrays.asList("{\"value\":12.0}", "{\"value\":\"hallo\"}"),
|
---|
58 | Arrays.asList(res.replace(" ", "").split("\n")));
|
---|
59 | // as in Java but true->True and pyson inserts some extra whitespace
|
---|
60 | }
|
---|
61 | } |
---|