def test_tarfile_not_addable(self, method_name='add'): expr = xg.UserExpression( self.empty_tar_expr('.{}(_)'.format(method_name))) arg = '.' with self.assertRaisesWrapped(AttributeError, xg.UserExpressionRuntimeError, arg): expr(arg)
def test_syntax_error(self, expr_s='lambda s:', wrapped_class=SyntaxError, *wrapper_args): with self.assertRaisesWrapped(wrapped_class, xg.UserExpressionCompileError, *wrapper_args): xg.UserExpression(expr_s)
def test_bad_mode_fails(self, mode=bad_mode): expr = xg.UserExpression(call_fmt.format(expr_func_name, mode)) with self.assertRaisesWrapped(ValueError, xg.UserExpressionRuntimeError, NONEXISTENT_PATH) as exc_check: expr(NONEXISTENT_PATH) orig_error_s = exc_check.exception.__cause__.args[0] self.assertTrue(orig_error_s.startswith('invalid mode: '))
def test_function_wrapped(self): expr = xg.UserExpression(expr_func_name) name_parts = iter(expr_func_name.split('.')) func = walk_attrs(expr._EVAL_VARS[next(name_parts)], name_parts) self.assertIsNotNone(func.__closure__) self.assertTrue( any(c.cell_contents is src_func for c in func.__closure__)) self.assertNotEqual(func.__code__.co_name, src_func.__name__)
def test_zipfile_not_addable(self, class_name='zipfile.ZipFile', method_name='write'): expr = xg.UserExpression( self.empty_zip_expr(class_name, '.{}(_)'.format(method_name))) arg = '.' with self.assertRaisesWrapped(AttributeError, xg.UserExpressionRuntimeError, arg): expr(arg)
def test_xg_contents_not_usable(self): for name in ['NameChecker', 'UserExpression', 'name']: expr_s = '{}(_)'.format(name) try: xg.UserExpression(expr_s) except xg.UserExpressionCompileError as exception: self.assertIsInstance(exception.__cause__, NameError) else: self.fail( "expression {!r} did not raise xg.UserExpressionCompileError" .format(expr_s))
def test_os_path_usable(self): expr = xg.UserExpression('os.path.basename') self.assertEqual(expr(os.path.join('dir', 'test')), 'test')
def test_builtin_usable(self): expr = xg.UserExpression('int') self.assertEqual(expr('019'), 19)
def test_arbitrary_shortcut_name(self): expr = xg.UserExpression('anything * 3') self.assertEqual(expr('t'), 'ttt')
def test_underscore_shortcut_operator(self): expr = xg.UserExpression('_ * 2') self.assertEqual(expr('test'), 'testtest')
def test_underscore_shortcut_function(self): expr = xg.UserExpression('float(_)') self.assertEqual(expr('1.24'), 1.24)
def test_underscore_shortcut_slice(self): expr = xg.UserExpression('_[:3]') self.assertEqual(expr('test'), 'tes')
def test_underscore_shortcut_method(self): expr = xg.UserExpression('_.upper()') self.assertEqual(expr('test'), 'TEST')
def test_simple_callable(self): expr = xg.UserExpression('lambda s: s.upper()') self.assertEqual(expr('test'), 'TEST')