blob: cff2400ece3d412228c38a98c3c9ed608f7a530e [file] [log] [blame]
Paul Vinciguerra7e0c48e2019-02-01 19:37:45 -08001#!/usr/bin/env python3
Ole Troan9d420872017-10-12 13:06:35 +02002
3import unittest
Ole Troand5a78a52019-09-18 12:12:47 +02004from vppapigen import VPPAPI, Option, ParseError, Union
Ole Troan9d420872017-10-12 13:06:35 +02005
6# TODO
7# - test parsing of options, typedefs, enums, defines, CRC
8# - test JSON, C output
9
10
11class 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
Ole Troand5a78a52019-09-18 12:12:47 +020021class TestUnion(unittest.TestCase):
22 @classmethod
23 def setUpClass(cls):
24 cls.parser = VPPAPI()
25
26 def test_union(self):
27 test_string = '''
28 union foo_union {
29 u32 a;
30 u8 b;
31 };
32 '''
33 r = self.parser.parse_string(test_string)
34 self.assertTrue(isinstance(r[0], Union))
35
36 def test_union_vla(self):
37 test_string = '''
38 union foo_union_vla {
39 u32 a;
40 u8 b[a];
41 };
42 autoreply define foo {
43 vl_api_foo_union_vla_t v;
44 };
45 '''
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)
50
51
52 test_string2 = '''
53 union foo_union_vla2 {
54 u32 a;
55 u8 b[a];
56 u32 c;
57 };
58 autoreply define foo2 {
59 vl_api_foo_union_vla2_t v;
60 };
61 '''
62 self.assertRaises(ValueError, self.parser.parse_string, test_string2)
63
64 test_string3 = '''
65 union foo_union_vla3 {
66 u32 a;
67 u8 b[a];
68 };
69 autoreply define foo3 {
70 vl_api_foo_union_vla3_t v;
71 u32 x;
72 };
73 '''
74 self.assertRaises(ValueError, self.parser.parse_string, test_string3)
Ole Troan9d420872017-10-12 13:06:35 +020075
76class TestTypedef(unittest.TestCase):
77 @classmethod
78 def setUpClass(cls):
79 cls.parser = VPPAPI()
80
81 def test_duplicatetype(self):
82 test_string = '''
Paul Vinciguerra4bf84902019-07-31 00:34:05 -040083 typedef foo1 { u8 dummy; };
84 typedef foo1 { u8 dummy; };
Ole Troan9d420872017-10-12 13:06:35 +020085 '''
86 self.assertRaises(KeyError, self.parser.parse_string, test_string)
87
88
89class TestDefine(unittest.TestCase):
90 @classmethod
91 def setUpClass(cls):
92 cls.parser = VPPAPI()
93
94 def test_unknowntype(self):
95 test_string = 'define foo { foobar foo;};'
Paul Vinciguerra4bf84902019-07-31 00:34:05 -040096 with self.assertRaises(ParseError) as ctx:
97 self.parser.parse_string(test_string)
98 self.assertIn('Undefined type: foobar', str(ctx.exception))
99
Ole Troan9d420872017-10-12 13:06:35 +0200100 test_string = 'define { u8 foo;};'
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400101 with self.assertRaises(ParseError) as ctx:
102 self.parser.parse_string(test_string)
Ole Troan9d420872017-10-12 13:06:35 +0200103
104 def test_flags(self):
105 test_string = '''
106 manual_print dont_trace manual_endian define foo { u8 foo; };
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400107 define foo_reply {u32 context; i32 retval; };
Ole Troan9d420872017-10-12 13:06:35 +0200108 '''
109 r = self.parser.parse_string(test_string)
110 self.assertIsNotNone(r)
111 s = self.parser.process(r)
112 self.assertIsNotNone(s)
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400113 for d in s['Define']:
114 if d.name == 'foo':
115 self.assertTrue(d.dont_trace)
116 self.assertTrue(d.manual_endian)
117 self.assertTrue(d.manual_print)
118 self.assertFalse(d.autoreply)
Ole Troan9d420872017-10-12 13:06:35 +0200119
120 test_string = '''
121 nonexisting_flag define foo { u8 foo; };
122 '''
Paul Vinciguerra7e0c48e2019-02-01 19:37:45 -0800123 with self.assertRaises(ParseError):
124 self.parser.parse_string(test_string)
Ole Troan9d420872017-10-12 13:06:35 +0200125
126
127class TestService(unittest.TestCase):
128 @classmethod
129 def setUpClass(cls):
130 cls.parser = VPPAPI()
131
132 def test_service(self):
133 test_string = '''
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400134 autoreply define show_version { u8 foo;};
135 service { rpc show_version returns show_version_reply; };
Ole Troan9d420872017-10-12 13:06:35 +0200136 '''
137 r = self.parser.parse_string(test_string)
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400138 s = self.parser.process(r)
139 self.assertEqual(s['Service'][0].caller, 'show_version')
140 self.assertEqual(s['Service'][0].reply, 'show_version_reply')
Ole Troan9d420872017-10-12 13:06:35 +0200141
142
143if __name__ == '__main__':
Paul Vinciguerra4bf84902019-07-31 00:34:05 -0400144 unittest.main(verbosity=2)