Skip to content

Commit

Permalink
Update of tests, using test directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ebranlard committed Nov 19, 2021
1 parent 959aa25 commit 8b6ce2a
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 101 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ else
endif

all:
python $(MAIN) _tests/files/test_class1.m
python $(MAIN) tests/files/test_class1.m

install:
python setup.py install
Expand Down
Empty file removed _tests/files/test_Zall_ref.py
Empty file.
53 changes: 5 additions & 48 deletions matlabparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
import os
import unittest
import smop
from smop.main import parse_matlab_lines

Expand Down Expand Up @@ -152,53 +151,8 @@ def merge_lines(LC):
i+=1
return LC_new

class TestParser(unittest.TestCase):

def test_names(self):
#self.assertEqual(extract_function_name('function y = f(a,b)'),'f'))
self.assertEqual(parse_function_def('function y = f(a,b)'),('f',['y'],['a','b']))
self.assertEqual(parse_function_def('function f; '),('f',[],[]))
self.assertEqual(parse_function_def('function f ; '),('f',[],[]))
self.assertEqual(parse_function_def('function foo '),('foo',[],[]))
self.assertEqual(parse_function_def('function foo() '),('foo',[],[]))
self.assertEqual(parse_function_def('function foo ( ); '),('foo',[],[]))
self.assertEqual(parse_function_def('function [a b] = f(c)'),('f',['a','b'],['c']))
self.assertEqual(parse_function_def('function [a,b] = f(c)'),('f',['a','b'],['c']))
self.assertEqual(parse_class_def('classdef y<h') ,('y',['h']))
self.assertEqual(parse_class_def('classdef y < h ;'),('y',['h']))
self.assertEqual(parse_class_def('classdef y') ,('y',[]))
self.assertEqual(parse_class_def('classdef y;') ,('y',[]))
self.assertRaises(Exception,parse_class_def,'classdef')

def test_comment(self):
self.assertEqual(separate_comment(''),('',''))
self.assertEqual(separate_comment('%%'),('','%%'))
self.assertEqual(separate_comment(' %%'),(' ','%%'))
self.assertEqual(separate_comment('%hi'),('','%hi'))
self.assertEqual(separate_comment("""s='%' %com"""),("""s='%' """,'%com'))
self.assertEqual(separate_comment("""A=B' %com"""),("""A=B' """,'%com'))
self.assertEqual(separate_comment("""s='%';A=B' %com"""),("""s='%';A=B' """,'%com'))
self.assertEqual(separate_comment("""s='%';A=B' %com '%com2"""),("""s='%';A=B' ""","""%com '%com2"""))

def test_string(self):
self.assertEqual(find_strings("""""") ,([],False))
self.assertEqual(find_strings("""AAAA""") ,([],False))
self.assertEqual(find_strings("""'""") ,([(0,'')],True))
self.assertEqual(find_strings("""''""") ,([(0,'')],False))
self.assertEqual(find_strings(""" 'a'"""),([(3,'a')],False))
self.assertEqual(find_strings("""x='a' """),([(2,'a')],False))
self.assertEqual(find_strings("""x= 'a'"""),([(3,'a')],False))
self.assertEqual(find_strings(""" x=A' """),([],False))
self.assertEqual(find_strings(""" [0]' """),([],False))
self.assertEqual(find_strings(""" x(:)' """),([],False))
self.assertEqual(find_strings(""" {0}' """),([],False))
self.assertEqual(find_strings(""" 0.' """),([],False))
self.assertEqual(find_strings(""" A'' """),([],False))
self.assertEqual(find_strings(""" (A')' """),([],False))
self.assertEqual(find_strings(""" [0] ' """),([],False))
self.assertEqual(find_strings(""" [a 'bbb'] """),([(4,'bbb')],False))
self.assertEqual(find_strings("""f('it''s') """),([(2,"""it''s""")],False))
self.assertEqual(find_strings("""x='a';y='b'"""),([(2,'a'),(8,'b')],False))



class line():
def __init__(self):
Expand All @@ -210,6 +164,9 @@ def __init__(self,l):
# __all__ = ['parse']


# --------------------------------------------------------------------------------}
# --- Matlab File
# --------------------------------------------------------------------------------{
class MatlabFile:
def __init__(self,filename=None,lines=None):

Expand Down
40 changes: 0 additions & 40 deletions matlabparser/parsing_tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
import re


Expand Down Expand Up @@ -180,47 +179,8 @@ def extract_quotedstring(s):
return s_backup[1:i]


class TestParsingTools(unittest.TestCase):

def assertEqual(self, first, second, msg=None):
#print('\n>',first,'<',' >',second,'<')
super(TestParsingTools, self).assertEqual(first, second, msg)

def test_strings(self):
self.assertEqual(string_contains_charset('g' ,r'[a-z]'),True)
self.assertEqual(string_contains_charset('09' ,r'[a-z]'),False)
self.assertEqual(string_contains_charset('0g9',r'[a-z]'),True)
self.assertEqual(previous_nonspace_pos ('01 8',8),1 )
self.assertEqual(previous_nonspace_pos (' 8',8),-1)
self.assertEqual(previous_nonspace_char('01 8',8),'1')
self.assertEqual(previous_nonspace_char(' 8',8),'')

def test_quotes(self):
# self.assertEqual(is_in_quotes("""0 '345' 7 """ ,4) ,True)
# self.assertEqual(is_in_quotes("""01'345' 7 """ ,2) ,False)
#self.assertEqual(is_in_quotes("""01'345' 7 """ ,6) ,False)
self.assertEqual(replace_inquotes("""''""" ,'X') ,'XX')
self.assertEqual(replace_inquotes("""0'23'5""" ,'X') ,'0XXXX5')
self.assertEqual(replace_inquotes("""0'2"'5""" ,'X') ,'0XXXX5')
self.assertEqual(replace_inquotes("""0"23"5""" ,'X') ,'0XXXX5')
self.assertEqual(replace_inquotes("""0'2''5'7""" ,'X') ,'0XXXXXX7')
self.assertEqual(replace_inquotes("""0'23""" ,'X') ,'0XXX')
self.assertEqual(replace_inquotes("""0"23""" ,'X') ,'0XXX')
self.assertEqual(extract_quotedstring("""''""") ,'')
self.assertEqual(extract_quotedstring("""'a'""") ,'a')
self.assertEqual(extract_quotedstring("""'a'b""") ,'a')
self.assertEqual(extract_quotedstring("""'a""") ,'a')
self.assertEqual(extract_quotedstring("""'a''a'""") ,'a\'\'a')
self.assertEqual(extract_quotedstring("""'a"a'""") ,'a"a')
self.assertEqual(extract_quotedstring('""') ,'')
#print('>>>>>>>>>>>>>>')
#print('>>>>>>>>>>>>>>')
#print('>>>>>>>>>>>>>>')
#print('>>>>>>>>>>>>>>')
#self.assertEqual(separate_comment('s='i'),(' ',' '))

if __name__ == "__main__":
unittest.main()
print(is_in_quotes("""0'""2'""",4))
# (b,p1,p2)=is_surrounded_by(r'function [\'a b c\']=issur([,])',11,r'\'',r'\'')
# print(b,p1,p2)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions _tests/test_common.py → tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

from matlabparser import *
from matlabparser.parser import TestParser
from matlabparser.parsing_tools import TestParsingTools
from matlabparser.parser import matlablines2python

# --------------------------------------------------------------------------------}
Expand Down
20 changes: 10 additions & 10 deletions _tests/test_files.py → tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ def __init__(self,output=None):
self.output=output

def convert(mfile):
opts = options(mfile.replace('.m','.py'))
#print('>>>>>>')
#print('>>>>>> Converting:',mfile)
output = os.path.join(os.path.dirname(mfile),'_'+os.path.basename(mfile).replace('.m','.py'))
opts = options(output)
#print('>>>>>> Converting:',mfile, opts)
# opts.smop=True
mparser.matlab2python(mfile,opts)

def compare_to_ref(mfile):
opts = options(mfile.replace('.m','.py'))
ref_output =mfile.replace('.m','_ref.py')
#print('>>>>>> Converting:',mfile)
output = os.path.join(os.path.dirname(mfile),'_'+os.path.basename(mfile).replace('.m','.py'))
opts = options(output)
#print('>>>>>> Converting:',mfile, opts)
mparser.matlab2python(mfile,opts)
#print('>>>>>>')

class TestMatlab2Python(unittest.TestCase):


def test_spectrum(self):
convert('_tests/files/run_all.m')
compare_to_ref('_tests/files/fSpectrum.m')
convert('tests/files/run_all.m')
compare_to_ref('tests/files/fSpectrum.m')


# --------------------------------------------------------------------------------}
Expand All @@ -47,8 +47,8 @@ def test_spectrum(self):

class TestZAllConverted(unittest.TestCase):
def test_Zspectrum(self):
import _tests.files
from _tests.files.fSpectrum import fSpectrum
import tests.files
from tests.files._fSpectrum import fSpectrum
dt = 0.1
t = np.arange(0,1+dt,dt)
y = np.sin(t)
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
import unittest

from matlabparser.parser import *

# --------------------------------------------------------------------------------}
# ---
# --------------------------------------------------------------------------------{
class TestParser(unittest.TestCase):

def test_names(self):
#self.assertEqual(extract_function_name('function y = f(a,b)'),'f'))
self.assertEqual(parse_function_def('function y = f(a,b)'),('f',['y'],['a','b']))
self.assertEqual(parse_function_def('function f; '),('f',[],[]))
self.assertEqual(parse_function_def('function f ; '),('f',[],[]))
self.assertEqual(parse_function_def('function foo '),('foo',[],[]))
self.assertEqual(parse_function_def('function foo() '),('foo',[],[]))
self.assertEqual(parse_function_def('function foo ( ); '),('foo',[],[]))
self.assertEqual(parse_function_def('function [a b] = f(c)'),('f',['a','b'],['c']))
self.assertEqual(parse_function_def('function [a,b] = f(c)'),('f',['a','b'],['c']))
self.assertEqual(parse_class_def('classdef y<h') ,('y',['h']))
self.assertEqual(parse_class_def('classdef y < h ;'),('y',['h']))
self.assertEqual(parse_class_def('classdef y') ,('y',[]))
self.assertEqual(parse_class_def('classdef y;') ,('y',[]))
self.assertRaises(Exception,parse_class_def,'classdef')

def test_comment(self):
self.assertEqual(separate_comment(''),('',''))
self.assertEqual(separate_comment('%%'),('','%%'))
self.assertEqual(separate_comment(' %%'),(' ','%%'))
self.assertEqual(separate_comment('%hi'),('','%hi'))
self.assertEqual(separate_comment("""s='%' %com"""),("""s='%' """,'%com'))
self.assertEqual(separate_comment("""A=B' %com"""),("""A=B' """,'%com'))
self.assertEqual(separate_comment("""s='%';A=B' %com"""),("""s='%';A=B' """,'%com'))
self.assertEqual(separate_comment("""s='%';A=B' %com '%com2"""),("""s='%';A=B' ""","""%com '%com2"""))

def test_string(self):
self.assertEqual(find_strings("""""") ,([],False))
self.assertEqual(find_strings("""AAAA""") ,([],False))
self.assertEqual(find_strings("""'""") ,([(0,'')],True))
self.assertEqual(find_strings("""''""") ,([(0,'')],False))
self.assertEqual(find_strings(""" 'a'"""),([(3,'a')],False))
self.assertEqual(find_strings("""x='a' """),([(2,'a')],False))
self.assertEqual(find_strings("""x= 'a'"""),([(3,'a')],False))
self.assertEqual(find_strings(""" x=A' """),([],False))
self.assertEqual(find_strings(""" [0]' """),([],False))
self.assertEqual(find_strings(""" x(:)' """),([],False))
self.assertEqual(find_strings(""" {0}' """),([],False))
self.assertEqual(find_strings(""" 0.' """),([],False))
self.assertEqual(find_strings(""" A'' """),([],False))
self.assertEqual(find_strings(""" (A')' """),([],False))
self.assertEqual(find_strings(""" [0] ' """),([],False))
self.assertEqual(find_strings(""" [a 'bbb'] """),([(4,'bbb')],False))
self.assertEqual(find_strings("""f('it''s') """),([(2,"""it''s""")],False))
self.assertEqual(find_strings("""x='a';y='b'"""),([(2,'a'),(8,'b')],False))

if __name__ == '__main__':
unittest.main()
45 changes: 45 additions & 0 deletions tests/test_parsingtools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import numpy as np
import unittest

from matlabparser.parsing_tools import *
# --------------------------------------------------------------------------------}
# ---
# --------------------------------------------------------------------------------{
class TestParsingTools(unittest.TestCase):

def assertEqual(self, first, second, msg=None):
#print('\n>',first,'<',' >',second,'<')
super(TestParsingTools, self).assertEqual(first, second, msg)

def test_strings(self):
self.assertEqual(string_contains_charset('g' ,r'[a-z]'),True)
self.assertEqual(string_contains_charset('09' ,r'[a-z]'),False)
self.assertEqual(string_contains_charset('0g9',r'[a-z]'),True)
self.assertEqual(previous_nonspace_pos ('01 8',8),1 )
self.assertEqual(previous_nonspace_pos (' 8',8),-1)
self.assertEqual(previous_nonspace_char('01 8',8),'1')
self.assertEqual(previous_nonspace_char(' 8',8),'')

def test_quotes(self):
# self.assertEqual(is_in_quotes("""0 '345' 7 """ ,4) ,True)
# self.assertEqual(is_in_quotes("""01'345' 7 """ ,2) ,False)
#self.assertEqual(is_in_quotes("""01'345' 7 """ ,6) ,False)
self.assertEqual(replace_inquotes("""''""" ,'X') ,'XX')
self.assertEqual(replace_inquotes("""0'23'5""" ,'X') ,'0XXXX5')
self.assertEqual(replace_inquotes("""0'2"'5""" ,'X') ,'0XXXX5')
self.assertEqual(replace_inquotes("""0"23"5""" ,'X') ,'0XXXX5')
self.assertEqual(replace_inquotes("""0'2''5'7""" ,'X') ,'0XXXXXX7')
self.assertEqual(replace_inquotes("""0'23""" ,'X') ,'0XXX')
self.assertEqual(replace_inquotes("""0"23""" ,'X') ,'0XXX')
self.assertEqual(extract_quotedstring("""''""") ,'')
self.assertEqual(extract_quotedstring("""'a'""") ,'a')
self.assertEqual(extract_quotedstring("""'a'b""") ,'a')
self.assertEqual(extract_quotedstring("""'a""") ,'a')
self.assertEqual(extract_quotedstring("""'a''a'""") ,'a\'\'a')
self.assertEqual(extract_quotedstring("""'a"a'""") ,'a"a')
self.assertEqual(extract_quotedstring('""') ,'')
#print('>>>>>>>>>>>>>>')
#print('>>>>>>>>>>>>>>')
#print('>>>>>>>>>>>>>>')
#print('>>>>>>>>>>>>>>')
#self.assertEqual(separate_comment('s='i'),(' ',' '))

0 comments on commit 8b6ce2a

Please sign in to comment.