Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 16 | import unittest |
| 17 | import os |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 18 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 19 | from helpers import XProtoTestHelpers, OUTPUT_DIR |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | |
| 21 | TEST_FILE = "test_file" |
| 22 | |
| 23 | TEST_OUTPUT = "Do re mi fa so la ti do" |
| 24 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 25 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 26 | class XProtoTargetTests(unittest.TestCase): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 27 | def setUp(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 28 | test_file = open(os.path.join(OUTPUT_DIR, TEST_FILE), "w") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 29 | test_file.write(TEST_OUTPUT) |
| 30 | test_file.close() |
| 31 | |
| 32 | def test_file_methods(self): |
| 33 | target = XProtoTestHelpers.write_tmp_target( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 34 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | {%% if file_exists("%s") %%} |
| 36 | {{ include_file("%s") }} |
| 37 | {%% endif %%} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 38 | """ |
| 39 | % (TEST_FILE, TEST_FILE) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 42 | args = XOSProcessorArgs() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 43 | args.inputs = "" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 51 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 52 | {{ xproto_first_non_empty([None, None, None, None, None, None, "Eureka"]) }} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 53 | """ |
| 54 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 55 | args = XOSProcessorArgs() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 56 | args.inputs = "" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 57 | args.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 58 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 59 | self.assertIn("Eureka", output) |
| 60 | |
| 61 | def test_context(self): |
| 62 | target = XProtoTestHelpers.write_tmp_target( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 63 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 64 | {{ context.what }} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 65 | """ |
| 66 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 67 | args = XOSProcessorArgs() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 68 | args.inputs = "" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 69 | args.target = target |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 70 | args.kv = "what:what is what" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 71 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 72 | self.assertIn("what is what", output) |
| 73 | |
| 74 | def test_singularize(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 75 | proto = """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 76 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 88 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 89 | {% for m in proto.messages.0.fields -%} |
| 90 | {{ xproto_singularize(m) }}, |
| 91 | {%- endfor %} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 92 | """ |
| 93 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 94 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 95 | args.inputs = proto |
| 96 | args.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 97 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 98 | self.assertEqual( |
| 99 | "one,sheep,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(",") |
| 100 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 101 | |
| 102 | def test_pluralize(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 103 | proto = """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 104 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 109 | 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 116 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 117 | {% for m in proto.messages.0.fields -%} |
| 118 | {{ xproto_pluralize(m) }}, |
| 119 | {%- endfor %} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 120 | """ |
| 121 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 122 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 123 | args.inputs = proto |
| 124 | args.target = target |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 125 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 126 | self.assertEqual( |
| 127 | "data,sheep,slices,networks,omf_friendlies", |
| 128 | output.lstrip().rstrip().rstrip(","), |
| 129 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 130 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 131 | |
| 132 | if __name__ == "__main__": |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 133 | unittest.main() |