Skip to content

Commit

Permalink
brought up test coverage with new setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nucccc committed Jun 10, 2024
1 parent dd5015b commit d0a466d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion markarth/convert/cythonize/_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand Down
6 changes: 4 additions & 2 deletions markarth/convert/cythonize/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
35 changes: 25 additions & 10 deletions tests/test_cythonize_pure.py → tests/test_cythonify_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''import pytest
import pytest

import ast

Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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 = {
Expand Down Expand Up @@ -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()
)
)

Expand All @@ -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}
)
)
Expand All @@ -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)'


Expand Down Expand Up @@ -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
Expand All @@ -203,4 +218,4 @@ def test_sort_funcs_by_line(mod3):


def test_pure(mod3):
pass'''
pass
20 changes: 20 additions & 0 deletions tests/test_pure_typs_conv.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit d0a466d

Please sign in to comment.