File size: 5,147 Bytes
065fee7 |
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
import sys
import unittest
import weakref
sys.path.insert(0, '..')
import pycparser.c_ast as c_ast
import pycparser.plyparser as plyparser
class Test_c_ast(unittest.TestCase):
def test_BinaryOp(self):
b1 = c_ast.BinaryOp(
op='+',
left=c_ast.Constant(type='int', value='6'),
right=c_ast.ID(name='joe'))
self.assertIsInstance(b1.left, c_ast.Constant)
self.assertEqual(b1.left.type, 'int')
self.assertEqual(b1.left.value, '6')
self.assertIsInstance(b1.right, c_ast.ID)
self.assertEqual(b1.right.name, 'joe')
def test_weakref_works_on_nodes(self):
c1 = c_ast.Constant(type='float', value='3.14')
wr = weakref.ref(c1)
cref = wr()
self.assertEqual(cref.type, 'float')
self.assertEqual(weakref.getweakrefcount(c1), 1)
def test_weakref_works_on_coord(self):
coord = plyparser.Coord(file='a', line=2)
wr = weakref.ref(coord)
cref = wr()
self.assertEqual(cref.line, 2)
self.assertEqual(weakref.getweakrefcount(coord), 1)
class TestNodeVisitor(unittest.TestCase):
class ConstantVisitor(c_ast.NodeVisitor):
def __init__(self):
self.values = []
def visit_Constant(self, node):
self.values.append(node.value)
def test_scalar_children(self):
b1 = c_ast.BinaryOp(
op='+',
left=c_ast.Constant(type='int', value='6'),
right=c_ast.ID(name='joe'))
cv = self.ConstantVisitor()
cv.visit(b1)
self.assertEqual(cv.values, ['6'])
b2 = c_ast.BinaryOp(
op='*',
left=c_ast.Constant(type='int', value='111'),
right=b1)
b3 = c_ast.BinaryOp(
op='^',
left=b2,
right=b1)
cv = self.ConstantVisitor()
cv.visit(b3)
self.assertEqual(cv.values, ['111', '6', '6'])
def tests_list_children(self):
c1 = c_ast.Constant(type='float', value='5.6')
c2 = c_ast.Constant(type='char', value='t')
b1 = c_ast.BinaryOp(
op='+',
left=c1,
right=c2)
b2 = c_ast.BinaryOp(
op='-',
left=b1,
right=c2)
comp = c_ast.Compound(
block_items=[b1, b2, c1, c2])
cv = self.ConstantVisitor()
cv.visit(comp)
self.assertEqual(cv.values,
['5.6', 't', '5.6', 't', 't', '5.6', 't'])
def test_repr(self):
c1 = c_ast.Constant(type='float', value='5.6')
c2 = c_ast.Constant(type='char', value='t')
b1 = c_ast.BinaryOp(
op='+',
left=c1,
right=c2)
b2 = c_ast.BinaryOp(
op='-',
left=b1,
right=c2)
comp = c_ast.Compound(
block_items=[b1, b2, c1, c2])
expected = ("Compound(block_items=[BinaryOp(op='+',\n"
" left=Constant(type='float',\n"
" value='5.6'\n"
" ),\n"
" right=Constant(type='char',\n"
" value='t'\n"
" )\n"
" ),\n"
" BinaryOp(op='-',\n"
" left=BinaryOp(op='+',\n"
" left=Constant(type='float',\n"
" value='5.6'\n"
" ),\n"
" right=Constant(type='char',\n"
" value='t'\n"
" )\n"
" ),\n"
" right=Constant(type='char',\n"
" value='t'\n"
" )\n"
" ),\n"
" Constant(type='float',\n"
" value='5.6'\n"
" ),\n"
" Constant(type='char',\n"
" value='t'\n"
" )\n"
" ]\n"
" )")
self.assertEqual(repr(comp),
expected)
if __name__ == '__main__':
unittest.main()
|