blob: d729be7f6a3e792908f677646702389c484d6e7e [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
18import os
Scott Baker1f7791d2018-10-04 13:21:20 -070019from xosgenx.generator import XOSProcessor, XOSProcessorArgs
20from helpers import XProtoTestHelpers, OUTPUT_DIR
Matteo Scandolo67654fa2017-06-09 09:33:17 -070021
22TEST_FILE = "test_file"
23
24TEST_OUTPUT = "Do re mi fa so la ti do"
25
26class XProtoTargetTests(unittest.TestCase):
27
28 def setUp(self):
29 test_file = open(os.path.join(OUTPUT_DIR, TEST_FILE), 'w')
30 test_file.write(TEST_OUTPUT)
31 test_file.close()
32
33 def test_file_methods(self):
34 target = XProtoTestHelpers.write_tmp_target(
35"""
36 {%% if file_exists("%s") %%}
37 {{ include_file("%s") }}
38 {%% endif %%}
39"""%(TEST_FILE, TEST_FILE)
40 )
41
Scott Baker1f7791d2018-10-04 13:21:20 -070042 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070043 args.inputs = ''
44 args.target = target
45 args.attic = OUTPUT_DIR
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080046 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070047 self.assertIn(TEST_OUTPUT, output)
48
49 def test_xproto_lib(self):
50 target = XProtoTestHelpers.write_tmp_target(
51"""
52 {{ xproto_first_non_empty([None, None, None, None, None, None, "Eureka"]) }}
53""")
Scott Baker1f7791d2018-10-04 13:21:20 -070054 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070055 args.inputs = ''
56 args.target = target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080057 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070058 self.assertIn("Eureka", output)
59
60 def test_context(self):
61 target = XProtoTestHelpers.write_tmp_target(
62"""
63 {{ context.what }}
64""")
Scott Baker1f7791d2018-10-04 13:21:20 -070065 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070066 args.inputs = ''
67 args.target = target
68 args.kv='what:what is what'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080069 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070070 self.assertIn("what is what", output)
71
72 def test_singularize(self):
73 proto = \
74"""
75 message TestSingularize {
76 // The following field has an explicitly specified singular
77 required int many = 1 [singular = "one"];
78 // The following fields have automatically computed singulars
79 required int sheep = 2;
Matteo Scandolo67654fa2017-06-09 09:33:17 -070080 required int slices = 2;
81 required int networks = 2;
82 required int omf_friendlies = 2;
83 }
84"""
85
86 target = XProtoTestHelpers.write_tmp_target(
87"""
88{% for m in proto.messages.0.fields -%}
89{{ xproto_singularize(m) }},
90{%- endfor %}
91""")
Scott Baker1f7791d2018-10-04 13:21:20 -070092 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070093 args.inputs = proto
94 args.target = target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080095 output = XOSProcessor.process(args)
Scott Bakera1b089a2018-10-05 09:59:17 -070096 self.assertEqual("one,sheep,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(','))
Matteo Scandolo67654fa2017-06-09 09:33:17 -070097
98 def test_pluralize(self):
99 proto = \
100"""
101 message TestPluralize {
102 // The following field has an explicitly specified plural
103 required int anecdote = 1 [plural = "data"];
104 // The following fields have automatically computed plurals
105 required int sheep = 2;
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700106 required int slice = 2;
107 required int network = 2;
108 required int omf_friendly = 2;
109 }
110"""
111
112 target = XProtoTestHelpers.write_tmp_target(
113"""
114{% for m in proto.messages.0.fields -%}
115{{ xproto_pluralize(m) }},
116{%- endfor %}
117""")
Scott Baker1f7791d2018-10-04 13:21:20 -0700118 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700119 args.inputs = proto
120 args.target = target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800121 output = XOSProcessor.process(args)
Scott Bakera1b089a2018-10-05 09:59:17 -0700122 self.assertEqual("data,sheep,slices,networks,omf_friendlies", output.lstrip().rstrip().rstrip(','))
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700123
124if __name__ == '__main__':
125 unittest.main()
126
127