xref: /aosp_15_r20/external/pigweed/pw_cli/py/plural_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1#!/usr/bin/env python3
2# Copyright 2024 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Tests for general purpose tools."""
16
17import unittest
18
19from pw_cli.plural import plural
20
21
22class PluralTest(unittest.TestCase):
23    """Test the plural function, which adds an 's' to nouns."""
24
25    def test_single_list(self):
26        self.assertEqual('1 item', plural([1], 'item'))
27
28    def test_single_count(self):
29        self.assertEqual('1 item', plural(1, 'item'))
30
31    def test_multiple_list(self):
32        self.assertEqual('3 items', plural([1, 2, 3], 'item'))
33
34    def test_multiple_count(self):
35        self.assertEqual('3 items', plural(3, 'item'))
36
37    def test_single_these(self):
38        self.assertEqual('this 1 item', plural(1, 'item', these=True))
39
40    def test_multiple_these(self):
41        self.assertEqual('these 3 items', plural(3, 'item', these=True))
42
43    def test_single_are(self):
44        self.assertEqual('1 item is', plural(1, 'item', are=True))
45
46    def test_multiple_are(self):
47        self.assertEqual('3 items are', plural(3, 'item', are=True))
48
49    def test_single_exist(self):
50        self.assertEqual('1 item exists', plural(1, 'item', exist=True))
51
52    def test_multiple_exist(self):
53        self.assertEqual('3 items exist', plural(3, 'item', exist=True))
54
55    def test_single_y(self):
56        self.assertEqual('1 thingy', plural(1, 'thingy'))
57
58    def test_multiple_y(self):
59        self.assertEqual('3 thingies', plural(3, 'thingy'))
60
61    def test_single_s(self):
62        self.assertEqual('1 bus', plural(1, 'bus'))
63
64    def test_multiple_s(self):
65        self.assertEqual('3 buses', plural(3, 'bus'))
66
67    def test_format_hex(self):
68        self.assertEqual(
69            '14 items',
70            plural(20, 'item', count_format='x'),
71        )
72
73
74if __name__ == '__main__':
75    unittest.main()
76