Commit 273df977 authored by Yori Fournier's avatar Yori Fournier
Browse files

Merge branch 'add-tests' into 'dev'

Add tests

See merge request !13
parents fd45731f b8a434f4
......@@ -114,11 +114,30 @@ else:
print(SEVR + "The backend you choosed is not supported interactive mode not available")
# myTool.*: interface functions to use myplotlib interactively
from .myTool import window_exists, getCurrentWindowIDs
from .myTool import print2file, print2screen, printListCurrentWindows
from .myTool import getWindow, getFigOnWindow, drawFigOnWindow, giveDataToWindow
from .myTool import closeWindow, closeAllWindows
from .test import myTest
from .test import test400, test300, test301, test302, test303, test304, test305, test306, test401, test402, test403
testList = [test400, test300, test301, test302, test303, test304, test305, test306, test401, test402, test403]
# import the tests
from .test import test100
from .test import test200, test201, test202
from .test import test300, test301, test302, test303, test304, test305
from .test import test306, test307, test308, test309, test310
from .test import test400, test401, test402, test403
from .test import test500, test501, test502, test503, test504
from .test import test505, test506, test507, test508
from .test import test600
# the tests to run
testList = [test100,
test200, test201, test202,
test300, test301, test302, test303, test304, test305,
test306, test307, test308, test309, test310,
test400, test401, test402,
test503, test506, test500, test507, test501, test502,
test504, test505, test508,
test600,
]
......@@ -33,6 +33,7 @@
# IMPORT ---------------------------------------------------------------
from .__init__ import INFO, SPCE, DBUG
from .__init__ import myTest, testList
from .__init__ import __dict__
import sys
import getopt
......@@ -67,9 +68,21 @@ for opt, arg in opts:
print(usage)
sys.exit(0)
elif opt in ("-t", "--test"):
elif opt in ("-t"):
run4Test = True
elif opt in ("--test"):
run4Test = True
tests = arg.split(', ')
testList = []
for test in tests:
if test in __dict__.keys():
testList.append(__dict__[test])
if len(testList) == 0:
print(" > sevr-: you need to give valid tests to be tested.")
exit(1)
elif opt in ("-p", "--plot"):
run4Plot = True
arg4Plot = str(arg)
......
......@@ -316,6 +316,7 @@ class MyFig(MyFig_base):
if((self.reformat) or (not self.formatted)):
status = self.formatRawData()
if(not status): return(False)
if(self.debug):
print(DBUG + "currently plotting the axes...")
......
......@@ -62,7 +62,7 @@ def setCurrentData(name):
def print2screen(ClassName, inputArg, *args, **kwargs):
try:
fig = ClassName(inputarg=inputArg, *args, **kwargs)
fig = ClassName(inputArg, *args, **kwargs)
except:
print(SEVR + 'The type of figure you gave does not exists.')
return(False)
......@@ -76,13 +76,37 @@ def print2screen(ClassName, inputArg, *args, **kwargs):
return(True)
def printListCurrentWindows():
def window_exists(ID):
if ID in getCurrentWindowIDs():
status = True
else:
status = False
return(status)
def getCurrentWindowIDs():
IDs = []
for window in _G_WINDOWS:
if window == _G_WINDOWS[0]:
print(INFO + "Figure " + window.num)
IDs.append(window.num)
return(IDs)
def printListCurrentWindows():
windowIDs = getCurrentWindowIDs()
for windowID in windowIDs:
if windowID == _G_WINDOWS[0].num:
print(INFO + "Figure " + str(windowID))
else:
print(SPCE + "Figure " + window.num)
print(SPCE + "Figure " + str(windowID))
return(True)
def getWindow(ID):
......@@ -107,13 +131,20 @@ def getFigOnWindow(ID):
def drawFigOnWindow(fig, ID):
return(MyWin(fig, fignum=ID))
if window_exists(ID):
win = getWindow(ID)
win.set_figure(fig)
win.refresh()
else:
win = MyWin(fig, fignum=ID)
return(win)
def giveDataToWindow(data, ID):
fig = getFigOnWindow(ID)
fig.update(inputarg = data)
fig.update(rawdata = data)
win = getWindow(ID)
win.refresh()
......@@ -146,7 +177,7 @@ def closeAllWindows():
def print2file(ClassName, inputArg, filename, *args, **kwargs):
try:
fig = ClassName(inputarg=inputArg, *args, **kwargs)
fig = ClassName(inputArg, *args, **kwargs)
except:
print(SEVR + 'The type of figure you gave does not exists.')
return(False)
......
......@@ -104,7 +104,7 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
# bounded to another window
if fig.boundedToWin:
# if it is the case close the bounded window.
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# Call FigureManagerGTK class
......@@ -116,7 +116,7 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
self.canvas.figure.fignum = num
# add the window in the global variable _G_WINDOWS
# this guarenty that you never loose a window
......@@ -136,14 +136,14 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = self.num
self.canvas.figure.fignum = self.num
# UNBOUND ----------------------------------------------------------
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.keywords['fignum'] = -1
self.canvas.figure.fignum = -1
# SETTERS ----------------------------------------------------------
......@@ -156,7 +156,7 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
......@@ -183,7 +183,7 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
def drawFigure(self, fig):
# if the figure is not the current one
if(fig.keywords['fignum'] != self.num):
if(fig.fignum != self.num):
# cleanly set the new figure
self.set_figure(fig)
......
......@@ -113,7 +113,7 @@ class MyWin_TkAgg(FigureManagerTkAgg):
# bounded to another window
if fig.boundedToWin:
# if it is the case close the bounded window.
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# Call FigureManagerTkAgg class
......@@ -133,7 +133,7 @@ class MyWin_TkAgg(FigureManagerTkAgg):
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
self.canvas.figure.fignum = num
# add the window in the global variable _G_WINDOWS
# this guarenty that you never loose a window
......@@ -152,14 +152,14 @@ class MyWin_TkAgg(FigureManagerTkAgg):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = self.num
self.canvas.figure.fignum = self.num
# UNBOUND ----------------------------------------------------------
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.keywords['fignum'] = -1
self.canvas.figure.fignum = -1
# SETTERS ----------------------------------------------------------
......@@ -172,7 +172,7 @@ class MyWin_TkAgg(FigureManagerTkAgg):
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
......@@ -199,7 +199,7 @@ class MyWin_TkAgg(FigureManagerTkAgg):
def drawFigure(self, fig):
# if the figure is not the current one
if(fig.keywords['fignum'] != self.num):
if(fig.fignum != self.num):
# cleanly set the new figure
self.set_figure(fig)
......
......@@ -107,7 +107,7 @@ class MyWin_WXAgg(FigureManagerWx):
# bounded to another window
if fig.boundedToWin:
# if it is the case close the bounded window.
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# Call the ugly FigureManagerWxAgg class
......@@ -129,7 +129,7 @@ class MyWin_WXAgg(FigureManagerWx):
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
self.canvas.figure.fignum = num
# add the window in the global variable _G_WINDOWS
# this guarenty that you never loose a window
......@@ -149,14 +149,14 @@ class MyWin_WXAgg(FigureManagerWx):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = self.num
self.canvas.figure.fignum = self.num
# UNBOUND ----------------------------------------------------------
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.keywords['fignum'] = -1
self.canvas.figure.fignum = -1
# SETTERS ----------------------------------------------------------
......@@ -169,7 +169,7 @@ class MyWin_WXAgg(FigureManagerWx):
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
......@@ -198,7 +198,7 @@ class MyWin_WXAgg(FigureManagerWx):
def drawFigure(self, fig):
# if the figure is not the current one
if(fig.keywords['fignum'] != self.num):
if(fig.fignum != self.num):
# cleanly set the new figure
self.set_figure(fig)
......
from myIOs import readStupidData, readStupidData2
from axTest1 import AxTest1
from figTest import FigTest1, FigTest2
from figTest import FigTest2
from tests import myTest
from tests import test400, test300, test301, test302, test303, test304, test305, test306, test401, test402, test403
# import the tests
from tests import test100
from tests import test200, test201, test202
from tests import test300, test301, test302, test303, test304, test305
from tests import test306, test307, test308, test309, test310
from tests import test400, test401, test402, test403
from tests import test500, test501, test502, test503, test504
from tests import test505, test506, test507, test508
from tests import test600
......@@ -75,28 +75,6 @@ D_XRANGE = [-1, 1]
D_YRANGE = [-2, 2]
# Class MyFig Overwriting Matplotlib.figure.Figure
class FigTest1(MyFig_base):
# Set the size of the Figure in inch
# (private variable can not be updated)
FIGSIZE = (8., 6.)
# CONSTRUCTOR --------------------------------------------------------
def __init__(self, rawdata, *args, **kwargs):
# parent constructor
MyFig.__init__(self, rawdata, *args, **kwargs)
self.keywords = {'xRange': D_XRANGE,
'yRange': D_YRANGE}
ratio = 6. / 8. # height/width of the axes (in inch)
frame = [0.0, 0.0, 1.0, 1.0] # part of the fig that is available
self.add_axes(AxTest1(self, ratio, frame, *args, **kwargs))
# Class MyFig Overwriting Matplotlib.figure.Figure
class FigTest2(MyFig):
......
from .. import os
from .. import D_OFORMAT, D_OPATH
from .. import MyWin
from .. import rcParams
from .. import MyData, MyAxes, MyFig, MyWin
from .. import SEVR, WARN, DBUG, SPCE
from .. import _G_WINDOWS
from .. import G_RAWDATAS, _G_WINDOWS
from . import readStupidData, readStupidData2
from . import FigTest2
from . import FigTest2, AxTest1
from .. import window_exists, getCurrentWindowIDs
from .. import getFigOnWindow, getWindow
from .. import closeWindow, closeAllWindows
from .. import print2screen, print2file, printListCurrentWindows
from .. import drawFigOnWindow, giveDataToWindow
# TESTER
def myTest(function, expected, failed, debug):
......@@ -32,6 +40,7 @@ def myTest(function, expected, failed, debug):
return(True)
# TEST 1.* for testing MyData
# TEST 2.* for testing MyAxes
# TEST 3.* for testing MyFig
......@@ -39,30 +48,168 @@ def myTest(function, expected, failed, debug):
# TEST 5.* for testing myTool
# TEST 4.00: test create of MyFig and PRINT TO SCREEN (EXPECTED: TRUE)
def test400(debug):
# TEST 1.00: test the Data Structure (EXPECTED: TRUE)
def test100(debug):
status = True
data = MyData()
data1 = readStupidData('data1')
fig = FigTest2((data1,))
if data.name == 'default':
status = True
else:
status = False
win = MyWin(fig)
if data.data is None:
status = True
else:
status = False
try:
if win.canvas.figure.debug:
pass
return(status)
# TEST 2.00: test MyAxes Constructor
#
# REMARK: Require to test MyFig First
#
def test200(debug):
# set some global variable for the test
rcParams['font.size'] = 22
MyFig.FIGSIZE = (8., 6.)
# create stupid data
data = MyData()
data.data = [[0, 0], [1, 1]] # Dummy data
# create a figure
fig = FigTest2((data,), dpi=75.)
# set ratio and frameRect
ratio = 4. / 6. # float
frameRect = [0, 0, 0.5, 1.0] # [x0, y0, x1, y1]
# create the MyAxes object.
ax = MyAxes(fig, ratio, frameRect)
# Some debug info if needed
if(debug):
# supposed values
# figWidth = 8.
# figHeight = 6.
# figDpi = 75.
print
print(DBUG + "figWidth = " + str(fig.get_figwidth()) + " should be 8.")
print(SPCE + "figHeight = " + str(fig.get_figheight()) + " should be 6.")
print(SPCE + "figDpi = " + str(fig.get_dpi()) + " should be 75.")
print(SPCE + "ratio = " + str(ratio) + " should be 4. / 6. = 0.6666...")
print
# fontsize = 22 / 75
print(DBUG + "fontsize = " + str(rcParams['font.size'] / 75.) + " should be 22. / 75. = 0.2933..")
print
# maxWidth = 8. * 0.5 - 8. * 22 / 75 = 124 / 75
# maxHeight = 6. * 1.0 - 4 * 22 / 75 = 362 / 75
print(DBUG + "maxHeight = " + str(fig.get_figheight() * frameRect[3] - 4. * rcParams['font.size'] / fig.get_dpi()) + " should be 362 / 75 = 4.82666...")
print(SPCE + "maxWidth = " + str(fig.get_figwidth() * frameRect[2] - 8. * rcParams['font.size'] / fig.get_dpi()) + " should be 124 / 75 = 1.65333...")
print
# hypoWidth = 362 / 75 / (4. / 6.) = 7.24
# hypoHeight = 124 / 75 * (4. / 6.) = 496 / 450
print(DBUG + "hypoWidth = " + str((fig.get_figheight() * frameRect[3] - 4. * rcParams['font.size'] / fig.get_dpi()) / ratio) + " should be 7.24")
print(SPCE + "hypoHeight = " + str((fig.get_figwidth() * frameRect[2] - 8. * rcParams['font.size'] / fig.get_dpi()) * ratio) + " should be 496 /450 = 1.10222...")
print
# hypoWidth > maxWidth --> Fit Height
print(DBUG + "hypoWidth should be > maxWidth.")
print
# height = 496 / 450 / 6. = 0.183...
# width = 124 / 75 / 8. = 0.62 / 3. = 0.206...
# posX = 4 * 22 / 75 / 8 = 11 / 75 = 0.1466...
# posY = 3 * 22 / 75 / 6 = 11 / 75 = 0.1466...
hypoHeight = (fig.get_figwidth() * frameRect[2] - 8. * rcParams['font.size'] / fig.get_dpi()) * ratio
maxWidth = fig.get_figwidth() * frameRect[2] - 8. * rcParams['font.size'] / fig.get_dpi()
print(DBUG + "height = " + str(hypoHeight / fig.get_figheight()) + " should be " + str(496. / 450. / 6.))
print(SPCE + "width = " + str(maxWidth / fig.get_figwidth()) + " should be 0.2066...")
print
# the expected value for rect
rect = [11. / 75., 11. / 75., 0.62 / 3., 496. / 450. / 6.]
# the value of rect
axRect = [ax._position.x0, ax._position.y0, ax._position.width, ax._position.height]
# debug infos
if(debug):
print(DBUG + "ax._position = \n" +
SPCE + " [ " + str(axRect[0]) + ", \n" +
SPCE + " " + str(axRect[1]) + ", \n" +
SPCE + " " + str(axRect[2]) + ", \n" +
SPCE + " " + str(axRect[3]) + " ] \n")
print(SPCE + "should be equal to: \n" +
SPCE + " [ " + str(rect[0]) + ", \n" +
SPCE + " " + str(rect[1]) + ", \n" +
SPCE + " " + str(rect[2]) + ", \n" +
SPCE + " " + str(rect[3]) + " ] \n")
# if rect has expected value
if axRect == rect:
status = True
except:
print(SEVR + "I can not access the figure from the manager...")
else:
status = False
win.close()
del win
del fig
del data1
return(status)
# TEST 2.01: Testing the function formatRawData
def test201(debug):
# add some empty data in G_RAWDATAS
G_RAWDATAS.update({'data': MyData()})
# create the environment
fig = FigTest2((G_RAWDATAS['data'],))
# create the axis
ax = AxTest1(fig, 6. / 8., [0, 0, 1, 1])
# Set the data (emulate some myIO function)
G_RAWDATAS['data'].data = [-1, 1, 1, -1]
# formatRawData
ax.formatRawData(G_RAWDATAS['data'])
# test if results compatible with inputs.
if(ax.data['xdata'] == [-1, 1] and ax.data['ydata'] == [1, -1]):
status = True
else:
status = False
return(status)
# TEST 2.02: Testing the function plotting
def test202(debug):
# create the environment
G_RAWDATAS.update({'data': MyData()})
fig = FigTest2((G_RAWDATAS['data'],))
# create the axis
ax = AxTest1(fig, 6. / 8., [0, 0, 1, 1])
# set the data (simulate some formatRawData function)
ax.data = {'xdata': [-1, 1], 'ydata': [1, -1]}
# plotting
ax.plotting()
# debug
if(debug):
print(DBUG + "the test on _x return: " + str((ax.lines[0]._x == [-1, 1]).all()))
print(SPCE + "the test on _y return: " + str((ax.lines[0]._y == [1, -1]).all()))
print(SPCE + "the -AND test returns: " + str((ax.lines[0]._x == [-1, 1]).all() and (ax.lines[0]._y == [1, -1]).all()))
# and test
if((ax.lines[0]._x == [-1, 1]).all() and (ax.lines[0]._y == [1, -1]).all()):
status = True
else:
status = False
return(status)
......@@ -98,6 +245,10 @@ def test301(debug):
data1 = readStupidData('data1')
fig = FigTest2((data1,))
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test301.png")
fig.print2file('test301.png', opath='myplotlib/img/') # create img/test301.png
if not os.path.exists("./myplotlib/img/test301.png"):
......@@ -207,7 +358,7 @@ def test305(debug):
return(status)
# TEST 306: Test MyFig::print2file (EXPECTED: True)
# TEST 3.06: Test MyFig::print2file (EXPECTED: True)
def test306(debug):
if os.path.exists("./myplotlib/img/test306.png"):
......@@ -233,6 +384,134 @@ def test306(debug):
return(status)
# TEST 3.07: test MyFig::printDebug
def test307(debug):
fig = MyFig(())
fig.printDebug()
return(True)
# TEST 3.08: test MyFig::plot
def test308(debug):
fig = FigTest2(('Non-Existing-Data',))
if(debug):
print(DBUG + "test without data: ")
status = fig.plot()
if(not status):
status = True
else:
status = False