blob: b45f5eddf7f1c71ce9a8d5c952413a0db3756b1c [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
19from xosgenx.generator import XOSGenerator
20from helpers import FakeArgs, XProtoTestHelpers, OUTPUT_DIR
21
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
42 args = FakeArgs()
43 args.inputs = ''
44 args.target = target
45 args.attic = OUTPUT_DIR
46 output = XOSGenerator.generate(args)
47 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""")
54 args = FakeArgs()
55 args.inputs = ''
56 args.target = target
57 output = XOSGenerator.generate(args)
58 self.assertIn("Eureka", output)
59
60 def test_context(self):
61 target = XProtoTestHelpers.write_tmp_target(
62"""
63 {{ context.what }}
64""")
65 args = FakeArgs()
66 args.inputs = ''
67 args.target = target
68 args.kv='what:what is what'
69 output = XOSGenerator.generate(args)
70 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;
80 required int radii = 2;
81 required int slices = 2;
82 required int networks = 2;
83 required int omf_friendlies = 2;
84 }
85"""
86
87 target = XProtoTestHelpers.write_tmp_target(
88"""
89{% for m in proto.messages.0.fields -%}
90{{ xproto_singularize(m) }},
91{%- endfor %}
92""")
93 args = FakeArgs()
94 args.inputs = proto
95 args.target = target
96 output = XOSGenerator.generate(args)
97 self.assertEqual("one,sheep,radius,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(','))
98
99 def test_pluralize(self):
100 proto = \
101"""
102 message TestPluralize {
103 // The following field has an explicitly specified plural
104 required int anecdote = 1 [plural = "data"];
105 // The following fields have automatically computed plurals
106 required int sheep = 2;
107 required int radius = 2;
108 required int slice = 2;
109 required int network = 2;
110 required int omf_friendly = 2;
111 }
112"""
113
114 target = XProtoTestHelpers.write_tmp_target(
115"""
116{% for m in proto.messages.0.fields -%}
117{{ xproto_pluralize(m) }},
118{%- endfor %}
119""")
120 args = FakeArgs()
121 args.inputs = proto
122 args.target = target
123 output = XOSGenerator.generate(args)
124 self.assertEqual("data,sheep,radii,slices,networks,omf_friendlies", output.lstrip().rstrip().rstrip(','))
125
126if __name__ == '__main__':
127 unittest.main()
128
129