Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 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: '))
Exemplo n.º 4
0
 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__)
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 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))
Exemplo n.º 7
0
 def test_os_path_usable(self):
     expr = xg.UserExpression('os.path.basename')
     self.assertEqual(expr(os.path.join('dir', 'test')), 'test')
Exemplo n.º 8
0
 def test_builtin_usable(self):
     expr = xg.UserExpression('int')
     self.assertEqual(expr('019'), 19)
Exemplo n.º 9
0
 def test_arbitrary_shortcut_name(self):
     expr = xg.UserExpression('anything * 3')
     self.assertEqual(expr('t'), 'ttt')
Exemplo n.º 10
0
 def test_underscore_shortcut_operator(self):
     expr = xg.UserExpression('_ * 2')
     self.assertEqual(expr('test'), 'testtest')
Exemplo n.º 11
0
 def test_underscore_shortcut_function(self):
     expr = xg.UserExpression('float(_)')
     self.assertEqual(expr('1.24'), 1.24)
Exemplo n.º 12
0
 def test_underscore_shortcut_slice(self):
     expr = xg.UserExpression('_[:3]')
     self.assertEqual(expr('test'), 'tes')
Exemplo n.º 13
0
 def test_underscore_shortcut_method(self):
     expr = xg.UserExpression('_.upper()')
     self.assertEqual(expr('test'), 'TEST')
Exemplo n.º 14
0
 def test_simple_callable(self):
     expr = xg.UserExpression('lambda s: s.upper()')
     self.assertEqual(expr('test'), 'TEST')