Skip to content

Commit

Permalink
Add proper Class test and manage new Class case.
Browse files Browse the repository at this point in the history
  • Loading branch information
pberger514 committed Aug 13, 2022
1 parent 86f36ae commit 16e9d8b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 50 deletions.
9 changes: 9 additions & 0 deletions matlabparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,22 @@ def parse_fclose(l):
if len(pylc.split("@")) == 2:
# Should be a static method
method_name, rest = pylc.split("@")
# Might have a return value though
if method_name.find("=") >= 0:
lhs, method_name = method_name.split("=")
lhs = lhs.strip(" ")
method_name = method_name.strip(" ")
else:
lhs = None
def split_first(string, token):
split = string.split(token)
if len(split) > 2:
split = [split[0], string[len(split[0])+1:-1]]
return split
class_name, params = split_first(rest, "(")
pylc = class_name + "." + method_name + "(" + params
if lhs is not None:
pylc = lhs + " = " + pylc

else:
raise Exception("Can't parse '@' statement: " + lc[0])
Expand Down
17 changes: 17 additions & 0 deletions tests/files/ChildClass.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
classdef ChildClass < TestClass
properties
child_value;
end
methods
function this = ChildClass(varargin)
this@TestClass(varargin);
this.child_value = "Child";
end
function value = parse(this, input)
value = this.child_value + input;
end
function value = read(this, value)
value = read@TestClass(this, @this.parse(value));
end
end
end
4 changes: 2 additions & 2 deletions tests/files/test_class1.m → tests/files/TestClass.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
classdef MyClass < handle;
classdef TestClass < handle;
% Documentation:
properties
% Public
Expand All @@ -8,7 +8,7 @@
prop_priv=-1;
end
methods
function o=MyClass(varargin)
function o=TestClass(varargin)
o.prop_pub= 3*pi+2
end
function read(o,value)
Expand Down
1 change: 1 addition & 0 deletions tests/files/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 0 additions & 16 deletions tests/files/test_class2.m

This file was deleted.

101 changes: 69 additions & 32 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,81 @@ def convert(mfile, prefix='_'):
class TestMatlab2Python(unittest.TestCase):

def test_runfile(self):
pyFilename = convert('tests/files/run_all.m')
remove(pyFilename)
test_file = 'tests/files/run_all.m'
with FileTestContextManager(temp_files=[tf.replace(".m", ".py") for tf in [test_file,]]):
convert(test_file)

def test_spectrum(self):
# Convert to python
pyFilename = convert('tests/files/fSpectrum.m')

# Open the file generated
from tests.files._fSpectrum import fSpectrum
dt = 0.1
t = np.arange(0,1+dt,dt)
y = np.sin(t)
# TODO does not work yet
#S,f = fSpectrum(y,len(y),1 / dt)
S_ref = np.array([0.2285364,0.0258482,0.0066528,0.0033719,0.0023203,0.0019575])
f_ref = np.array([0.0,0.90909,1.81818,2.72727,3.63636,4.54545])
#print(S-S_ref)
#print(f-f_ref)

# Clean up
remove(pyFilename)
test_file = 'tests/files/fSpectrum.m'
with FileTestContextManager(temp_files=[tf.replace(".m", ".py") for tf in [test_file,]]):
# Convert to python
convert(test_file)

# Open the file generated
from tests.files._fSpectrum import fSpectrum
dt = 0.1
t = np.arange(0,1+dt,dt)
y = np.sin(t)
# TODO does not work yet
#S,f = fSpectrum(y,len(y),1 / dt)
S_ref = np.array([0.2285364,0.0258482,0.0066528,0.0033719,0.0023203,0.0019575])
f_ref = np.array([0.0,0.90909,1.81818,2.72727,3.63636,4.54545])
#print(S-S_ref)
#print(f-f_ref)

def test_class(self):
# Convert to python
pyFilename = convert('tests/files/test_class1.m')

# Open the file generated and test the class
from tests.files._test_class1 import MyClass
with FileTestContextManager(temp_files=["TestClass.py", "ChildClass.py"],
test_file_dir="./tests/files/"):

# Convert base class to python
pyFilename_parent = convert('TestClass.m', prefix="")

# Open the file generated and test the class
from TestClass import TestClass

obj = TestClass()
self.assertEqual(obj.prop_priv, -1)

obj.read(value=12)
self.assertEqual(obj.prop_pub, 12)
self.assertEqual(obj.prop_priv, 3)

# Convert to child class to python
pyFilename_child = convert('ChildClass.m', prefix="")

# Test it
from ChildClass import ChildClass

child_obj = ChildClass()

self.assertEqual(child_obj.parse("Class"), "ChildClass")

child_obj.read("Class")
self.assertEqual(child_obj.prop_pub, "ChildClass")


class FileTestContextManager:
def __init__(self, temp_files=[], test_file_dir=None):
self.__project_dir = os.getcwd()
self.__test_file_dir=test_file_dir
self.__temp_files=temp_files

def __enter__(self):
if self.__test_file_dir is not None:
os.chdir(self.__test_file_dir)

sys.path.append(
os.path.join(self.__project_dir,
self.__test_file_dir)
)

def __exit__(self, exc_type, exc_value, exc_tb):
for tfile in self.__temp_files:
remove(tfile)

os.chdir(self.__project_dir)

obj = MyClass()
self.assertEqual(obj.prop_priv, -1)

obj.read(value=12)
self.assertEqual(obj.prop_pub, 12)
self.assertEqual(obj.prop_priv, 3)

# Clean up
remove(pyFilename)

if __name__ == '__main__':
unittest.main()

0 comments on commit 16e9d8b

Please sign in to comment.