| 1 |
import sys |
| 2 |
import unittest |
| 3 |
import weakref |
| 4 |
|
| 5 |
sys.path.insert(0, "..") |
| 6 |
import pycparser.c_ast as c_ast |
| 7 |
from pycparser.c_parser import Coord |
| 8 |
|
| 9 |
|
| 10 |
class Test_c_ast(unittest.TestCase): |
| 11 |
def test_BinaryOp(self): |
| 12 |
b1 = c_ast.BinaryOp( |
| 13 |
op="+", |
| 14 |
left=c_ast.Constant(type="int", value="6"), |
| 15 |
right=c_ast.ID(name="joe"), |
| 16 |
) |
| 17 |
|
| 18 |
self.assertIsInstance(b1.left, c_ast.Constant) |
| 19 |
self.assertEqual(b1.left.type, "int") |
| 20 |
self.assertEqual(b1.left.value, "6") |
| 21 |
|
| 22 |
self.assertIsInstance(b1.right, c_ast.ID) |
| 23 |
self.assertEqual(b1.right.name, "joe") |
| 24 |
|
| 25 |
def test_weakref_works_on_nodes(self): |
| 26 |
c1 = c_ast.Constant(type="float", value="3.14") |
| 27 |
wr = weakref.ref(c1) |
| 28 |
cref = wr() |
| 29 |
self.assertIsNotNone(cref) |
| 30 |
assert cref is not None |
| 31 |
self.assertEqual(cref.type, "float") |
| 32 |
self.assertEqual(weakref.getweakrefcount(c1), 1) |
| 33 |
|
| 34 |
def test_weakref_works_on_coord(self): |
| 35 |
coord = Coord(file="a", line=2) |
| 36 |
wr = weakref.ref(coord) |
| 37 |
cref = wr() |
| 38 |
self.assertIsNotNone(cref) |
| 39 |
assert cref is not None |
| 40 |
self.assertEqual(cref.line, 2) |
| 41 |
self.assertEqual(weakref.getweakrefcount(coord), 1) |
| 42 |
|
| 43 |
|
| 44 |
class TestNodeVisitor(unittest.TestCase): |
| 45 |
class ConstantVisitor(c_ast.NodeVisitor): |
| 46 |
def __init__(self): |
| 47 |
self.values = [] |
| 48 |
|
| 49 |
def visit_Constant(self, node): |
| 50 |
self.values.append(node.value) |
| 51 |
|
| 52 |
def test_scalar_children(self): |
| 53 |
b1 = c_ast.BinaryOp( |
| 54 |
op="+", |
| 55 |
left=c_ast.Constant(type="int", value="6"), |
| 56 |
right=c_ast.ID(name="joe"), |
| 57 |
) |
| 58 |
|
| 59 |
cv = self.ConstantVisitor() |
| 60 |
cv.visit(b1) |
| 61 |
|
| 62 |
self.assertEqual(cv.values, ["6"]) |
| 63 |
|
| 64 |
b2 = c_ast.BinaryOp( |
| 65 |
op="*", left=c_ast.Constant(type="int", value="111"), right=b1 |
| 66 |
) |
| 67 |
|
| 68 |
b3 = c_ast.BinaryOp(op="^", left=b2, right=b1) |
| 69 |
|
| 70 |
cv = self.ConstantVisitor() |
| 71 |
cv.visit(b3) |
| 72 |
|
| 73 |
self.assertEqual(cv.values, ["111", "6", "6"]) |
| 74 |
|
| 75 |
def tests_list_children(self): |
| 76 |
c1 = c_ast.Constant(type="float", value="5.6") |
| 77 |
c2 = c_ast.Constant(type="char", value="t") |
| 78 |
|
| 79 |
b1 = c_ast.BinaryOp(op="+", left=c1, right=c2) |
| 80 |
|
| 81 |
b2 = c_ast.BinaryOp(op="-", left=b1, right=c2) |
| 82 |
|
| 83 |
comp = c_ast.Compound(block_items=[b1, b2, c1, c2]) |
| 84 |
|
| 85 |
cv = self.ConstantVisitor() |
| 86 |
cv.visit(comp) |
| 87 |
|
| 88 |
self.assertEqual(cv.values, ["5.6", "t", "5.6", "t", "t", "5.6", "t"]) |
| 89 |
|
| 90 |
def test_repr(self): |
| 91 |
c1 = c_ast.Constant(type="float", value="5.6") |
| 92 |
c2 = c_ast.Constant(type="char", value="t") |
| 93 |
|
| 94 |
b1 = c_ast.BinaryOp(op="+", left=c1, right=c2) |
| 95 |
|
| 96 |
b2 = c_ast.BinaryOp(op="-", left=b1, right=c2) |
| 97 |
|
| 98 |
comp = c_ast.Compound(block_items=[b1, b2, c1, c2]) |
| 99 |
|
| 100 |
expected = ( |
| 101 |
"Compound(block_items=[BinaryOp(op='+',\n" |
| 102 |
" left=Constant(type='float',\n" |
| 103 |
" value='5.6'\n" |
| 104 |
" ),\n" |
| 105 |
" right=Constant(type='char',\n" |
| 106 |
" value='t'\n" |
| 107 |
" )\n" |
| 108 |
" ),\n" |
| 109 |
" BinaryOp(op='-',\n" |
| 110 |
" left=BinaryOp(op='+',\n" |
| 111 |
" left=Constant(type='float',\n" |
| 112 |
" value='5.6'\n" |
| 113 |
" ),\n" |
| 114 |
" right=Constant(type='char',\n" |
| 115 |
" value='t'\n" |
| 116 |
" )\n" |
| 117 |
" ),\n" |
| 118 |
" right=Constant(type='char',\n" |
| 119 |
" value='t'\n" |
| 120 |
" )\n" |
| 121 |
" ),\n" |
| 122 |
" Constant(type='float',\n" |
| 123 |
" value='5.6'\n" |
| 124 |
" ),\n" |
| 125 |
" Constant(type='char',\n" |
| 126 |
" value='t'\n" |
| 127 |
" )\n" |
| 128 |
" ]\n" |
| 129 |
" )" |
| 130 |
) |
| 131 |
|
| 132 |
self.assertEqual(repr(comp), expected) |
| 133 |
|
| 134 |
|
| 135 |
if __name__ == "__main__": |
| 136 |
unittest.main() |