blob: 859d6409c1c15004f340c8267619ce4f85ef0ae0 [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
16import unittest
17from xosgenx.jinja2_extensions.base import *
Scott Bakera1b089a2018-10-05 09:59:17 -070018
19
20# Several of the base functions require a Field object.
21def _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 Bakera33ccb02018-01-26 13:03:28 -080030
Zack Williams045b63d2019-01-22 16:30:57 -070031
Scott Bakera33ccb02018-01-26 13:03:28 -080032class 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 Bakera1b089a2018-10-05 09:59:17 -070043 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 Williams045b63d2019-01-22 16:30:57 -070071 self.assertEqual(
72 xproto_singularize_pluralize(_field("omf_friendlies")), "omf_friendlies"
73 )
Scott Bakera1b089a2018-10-05 09:59:17 -070074 # 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 Williams045b63d2019-01-22 16:30:57 -070077 self.assertEqual(
78 xproto_singularize(_field("sheep", singular="turtle")), "turtle"
79 )
Scott Bakera1b089a2018-10-05 09:59:17 -070080
81
Zack Williams045b63d2019-01-22 16:30:57 -070082if __name__ == "__main__":
Scott Bakera33ccb02018-01-26 13:03:28 -080083 unittest.main()