blob: ade625aed3c36feb547ad8bdd66fed98cbbc649b [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# 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
Zack Williams9a42f872019-02-15 17:56:04 -070015from __future__ import absolute_import
Matteo Scandolo67654fa2017-06-09 09:33:17 -070016import unittest
Scott Baker1f7791d2018-10-04 13:21:20 -070017from xosgenx.generator import XOSProcessor, XOSProcessorArgs
18from helpers import XProtoTestHelpers
Matteo Scandolo67654fa2017-06-09 09:33:17 -070019
Zack Williams045b63d2019-01-22 16:30:57 -070020
Matteo Scandolo67654fa2017-06-09 09:33:17 -070021class XProtoParseTests(unittest.TestCase):
22 def test_global_options(self):
23
24 xtarget = XProtoTestHelpers.write_tmp_target("{{ options }}")
25
Zack Williams045b63d2019-01-22 16:30:57 -070026 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070027 option kind = "vsg";
28 option verbose_name = "vSG Service";
29"""
Scott Baker1f7791d2018-10-04 13:21:20 -070030 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070031 args.inputs = xproto
32 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080033 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070034 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 Williams045b63d2019-01-22 16:30:57 -070040 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070041message 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 Baker1f7791d2018-10-04 13:21:20 -070058 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070059 args.inputs = xproto
60 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080061 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070062 self.assertIn("PhoneNumber", output)
63
64 def test_link_extensions(self):
65
66 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links }}")
Zack Williams045b63d2019-01-22 16:30:57 -070067 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070068message links {
69 required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
70}
71"""
Scott Baker1f7791d2018-10-04 13:21:20 -070072 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070073 args.inputs = xproto
74 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080075 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070076 self.assertIn("VRouterService", output)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070077
78 def test_through_extensions(self):
Zack Williams045b63d2019-01-22 16:30:57 -070079 xtarget = XProtoTestHelpers.write_tmp_target(
80 "{{ proto.messages.0.links.0.through }}"
81 )
82 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070083message links {
84 required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False];
85}
86"""
Scott Baker1f7791d2018-10-04 13:21:20 -070087 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070088 args.inputs = xproto
89 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080090 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070091 self.assertIn("ServiceProxy", output)
92
93 def test_message_options(self):
Zack Williams045b63d2019-01-22 16:30:57 -070094 xtarget = XProtoTestHelpers.write_tmp_target(
95 "{{ proto.messages.0.options.type }}"
96 )
97 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070098message link {
99 option type = "e1000";
100}
101"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700102 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700103 args.inputs = xproto
104 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800105 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700106 self.assertIn("e1000", output)
107
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700108 def test_message_base(self):
109 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.bases }}")
Zack Williams045b63d2019-01-22 16:30:57 -0700110 xproto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700111message base(Base) {
112}
113"""
114
Scott Baker1f7791d2018-10-04 13:21:20 -0700115 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700116 args.inputs = xproto
117 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800118 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700119 self.assertIn("Base", output)
120
Zack Williams045b63d2019-01-22 16:30:57 -0700121
122if __name__ == "__main__":
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700123 unittest.main()