blob: 3b988e58aad5688fbfc910d8fd216b1e163d9b90 [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 Bakera1b089a2018-10-05 09:59:17 -070019
20
21# Several of the base functions require a Field object.
22def _field(name, singular=None, plural=None):
23 f = {}
24 f["name"] = name
25 f["options"] = {}
26 if singular:
27 f["options"]["singular"] = singular
28 if plural:
29 f["options"]["plural"] = plural
30 return f
Scott Bakera33ccb02018-01-26 13:03:28 -080031
Zack Williams045b63d2019-01-22 16:30:57 -070032
Scott Bakera33ccb02018-01-26 13:03:28 -080033class Jinja2BaseTests(unittest.TestCase):
34 def test_xproto_is_true(self):
35 self.assertTrue(xproto_is_true(True))
36 self.assertTrue(xproto_is_true("True"))
37 self.assertTrue(xproto_is_true('"True"'))
38 self.assertFalse(xproto_is_true(False))
39 self.assertFalse(xproto_is_true("False"))
40 self.assertFalse(xproto_is_true('"False"'))
41 self.assertFalse(xproto_is_true(None))
42 self.assertFalse(xproto_is_true("something else"))
43
Scott Bakera1b089a2018-10-05 09:59:17 -070044 def test_unquote(self):
45 self.assertEqual(xproto_unquote("foo"), "foo")
46 self.assertEqual(xproto_unquote('"foo"'), "foo")
47
48 def test_pluralize(self):
49 self.assertEqual(xproto_pluralize(_field("sheep")), "sheep")
50 self.assertEqual(xproto_pluralize(_field("slice")), "slices")
51 self.assertEqual(xproto_pluralize(_field("network")), "networks")
52 self.assertEqual(xproto_pluralize(_field("omf_friendly")), "omf_friendlies")
53 # invalid words, should usually return <word>-es
54 self.assertEqual(xproto_pluralize(_field("xxx")), "xxxes")
55 # if a field option is set, use that
56 self.assertEqual(xproto_pluralize(_field("sheep", plural="turtles")), "turtles")
57
58 def test_singularize(self):
59 self.assertEqual(xproto_singularize(_field("sheep")), "sheep")
60 self.assertEqual(xproto_singularize(_field("slices")), "slice")
61 self.assertEqual(xproto_singularize(_field("networks")), "network")
62 self.assertEqual(xproto_singularize(_field("omf_friendlies")), "omf_friendly")
63 # invalid words, return the original word
64 self.assertEqual(xproto_singularize(_field("xxx")), "xxx")
65 # if a field option is set, use that
66 self.assertEqual(xproto_pluralize(_field("sheep", plural="turtles")), "turtles")
67
68 def test_singularize_pluralize(self):
69 self.assertEqual(xproto_singularize_pluralize(_field("sheep")), "sheep")
70 self.assertEqual(xproto_singularize_pluralize(_field("slices")), "slices")
71 self.assertEqual(xproto_singularize_pluralize(_field("networks")), "networks")
Zack Williams045b63d2019-01-22 16:30:57 -070072 self.assertEqual(
73 xproto_singularize_pluralize(_field("omf_friendlies")), "omf_friendlies"
74 )
Scott Bakera1b089a2018-10-05 09:59:17 -070075 # invalid words, should usually return <word>-es
76 self.assertEqual(xproto_singularize_pluralize(_field("xxx")), "xxxes")
77 # if a field option is set, use that
Zack Williams045b63d2019-01-22 16:30:57 -070078 self.assertEqual(
79 xproto_singularize(_field("sheep", singular="turtle")), "turtle"
80 )
Scott Bakera1b089a2018-10-05 09:59:17 -070081
82
Zack Williams045b63d2019-01-22 16:30:57 -070083if __name__ == "__main__":
Scott Bakera33ccb02018-01-26 13:03:28 -080084 unittest.main()