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