Browse Source

Added ability to use class methods as variable depend function. fixed #54

master
Иванов Денис 3 months ago
parent
commit
76a49228f0
5 changed files with 45 additions and 16 deletions
  1. +10
    -9
      calculate/variables/datavars.py
  2. +6
    -5
      calculate/vars/main/__init__.py
  3. +6
    -0
      tests/variables/test_datavars.py
  4. +21
    -1
      tests/variables/testfiles/variables_7/main/__init__.py
  5. +2
    -1
      tests/vars/test_vars.py

+ 10
- 9
calculate/variables/datavars.py View File

@@ -506,15 +506,16 @@ class DependenceSource:
'''Метод для проверки того, возращает ли функция какое-либо значение, а
также того, совпадает ли число подписок с числом аргументов этой
функции.'''
if not isinstance(function_to_check, LambdaType):
# Если функция не лямбда, проверяем есть ли у нее возвращаемое
# значение.
for node in ast.walk(ast.parse(getsource(function_to_check))):
if isinstance(node, ast.Return):
break
else:
raise VariableError("the depend function does not return"
" anything in variable")
# if not isinstance(function_to_check, LambdaType):
# # Если функция не лямбда, проверяем есть ли у нее возвращаемое
# # значение.
# source_code = getsource(function_to_check)
# for node in ast.walk(ast.parse(source_code)):
# if isinstance(node, ast.Return):
# break
# else:
# raise VariableError("the depend function does not return"
# " anything in variable")

# Проверяем совпадение количества аргументов функции и заданных для
# функции переменных.


+ 6
- 5
calculate/vars/main/__init__.py View File

@@ -2,6 +2,7 @@ from calculate.variables.datavars import (
Variable,
StringType,
ListType,
Calculate
)
'''
main:
@@ -10,13 +11,13 @@ main:
'''


# class A:
# @classmethod
# def test(cls):
# return "test"
class A:
@classmethod
def test(cls):
return "test"


# Variable("test", type=StringType, source=Calculate(A.test))
Variable("test", type=StringType, source=Calculate(A.test))

Variable('cl_chroot_path', type=StringType.readonly, source='/')



+ 6
- 0
tests/variables/test_datavars.py View File

@@ -1658,6 +1658,12 @@ exit 0'''
result = template_engine.template_text
assert result == expected_output

def test_vars_with_class_method_as_depend_function(self):
datavars = Datavars(variables_path=os.path.join(TESTFILES_PATH,
'variables_7'))
assert datavars.main.test_0 == "test_0"
assert datavars.main.test_1 == "test_1"

def test_for_removing_testfiles(self):
shutil.rmtree(os.path.join(TESTFILES_PATH, 'gentoo'))
assert not os.path.exists(os.path.join(TESTFILES_PATH, 'gentoo'))


+ 21
- 1
tests/variables/testfiles/variables_7/main/__init__.py View File

@@ -1,4 +1,24 @@
from calculate.variables.datavars import Variable, StringType
from calculate.variables.datavars import Variable, StringType, Calculate


class A:
@classmethod
def test(cls):
return "test_0"


class B:
def __init__(self):
self._value = "test_1"

def test(self):
return self._value


b = B()


Variable("test_0", type=StringType, source=Calculate(A.test))
Variable("test_1", type=StringType, source=Calculate(b.test))

Variable('chroot', type=StringType.readonly, source='/')

+ 2
- 1
tests/vars/test_vars.py View File

@@ -144,7 +144,8 @@ def test_main_os_get_arch_gentoo(case):
ids=lambda x: x["name"])
@pytest.mark.calculate_vars
def test_install_os_get_available_audio_system(case):
with mock.patch('calculate.vars.install.os.func.PackageAtomParser.is_package_exists') as exists:
with mock.patch('calculate.vars.install.os.func.'
'PackageAtomParser.is_package_exists') as exists:
exists.return_value = case["exists"]
assert install_os.get_available_audio_system() == case["result"]



Loading…
Cancel
Save