Пример #1
0
  private Test genTestSuite(String className, String filename, String bundlePath)
      throws IOException {

    // remove any "non-word" characters, i.e., leave only letters
    // that should ensure the python class name is syntatically valid
    className = className.replaceAll("\\W", "");

    TestSuite ret = new TestSuite(className);
    PythonInterpreter interp = new PythonInterpreter();
    String testCode =
        "# coding=utf-8\n"
            + "from __future__ import with_statement\n"
            + "import junit\n"
            + "from junit.framework.Assert import *\n"
            + "from sikuli.Sikuli import *\n"
            + "class "
            + className
            + " (junit.framework.TestCase):\n"
            + "\tdef __init__(self, name):\n"
            + "\t\tjunit.framework.TestCase.__init__(self,name)\n"
            + "\t\tself.theTestFunction = getattr(self,name)\n"
            + "\t\tsetBundlePath('"
            + bundlePath
            + "')\n"
            + "\tdef runTest(self):\n"
            + "\t\tself.theTestFunction()\n";

    BufferedReader in = new BufferedReader(new FileReader(filename));
    String line;
    // int lineNo = 0;
    // Pattern patDef = Pattern.compile("def\\s+(\\w+)\\s*\\(");
    while ((line = in.readLine()) != null) {
      // lineNo++;
      testCode += "\t" + line + "\n";
      /*
      Matcher matcher = patDef.matcher(line);
      if(matcher.find()){
         String func = matcher.group(1);
         Debug.log("Parsed " + lineNo + ": " + func);
         _lineNoOfTest.put( func, lineNo );
      }
      */
    }
    interp.exec(testCode);
    PyList tests =
        (PyList)
            interp.eval(
                "[" + className + "(f) for f in dir(" + className + ") if f.startswith(\"test\")]");
    while (tests.size() > 0) {
      PyObject t = tests.pop();
      Test t2 = (Test) (t).__tojava__(TestCase.class);
      ret.addTest(t2);
    }

    return ret;
  }
Пример #2
0
 public Object evaluateString(
     LocalhostConnector connector,
     Path locator,
     String content,
     String name,
     int lineno,
     Object security)
     throws Exception {
   final PyCode code = interpreter.compile(content, name);
   return interpreter.eval(code);
 }
Пример #3
0
 public Object evaluateReader(
     LocalhostConnector connector,
     Path locator,
     FileReader reader,
     String filename,
     int lineno,
     Object security)
     throws Exception {
   ScriptContext.get().setCurrentScriptPath(Paths.get(filename));
   final PyCode code = interpreter.compile(reader, filename);
   return interpreter.eval(code);
 }