blob: c46801807cb0d46fcba10468b57ac22b2bbaa188 [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
15
Matteo Scandolo67654fa2017-06-09 09:33:17 -070016import unittest
17import os
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
19from helpers import XProtoTestHelpers, OUTPUT_DIR
Matteo Scandolo67654fa2017-06-09 09:33:17 -070020
21TEST_FILE = "test_file"
22
23TEST_OUTPUT = "Do re mi fa so la ti do"
24
Matteo Scandolo67654fa2017-06-09 09:33:17 -070025
Zack Williams045b63d2019-01-22 16:30:57 -070026class XProtoTargetTests(unittest.TestCase):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070027 def setUp(self):
Zack Williams045b63d2019-01-22 16:30:57 -070028 test_file = open(os.path.join(OUTPUT_DIR, TEST_FILE), "w")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070029 test_file.write(TEST_OUTPUT)
30 test_file.close()
31
32 def test_file_methods(self):
33 target = XProtoTestHelpers.write_tmp_target(
Zack Williams045b63d2019-01-22 16:30:57 -070034 """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070035 {%% if file_exists("%s") %%}
36 {{ include_file("%s") }}
37 {%% endif %%}
Zack Williams045b63d2019-01-22 16:30:57 -070038"""
39 % (TEST_FILE, TEST_FILE)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070040 )
41
Scott Baker1f7791d2018-10-04 13:21:20 -070042 args = XOSProcessorArgs()
Zack Williams045b63d2019-01-22 16:30:57 -070043 args.inputs = ""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070044 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(
Zack Williams045b63d2019-01-22 16:30:57 -070051 """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070052 {{ xproto_first_non_empty([None, None, None, None, None, None, "Eureka"]) }}
Zack Williams045b63d2019-01-22 16:30:57 -070053"""
54 )
Scott Baker1f7791d2018-10-04 13:21:20 -070055 args = XOSProcessorArgs()
Zack Williams045b63d2019-01-22 16:30:57 -070056 args.inputs = ""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070057 args.target = target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080058 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070059 self.assertIn("Eureka", output)
60
61 def test_context(self):
62 target = XProtoTestHelpers.write_tmp_target(
Zack Williams045b63d2019-01-22 16:30:57 -070063 """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064 {{ context.what }}
Zack Williams045b63d2019-01-22 16:30:57 -070065"""
66 )
Scott Baker1f7791d2018-10-04 13:21:20 -070067 args = XOSProcessorArgs()
Zack Williams045b63d2019-01-22 16:30:57 -070068 args.inputs = ""
Matteo Scandolo67654fa2017-06-09 09:33:17 -070069 args.target = target
Zack Williams045b63d2019-01-22 16:30:57 -070070 args.kv = "what:what is what"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080071 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070072 self.assertIn("what is what", output)
73
74 def test_singularize(self):
Zack Williams045b63d2019-01-22 16:30:57 -070075 proto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070076 message TestSingularize {
77 // The following field has an explicitly specified singular
78 required int many = 1 [singular = "one"];
79 // The following fields have automatically computed singulars
80 required int sheep = 2;
Matteo Scandolo67654fa2017-06-09 09:33:17 -070081 required int slices = 2;
82 required int networks = 2;
83 required int omf_friendlies = 2;
84 }
85"""
86
87 target = XProtoTestHelpers.write_tmp_target(
Zack Williams045b63d2019-01-22 16:30:57 -070088 """
Matteo Scandolo67654fa2017-06-09 09:33:17 -070089{% for m in proto.messages.0.fields -%}
90{{ xproto_singularize(m) }},
91{%- endfor %}
Zack Williams045b63d2019-01-22 16:30:57 -070092"""
93 )
Scott Baker1f7791d2018-10-04 13:21:20 -070094 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -070095 args.inputs = proto
96 args.target = target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080097 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -070098 self.assertEqual(
99 "one,sheep,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(",")
100 )
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700101
102 def test_pluralize(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700103 proto = """
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700104 message TestPluralize {
105 // The following field has an explicitly specified plural
106 required int anecdote = 1 [plural = "data"];
107 // The following fields have automatically computed plurals
108 required int sheep = 2;
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700109 required int slice = 2;
110 required int network = 2;
111 required int omf_friendly = 2;
112 }
113"""
114
115 target = XProtoTestHelpers.write_tmp_target(
Zack Williams045b63d2019-01-22 16:30:57 -0700116 """
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700117{% for m in proto.messages.0.fields -%}
118{{ xproto_pluralize(m) }},
119{%- endfor %}
Zack Williams045b63d2019-01-22 16:30:57 -0700120"""
121 )
Scott Baker1f7791d2018-10-04 13:21:20 -0700122 args = XOSProcessorArgs()
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700123 args.inputs = proto
124 args.target = target
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800125 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -0700126 self.assertEqual(
127 "data,sheep,slices,networks,omf_friendlies",
128 output.lstrip().rstrip().rstrip(","),
129 )
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700130
Zack Williams045b63d2019-01-22 16:30:57 -0700131
132if __name__ == "__main__":
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700133 unittest.main()