Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | from xosgenx.generator import XOSGenerator |
| 3 | from helpers import FakeArgs, XProtoTestHelpers |
| 4 | |
| 5 | class XProtoParseTests(unittest.TestCase): |
| 6 | def test_global_options(self): |
| 7 | |
| 8 | xtarget = XProtoTestHelpers.write_tmp_target("{{ options }}") |
| 9 | |
| 10 | xproto = \ |
| 11 | """ |
| 12 | option kind = "vsg"; |
| 13 | option verbose_name = "vSG Service"; |
| 14 | """ |
| 15 | args = FakeArgs() |
| 16 | args.inputs = xproto |
| 17 | args.target = xtarget |
| 18 | output = XOSGenerator.generate(args) |
| 19 | self.assertIn("vsg", output) |
| 20 | self.assertIn("vSG Service", output) |
| 21 | |
| 22 | def test_basic_proto(self): |
| 23 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto }}") |
| 24 | |
| 25 | xproto = \ |
| 26 | """ |
| 27 | message Person { |
| 28 | required string name = 1; |
| 29 | required int32 id = 2; // Unique ID number for this person. |
| 30 | optional string email = 3 [symphony = "da da da dum"]; |
| 31 | |
| 32 | enum PhoneType { |
| 33 | MOBILE = 0; |
| 34 | HOME = 1; |
| 35 | WORK = 2; |
| 36 | } |
| 37 | |
| 38 | required string number = 1; |
| 39 | optional PhoneType type = 2; |
| 40 | |
| 41 | repeated PhoneNumber phones = 4; |
| 42 | } |
| 43 | """ |
| 44 | args = FakeArgs() |
| 45 | args.inputs = xproto |
| 46 | args.target = xtarget |
| 47 | output = XOSGenerator.generate(args) |
| 48 | self.assertIn("PhoneNumber", output) |
| 49 | |
| 50 | def test_link_extensions(self): |
| 51 | |
| 52 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links }}") |
| 53 | xproto = \ |
| 54 | """ |
| 55 | message links { |
| 56 | required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False]; |
| 57 | } |
| 58 | """ |
| 59 | args = FakeArgs() |
| 60 | args.inputs = xproto |
| 61 | args.target = xtarget |
| 62 | output = XOSGenerator.generate(args) |
| 63 | self.assertIn("VRouterService", output) |
| 64 | |
| 65 | pass |
| 66 | |
| 67 | def test_through_extensions(self): |
| 68 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links.0.through }}") |
| 69 | xproto = \ |
| 70 | """ |
| 71 | message links { |
| 72 | required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False]; |
| 73 | } |
| 74 | """ |
| 75 | args = FakeArgs() |
| 76 | args.inputs = xproto |
| 77 | args.target = xtarget |
| 78 | output = XOSGenerator.generate(args) |
| 79 | self.assertIn("ServiceProxy", output) |
| 80 | |
| 81 | def test_message_options(self): |
| 82 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.options.type }}") |
| 83 | xproto = \ |
| 84 | """ |
| 85 | message link { |
| 86 | option type = "e1000"; |
| 87 | } |
| 88 | """ |
| 89 | args = FakeArgs() |
| 90 | args.inputs = xproto |
| 91 | args.target = xtarget |
| 92 | output = XOSGenerator.generate(args) |
| 93 | self.assertIn("e1000", output) |
| 94 | |
| 95 | pass |
| 96 | |
| 97 | def test_message_base(self): |
| 98 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.bases }}") |
| 99 | xproto = \ |
| 100 | """ |
| 101 | message base(Base) { |
| 102 | } |
| 103 | """ |
| 104 | |
| 105 | args = FakeArgs() |
| 106 | args.inputs = xproto |
| 107 | args.target = xtarget |
| 108 | output = XOSGenerator.generate(args) |
| 109 | self.assertIn("Base", output) |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | unittest.main() |
| 113 | |
| 114 | |