Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
a08d2d04
Commit
a08d2d04
authored
Jul 27, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for JediTyper (includes disabled tests for things that currently do not work but should)
parent
6f930abe
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
133 additions
and
1 deletion
+133
-1
Cython/Compiler/Tests/TestJediTyper.py
Cython/Compiler/Tests/TestJediTyper.py
+131
-0
runtests.py
runtests.py
+2
-1
No files found.
Cython/Compiler/Tests/TestJediTyper.py
0 → 100644
View file @
a08d2d04
# -*- 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
)
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
runtests.py
View file @
a08d2d04
...
...
@@ -95,7 +95,8 @@ EXT_DEP_MODULES = {
'tag:pstats'
:
'pstats'
,
'tag:posix'
:
'posix'
,
'tag:array'
:
'array'
,
'tag:ipython'
:
'IPython'
'tag:ipython'
:
'IPython'
,
'tag:jedi'
:
'jedi'
,
}
def
patch_inspect_isfunction
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment