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