vppapigen: fix tests and run on verify
[vpp.git] / src / tools / vppapigen / test_vppapigen.py
1 #!/usr/bin/env python3
2
3 import unittest
4 from vppapigen import VPPAPI, Option, ParseError
5
6 # TODO
7 # - test parsing of options, typedefs, enums, defines, CRC
8 # - test JSON, C output
9
10
11 class TestVersion(unittest.TestCase):
12     @classmethod
13     def setUpClass(cls):
14         cls.parser = VPPAPI()
15
16     def test_version(self):
17         version_string = 'option version = "1.0.0";'
18         r = self.parser.parse_string(version_string)
19         self.assertTrue(isinstance(r[0], Option))
20
21
22 class TestTypedef(unittest.TestCase):
23     @classmethod
24     def setUpClass(cls):
25         cls.parser = VPPAPI()
26
27     def test_duplicatetype(self):
28         test_string = '''
29         typedef foo1 { u8 dummy; };
30         typedef foo1 { u8 dummy; };
31         '''
32         self.assertRaises(KeyError, self.parser.parse_string, test_string)
33
34
35 class TestDefine(unittest.TestCase):
36     @classmethod
37     def setUpClass(cls):
38         cls.parser = VPPAPI()
39
40     def test_unknowntype(self):
41         test_string = 'define foo { foobar foo;};'
42         with self.assertRaises(ParseError) as ctx:
43             self.parser.parse_string(test_string)
44         self.assertIn('Undefined type: foobar', str(ctx.exception))
45
46         test_string = 'define { u8 foo;};'
47         with self.assertRaises(ParseError) as ctx:
48             self.parser.parse_string(test_string)
49
50     def test_flags(self):
51         test_string = '''
52           manual_print dont_trace manual_endian define foo { u8 foo; };
53           define foo_reply {u32 context; i32 retval; };
54         '''
55         r = self.parser.parse_string(test_string)
56         self.assertIsNotNone(r)
57         s = self.parser.process(r)
58         self.assertIsNotNone(s)
59         for d in s['Define']:
60             if d.name == 'foo':
61                 self.assertTrue(d.dont_trace)
62                 self.assertTrue(d.manual_endian)
63                 self.assertTrue(d.manual_print)
64                 self.assertFalse(d.autoreply)
65
66         test_string = '''
67           nonexisting_flag define foo { u8 foo; };
68         '''
69         with self.assertRaises(ParseError):
70             self.parser.parse_string(test_string)
71
72
73 class TestService(unittest.TestCase):
74     @classmethod
75     def setUpClass(cls):
76         cls.parser = VPPAPI()
77
78     def test_service(self):
79         test_string = '''
80          autoreply define show_version { u8 foo;};
81          service { rpc show_version returns show_version_reply; };
82         '''
83         r = self.parser.parse_string(test_string)
84         s = self.parser.process(r)
85         self.assertEqual(s['Service'][0].caller, 'show_version')
86         self.assertEqual(s['Service'][0].reply, 'show_version_reply')
87
88
89 if __name__ == '__main__':
90     unittest.main(verbosity=2)