1 package jep.test;
2 
3 import java.util.List;
4 
5 import jep.Interpreter;
6 import jep.JepConfig;
7 import jep.JepException;
8 import jep.MainInterpreter;
9 import jep.SubInterpreter;
10 
11 /**
12  * Tests that shared modules can have sys.argv set before they are imported as
13  * shared modules.
14  *
15  * Created: July 2017
16  *
17  * @author Nate Jensen
18  * @see "https://github.com/ninia/jep/issues/81"
19  */
20 public class TestSharedArgv {
21 
22     @SuppressWarnings("unchecked")
main(String[] args)23     public static void main(String[] args) throws JepException {
24 
25         final String[] argv = new String[] { "", "-h", "other" };
26 
27         MainInterpreter.setSharedModulesArgv(argv);
28         JepConfig cfg = new JepConfig();
29         cfg.addSharedModules("logging");
30         cfg.addIncludePaths(".");
31 
32         try (Interpreter interp = new SubInterpreter(cfg)) {
33             /*
34              * since logging is a shared module in this test it will
35              * automatically import and get its sys.argv setup in the Jep
36              * constructor call
37              */
38             interp.eval("import logging");
39             List<String> result = (List<String>) interp
40                     .getValue("logging.sys.argv");
41             for (int i = 0; i < result.size(); i++) {
42                 if (!result.get(i).equals(argv[i])) {
43                     throw new RuntimeException("argv[" + i + "] did not match");
44                 }
45             }
46         }
47     }
48 
49 }
50