aboutsummaryrefslogtreecommitdiff
path: root/tests/test_declarations.py
blob: 9eaf488e555d47fd83af48af3766e2a753f4d594 (plain)
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
import unittest

from crowbar_reference_compiler import compile_to_ssa, load_declarations, parse_header, parse_implementation, scan
from crowbar_reference_compiler.declarations import ArrayType, BasicType, EnumDeclaration, PointerType, \
    StructDeclaration, UnionDeclaration, VariableDeclaration


class TestDeclarationLoading(unittest.TestCase):
    def test_kitchen_sink(self):
        code = r"""
struct normal {
    bool fake;
    (uint8[3])* data;
}

opaque struct ope;

enum sample {
    Testing,
}

union robust {
    enum sample tag;
    
    switch (tag) {
        case Testing: bool testPassed;
    }
}

fragile union not_robust {
    int8 sample;
    bool nope;
}
"""
        tokens = scan(code)
        parse_tree = parse_header(tokens)
        decls = load_declarations(parse_tree, [])
        normal = StructDeclaration('normal', [
            VariableDeclaration('fake', BasicType('bool'), None),
            VariableDeclaration('data', PointerType(ArrayType(BasicType('uint8'), 3)), None),
        ])
        ope = StructDeclaration('ope', None)
        sample = EnumDeclaration('sample', [('Testing', None)])
        robust = UnionDeclaration('robust', VariableDeclaration('tag', BasicType('enum sample'), None),
                                  [('Testing', VariableDeclaration('testPassed', BasicType('bool'), None))])
        not_robust = UnionDeclaration('not_robust', None,
                                      [VariableDeclaration('sample', BasicType('int8'), None),
                                       VariableDeclaration('nope', BasicType('bool'), None)])
        self.assertListEqual(decls, [normal, ope, sample, robust, not_robust])


if __name__ == '__main__':
    unittest.main()