4 from vppapigen import VPPAPI, Option, ParseError, Union
7 # - test parsing of options, typedefs, enums, defines, CRC
8 # - test JSON, C output
11 class TestVersion(unittest.TestCase):
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))
21 class TestUnion(unittest.TestCase):
33 r = self.parser.parse_string(test_string)
34 self.assertTrue(isinstance(r[0], Union))
36 def test_union_vla(self):
42 autoreply define foo {
43 vl_api_foo_union_vla_t v;
46 r = self.parser.parse_string(test_string)
47 self.assertTrue(isinstance(r[0], Union))
48 self.assertTrue(r[0].vla)
49 s = self.parser.process(r)
53 union foo_union_vla2 {
58 autoreply define foo2 {
59 vl_api_foo_union_vla2_t v;
62 self.assertRaises(ValueError, self.parser.parse_string, test_string2)
65 union foo_union_vla3 {
69 autoreply define foo3 {
70 vl_api_foo_union_vla3_t v;
74 self.assertRaises(ValueError, self.parser.parse_string, test_string3)
76 class TestTypedef(unittest.TestCase):
81 def test_duplicatetype(self):
83 typedef foo1 { u8 dummy; };
84 typedef foo1 { u8 dummy; };
86 self.assertRaises(KeyError, self.parser.parse_string, test_string)
89 class TestDefine(unittest.TestCase):
94 def test_unknowntype(self):
95 test_string = 'define foo { foobar foo;};'
96 with self.assertRaises(ParseError) as ctx:
97 self.parser.parse_string(test_string)
98 self.assertIn('Undefined type: foobar', str(ctx.exception))
100 test_string = 'define { u8 foo;};'
101 with self.assertRaises(ParseError) as ctx:
102 self.parser.parse_string(test_string)
104 def test_flags(self):
106 manual_print dont_trace manual_endian define foo { u8 foo; };
107 define foo_reply {u32 context; i32 retval; };
109 r = self.parser.parse_string(test_string)
110 self.assertIsNotNone(r)
111 s = self.parser.process(r)
112 self.assertIsNotNone(s)
113 for d in s['Define']:
115 self.assertTrue(d.dont_trace)
116 self.assertTrue(d.manual_endian)
117 self.assertTrue(d.manual_print)
118 self.assertFalse(d.autoreply)
121 nonexisting_flag define foo { u8 foo; };
123 with self.assertRaises(ParseError):
124 self.parser.parse_string(test_string)
127 class TestService(unittest.TestCase):
130 cls.parser = VPPAPI()
132 def test_service(self):
134 autoreply define show_version { u8 foo;};
135 service { rpc show_version returns show_version_reply; };
137 r = self.parser.parse_string(test_string)
138 s = self.parser.process(r)
139 self.assertEqual(s['Service'][0].caller, 'show_version')
140 self.assertEqual(s['Service'][0].reply, 'show_version_reply')
143 if __name__ == '__main__':
144 unittest.main(verbosity=2)