def test_tree_leaves(): def f(a): return a tree = parse(thunk(f, (thunk.fromexpr(1) + 2) - 3)) assert set(tree.leaves()) == set(map(Normal, (f, op.add, op.sub, 1, 2, 3)))
def prep_magic_func_args(func, args): try: first_arg = args[0] except IndexError: first_arg = None if first_arg is no_implicit_thunk: args = args[1:] elif first_arg is reflect_implicit: args = args[1:] + (thunk(object), ) elif first_arg is magic_class: args = (class_factory(func)(), ) else: args = (thunk(object), ) + args[1:] return func, args
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 prep_magic_func_args(func, args): try: first_arg = args[0] except IndexError: first_arg = None if first_arg is no_implicit_thunk: args = args[1:] elif first_arg is reflect_implicit: args = args[1:] + (thunk(object),) elif first_arg is magic_class: args = (class_factory(func)(),) else: args = (thunk(object),) + args[1:] return func, args
def test_laziness(): def raiser(): raise ValueError('raiser raised') a = thunk(raiser) with pytest.raises(ValueError): strict(a)
def test_laziness(self): def raiser(): raise ValueError('raiser raised') a = thunk(raiser) with self.assertRaises(ValueError): strict(a)
def __rshift__(self, other): other = self._box_literal(other) if_not_alt = thunk(op.rshift, self, other) self._constructors[self._name] = Alternative( self._name, self._args, self._kwargs, other, if_not_alt, ) return if_not_alt
def __new__(mcls, name, bases, dict_): for func, args in dict_.get('test_operators_lazy', ()): name = func.__name__ try: first_arg = args[0] except IndexError: first_arg = None if first_arg is no_implicit_thunk: args = args[1:] elif first_arg is reflect_implicit: args = args[1:] + (thunk(object), ) elif first_arg is magic_class: args = (class_factory(func)(), ) else: args = (thunk(object), ) + args[1:] full_name = 'test_%s_lazy' % name dict_[full_name] = _test_magic_func(func, full_name, args) return type.__new__(mcls, name, bases, dict_)
def __new__(mcls, name, bases, dict_): for func, args in dict_.get('test_operators_lazy', ()): name = func.__name__ try: first_arg = args[0] except IndexError: first_arg = None if first_arg is no_implicit_thunk: args = args[1:] elif first_arg is reflect_implicit: args = args[1:] + (thunk(object),) elif first_arg is magic_class: args = (class_factory(func)(),) else: args = (thunk(object),) + args[1:] full_name = 'test_%s_lazy' % name dict_[full_name] = _test_magic_func(func, full_name, args) return type.__new__(mcls, name, bases, dict_)
def test_iter(self): """ Tests that lazy iteration is correct and terminates. This is a strict point. """ t = thunk(lambda: (1, 2, 3)) it = iter(t) self.assertIsInstance(it, thunk) vals = tuple(self.assertIsInstance(a, thunk) or a for a in it) self.assertEquals(vals, t) with self.assertRaises(StopIteration): next(it)
def test_iter(): """ Tests that lazy iteration is correct and terminates. This is a strict point. """ t = thunk(lambda: (1, 2, 3)) it = iter(t) assert isinstance(it, thunk) def isthunk(a): assert isinstance(a, thunk) return a vals = tuple(map(isthunk, it)) assert vals == t with pytest.raises(StopIteration): next(it)
def test_isinstance_strict(self): th = thunk(lambda: 2) self.assertIsInstance(th, int) self.assertIsInstance(th, thunk)
def wrapper(self): a = thunk(f, *args) with self.assertRaises(TypeError): strict(a)
class ThunkTestCase(TestCase, metaclass=MagicTestDispatchMeta): def test_laziness(self): def raiser(): raise ValueError('raiser raised') a = thunk(raiser) with self.assertRaises(ValueError): strict(a) def test_isinstance_strict(self): th = thunk(lambda: 2) self.assertIsInstance(th, int) self.assertIsInstance(th, thunk) # MagicTestDispatchMeta makes tests for all of these. test_operators_lazy = ( (operator.eq, (thunk(object), )), (operator.ne, (thunk(object), )), (operator.lt, (thunk(object), )), (operator.gt, (thunk(object), )), (operator.le, (thunk(object), )), (operator.ge, (thunk(object), )), (operator.pos, ()), (operator.neg, ()), (abs, ()), (operator.invert, ()), (round, ()), (math.floor, ()), (math.ceil, ()), (math.trunc, (magic_class, )), (operator.add, (int(), )), (operator.add, (reflect_implicit, int())), (operator.iadd, ()), (operator.sub, (int(), )), (operator.sub, (reflect_implicit, int())), (operator.isub, ()), (operator.mul, (int(), )), (operator.mul, (reflect_implicit, int())), (operator.imul, ()), (operator.floordiv, (int(), )), (operator.floordiv, (reflect_implicit, int())), (operator.ifloordiv, (int(), )), (operator.truediv, (int(), )), (operator.truediv, (reflect_implicit, int())), (operator.itruediv, (int(), )), (operator.mod, (int(), )), (operator.mod, (reflect_implicit, int())), (operator.imod, (int(), )), (divmod, (int(), )), (divmod, (reflect_implicit, int())), (pow, (int(), )), ( pow, (reflect_implicit, int()), ), (operator.ipow, ()), (operator.lshift, (int(), )), (operator.lshift, (reflect_implicit, int())), (operator.ilshift, ()), (operator.rshift, (int(), )), (operator.rshift, (reflect_implicit, int())), (operator.irshift, ()), (operator.and_, (int(), )), (operator.and_, (reflect_implicit, int())), (operator.iand, ()), (operator.or_, (int(), )), (operator.or_, (reflect_implicit, int())), (operator.ior, ()), (operator.xor, (int(), )), (operator.xor, (reflect_implicit, int())), (operator.ixor, (int(), )), (int, (magic_class, )), (float, (magic_class, )), (complex, (magic_class, )), (oct, (magic_class, )), (hex, (magic_class, )), (operator.index, (magic_class, )), (str, (magic_class, )), (bytes, (magic_class, )), (repr, (magic_class, )), (hash, (magic_class, )), (bool, (magic_class, )), (dir, (magic_class, )), (len, (magic_class, )), (iter, (magic_class, )), (reversed, (magic_class, )), (call, (magic_class, )), ) def test_iter(self): """ Tests that lazy iteration is correct and terminates. This is a strict point. """ t = thunk(lambda: (1, 2, 3)) it = iter(t) self.assertIsInstance(it, thunk) vals = tuple(self.assertIsInstance(a, thunk) or a for a in it) self.assertEquals(vals, t) with self.assertRaises(StopIteration): next(it)
def raiser(self): raise TypeError(magic) return type('%s_test_class' % magic, (object, ), {magic: raiser}) def call(f, *args, **kwargs): """ Alias to make the metaclass magic defer to __call__. """ return f(*args, **kwargs) @pytest.mark.parametrize('f,args', starmap(prep_magic_func_args, ( (operator.eq, (thunk(object), )), (operator.ne, (thunk(object), )), (operator.lt, (thunk(object), )), (operator.gt, (thunk(object), )), (operator.le, (thunk(object), )), (operator.ge, (thunk(object), )), (operator.pos, ()), (operator.neg, ()), (abs, ()), (operator.invert, ()), (round, ()), (math.floor, ()), (math.ceil, ()), (math.trunc, (magic_class, )), (operator.add, (int(), )), (operator.add, (reflect_implicit, int())),
def raiser(self): raise TypeError(magic) return type('%s_test_class' % magic, (object,), {magic: raiser}) def call(f, *args, **kwargs): """ Alias to make the metaclass magic defer to __call__. """ return f(*args, **kwargs) @pytest.mark.parametrize('f,args', starmap(prep_magic_func_args, ( (operator.eq, (thunk(object),)), (operator.ne, (thunk(object),)), (operator.lt, (thunk(object),)), (operator.gt, (thunk(object),)), (operator.le, (thunk(object),)), (operator.ge, (thunk(object),)), (operator.pos, ()), (operator.neg, ()), (abs, ()), (operator.invert, ()), (round, ()), (math.floor, ()), (math.ceil, ()), (math.trunc, (magic_class,)), (operator.add, (int(),)), (operator.add, (reflect_implicit, int())),
def test_strict_thunk(): assert strict(thunk.fromvalue(5)) is 5 assert strict(thunk(lambda a: a, 5)) is 5
def test_strict(self): self.assertIs(strict(5), 5) self.assertEqual(thunk(lambda: 5), 5)
def test_isinstance_strict(): th = thunk(lambda: 2) assert isinstance(th, int) assert isinstance(th, thunk)
def test_strict_thunk(): assert strict(thunk.fromexpr(5)) is 5 assert strict(thunk(lambda a: a, 5)) is 5