Example #1
0
def test_contains():
    l = [1, 2, 3]
    L = MName('l')
    d = MContains(X, L)
    print(type(X), type(L))
    print(type(d))
    dv = d.evaluate({'l': l})
    assert isinstance(dv, bool)
    assert not dv
    l.append(X.value)
    dv = d.evaluate({'l': l})
    assert isinstance(dv, bool)
    assert dv
    assert d.unparse() == "10 in l"
import collections

import pytest

from zirkon.toolbox.macro import MName

py_globals_d = {
    't': 10,
    'x': 4,
    'y': 5,
    'z': 6,
    'w': 7,
}

t = MName(10)
x = MName(4)
y = MName(5)
z = MName(6)
w = MName(7)

de_globals_d = {k: MName(k) for k in py_globals_d}

_data = [
    "x + z // y",
    "x * y + z * w",
    "x * (y + z) * w",
    "x * y * z * w",
    "x * (y * z) * w",
    "x * y // z * w",
    "x * (y // z) * w",
Example #3
0
Param = collections.namedtuple('Param', ('de', 'expression', 'globals_d'))


def myfun1(a, b, c):
    return a + b // c


def myfun2(a, b, c):
    return a - b + c


y_20 = 20
y_10 = 10

MYFUN = MName("myfun")
X = MConst(10)
Y = MName('y')
Z = MConst(4)

Param = collections.namedtuple('Param', ('globals_d', 'expression'))
_data = [
    Param(globals_d={
        'myfun': myfun1,
        'y': y_20
    },
          expression="myfun(10, c=4, b=y)"),
    Param(globals_d={
        'myfun': myfun2,
        'y': y_20
    },
Example #4
0
# -*- coding: utf-8 -*-

import collections

import pytest

from zirkon.toolbox.macro import \
    Macro, MName

Param = collections.namedtuple('Param', ('de', 'expression', 'globals_d'))

_data = [
    Param(de=3 * MName('x') - 5,
           expression="3 * x - 5",
           globals_d={'x': 5}),
    Param(de=3 * (MName('x') - 5),
           expression="3 * (x - 5)",
           globals_d={'x': 4}),
    Param(de=MName('t') + 3 * (MName('x') *(MName('z')- MName('y'))//MName('w')),
           expression="t + 3 * (x * (z - y) // w)",
           globals_d={'t': 10, 'x': 4, 'y': 5, 'z': 6, 'w': 7}),
]

@pytest.fixture(params=_data, ids=tuple(enumerate(_data)))
def param(request):
    return request.param

def test_MName(param):
    de = param.de
    assert isinstance(de, Macro)
    expression = param.expression
def test_MName_no_globals():
    m = MName('a')
    with pytest.raises(NameError):
        print(m.evaluate())
def test_MName_init_globals_globals():
    m = MName('a', globals_d={'a': 20})
    assert m.evaluate(globals_d={'a': 3}) == 20
def test_MName_globals():
    m = MName('a')
    assert m.evaluate(globals_d={'a': 10}) == 10