blob: d7edcb77f87aa160b63aefe3eb78172418f242bf [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo67654fa2017-06-09 09:33:17 -070017import unittest
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
19from helpers import XProtoTestHelpers
Matteo Scandolo67654fa2017-06-09 09:33:17 -070020
21class 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 Baker1f7791d2018-10-04 13:21:20 -070031 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070032 args.inputs = xproto
33 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080034 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070035 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"""
43message 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 Baker1f7791d2018-10-04 13:21:20 -070060 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070061 args.inputs = xproto
62 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080063 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064 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"""
71message links {
72 required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
73}
74"""
Scott Baker1f7791d2018-10-04 13:21:20 -070075 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070076 args.inputs = xproto
77 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080078 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070079 self.assertIn("VRouterService", output)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070080
81 def test_through_extensions(self):
82 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links.0.through }}")
83 xproto = \
84"""
85message links {
86 required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False];
87}
88"""
Scott Baker1f7791d2018-10-04 13:21:20 -070089 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070090 args.inputs = xproto
91 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080092 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070093 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"""
99message link {
100 option type = "e1000";
101}
102"""
Scott Baker1f7791d2018-10-04 13:21:20 -0700103 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700104 args.inputs = xproto
105 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800106 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700107 self.assertIn("e1000", output)
108
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700109 def test_message_base(self):
110 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.bases }}")
111 xproto = \
112"""
113message base(Base) {
114}
115"""
116
Scott Baker1f7791d2018-10-04 13:21:20 -0700117 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700118 args.inputs = xproto
119 args.target = xtarget
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800120 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700121 self.assertIn("Base", output)
122
123if __name__ == '__main__':
124 unittest.main()
125
126