BlackBox.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from vtkmodules.util import vtkMethodParser
  2. class Tester:
  3. def __init__(self, debug=0):
  4. self.setDebug(debug)
  5. self.parser = vtkMethodParser.VtkDirMethodParser()
  6. self.obj = None
  7. def setDebug(self, val):
  8. """Sets debug value of the vtkMethodParser. 1 is verbose and
  9. 0 is not. 0 is default."""
  10. vtkMethodParser.DEBUG = val
  11. def testParse(self, obj):
  12. """ Testing if the object is parseable."""
  13. self.parser.parse_methods(obj)
  14. self.obj = obj
  15. def testGetSet(self, obj, excluded_methods=[]):
  16. """ Testing Get/Set methods."""
  17. if obj != self.obj:
  18. self.testParse(obj)
  19. methods = self.parser.get_set_methods()
  20. toggle = [x[:-2] for x in self.parser.toggle_methods()]
  21. methods.extend(toggle)
  22. for method in methods:
  23. if method in excluded_methods:
  24. continue
  25. setm = "Set%s"%method
  26. getm = "Get%s"%method
  27. val = eval("obj.%s()"%getm)
  28. try:
  29. eval("obj.%s"%setm)(*val)
  30. except TypeError:
  31. eval("obj.%s"%setm)(*(val,))
  32. val1 = eval("obj.%s()"%getm)
  33. if val1 != val:
  34. name = obj.GetClassName()
  35. msg = "Failed test for %(name)s.Get/Set%(method)s\n"\
  36. "Before Set, value = %(val)s; "\
  37. "After Set, value = %(val1)s"%locals()
  38. raise AssertionError(msg)
  39. def testBoolean(self, obj, excluded_methods=[]):
  40. """ Testing boolean (On/Off) methods."""
  41. if obj != self.obj:
  42. self.testParse(obj)
  43. methods = self.parser.toggle_methods()
  44. for method1 in methods:
  45. method = method1[:-2]
  46. if method in excluded_methods:
  47. continue
  48. getm = "Get%s"%method
  49. orig_val = eval("obj.%s()"%getm)
  50. # Turn on
  51. eval("obj.%sOn()"%method)
  52. val = eval("obj.%s()"%getm)
  53. if val != 1:
  54. name = obj.GetClassName()
  55. msg = "Failed test for %(name)s.%(method)sOn\n"\
  56. "Result not equal to 1 "%locals()
  57. raise AssertionError(msg)
  58. # Turn on
  59. eval("obj.%sOff()"%method)
  60. val = eval("obj.%s()"%getm)
  61. if val != 0:
  62. name = obj.GetClassName()
  63. msg = "Failed test for %(name)s.%(method)sOff\n"\
  64. "Result not equal to 0 "%locals()
  65. raise AssertionError(msg)
  66. # set the value back to the original value.
  67. eval("obj.Set%s(orig_val)"%method)
  68. def test(self, obj):
  69. """Test the given vtk object."""
  70. # first try parsing the object.
  71. self.testParse(obj)
  72. # test the get/set methods
  73. self.testGetSet(obj)
  74. # test the boolean methods
  75. self.testBoolean(obj)