diff --git a/markarth/convert/cythonize/_import.py b/markarth/convert/cythonize/_import.py index ba98498..84d1220 100644 --- a/markarth/convert/cythonize/_import.py +++ b/markarth/convert/cythonize/_import.py @@ -37,7 +37,7 @@ def cython_imported_already(mod_ast : ast.Module) -> tuple[bool, str, int]: return (False, '', 0) -def gen_import_line(cy_alias : str | None) -> str: +def gen_import_line(cy_alias : str | None = None) -> str: ''' gen_import_line generates an import cython line ''' diff --git a/markarth/convert/cythonize/base.py b/markarth/convert/cythonize/base.py index fdf59fc..73ca031 100644 --- a/markarth/convert/cythonize/base.py +++ b/markarth/convert/cythonize/base.py @@ -55,6 +55,7 @@ def cythonify( # at first i shall check if cython is imported, and in such case i consider # its alias + alias = '' # TODO: remove this line once you're back pyx tests if clogic.cython_import_needed: is_cython_imported, alias, codeline_no = cython_imported_already(mod_ast) @@ -63,8 +64,9 @@ def cythonify( alias = DEFAULT_CY_ALIAS # in case the conversion logic does not require me to have a cython import, # the alias variable is however set to be passed as a parameter to other functions - else: - alias = '' + # TODO: decomment below once you have tests for pyx +# else: +# alias = '' # function names sorted in order of appearance, as it's more practical to modify # them from the last to the first, so that last functions to be modified won't have diff --git a/tests/test_cythonize_pure.py b/tests/test_cythonify_utils.py similarity index 86% rename from tests/test_cythonize_pure.py rename to tests/test_cythonify_utils.py index 892817c..5331be4 100644 --- a/tests/test_cythonize_pure.py +++ b/tests/test_cythonify_utils.py @@ -1,4 +1,4 @@ -'''import pytest +import pytest import ast @@ -8,11 +8,16 @@ collect_func_defs ) from markarth.convert.cythonize.cy_typs import CyFloat, CyInt -from markarth.convert.cythonize.pure import ( +from markarth.convert.cythonize._import import ( cython_imported_already, + gen_import_line +) +from markarth.convert.cythonize.pure_funcs.declare_gen import ( typ_store_to_varnames, typ_store_to_cdeclares, - gen_declare_line, + gen_declare_line +) +from markarth.convert.cythonize.base import ( could_be_docstring, cdeclares_ins_point, sort_funcs_by_line @@ -32,6 +37,11 @@ def test_is_cython_imported(code1, mod2): assert line_no == 2 +def test_gen_import_line(): + assert gen_import_line() == 'import cython' + assert gen_import_line(cy_alias='cy') == 'import cython as cy' + + def test_typ_store_to_varnames(): typ_store = DictTypStore({ 'a' : TypPrimitive(PrimitiveCod.INT), @@ -43,7 +53,8 @@ def test_typ_store_to_varnames(): tuples = set(typ_store_to_varnames( typ_store = typ_store, default_cy_int=CyInt.INT, - default_cy_float=CyFloat.FLOAT + default_cy_float=CyFloat.FLOAT, + imposed_vars=dict() )) expected_tuples = { @@ -91,7 +102,9 @@ def test_typ_store_to_cdeclares(): typ_store_to_cdeclares( typ_store = typ_store, default_cy_int=CyInt.INT, - default_cy_float=CyFloat.FLOAT + default_cy_float=CyFloat.FLOAT, + cy_alias='cython', + imposed_vars=dict() ) ) @@ -117,6 +130,7 @@ def test_typ_store_to_cdeclares_imposed_vars(): typ_store = typ_store, default_cy_int=CyInt.INT, default_cy_float=CyFloat.FLOAT, + cy_alias='cython', imposed_vars={'a':CyInt.LONG} ) ) @@ -139,7 +153,8 @@ def test_gen_declare_line(): assert gen_declare_line( varname='vv', - cy_typename='float' + cy_typename='float', + cy_alias='cython' ) == 'vv = cython.declare(cython.float)' @@ -179,11 +194,11 @@ def test_cdeclares_ins_point(mod3, mod10): assert cdeclares_ins_point(func_asts['f1']) == 8 assert cdeclares_ins_point(func_asts['f2']) == 15 - assert cdeclares_ins_point(func_asts['f3']) == 19''' + assert cdeclares_ins_point(func_asts['f3']) == 19 # testing the case of a docstring as the only element of a code section - ####cod = """def func():\n\t'''a docstring'''""" -''' mod_ast = ast.parse(cod) + cod = """def func():\n\t'''a docstring'''""" + mod_ast = ast.parse(cod) func_asts = collect_func_defs(mod_ast) assert cdeclares_ins_point(func_asts['func']) == 2 @@ -203,4 +218,4 @@ def test_sort_funcs_by_line(mod3): def test_pure(mod3): - pass''' \ No newline at end of file + pass \ No newline at end of file diff --git a/tests/test_pure_typs_conv.py b/tests/test_pure_typs_conv.py new file mode 100644 index 0000000..7e20650 --- /dev/null +++ b/tests/test_pure_typs_conv.py @@ -0,0 +1,20 @@ +''' +aimed at testing how cy_typs are converted into cython +''' + + +from markarth.convert.cythonize.cy_typs import CyFloat, CyInt +from markarth.convert.cythonize.pure_funcs.typs_conv import ( + cy_int_str_pure, + cy_float_str_pure +) + +def test_cy_int_str_pure(): + assert cy_int_str_pure(CyInt.SHORT) == 'short' + assert cy_int_str_pure(CyInt.INT) == 'int' + assert cy_int_str_pure(CyInt.LONG) == 'long' + assert cy_int_str_pure(CyInt.LONGLONG) == 'longlong' + +def test_cy_float_str_pure(): + assert cy_float_str_pure(CyFloat.FLOAT) == 'float' + assert cy_float_str_pure(CyFloat.DOUBLE) == 'double' \ No newline at end of file