def test_strict_method(): class C: def __strict__(self): return 5 assert strict(C()) is 5 assert strict(C) is C
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)
def test_cannot_strict_undefined(): from lazy import undefined with pytest.raises(Exception) as e: strict(undefined) assert type(e.value).__name__ == 'undefined'
def test_laziness(self): def raiser(): raise ValueError('raiser raised') a = thunk(raiser) with self.assertRaises(ValueError): strict(a)
def test_laziness(): def raiser(): raise ValueError('raiser raised') a = thunk(raiser) with pytest.raises(ValueError): strict(a)
def test_lazy_call(): called = False @lazy_function def g(): nonlocal called called = True result = g() assert not called strict(result) assert called
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')
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))
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)
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)
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))
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
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())
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_)
def wrapper(self): a = thunk(f, *args) with self.assertRaises(TypeError): strict(a)
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_)
def test_strict_prim(): assert strict(5) is 5
def test_const(f, val): f = strict(lazy_function(f)) assert isinstance(f(), thunk) assert f() == val
def test_strict_thunk(): assert strict(thunk.fromexpr(5)) is 5 assert strict(thunk(lambda a: a, 5)) is 5
def __rshift__(self, other): return strict(self) >> other
def test_strict(self): self.assertIs(strict(5), 5) self.assertEqual(thunk(lambda: 5), 5)
def test_strict_thunk(): assert strict(thunk.fromvalue(5)) is 5 assert strict(thunk(lambda a: a, 5)) is 5
def test_compile_of_parse_identity(expr): assert strict(parse(expr()).lcompile()) == strict(expr())