Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | import os |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 19 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 20 | from helpers import XProtoTestHelpers, OUTPUT_DIR |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 21 | |
| 22 | TEST_FILE = "test_file" |
| 23 | |
| 24 | TEST_OUTPUT = "Do re mi fa so la ti do" |
| 25 | |
| 26 | class 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 42 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 43 | args.inputs = '' |
| 44 | args.target = target |
| 45 | args.attic = OUTPUT_DIR |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 46 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | """) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 54 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 55 | args.inputs = '' |
| 56 | args.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 57 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 58 | self.assertIn("Eureka", output) |
| 59 | |
| 60 | def test_context(self): |
| 61 | target = XProtoTestHelpers.write_tmp_target( |
| 62 | """ |
| 63 | {{ context.what }} |
| 64 | """) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 65 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 66 | args.inputs = '' |
| 67 | args.target = target |
| 68 | args.kv='what:what is what' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 69 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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; |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 80 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 92 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 93 | args.inputs = proto |
| 94 | args.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 95 | output = XOSProcessor.process(args) |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 96 | self.assertEqual("one,sheep,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(',')) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 97 | |
| 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 106 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 118 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 119 | args.inputs = proto |
| 120 | args.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 121 | output = XOSProcessor.process(args) |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 122 | self.assertEqual("data,sheep,slices,networks,omf_friendlies", output.lstrip().rstrip().rstrip(',')) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 123 | |
| 124 | if __name__ == '__main__': |
| 125 | unittest.main() |
| 126 | |
| 127 | |