summaryrefslogtreecommitdiff
path: root/Cython/Compiler/Tests
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2014-08-29 19:35:28 +0200
committerStefan Behnel <stefan_ml@behnel.de>2014-08-29 19:35:28 +0200
commit4cb1368a075707065439f8245e12af52e511c795 (patch)
tree19e2796ef15b7df538ff0c0314accea69eeffe70 /Cython/Compiler/Tests
parent1133c6d5beccfb5d75da15870e5fe0265534e752 (diff)
downloadcython-4cb1368a075707065439f8245e12af52e511c795.tar.gz
move JediTyper into Tools directory as it's not in a state that would suggest making it a part of Cython
--HG-- rename : Cython/Compiler/Tests/TestJediTyper.py => Cython/Tests/TestJediTyper.py rename : Cython/Compiler/JediTyper.py => Tools/jedi-typer.py
Diffstat (limited to 'Cython/Compiler/Tests')
-rw-r--r--Cython/Compiler/Tests/TestJediTyper.py134
1 files changed, 0 insertions, 134 deletions
diff --git a/Cython/Compiler/Tests/TestJediTyper.py b/Cython/Compiler/Tests/TestJediTyper.py
deleted file mode 100644
index 8e974eb68..000000000
--- a/Cython/Compiler/Tests/TestJediTyper.py
+++ /dev/null
@@ -1,134 +0,0 @@
-# -*- coding: utf-8 -*-
-# tag: jedi
-
-from __future__ import absolute_import
-
-import unittest
-from textwrap import dedent
-from contextlib import contextmanager
-from tempfile import NamedTemporaryFile
-
-from ..ParseTreeTransforms import NormalizeTree, InterpretCompilerDirectives
-from .. import Main, Symtab, Visitor
-from ...TestUtils import TransformTest
-
-
-@contextmanager
-def _tempfile(code):
- code = dedent(code)
- if isinstance(code, unicode):
- code = code.encode('utf8')
-
- with NamedTemporaryFile(suffix='.py') as f:
- f.write(code)
- f.seek(0)
- yield f
-
-
-def _test_typing(code, inject=False):
- from ..JediTyper import analyse, inject_types
- lines = []
- with _tempfile(code) as f:
- types = analyse(f.name)
- if inject:
- lines = inject_types(f.name, types)
- return types, lines
-
-
-class DeclarationsFinder(Visitor.VisitorTransform):
- directives = None
-
- visit_Node = Visitor.VisitorTransform.recurse_to_children
-
- def visit_CompilerDirectivesNode(self, node):
- if not self.directives:
- self.directives = []
- self.directives.append(node)
- self.visitchildren(node)
- return node
-
-
-class TestJediTyper(TransformTest):
- def _test(self, code):
- return _test_typing(code)[0]
-
- def test_typing_global_int_loop(self):
- code = '''\
- for i in range(10):
- a = i + 1
- '''
- types = self._test(code)
- if not types:
- # old Jedi version
- return
- self.assertIn((None, (1, 0)), types)
- variables = types.pop((None, (1, 0)))
- self.assertFalse(types)
- self.assertEqual({'a': set(['int']), 'i': set(['int'])}, variables)
-
- def test_typing_function_int_loop(self):
- code = '''\
- def func(x):
- for i in range(x):
- a = i + 1
- return a
- '''
- types = self._test(code)
- self.assertIn(('func', (1, 0)), types)
- variables = types.pop(('func', (1, 0)))
- self.assertFalse(types)
- self.assertEqual({'a': set(['int']), 'i': set(['int'])}, variables)
-
- def _test_conflicting_types_in_function(self):
- code = '''\
- def func(a, b):
- print(a)
- a = 1
- b += a
- a = 'abc'
- return a, str(b)
-
- print(func(1.5, 2))
- '''
- types = self._test(code)
- self.assertIn(('func', (1, 0)), types)
- variables = types.pop(('func', (1, 0)))
- self.assertFalse(types)
- self.assertEqual({'a': set(['int', 'str']), 'i': set(['int'])}, variables)
-
- def _test_typing_function_char_loop(self):
- code = '''\
- def func(x):
- l = []
- for c in x:
- l.append(c)
- return l
-
- print(func('abcdefg'))
- '''
- types = self._test(code)
- self.assertIn(('func', (1, 0)), types)
- variables = types.pop(('func', (1, 0)))
- self.assertFalse(types)
- self.assertEqual({'a': set(['int']), 'i': set(['int'])}, variables)
-
-
-class TestTypeInjection(TestJediTyper):
- """
- Subtype of TestJediTyper that additionally tests type injection and compilation.
- """
- def setUp(self):
- super(TestTypeInjection, self).setUp()
- compilation_options = Main.CompilationOptions(Main.default_options)
- ctx = compilation_options.create_context()
- transform = InterpretCompilerDirectives(ctx, ctx.compiler_directives)
- transform.module_scope = Symtab.ModuleScope('__main__', None, ctx)
- self.declarations_finder = DeclarationsFinder()
- self.pipeline = [NormalizeTree(None), transform, self.declarations_finder]
-
- def _test(self, code):
- types, lines = _test_typing(code, inject=True)
- tree = self.run_pipeline(self.pipeline, ''.join(lines))
- directives = self.declarations_finder.directives
- # TODO: validate directives
- return types