Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame^] | 1 | import unittest |
| 2 | import os |
| 3 | from xosgenx.generator import XOSGenerator |
| 4 | from helpers import FakeArgs, XProtoTestHelpers, OUTPUT_DIR |
| 5 | |
| 6 | TEST_FILE = "test_file" |
| 7 | |
| 8 | TEST_OUTPUT = "Do re mi fa so la ti do" |
| 9 | |
| 10 | class XProtoTargetTests(unittest.TestCase): |
| 11 | |
| 12 | def setUp(self): |
| 13 | test_file = open(os.path.join(OUTPUT_DIR, TEST_FILE), 'w') |
| 14 | test_file.write(TEST_OUTPUT) |
| 15 | test_file.close() |
| 16 | |
| 17 | def test_file_methods(self): |
| 18 | target = XProtoTestHelpers.write_tmp_target( |
| 19 | """ |
| 20 | {%% if file_exists("%s") %%} |
| 21 | {{ include_file("%s") }} |
| 22 | {%% endif %%} |
| 23 | """%(TEST_FILE, TEST_FILE) |
| 24 | ) |
| 25 | |
| 26 | args = FakeArgs() |
| 27 | args.inputs = '' |
| 28 | args.target = target |
| 29 | args.attic = OUTPUT_DIR |
| 30 | output = XOSGenerator.generate(args) |
| 31 | self.assertIn(TEST_OUTPUT, output) |
| 32 | |
| 33 | def test_xproto_lib(self): |
| 34 | target = XProtoTestHelpers.write_tmp_target( |
| 35 | """ |
| 36 | {{ xproto_first_non_empty([None, None, None, None, None, None, "Eureka"]) }} |
| 37 | """) |
| 38 | args = FakeArgs() |
| 39 | args.inputs = '' |
| 40 | args.target = target |
| 41 | output = XOSGenerator.generate(args) |
| 42 | self.assertIn("Eureka", output) |
| 43 | |
| 44 | def test_context(self): |
| 45 | target = XProtoTestHelpers.write_tmp_target( |
| 46 | """ |
| 47 | {{ context.what }} |
| 48 | """) |
| 49 | args = FakeArgs() |
| 50 | args.inputs = '' |
| 51 | args.target = target |
| 52 | args.kv='what:what is what' |
| 53 | output = XOSGenerator.generate(args) |
| 54 | self.assertIn("what is what", output) |
| 55 | |
| 56 | def test_singularize(self): |
| 57 | proto = \ |
| 58 | """ |
| 59 | message TestSingularize { |
| 60 | // The following field has an explicitly specified singular |
| 61 | required int many = 1 [singular = "one"]; |
| 62 | // The following fields have automatically computed singulars |
| 63 | required int sheep = 2; |
| 64 | required int radii = 2; |
| 65 | required int slices = 2; |
| 66 | required int networks = 2; |
| 67 | required int omf_friendlies = 2; |
| 68 | } |
| 69 | """ |
| 70 | |
| 71 | target = XProtoTestHelpers.write_tmp_target( |
| 72 | """ |
| 73 | {% for m in proto.messages.0.fields -%} |
| 74 | {{ xproto_singularize(m) }}, |
| 75 | {%- endfor %} |
| 76 | """) |
| 77 | args = FakeArgs() |
| 78 | args.inputs = proto |
| 79 | args.target = target |
| 80 | output = XOSGenerator.generate(args) |
| 81 | self.assertEqual("one,sheep,radius,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(',')) |
| 82 | |
| 83 | def test_pluralize(self): |
| 84 | proto = \ |
| 85 | """ |
| 86 | message TestPluralize { |
| 87 | // The following field has an explicitly specified plural |
| 88 | required int anecdote = 1 [plural = "data"]; |
| 89 | // The following fields have automatically computed plurals |
| 90 | required int sheep = 2; |
| 91 | required int radius = 2; |
| 92 | required int slice = 2; |
| 93 | required int network = 2; |
| 94 | required int omf_friendly = 2; |
| 95 | } |
| 96 | """ |
| 97 | |
| 98 | target = XProtoTestHelpers.write_tmp_target( |
| 99 | """ |
| 100 | {% for m in proto.messages.0.fields -%} |
| 101 | {{ xproto_pluralize(m) }}, |
| 102 | {%- endfor %} |
| 103 | """) |
| 104 | args = FakeArgs() |
| 105 | args.inputs = proto |
| 106 | args.target = target |
| 107 | output = XOSGenerator.generate(args) |
| 108 | self.assertEqual("data,sheep,radii,slices,networks,omf_friendlies", output.lstrip().rstrip().rstrip(',')) |
| 109 | |
| 110 | if __name__ == '__main__': |
| 111 | unittest.main() |
| 112 | |
| 113 | |