Skip to content

Commit

Permalink
Refs #4316: A real live test for MantidPlot! WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed Dec 22, 2011
1 parent 9640190 commit 2e2e44d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 15 deletions.
32 changes: 29 additions & 3 deletions Code/Mantid/Build/doxygen_to_sip.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ def doxygen_to_docstring(doxygen, method):
def process_sip(filename):
""" Reads an input .sip file and finds methods from
classes. Retrieves doxygen and adds them as
docstrings """
docstrings
@param filename :: input .sip
@return outlines :: processed sip file, as a list of strings
@return dirtext :: text for use in the __dir__() method
(basically a list of the method names found).
"""

root = os.path.split(os.path.abspath(filename))[0]
# Read and split into a buncha lines
Expand All @@ -85,6 +90,8 @@ def process_sip(filename):
classname = ''
classcpp = ''
outlines = []
dirtext = ""

for i in range(len(lines)):
# Copy to output
outlines.append(lines[i])
Expand All @@ -100,6 +107,8 @@ def process_sip(filename):
print "WARNING: Could not find cpp file for class %s" % classname
else:
print "Found class '%s' .cpp file " % classname

dirtext += "\n\n__dir__() for %s\nreturn [" % classname


if classname != "":
Expand All @@ -110,17 +119,25 @@ def process_sip(filename):
if n > 0:
method = line[0:n+1]
n = method.find(' ')

# Find the name of the method (for the __dir__ method)
methodname = method[n+1:]
n2 = methodname.find('(')
methodname = methodname[0:n2]
dirtext += '"%s", ' % methodname

# Make the string like this:
# "void ClassName::method(arguments)"
method = method[0:n+1] + classname + "::" + method[n+1:]

# Now extract the doxygen
doxygen = grab_doxygen(classcpp, method)
# Convert to a docstring
docstring = doxygen_to_docstring(doxygen, method)
# And add to the output
outlines += docstring
# Give back the generated lines
return outlines
return (outlines, dirtext)


#----------------------------------------------------------
Expand All @@ -139,6 +156,9 @@ def process_sip(filename):

parser.add_option('-o', metavar='OUTPUTFILE', dest="outputfile",
help='The name of the output file')

parser.add_option('-d', metavar='DIRFILE', dest="dirfile",
help='The name of the file containing __dir__ methods')

(options, args) = parser.parse_args()

Expand All @@ -148,13 +168,19 @@ def process_sip(filename):
raise Exception("Must specify an output file with -o !")

print "---- Reading from %s ---- " % options.sipfile
out = process_sip(options.sipfile)
(out, dirtext) = process_sip(options.sipfile)

if not (options.outputfile is None):
print "---- Writing to %s ---- " % options.outputfile
f = open(options.outputfile, 'w')
f.write('\n'.join(out))
f.close()

if not (options.dirfile is None):
print "---- Writing to %s ---- " % options.dirfile
f = open(options.dirfile, 'w')
f.write(dirtext)
f.close()



14 changes: 14 additions & 0 deletions Code/Mantid/MantidPlot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,20 @@ foreach ( PYFILE ${PYTHON_INSTALL_FILES} )
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR} COPYONLY )
endforeach ( PYFILE ${PYTHON_INSTALL_FILES} )

###########################################################################
# MantidPlot Python Unit Tests
###########################################################################

# List of .py files than must be run WITHIN MantidPlot.
set ( MANTIDPLOT_TEST_PY_FILES
MantidPlotSliceViewerTest.py
)

# Make a ctest target for each file
foreach ( PYFILE ${MANTIDPLOT_TEST_PY_FILES} )
# ADD_TEST( ${PYFILE} ${CMAKE_BINARY_DIR}/${BIN_DIR}/MantidPlot -xq ${CMAKE_CURRENT_SOURCE_DIR}/test/${PYFILE} )
endforeach ()

###########################################################################
# Installation settings
###########################################################################
Expand Down
23 changes: 21 additions & 2 deletions Code/Mantid/MantidPlot/mantidplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ def getSliceViewer(source, label=""):
return SliceViewerWindowProxy(svw)


#-----------------------------------------------------------------------------
def closeAllSliceViewers():
"""
Closes all open SliceViewer windows.
"""
import mantidqtpython
mantidqtpython.MantidQt.Factory.WidgetFactory.Instance().closeAllSliceViewerWindows()

#-----------------------------------------------------------------------------
# Legacy function
Expand Down Expand Up @@ -264,6 +271,8 @@ class SliceViewerWindowProxy(QtProxyObject):
"""
def __init__(self, toproxy):
QtProxyObject.__init__(self, toproxy)
# List of methods in slicer to pass-through
self.slicer_methods = ["setWorkspace", "getWorkspaceName", "showControls", "openFromXML", "setXYDim", "setXYDim", "getDimX", "getDimY", "setSlicePoint", "setSlicePoint", "getSlicePoint", "getSlicePoint", "setXYLimits", "getXLimits", "getYLimits", "zoomBy", "setXYCenter", "resetZoom", "loadColorMap", "setColorScale", "setColorScaleMin", "setColorScaleMax", "setColorScaleLog", "getColorScaleMin", "getColorScaleMax", "getColorScaleLog", "setColorScaleAutoFull", "setColorScaleAutoSlice"]

def __getattr__(self, attr):
"""
Expand All @@ -274,7 +283,7 @@ def __getattr__(self, attr):

# Pass-through to the contained SliceViewer widget.
sv = self._getHeldObject().getSlicer()
if hasattr(sv, attr):
if attr in self.slicer_methods:
return getattr(sv, attr)
else:
return getattr(self._getHeldObject(), attr)
Expand All @@ -283,13 +292,23 @@ def __str__(self):
"""
Return a string representation of the proxied object
"""
return str(self._getHeldObject())
if self._getHeldObject() is None:
return "None"
else:
return 'SliceViewerWindow(workspace="%s")' % self._getHeldObject().getSlicer().getWorkspaceName()

def __repr__(self):
"""
Return a string representation of the proxied object
"""
return `self._getHeldObject()`

def __dir__(self):
"""
Returns the list of attributes for this object.
Might allow tab-completion to work under ipython
"""
return self.slicer_methods



Expand Down
86 changes: 86 additions & 0 deletions Code/Mantid/MantidPlot/test/MantidPlotSliceViewerTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
""" Test script for running python commands within MantidPlot.
This will test the interface to SliceViewer widgets.
Note: the SliceViewerPythonInterfaceTest.py offers
more tests of specific functions. Might be merged together with
this in the future?
"""
import sys
import sys
import os
import unittest
import time
from PyQt4 import Qt


CreateMDWorkspace(Dimensions='3',Extents='0,10,0,10,0,10',Names='x,y,z',Units='m,m,m',SplitInto='5',MaxRecursionDepth='20',OutputWorkspace='mdw')
FakeMDEventData("mdw", UniformParams="1e5")
FakeMDEventData("mdw", PeakParams="1e4, 2,4,6, 1.5")
BinToMDHistoWorkspace("mdw", "uniform", 1, "x,0,10,30", "y,0,10,30", "z,0,10,30", IterateEvents="1", Parallel="0")

class MantidPlotSliceViewerTest(unittest.TestCase):

def setUp(self):
pass

def tearDown(self):
closeAllSliceViewers()
pass

def test_plotSlice(self):
""" Basic plotSlice() usage """
svw = plotSlice('uniform')
self.assertEqual(svw.getSlicer().getWorkspaceName(), "uniform")

def test_closingWindowIsSafe(self):
svw = plotSlice('uniform', label='closing!')
svw.close()

def test_methods_pass_through(self):
""" Methods called on SliceViewerWindow pass-through to the SliceViewer widget"""
svw = plotSlice('uniform')
svw.setSlicePoint(0, 2.5)
self.assertAlmostEqual(svw.getSlicePoint(0), 2.5, 3)
svw.setXYDim("z", "x")
self.assertEqual(svw.getDimX(), 2)
self.assertEqual(svw.getDimY(), 0)

def test_plotSlice_arguments(self):
""" Pass arguments to plotSlice """
svw = plotSlice('uniform', label='test_label', xydim=[1,2],
slicepoint=[2.5, 0, 0], colormin=20, colormax=5000, colorscalelog=True,
limits=[2, 8, 3, 9])
self.assertEqual(svw.getLabel(), "test_label")
self.assertEqual(svw.getDimX(), 1)
self.assertEqual(svw.getDimY(), 2)
self.assertAlmostEqual(svw.getSlicePoint(0), 2.5, 3)
self.assertAlmostEqual(svw.getColorScaleMin(), 20, 2)
self.assertAlmostEqual(svw.getColorScaleMax(), 5000, 2)
assert svw.getColorScaleLog()
self.assertEqual(svw.getXLimits(), [2,8])
self.assertEqual(svw.getYLimits(), [3,9])

def test_plotSlice_arguments2(self):
""" Another way to pass xydim """
svw = plotSlice('uniform', xydim=["y", "z"])
self.assertEqual(svw.getDimX(), 1)
self.assertEqual(svw.getDimY(), 2)

def test_getSliceViewer(self):
""" Retrieving an open SliceViewer """
svw1 = plotSlice('uniform')
svw2 = getSliceViewer('uniform')
assert svw2 is not None
self.assertEqual(svw2.getSlicer().getWorkspaceName(), "uniform")

def test_getSliceViewer_failure(self):
""" Retrieving an open SliceViewer, cases where it fails """
self.assertRaises(Exception, getSliceViewer, 'nonexistent')
svw = plotSlice('uniform', label='alabel')
self.assertRaises(Exception, getSliceViewer, 'uniform', 'different_label')


# Custom code to create and run this single test suite
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite(MantidPlotSliceViewerTest) )
unittest.TextTestRunner().run(suite)
3 changes: 3 additions & 0 deletions Code/Mantid/MantidQt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Files generated by process_sip.sh
mantidqt_dir.txt
mantidqt_wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
class SliceViewerPythonInterfaceTest(unittest.TestCase):
"""Test for accessing SliceViewer widgets from MantidPlot
python interpreter"""

@classmethod
def setUpClass(cls):
# Needs python 2.7+ it seems :(
pass

@classmethod
def tearDownClass(cls):
pass

def setUp(self):
""" Set up and create a SliceViewer widget """
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/MantidQt/process_sip.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
# Perform processing of the .sip file to add docstrings
echo ""
python ../Build/doxygen_to_sip.py -i mantidqt.in.sip -o mantidqt.sip
python ../Build/doxygen_to_sip.py -i mantidqt.in.sip -o mantidqt.sip -d mantidqt_dir.txt
echo ""
python ../Build/sip_strip_docstring.py -i mantidqt.sip -o mantidqt.rhel5.sip -w mantidqt_wiki.txt
echo ""
Expand Down

0 comments on commit 2e2e44d

Please sign in to comment.