Added `mcpyrate` test case to experiments.
This commit is contained in:
parent
5a24d72772
commit
270816c97f
|
|
@ -0,0 +1,9 @@
|
|||
[run]
|
||||
source =
|
||||
.
|
||||
|
||||
[report]
|
||||
exclude_lines =
|
||||
pragma: no cover
|
||||
# raise RuntimeError
|
||||
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/make
|
||||
PY_COV=python3-coverage
|
||||
#PY_COV=$(HOME)/.local/bin/coverage3
|
||||
|
||||
all:
|
||||
$(PY_COV) erase
|
||||
$(PY_COV) run -a ./try_mymacros.py
|
||||
$(PY_COV) report
|
||||
$(PY_COV) html
|
||||
x-www-browser htmlcov/index.html
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Coverage and mcpyrate
|
||||
|
||||
This example implements the correct solution.
|
||||
|
||||
- We must ensure all the new nodes has the proper line info.
|
||||
- `mcpyrate` can fill it, but will be the line number for the `with` statement.
|
||||
- We recycle the Str node and copy the line info to the newly created nodes.
|
||||
- Lines with just an Exp are optimized out and never show coverage.
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
from mymacros import macros, document # noqa: F401
|
||||
from mcpyrate.debug import macros, step_expansion
|
||||
|
||||
|
||||
with step_expansion["dump"]:
|
||||
with document: # <--- Not covered?
|
||||
# comentario a
|
||||
a = "5.1"
|
||||
""" docu a """
|
||||
b = False
|
||||
""" docu b """
|
||||
c = 3
|
||||
""" docu c """ # <--- Not covered? Note: if no macro is expanded it could be optimized out.
|
||||
|
||||
|
||||
class d(object):
|
||||
def __init__(self):
|
||||
with document:
|
||||
self.at1 = 4.5
|
||||
""" documenting d.at1 """ # <--- Not covered?
|
||||
|
||||
print("a = "+str(a)+" # "+_help_a) # noqa: F821
|
||||
print("b = "+str(b)+" # "+_help_b) # noqa: F821
|
||||
print("c = "+str(c)+" # "+_help_c) # noqa: F821
|
||||
e = d()
|
||||
print("e.at1 = "+str(e.at1)+" # "+e._help_at1) # noqa: F821
|
||||
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
from ast import (Assign, Name, Attribute, Expr, Num, Str, NameConstant, Load, Store, copy_location)
|
||||
|
||||
|
||||
def document(tree, **kw):
|
||||
""" This macro takes literal strings and converts them into:
|
||||
_help_ID = type_hint+STRING
|
||||
where:
|
||||
ID is the first target of the last assignment.
|
||||
type_hint is the assigned type and default value (only works for a few types)
|
||||
STRING is the literal string """
|
||||
# Simplify it just to show the problem isn't related to the content of the macro
|
||||
# Note: This triggers another issue, Expr nodes can be optimized out if not assigned to a target
|
||||
# return tree
|
||||
for n in range(len(tree)):
|
||||
s = tree[n]
|
||||
if not n:
|
||||
prev = s
|
||||
continue
|
||||
# The whole sentence is a string?
|
||||
if (isinstance(s, Expr) and isinstance(s.value, Str) and
|
||||
# and the previous is an assign
|
||||
isinstance(prev, Assign)): # noqa: E128
|
||||
# Apply it to the first target
|
||||
target = prev.targets[0]
|
||||
value = prev.value
|
||||
# Extract its name
|
||||
# variables and attributes are supported
|
||||
if isinstance(target, Name):
|
||||
name = target.id
|
||||
is_attr = False
|
||||
elif isinstance(target, Attribute):
|
||||
name = target.attr
|
||||
is_attr = True
|
||||
# Create a _help_ID
|
||||
doc_id = '_help_'+name
|
||||
# Create the type hint for numbers, strings and booleans
|
||||
type_hint = ''
|
||||
if isinstance(value, Num):
|
||||
type_hint = '[number={}]'.format(value.n)
|
||||
elif isinstance(value, Str):
|
||||
type_hint = "[string='{}']".format(value.s)
|
||||
elif isinstance(value, NameConstant) and isinstance(value.value, bool):
|
||||
type_hint = '[boolean={}]'.format(str(value.value).lower())
|
||||
# Transform the string into an assign for _help_ID
|
||||
if is_attr:
|
||||
target = Attribute(value=Name(id='self', ctx=Load()), attr=doc_id, ctx=Store())
|
||||
else:
|
||||
target = Name(id=doc_id, ctx=Store())
|
||||
help_str = s.value
|
||||
help_str.s=type_hint+s.value.s
|
||||
tree[n] = Assign(targets=[target], value=help_str)
|
||||
# Copy the line number from the original docstring
|
||||
copy_location(target, s)
|
||||
copy_location(tree[n], s)
|
||||
prev = s
|
||||
# Return the modified AST
|
||||
return tree
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/python3
|
||||
import mcpyrate.activate # noqa: F401
|
||||
import application # noqa: F401
|
||||
Loading…
Reference in New Issue