/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/base/tests/test_ast_utils.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | import ast
from textwrap import dedent
import unittest
from pysph.base.ast_utils import (get_assigned, get_aug_assign_symbols, get_symbols,
has_node, has_return)
class TestASTUtils(unittest.TestCase):
def test_get_symbols(self):
code = '''
x = 1
d_x[d_idx] += s_x[s_idx]
'''
tree = ast.parse(dedent(code))
result = list(get_symbols(tree))
result.sort()
expect = ['d_idx', 'd_x', 's_idx', 's_x', 'x']
self.assertEqual(result, expect)
# Test if it parses with the code itself instead of a tree.
result = list(get_symbols(dedent(code)))
result.sort()
self.assertEqual(result, expect)
result = list(get_symbols(tree, ctx=ast.Store))
result.sort()
self.assertEqual(result, ['x'])
def test_get_aug_assign_symbols(self):
code = '''
x = 1
'''
result = list(get_aug_assign_symbols(dedent(code)))
result.sort()
expect = []
self.assertEqual(result, expect)
code = '''
x += 1
d_x[d_idx] += s_x[s_idx]
'''
result = list(get_aug_assign_symbols(dedent(code)))
result.sort()
expect = ['d_x', 'x']
self.assertEqual(result, expect)
def test_has_return(self):
code = dedent('''
x = 1
''')
self.assertFalse(has_return(code))
code = dedent('''
def f():
pass
''')
self.assertFalse(has_return(code))
code = dedent('''
def f(x):
return x+1
''')
self.assertTrue(has_return(code))
def test_has_node(self):
code = dedent('''
x = 1
''')
self.assertFalse(has_node(code, (ast.Return, ast.AugAssign)))
code = dedent('''
def f():
pass
''')
self.assertTrue(has_node(code, (ast.AugAssign, ast.FunctionDef)))
def test_assigned_values(self):
code = dedent('''
u[0] = 0.0
x = 1
y = sin(x)*theta
z += 1
''')
assigned = list(sorted(get_assigned(code)))
# sin or theta should not be detected.
expect = ['u', 'x', 'y', 'z']
self.assertEqual(assigned, expect)
def test_assigned_tuple_expansion(self):
code = dedent('''
u, v = 0.0, 1.0
[x, y] = 0.0, 1.0
''')
assigned = list(sorted(get_assigned(code)))
expect = ['u', 'v', 'x', 'y']
self.assertEqual(assigned, expect)
if __name__ == '__main__':
unittest.main()
|