Exemplo n.º 1
0
def test_strict_method():
    class C:
        def __strict__(self):
            return 5

    assert strict(C()) is 5
    assert strict(C) is C
Exemplo n.º 2
0
def test_magic_func(f, args):
    """
    The actual test case for a the magic inner type.
    """
    a = thunk(f, *args)
    with pytest.raises(TypeError):
        strict(a)
Exemplo n.º 3
0
def test_strict_method():
    class C:
        def __strict__(self):
            return 5

    assert strict(C()) is 5
    assert strict(C) is C
Exemplo n.º 4
0
def test_magic_func(f, args):
    """
    The actual test case for a the magic inner type.
    """
    a = thunk(f, *args)
    with pytest.raises(TypeError):
        strict(a)
Exemplo n.º 5
0
def test_cannot_strict_undefined():
    from lazy import undefined

    with pytest.raises(Exception) as e:
        strict(undefined)

    assert type(e.value).__name__ == 'undefined'
Exemplo n.º 6
0
    def test_laziness(self):
        def raiser():
            raise ValueError('raiser raised')

        a = thunk(raiser)

        with self.assertRaises(ValueError):
            strict(a)
Exemplo n.º 7
0
def test_laziness():
    def raiser():
        raise ValueError('raiser raised')

    a = thunk(raiser)

    with pytest.raises(ValueError):
        strict(a)
Exemplo n.º 8
0
    def test_laziness(self):
        def raiser():
            raise ValueError('raiser raised')

        a = thunk(raiser)

        with self.assertRaises(ValueError):
            strict(a)
Exemplo n.º 9
0
def test_laziness():
    def raiser():
        raise ValueError('raiser raised')

    a = thunk(raiser)

    with pytest.raises(ValueError):
        strict(a)
Exemplo n.º 10
0
def test_lazy_call():
    called = False

    @lazy_function
    def g():
        nonlocal called
        called = True

    result = g()
    assert not called

    strict(result)
    assert called
Exemplo n.º 11
0
    def test_lazy_call(self):
        called = False

        @lazy_function
        def g():
            nonlocal called
            called = True

        result = g()
        self.assertFalse(called, 'The function call was strict')

        strict(result)
        self.assertTrue(called, 'The function call did not evaluate')
Exemplo n.º 12
0
    def test_not(self):
        @lazy_function
        def g():
            return not 1

        a = g()
        self.assertIsInstance(a, thunk)
        self.assertFalse(strict(a))

        @lazy_function
        def h(a):
            return not a

        b = h(False)
        self.assertIsInstance(b, thunk)
        self.assertTrue(strict(b))

        c = h(True)
        self.assertIsInstance(c, thunk)
        self.assertFalse(strict(c))
Exemplo n.º 13
0
def test_is():
    @lazy_function
    def g():
        return 1 is 1

    a = g()
    assert isinstance(a, thunk)
    assert strict(a)

    @lazy_function
    def h(a):
        return a is None

    b = h(None)
    assert isinstance(b, thunk)
    assert strict(b)

    c = h('not none')
    assert isinstance(c, thunk)
    assert not strict(c)
Exemplo n.º 14
0
def test_not():
    @lazy_function
    def g():
        return not 1

    a = g()
    assert isinstance(a, thunk)
    assert not strict(a)

    @lazy_function
    def h(a):
        return not a

    b = h(False)
    assert isinstance(b, thunk)
    assert strict(b)

    c = h(True)
    assert isinstance(c, thunk)
    assert not strict(c)
Exemplo n.º 15
0
    def test_is(self):
        @lazy_function
        def g():
            return 1 is 1

        a = g()
        self.assertIsInstance(a, thunk)
        self.assertTrue(strict(a))

        @lazy_function
        def h(a):
            return a is None

        b = h(None)
        self.assertIsInstance(b, thunk)
        self.assertTrue(strict(b))

        c = h('not none')
        self.assertIsInstance(c, thunk)
        self.assertFalse(strict(c))
Exemplo n.º 16
0
def test_import_name():
    sys.modules.pop('__hello__', None)

    @strict
    @lazy_function
    def f():
        import __hello__
        return __hello__

    hello_module_thunk = f()
    assert '__hello__' not in sys.modules
    hello_module = strict(hello_module_thunk)
    assert hello_module.__name__ == '__hello__'
    assert '__hello__' in sys.modules
Exemplo n.º 17
0
    def scrutinize(self, scrutine, context_frame):
        constructor = type(scrutine)
        if constructor.__name__ != self._constructor_name:
            raise NoMatch()

        kwargs = scrutine._kwargs
        # the context to evaluate the thunk in
        context = {
            Call(Normal(name_lookup), (Normal(name),), {}): Normal(value)
            for name, value in merge(
                vars(builtins),
                context_frame.f_globals,
                context_frame.f_locals,
                # the newly bound arguments have the highest precedence
                dict(zip(self._argnames, scrutine._args)),
                {v: kwargs[k] for k, v in self._kwargnames.items()},
            ).items()
        }
        bound_tree = LTree.parse(self._expr).subs(context)
        return strict(bound_tree.lcompile())
Exemplo n.º 18
0
def test_fromexpr_of_thunk(type_):
    a = thunk.fromexpr(type_.fromexpr(1))
    assert isinstance(a, type_)
    assert isinstance(strict(a), int)
    assert not isinstance(strict(a), type_)
Exemplo n.º 19
0
 def wrapper(self):
     a = thunk(f, *args)
     with self.assertRaises(TypeError):
         strict(a)
Exemplo n.º 20
0
def test_fromvalue_of_thunk(type_):
    a = thunk.fromvalue(type_.fromvalue(1))
    assert isinstance(a, type_)
    assert isinstance(strict(a), int)
    assert not isinstance(strict(a), type_)
Exemplo n.º 21
0
def test_strict_prim():
    assert strict(5) is 5
Exemplo n.º 22
0
def test_strict_prim():
    assert strict(5) is 5
Exemplo n.º 23
0
def test_const(f, val):
    f = strict(lazy_function(f))

    assert isinstance(f(), thunk)
    assert f() == val
Exemplo n.º 24
0
def test_strict_thunk():
    assert strict(thunk.fromexpr(5)) is 5
    assert strict(thunk(lambda a: a, 5)) is 5
Exemplo n.º 25
0
 def __rshift__(self, other):
     return strict(self) >> other
Exemplo n.º 26
0
 def wrapper(self):
     a = thunk(f, *args)
     with self.assertRaises(TypeError):
         strict(a)
Exemplo n.º 27
0
 def test_strict(self):
     self.assertIs(strict(5), 5)
     self.assertEqual(thunk(lambda: 5), 5)
Exemplo n.º 28
0
def test_strict_thunk():
    assert strict(thunk.fromvalue(5)) is 5
    assert strict(thunk(lambda a: a, 5)) is 5
Exemplo n.º 29
0
def test_compile_of_parse_identity(expr):
    assert strict(parse(expr()).lcompile()) == strict(expr())
Exemplo n.º 30
0
def test_compile_of_parse_identity(expr):
    assert strict(parse(expr()).lcompile()) == strict(expr())