blob: 88c737275ec1a5c51b53fd75f7b549f4a1414da4 [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
18from xosgenx.generator import XOSGenerator
19from helpers import FakeArgs, XProtoTestHelpers
20
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"""
31 args = FakeArgs()
32 args.inputs = xproto
33 args.target = xtarget
34 output = XOSGenerator.generate(args)
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"""
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"""
60 args = FakeArgs()
61 args.inputs = xproto
62 args.target = xtarget
63 output = XOSGenerator.generate(args)
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"""
71message links {
72 required manytoone vrouter_service->VRouterService: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("VRouterService", output)
80
81 pass
82
83 def test_through_extensions(self):
84 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links.0.through }}")
85 xproto = \
86"""
87message links {
88 required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False];
89}
90"""
91 args = FakeArgs()
92 args.inputs = xproto
93 args.target = xtarget
94 output = XOSGenerator.generate(args)
95 self.assertIn("ServiceProxy", output)
96
97 def test_message_options(self):
98 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.options.type }}")
99 xproto = \
100"""
101message link {
102 option type = "e1000";
103}
104"""
105 args = FakeArgs()
106 args.inputs = xproto
107 args.target = xtarget
108 output = XOSGenerator.generate(args)
109 self.assertIn("e1000", output)
110
111 pass
112
113 def test_message_base(self):
114 xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.bases }}")
115 xproto = \
116"""
117message base(Base) {
118}
119"""
120
121 args = FakeArgs()
122 args.inputs = xproto
123 args.target = xtarget
124 output = XOSGenerator.generate(args)
125 self.assertIn("Base", output)
126
127if __name__ == '__main__':
128 unittest.main()
129
130