Beispiel #1
0
def test_runFunctionAndReturns():
    caller = FunctionCaller('testCallerName')

    def function():
        return 1

    caller.addFunction('testFunction', function)
    assert caller.runFunction('testFunction') == 1
Beispiel #2
0
def test_runFunctionException():
    caller = FunctionCaller('testCallerName')

    def function():
        a = 1 / 0

    caller.addFunction('testFunction', function)
    assert caller.runFunction('testFunction') == None
Beispiel #3
0
def test_removeFunction():
    caller = FunctionCaller('testCallerName')

    def empty():
        pass

    caller.addFunction('emptyFunctionName', empty)
    caller.removeFunction('emptyFunctionName')
    assert 'emptyFunctionName' not in caller.getFunctionDict()
Beispiel #4
0
def test_runFunctionNoArgs():
    caller = FunctionCaller('testCallerName')
    extvar = 0

    def function():
        nonlocal extvar
        extvar = 1

    caller.addFunction('testFunction', function)
    caller.runFunction('testFunction')
    assert extvar == 1
Beispiel #5
0
def test_runFunctionPositionalArgs():
    caller = FunctionCaller('testCallerName')
    extvar1 = 0
    extvar2 = 0
    extvar3 = 0

    def function(arg1, arg2, arg3):
        nonlocal extvar1, extvar2, extvar3
        extvar1 = arg1
        extvar2 = arg2
        extvar3 = arg3

    caller.addFunction('testFunction', function)
    caller.runFunction('testFunction', 1, 2, 3)
    assert extvar1 == 1 and extvar2 == 2 and extvar3 == 3
Beispiel #6
0
def test_runFunctionErrorProcessor():
    caller = FunctionCaller('testCallerName')
    extvar1 = 0
    extvar2 = 0
    extvar3 = 0

    def function(arg1, arg2):
        nonlocal extvar1, extvar2, extvar3
        extvar1 = arg1
        extvar2 = arg2
        assert False

    caller.addFunction('testFunction', function)
    res = caller.runFunction('testFunction', arg2=2, arg1=1, arg3=3)
    assert res == None
    assert extvar1 == 0 and extvar2 == 0 and extvar3 == 0
Beispiel #7
0
def test_runFunctionPositionalArgsTooManyArgs():
    caller = FunctionCaller('testCallerName')
    extvar1 = 0
    extvar2 = 0
    extvar3 = 0

    def function(arg1, arg2):
        nonlocal extvar1
        extvar1 = 1
        assert False
        return 1

    caller.addFunction('testFunction', function)
    res = caller.runFunction('testFunction', 1, 2, 3)
    assert res == None
    assert extvar1 == 0
Beispiel #8
0
def test_runFunctionExceptionProcessor():
    caller = FunctionCaller('testCallerName')

    def function():
        a = 1 / 0

    caller.addFunction('testFunction', function)
    extName = ''
    extException = ''

    def checkException(name, exc):
        nonlocal extName, extException
        extName = name
        extException = str(exc)

    caller.functionErrorProcessor = checkException
    caller.runFunction('testFunction')
    assert extName == 'testFunction' and extException == 'division by zero'
Beispiel #9
0
def test_addNonStrKeyFunction():
    caller = FunctionCaller('testCallerName')
    with pytest.raises(TypeError):
        caller.addFunction(1, 'str')
Beispiel #10
0
def test_addNonFunction():
    caller = FunctionCaller('testCallerName')
    with pytest.raises(TypeError):
        caller.addFunction('returnerFunctionName', 'str')
Beispiel #11
0
def test_addFunction():
    caller = FunctionCaller('testCallerName')
    caller.addFunction('returnerFunctionName', returner)
    assert 'returnerFunctionName' in caller.getFunctionDict()