Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [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 | |
| 16 | import unittest |
| 17 | from xosgenx.jinja2_extensions.base import * |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | # Several of the base functions require a Field object. |
| 21 | def _field(name, singular=None, plural=None): |
| 22 | f = {} |
| 23 | f["name"] = name |
| 24 | f["options"] = {} |
| 25 | if singular: |
| 26 | f["options"]["singular"] = singular |
| 27 | if plural: |
| 28 | f["options"]["plural"] = plural |
| 29 | return f |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 30 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 32 | class Jinja2BaseTests(unittest.TestCase): |
| 33 | def test_xproto_is_true(self): |
| 34 | self.assertTrue(xproto_is_true(True)) |
| 35 | self.assertTrue(xproto_is_true("True")) |
| 36 | self.assertTrue(xproto_is_true('"True"')) |
| 37 | self.assertFalse(xproto_is_true(False)) |
| 38 | self.assertFalse(xproto_is_true("False")) |
| 39 | self.assertFalse(xproto_is_true('"False"')) |
| 40 | self.assertFalse(xproto_is_true(None)) |
| 41 | self.assertFalse(xproto_is_true("something else")) |
| 42 | |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 43 | def test_unquote(self): |
| 44 | self.assertEqual(xproto_unquote("foo"), "foo") |
| 45 | self.assertEqual(xproto_unquote('"foo"'), "foo") |
| 46 | |
| 47 | def test_pluralize(self): |
| 48 | self.assertEqual(xproto_pluralize(_field("sheep")), "sheep") |
| 49 | self.assertEqual(xproto_pluralize(_field("slice")), "slices") |
| 50 | self.assertEqual(xproto_pluralize(_field("network")), "networks") |
| 51 | self.assertEqual(xproto_pluralize(_field("omf_friendly")), "omf_friendlies") |
| 52 | # invalid words, should usually return <word>-es |
| 53 | self.assertEqual(xproto_pluralize(_field("xxx")), "xxxes") |
| 54 | # if a field option is set, use that |
| 55 | self.assertEqual(xproto_pluralize(_field("sheep", plural="turtles")), "turtles") |
| 56 | |
| 57 | def test_singularize(self): |
| 58 | self.assertEqual(xproto_singularize(_field("sheep")), "sheep") |
| 59 | self.assertEqual(xproto_singularize(_field("slices")), "slice") |
| 60 | self.assertEqual(xproto_singularize(_field("networks")), "network") |
| 61 | self.assertEqual(xproto_singularize(_field("omf_friendlies")), "omf_friendly") |
| 62 | # invalid words, return the original word |
| 63 | self.assertEqual(xproto_singularize(_field("xxx")), "xxx") |
| 64 | # if a field option is set, use that |
| 65 | self.assertEqual(xproto_pluralize(_field("sheep", plural="turtles")), "turtles") |
| 66 | |
| 67 | def test_singularize_pluralize(self): |
| 68 | self.assertEqual(xproto_singularize_pluralize(_field("sheep")), "sheep") |
| 69 | self.assertEqual(xproto_singularize_pluralize(_field("slices")), "slices") |
| 70 | self.assertEqual(xproto_singularize_pluralize(_field("networks")), "networks") |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 71 | self.assertEqual( |
| 72 | xproto_singularize_pluralize(_field("omf_friendlies")), "omf_friendlies" |
| 73 | ) |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 74 | # invalid words, should usually return <word>-es |
| 75 | self.assertEqual(xproto_singularize_pluralize(_field("xxx")), "xxxes") |
| 76 | # if a field option is set, use that |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 77 | self.assertEqual( |
| 78 | xproto_singularize(_field("sheep", singular="turtle")), "turtle" |
| 79 | ) |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 80 | |
| 81 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | if __name__ == "__main__": |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 83 | unittest.main() |