File: test__roman_numerals.py

package info (click to toggle)
python-docutils 0.22%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 11,448 kB
  • sloc: python: 53,302; lisp: 14,475; xml: 1,807; javascript: 1,032; makefile: 102; sh: 96
file content (210 lines) | stat: -rw-r--r-- 7,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#! /usr/bin/env python3

# $Id$
# Author: Adam Turner.
# Copyright: This module is placed in the public domain
#            or under the `Zero Clause BSD licence`_,
#            whichever is more permissive.
#
# .. _Zero Clause BSD licence: https://opensource.org/license/0BSD

"""
Tests for `docutils.utils._roman_numerals`.
"""

from pathlib import Path
import sys
import unittest

if __name__ == '__main__':
    # prepend the "docutils root" to the Python library path
    # so we import the local `docutils` package.
    sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

from docutils.utils._roman_numerals import (
    MIN,
    MAX,
    RomanNumeral,
    OutOfRangeError,
    InvalidRomanNumeralError,
)


class NewRomanNumeralTestCase(unittest.TestCase):
    def test_zero(self) -> None:
        with self.assertRaises(OutOfRangeError) as ctx:
            RomanNumeral(0)
        msg = str(ctx.exception)
        self.assertEqual(
            msg,
            'Number out of range (must be between 1 and 4,999). Got 0.',
        )

    def test_one(self) -> None:
        self.assertEqual(int(RomanNumeral(1)), 1)

    def test_MIN(self) -> None:
        self.assertEqual(int(RomanNumeral(MIN)), MIN)

    def test_forty_two(self) -> None:
        self.assertEqual(int(RomanNumeral(42)), 42)

    def test_four_thousand_nine_hundred_and_ninety_nine(self) -> None:
        self.assertEqual(int(RomanNumeral(4_999)), 4_999)

    def test_MAX(self) -> None:
        self.assertEqual(int(RomanNumeral(MAX)), MAX)

    def test_five_thousand(self) -> None:
        with self.assertRaises(OutOfRangeError) as ctx:
            RomanNumeral(5_000)
        msg = str(ctx.exception)
        self.assertEqual(
            msg,
            'Number out of range (must be between 1 and 4,999). Got 5000.',
        )

    def test_minus_one(self) -> None:
        with self.assertRaises(OutOfRangeError) as ctx:
            RomanNumeral(-1)
        msg = str(ctx.exception)
        self.assertEqual(
            msg,
            'Number out of range (must be between 1 and 4,999). Got -1.',
        )

    def test_float(self) -> None:
        with self.assertRaises(TypeError) as ctx:
            RomanNumeral(4.2)
        msg = str(ctx.exception)
        self.assertEqual(
            msg,
            "RomanNumeral: an integer is required, not 'float'",
        )


class ToStringTestCase(unittest.TestCase):
    def test_str(self):
        test_numerals = [
            'I', 'II', 'III', 'IV', 'V',
            'VI', 'VII', 'VIII', 'IX', 'X',
            'XI', 'XII', 'XIII', 'XIV', 'XV',
            'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
            'XXI', 'XXII', 'XXIII', 'XXIV',
        ]
        for n, roman_str in enumerate(test_numerals, start=1):
            with self.subTest(id=n, roman_str=roman_str):
                num = RomanNumeral(n)
                self.assertEqual(f'{num}', roman_str)

    def test_uppercase(self):
        test_numerals = [
            'I', 'II', 'III', 'IV', 'V',
            'VI', 'VII', 'VIII', 'IX', 'X',
            'XI', 'XII', 'XIII', 'XIV', 'XV',
            'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
            'XXI', 'XXII', 'XXIII', 'XXIV',
        ]
        for n, roman_str in enumerate(test_numerals, start=1):
            with self.subTest(id=n, roman_str=roman_str):
                num = RomanNumeral(n)
                self.assertEqual(num.to_uppercase(), roman_str)

    def test_lowercase(self):
        test_numerals = [
            'i', 'ii', 'iii', 'iv', 'v',
            'vi', 'vii', 'viii', 'ix', 'x',
            'xi', 'xii', 'xiii', 'xiv', 'xv',
            'xvi', 'xvii', 'xviii', 'xix', 'xx',
            'xxi', 'xxii', 'xxiii', 'xxiv',
        ]
        for n, roman_str in enumerate(test_numerals, start=1):
            with self.subTest(id=n, roman_str=roman_str):
                num = RomanNumeral(n)
                self.assertEqual(num.to_lowercase(), roman_str)

    def test_minitrue(self):
        # IGNORANCE IS STRENGTH
        num = RomanNumeral(1984)
        self.assertEqual(f'{num}', 'MCMLXXXIV')
        self.assertEqual(num.to_uppercase(), 'MCMLXXXIV')
        self.assertEqual(num.to_lowercase(), 'mcmlxxxiv')


class FromStringTestCase(unittest.TestCase):
    def test_uppercase(self):
        test_numerals = [
            'I', 'II', 'III', 'IV', 'V',
            'VI', 'VII', 'VIII', 'IX', 'X',
            'XI', 'XII', 'XIII', 'XIV', 'XV',
            'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
            'XXI', 'XXII', 'XXIII', 'XXIV',
        ]
        for n, roman_str in enumerate(test_numerals, start=1):
            with self.subTest(id=n, roman_str=roman_str):
                expected = RomanNumeral(n)
                parsed = RomanNumeral.from_string(roman_str)
                self.assertEqual(expected, parsed)

    def test_lowercase(self):
        test_numerals = [
            'i', 'ii', 'iii', 'iv', 'v',
            'vi', 'vii', 'viii', 'ix', 'x',
            'xi', 'xii', 'xiii', 'xiv', 'xv',
            'xvi', 'xvii', 'xviii', 'xix', 'xx',
            'xxi', 'xxii', 'xxiii', 'xxiv',
        ]
        for n, roman_str in enumerate(test_numerals, start=1):
            with self.subTest(id=n, roman_str=roman_str):
                expected = RomanNumeral(n)
                parsed = RomanNumeral.from_string(roman_str)
                self.assertEqual(expected, parsed)

    def test_special(self):
        parsed = RomanNumeral.from_string('MDCCCXXIII')
        self.assertEqual(RomanNumeral(1823), parsed)

        parsed = RomanNumeral.from_string('mdcccxxiii')
        self.assertEqual(RomanNumeral(1823), parsed)

        parsed = RomanNumeral.from_string('MCMLXXXIV')
        self.assertEqual(RomanNumeral(1984), parsed)

        parsed = RomanNumeral.from_string('mcmlxxxiv')
        self.assertEqual(RomanNumeral(1984), parsed)

        parsed = RomanNumeral.from_string('MM')
        self.assertEqual(RomanNumeral(2000), parsed)

        parsed = RomanNumeral.from_string('mm')
        self.assertEqual(RomanNumeral(2000), parsed)

        parsed = RomanNumeral.from_string('MMMMCMXCIX')
        self.assertEqual(RomanNumeral(4_999), parsed)

        parsed = RomanNumeral.from_string('mmmmcmxcix')
        self.assertEqual(RomanNumeral(4_999), parsed)

    def test_invalid(self):
        with self.assertRaises(InvalidRomanNumeralError) as ctx:
            RomanNumeral.from_string('Not a Roman numeral!')
        msg = str(ctx.exception)
        self.assertEqual(msg, 'Invalid Roman numeral: Not a Roman numeral!')

    def test_mixed_case(self):
        with self.assertRaises(InvalidRomanNumeralError) as ctx:
            RomanNumeral.from_string('McMlXxXiV')
        msg = str(ctx.exception)
        self.assertEqual(msg, 'Invalid Roman numeral: McMlXxXiV')


class RoundTripTestCase(unittest.TestCase):
    def test_round_trip(self):
        for n in range(MIN, MAX + 1, 19):
            num = RomanNumeral(n)
            parsed = RomanNumeral.from_string(str(num))
            self.assertEqual(num, parsed)


if __name__ == '__main__':
    unittest.main()