Example #1
0
def test_select_check(typ):
    hint = typ.__name__.lower()
    with pytest.raises(TypeError, match=f"items are not a list of {hint}"):
        SELECTS[typ](None, Number(42))

    with pytest.raises(TypeError, match=f"index is not a static number"):
        SELECTS[typ](LISTS[typ](), Number())
Example #2
0
def test_cidr_check():
    with pytest.raises(TypeError, match="block is not a string"):
        CIDR(block=None, count=Number(), bits=Number())

    with pytest.raises(TypeError, match="count is not a number"):
        CIDR(block=String(), count=None, bits=Number())

    with pytest.raises(TypeError, match="bits is not a number"):
        CIDR(block=String(), count=Number(), bits=None)
Example #3
0
 def __init__(
         self,
         *,
         p1: int,
         p1d: int = Number(42),
         p2: float,
         p2d: float = Number(42.42),
         p3: str,
         p3d: str = String("foo"),
         p4: List[int],
         p4d: List[int] = LISTS[Number]([Number(42)]),
         p5: List[float],
         p5d: List[float] = LISTS[Number]([Number(42.42)]),
         p6: List[str],
         p6d: List[str] = LISTS[String]([String("foo")]),
 ):
     assert not isinstance(p1, Static)
     assert not isinstance(p1d, Static)
     assert not isinstance(p2, Static)
     assert not isinstance(p2d, Static)
     assert not isinstance(p3, Static)
     assert not isinstance(p3d, Static)
     assert not isinstance(p4, Static)
     assert not isinstance(p4[Number(0)], SELECTS[Number])
     assert not isinstance(p4[Number(0)], Static)
     assert not isinstance(p4d, Static)
     assert not isinstance(p4d[Number(0)], SELECTS[Number])
     assert not isinstance(p4d[Number(0)], Static)
     assert not isinstance(p5, Static)
     assert not isinstance(p5d, Static)
     assert not isinstance(p6, Static)
     assert not isinstance(p6d, Static)
Example #4
0
def test_external_paramvalue(mocker):
    from typing import List

    node = mocker.sentinel.NODE

    class Foo(ExternalResource):
        def __init__(
                self,
                *,
                p1: int,
                p1d: int = Number(42),
                p2: float,
                p2d: float = Number(42.42),
                p3: str,
                p3d: str = String("foo"),
                p4: List[int],
                p4d: List[int] = LISTS[Number]([Number(42)]),
                p5: List[float],
                p5d: List[float] = LISTS[Number]([Number(42.42)]),
                p6: List[str],
                p6d: List[str] = LISTS[String]([String("foo")]),
        ):
            assert not isinstance(p1, Static)
            assert not isinstance(p1d, Static)
            assert not isinstance(p2, Static)
            assert not isinstance(p2d, Static)
            assert not isinstance(p3, Static)
            assert not isinstance(p3d, Static)
            assert not isinstance(p4, Static)
            assert not isinstance(p4[Number(0)], SELECTS[Number])
            assert not isinstance(p4[Number(0)], Static)
            assert not isinstance(p4d, Static)
            assert not isinstance(p4d[Number(0)], SELECTS[Number])
            assert not isinstance(p4d[Number(0)], Static)
            assert not isinstance(p5, Static)
            assert not isinstance(p5d, Static)
            assert not isinstance(p6, Static)
            assert not isinstance(p6d, Static)

    Foo(
        _ig_node=node,
        p1=Number(42),
        p2=Number(42.42),
        p3=String("foo"),
        p4=LISTS[Number]([Number(42)]),
        p5=LISTS[Number]([Number(42.42)]),
        p6=LISTS[String]([String("foo")]),
    )
Example #5
0
def test_string_format():
    res = String("{} {foo} {foo}").format(String("bar"), foo=Number())
    assert isinstance(res, Sub)
    assert isinstance(res, String)
    assert not isinstance(res, Static)
    assert res._ig_format._ig_value == "${A0} ${A1} ${A1}"
    assert len(res._ig_kwargs._ig_value) == 2
Example #6
0
 def __init__(
         self,
         *,
         p1: int,
         p1d: int = Number(42),
         p2: float,
         p2d: float = Number(42.42),
         p3: str,
         p3d: str = String("foo"),
         p4: List[int],
         p4d: List[int] = LISTS[Number]([Number(42)]),
         p5: List[float],
         p5d: List[float] = LISTS[Number]([Number(42.42)]),
         p6: List[str],
         p6d: List[str] = LISTS[String]([String("foo")]),
 ):
     ...
Example #7
0
def test_getparmeters():
    from typing import List

    defaults = dict(
        p1d=Number(42),
        p2d=Number(42.42),
        p3d=String("foo"),
        p4d=LISTS[Number]([Number(42)]),
        p5d=LISTS[Number]([Number(42.42)]),
        p6d=LISTS[String]([String("foo")]),
    )

    class Foo(ExternalResource):
        def __init__(
            self,
            *,
            p1: int,
            p1d: int = defaults["p1d"],
            p2: float,
            p2d: float = defaults["p2d"],
            p3: str,
            p3d: str = defaults["p3d"],
            p4: List[int],
            p4d: List[int] = defaults["p4d"],
            p5: List[float],
            p5d: List[float] = defaults["p5d"],
            p6: List[str],
            p6d: List[str] = defaults["p6d"],
        ):
            ...

    params = _getparmeters(Foo)
    for k, v in params.items():
        d = f"{k}d" if not k.endswith("d") else k
        typ = type(defaults[d])
        assert isinstance(v, PARAMETERS[typ])
        assert isinstance(v, typ)
        assert not isinstance(v, Static)
        assert v._ig_name == k
        assert v._ig_default == defaults.get(k, None)
Example #8
0
def test_external_immutattr(mocker):
    node = mocker.sentinel.NODE

    class Foo(ExternalResource):
        bar: int

        def __init__(self):
            self.bar = Number()

    err = "attribute 'bar' is read-only"
    with pytest.raises(AttributeError, match=err):
        foo = Foo(_ig_node=node)
        foo.bar = Number()
Example #9
0
def test_split_check():
    with pytest.raises(TypeError, match="target is not a string"):
        Split(Number(), String("-"))

    with pytest.raises(TypeError, match="separator is not a static string"):
        Split(String(), String())
Example #10
0
def test_list_index(target, typ):
    for i in range(0, 2):
        res = target[Number(i)]
        assert isinstance(res, typ)
        assert not isinstance(res, Static)
Example #11
0
def test_list_index_check(target):
    with pytest.raises(TypeError, match="index is not a static number"):
        target[Number()]
Example #12
0
def test_select(typ, items):
    select = SELECTS[typ](items, Number(42))
    assert isinstance(select, SELECTS[typ])
    assert isinstance(select, typ)
    assert not isinstance(select, Static)
Example #13
0
def test_number_int():
    with pytest.raises(TypeError, match="number is not static"):
        int(Number())

    assert int(Number(42)) == 42
Example #14
0
 def __init__(self):
     self.bar = Number()
Example #15
0
     "value is not a valid list",
 ),
 (
     "list[native]_empty",
     NativeResourceList,
     [],
     "value is not a valid native resource list",
 ),
 (
     "list[native]_ext",
     NativeResourceList,
     lambda node: [_EXTERNAL(_ig_node=node)],
     "value is not a valid native resource list",
 ),
 ("map_key", Map, {
     String(): Number()
 }, "value is not a static map"),
 (
     "map_value_native",
     Map,
     lambda node: ({
         String(""): _NATIVE(_ig_node=node)
     }),
     "value is not a static map",
 ),
 (
     "map_value_ext",
     Map,
     lambda node: ({
         String(""): _EXTERNAL(_ig_node=node)
     }),