Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | typeonly define foo1 { u8 dummy; }; |
| 30 | typeonly define 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 | self.assertRaises(ParseError, self.parser.parse_string, test_string) |
| 43 | test_string = 'define { u8 foo;};' |
| 44 | self.assertRaises(ParseError, self.parser.parse_string, test_string) |
| 45 | |
| 46 | def test_flags(self): |
| 47 | test_string = ''' |
| 48 | manual_print dont_trace manual_endian define foo { u8 foo; }; |
| 49 | ''' |
| 50 | r = self.parser.parse_string(test_string) |
| 51 | self.assertIsNotNone(r) |
| 52 | s = self.parser.process(r) |
| 53 | self.assertIsNotNone(s) |
| 54 | for d in s['defines']: |
| 55 | self.assertTrue(d.dont_trace) |
| 56 | self.assertTrue(d.manual_endian) |
| 57 | self.assertTrue(d.manual_print) |
| 58 | self.assertFalse(d.autoreply) |
| 59 | |
| 60 | test_string = ''' |
| 61 | nonexisting_flag define foo { u8 foo; }; |
| 62 | ''' |
| 63 | self.assertRaises(ParseError, self.parser.parse_string, test_string) |
| 64 | |
| 65 | |
| 66 | class TestService(unittest.TestCase): |
| 67 | @classmethod |
| 68 | def setUpClass(cls): |
| 69 | cls.parser = VPPAPI() |
| 70 | |
| 71 | def test_service(self): |
| 72 | test_string = ''' |
| 73 | service foo { rpc foo (show_version) returns (show_version) }; |
| 74 | ''' |
| 75 | r = self.parser.parse_string(test_string) |
| 76 | print('R', r) |
| 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | unittest.main() |