Paul Vinciguerra | 7e0c48e | 2019-02-01 19:37:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 2 | |
| 3 | import unittest |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 4 | from vppapigen import VPPAPI, Option, ParseError, Union |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 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 | |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 21 | class 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 Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 75 | |
| 76 | class TestTypedef(unittest.TestCase): |
| 77 | @classmethod |
| 78 | def setUpClass(cls): |
| 79 | cls.parser = VPPAPI() |
| 80 | |
| 81 | def test_duplicatetype(self): |
| 82 | test_string = ''' |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 83 | typedef foo1 { u8 dummy; }; |
| 84 | typedef foo1 { u8 dummy; }; |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 85 | ''' |
| 86 | self.assertRaises(KeyError, self.parser.parse_string, test_string) |
| 87 | |
| 88 | |
| 89 | class 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 Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 96 | with self.assertRaises(ParseError) as ctx: |
| 97 | self.parser.parse_string(test_string) |
| 98 | self.assertIn('Undefined type: foobar', str(ctx.exception)) |
| 99 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 100 | test_string = 'define { u8 foo;};' |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 101 | with self.assertRaises(ParseError) as ctx: |
| 102 | self.parser.parse_string(test_string) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 103 | |
| 104 | def test_flags(self): |
| 105 | test_string = ''' |
| 106 | manual_print dont_trace manual_endian define foo { u8 foo; }; |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 107 | define foo_reply {u32 context; i32 retval; }; |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 108 | ''' |
| 109 | r = self.parser.parse_string(test_string) |
| 110 | self.assertIsNotNone(r) |
| 111 | s = self.parser.process(r) |
| 112 | self.assertIsNotNone(s) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 113 | 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 Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 119 | |
| 120 | test_string = ''' |
| 121 | nonexisting_flag define foo { u8 foo; }; |
| 122 | ''' |
Paul Vinciguerra | 7e0c48e | 2019-02-01 19:37:45 -0800 | [diff] [blame] | 123 | with self.assertRaises(ParseError): |
| 124 | self.parser.parse_string(test_string) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 125 | |
| 126 | |
| 127 | class TestService(unittest.TestCase): |
| 128 | @classmethod |
| 129 | def setUpClass(cls): |
| 130 | cls.parser = VPPAPI() |
| 131 | |
| 132 | def test_service(self): |
| 133 | test_string = ''' |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 134 | autoreply define show_version { u8 foo;}; |
| 135 | service { rpc show_version returns show_version_reply; }; |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 136 | ''' |
| 137 | r = self.parser.parse_string(test_string) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 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') |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 141 | |
| 142 | |
| 143 | if __name__ == '__main__': |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 144 | unittest.main(verbosity=2) |