-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assertion Roulette::Detecting AssertCount in each method
- Loading branch information
Talismanic
committed
Aug 14, 2020
1 parent
c581910
commit 735cfbf
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
# In[1]: | ||
|
||
|
||
import ast | ||
from pprint import pprint | ||
import inspect | ||
import astor | ||
|
||
|
||
# In[67]: | ||
|
||
|
||
def main(): | ||
print(detectAssertionRoulette("test_code_for_parsing.py")) | ||
|
||
|
||
# In[68]: | ||
|
||
|
||
def detectAssertionRoulette(script_name): | ||
# Defining Array to hold the function name, count of Assert statements & presence of smell | ||
fns = [] | ||
|
||
#parsing the script using AST | ||
with open(script_name, "r") as source: | ||
tree = ast.parse(source.read()) | ||
for block in tree.body: | ||
for node in ast.walk(block): | ||
fn = {} | ||
#checking whether current node is a function | ||
if isinstance(node, ast.FunctionDef): | ||
fn['name'] = node.name | ||
# fn['body'] = astor.to_source(node) | ||
fn['countAssert'] = 0 | ||
fn['hasSmell'] = False | ||
for body_item in node.body: | ||
#checking if the function body has any assert statement | ||
#and keeping the count of that | ||
if isinstance(body_item, ast.Assert): | ||
fn['countAssert'] = fn['countAssert'] + 1 | ||
|
||
if fn['countAssert'] >1: | ||
fn['hasSmell'] = True | ||
|
||
|
||
fns.append(fn) | ||
return fns | ||
|
||
|
||
# In[71]: | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
||
# In[ ]: | ||
|
||
|
||
#https://stackoverflow.com/questions/54092879/how-to-find-out-if-the-source-code-of-a-function-contains-a-loop | ||
|