Prechádzať zdrojové kódy

ENH: Add test for ctkAbstractPythonManager::pythonAttributes()

This commit adds "PythonAttributes-test.py" scripts and improve
python manager test to check that expected attributes are returned.

For example, `d.foo_class()` should return the 'foo_instance_member'

Co-authored-by: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Mayeul Chassagnard 8 rokov pred
rodič
commit
5bae457250

+ 53 - 0
Libs/Scripting/Python/Core/Testing/Cpp/PythonAttributes-test.py

@@ -0,0 +1,53 @@
+
+class Maths(object):
+
+  MATHS_CLASS_MEMBER=0.1
+
+  def __init__(self,num):
+    self.math_instance_member = num
+
+  def maths_instance_method(self):
+    print("Hello from instance method")
+
+
+class Bar(object):
+
+  BAR_CLASS_MEMBER = 1
+
+  def __init__(self):
+    self.bar_instance_member = 1
+
+  def bar_instance_method(self):
+    print("Hello from instance method")
+
+  def bar_maths(self,num):
+    return Maths(num)
+
+  @staticmethod
+  def bar_class_method():
+    print("Hello from class method")
+
+
+class Foo(object):
+
+  FOO_CLASS_MEMBER = 1
+
+  def __init__(self):
+    self.foo_instance_member = 1
+
+  def foo_instance_method(self):
+    print("Hello from instance method")
+
+  def instantiate_bar(self):
+    return Bar()
+
+  @staticmethod
+  def foo_class_method():
+    print("Hello from class method")
+
+f = Foo()
+
+class Object(object): pass
+
+d = Object()
+setattr(d, 'foo_class', Foo)

+ 49 - 1
Libs/Scripting/Python/Core/Testing/Cpp/ctkAbstractPythonManagerTest.cpp

@@ -54,7 +54,8 @@ private Q_SLOTS:
   void testExecuteFile();
   void testExecuteFile_data();
 
-  //void testPythonAttributes(); // TODO
+  void testPythonAttributes();
+  void testPythonAttributes_data();
 
   void testPythonModule();
   void testPythonModule_data();
@@ -271,6 +272,53 @@ void ctkAbstractPythonManagerTester::testExecuteFile_data()
 }
 
 // ----------------------------------------------------------------------------
+void ctkAbstractPythonManagerTester::testPythonAttributes()
+{
+  QFETCH(QString, pythonVariableName);
+  QFETCH(QStringList, expectedAttributeList);
+
+  QString test_path = __FILE__; // return the local path of this source file
+  // replace "ctkAbstractPythonManagerTest.py" by "PythonAttributes-test.py"
+  test_path.lastIndexOf("/");
+  test_path.replace(test_path.lastIndexOf("/"),
+                         test_path.size() - test_path.lastIndexOf("/"),
+                         "/PythonAttributes-test.py");
+  this->PythonManager.executeFile(test_path);
+
+  QStringList AttributeList = this->PythonManager.pythonAttributes(pythonVariableName, QString("__main__").toLatin1(), false);
+
+  foreach (const QString& expectedAttribute, expectedAttributeList)
+    {
+    QVERIFY(AttributeList.contains(expectedAttribute));
+    }
+}
+
+// ----------------------------------------------------------------------------
+void ctkAbstractPythonManagerTester::testPythonAttributes_data()
+{
+  QTest::addColumn<QString>("pythonVariableName");
+  QTest::addColumn<QStringList>("expectedAttributeList");
+
+  QTest::newRow("d.foo_class()")
+                     << "d.foo_class()"
+                     << (QStringList()
+                         << "FOO_CLASS_MEMBER"
+                         << "foo_class_method"
+                         << "foo_instance_member"
+                         << "foo_instance_method"
+                         << "instantiate_bar");
+
+  QTest::newRow("d.foo_class().instantiate_bar()")
+                     << "d.foo_class().instantiate_bar()"
+                     << (QStringList()
+                         << "BAR_CLASS_MEMBER"
+                         << "bar_class_method"
+                         << "bar_instance_member"
+                         << "bar_instance_method");
+
+}
+
+// ----------------------------------------------------------------------------
 void ctkAbstractPythonManagerTester::testPythonModule()
 {
   QFETCH(QString, pythonCode);