blob: b2cbdef9f9927c37ad2445fdb3a0b186e4f4a155 [file] [log] [blame]
Scott Bakera33ccb02018-01-26 13:03:28 -08001# 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 Williams9a42f872019-02-15 17:56:04 -070016from __future__ import absolute_import
Scott Bakera33ccb02018-01-26 13:03:28 -080017import unittest
18from xosgenx.jinja2_extensions.base import *
Scott Bakerbe2a5172019-04-10 18:02:50 -070019from jinja2.runtime import Undefined
Scott Bakera1b089a2018-10-05 09:59:17 -070020
21
22# Several of the base functions require a Field object.
23def _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 Bakera33ccb02018-01-26 13:03:28 -080032
Zack Williams045b63d2019-01-22 16:30:57 -070033
Scott Bakera33ccb02018-01-26 13:03:28 -080034class 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 Bakera1b089a2018-10-05 09:59:17 -070045 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 Williams045b63d2019-01-22 16:30:57 -070073 self.assertEqual(
74 xproto_singularize_pluralize(_field("omf_friendlies")), "omf_friendlies"
75 )
Scott Bakera1b089a2018-10-05 09:59:17 -070076 # 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 Williams045b63d2019-01-22 16:30:57 -070079 self.assertEqual(
80 xproto_singularize(_field("sheep", singular="turtle")), "turtle"
81 )
Scott Bakera1b089a2018-10-05 09:59:17 -070082
Scott Bakerbe2a5172019-04-10 18:02:50 -070083 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 Bakera1b089a2018-10-05 09:59:17 -0700101
Zack Williams045b63d2019-01-22 16:30:57 -0700102if __name__ == "__main__":
Scott Bakera33ccb02018-01-26 13:03:28 -0800103 unittest.main()