Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 19 | from helpers import XProtoTestHelpers |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | |
| 21 | class XProtoParseTests(unittest.TestCase): |
| 22 | def test_global_options(self): |
| 23 | |
| 24 | xtarget = XProtoTestHelpers.write_tmp_target("{{ options }}") |
| 25 | |
| 26 | xproto = \ |
| 27 | """ |
| 28 | option kind = "vsg"; |
| 29 | option verbose_name = "vSG Service"; |
| 30 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 31 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 32 | args.inputs = xproto |
| 33 | args.target = xtarget |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 34 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | self.assertIn("vsg", output) |
| 36 | self.assertIn("vSG Service", output) |
| 37 | |
| 38 | def test_basic_proto(self): |
| 39 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto }}") |
| 40 | |
| 41 | xproto = \ |
| 42 | """ |
| 43 | message Person { |
| 44 | required string name = 1; |
| 45 | required int32 id = 2; // Unique ID number for this person. |
| 46 | optional string email = 3 [symphony = "da da da dum"]; |
| 47 | |
| 48 | enum PhoneType { |
| 49 | MOBILE = 0; |
| 50 | HOME = 1; |
| 51 | WORK = 2; |
| 52 | } |
| 53 | |
| 54 | required string number = 1; |
| 55 | optional PhoneType type = 2; |
| 56 | |
| 57 | repeated PhoneNumber phones = 4; |
| 58 | } |
| 59 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 60 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 61 | args.inputs = xproto |
| 62 | args.target = xtarget |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 63 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 64 | self.assertIn("PhoneNumber", output) |
| 65 | |
| 66 | def test_link_extensions(self): |
| 67 | |
| 68 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links }}") |
| 69 | xproto = \ |
| 70 | """ |
| 71 | message links { |
| 72 | required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False]; |
| 73 | } |
| 74 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 75 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 76 | args.inputs = xproto |
| 77 | args.target = xtarget |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 78 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 79 | self.assertIn("VRouterService", output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 80 | |
| 81 | def test_through_extensions(self): |
| 82 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links.0.through }}") |
| 83 | xproto = \ |
| 84 | """ |
| 85 | message links { |
| 86 | required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False]; |
| 87 | } |
| 88 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 89 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 90 | args.inputs = xproto |
| 91 | args.target = xtarget |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 92 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 93 | self.assertIn("ServiceProxy", output) |
| 94 | |
| 95 | def test_message_options(self): |
| 96 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.options.type }}") |
| 97 | xproto = \ |
| 98 | """ |
| 99 | message link { |
| 100 | option type = "e1000"; |
| 101 | } |
| 102 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 103 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 104 | args.inputs = xproto |
| 105 | args.target = xtarget |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 106 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 107 | self.assertIn("e1000", output) |
| 108 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 109 | def test_message_base(self): |
| 110 | xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.bases }}") |
| 111 | xproto = \ |
| 112 | """ |
| 113 | message base(Base) { |
| 114 | } |
| 115 | """ |
| 116 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 117 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 118 | args.inputs = xproto |
| 119 | args.target = xtarget |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 120 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 121 | self.assertIn("Base", output) |
| 122 | |
| 123 | if __name__ == '__main__': |
| 124 | unittest.main() |
| 125 | |
| 126 | |