| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
""" |
| 12 |
Tests for `docutils.utils._roman_numerals`. |
| 13 |
""" |
| 14 |
|
| 15 |
from pathlib import Path |
| 16 |
import sys |
| 17 |
import unittest |
| 18 |
|
| 19 |
if __name__ == '__main__': |
| 20 |
|
| 21 |
|
| 22 |
sys.path.insert(0, str(Path(__file__).resolve().parents[2])) |
| 23 |
|
| 24 |
from docutils.utils._roman_numerals import ( |
| 25 |
MIN, |
| 26 |
MAX, |
| 27 |
RomanNumeral, |
| 28 |
OutOfRangeError, |
| 29 |
InvalidRomanNumeralError, |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
class NewRomanNumeralTestCase(unittest.TestCase): |
| 34 |
def test_zero(self) -> None: |
| 35 |
with self.assertRaises(OutOfRangeError) as ctx: |
| 36 |
RomanNumeral(0) |
| 37 |
msg = str(ctx.exception) |
| 38 |
self.assertEqual( |
| 39 |
msg, |
| 40 |
'Number out of range (must be between 1 and 4,999). Got 0.', |
| 41 |
) |
| 42 |
|
| 43 |
def test_one(self) -> None: |
| 44 |
self.assertEqual(int(RomanNumeral(1)), 1) |
| 45 |
|
| 46 |
def test_MIN(self) -> None: |
| 47 |
self.assertEqual(int(RomanNumeral(MIN)), MIN) |
| 48 |
|
| 49 |
def test_forty_two(self) -> None: |
| 50 |
self.assertEqual(int(RomanNumeral(42)), 42) |
| 51 |
|
| 52 |
def test_four_thousand_nine_hundred_and_ninety_nine(self) -> None: |
| 53 |
self.assertEqual(int(RomanNumeral(4_999)), 4_999) |
| 54 |
|
| 55 |
def test_MAX(self) -> None: |
| 56 |
self.assertEqual(int(RomanNumeral(MAX)), MAX) |
| 57 |
|
| 58 |
def test_five_thousand(self) -> None: |
| 59 |
with self.assertRaises(OutOfRangeError) as ctx: |
| 60 |
RomanNumeral(5_000) |
| 61 |
msg = str(ctx.exception) |
| 62 |
self.assertEqual( |
| 63 |
msg, |
| 64 |
'Number out of range (must be between 1 and 4,999). Got 5000.', |
| 65 |
) |
| 66 |
|
| 67 |
def test_minus_one(self) -> None: |
| 68 |
with self.assertRaises(OutOfRangeError) as ctx: |
| 69 |
RomanNumeral(-1) |
| 70 |
msg = str(ctx.exception) |
| 71 |
self.assertEqual( |
| 72 |
msg, |
| 73 |
'Number out of range (must be between 1 and 4,999). Got -1.', |
| 74 |
) |
| 75 |
|
| 76 |
def test_float(self) -> None: |
| 77 |
with self.assertRaises(TypeError) as ctx: |
| 78 |
RomanNumeral(4.2) |
| 79 |
msg = str(ctx.exception) |
| 80 |
self.assertEqual( |
| 81 |
msg, |
| 82 |
"RomanNumeral: an integer is required, not 'float'", |
| 83 |
) |
| 84 |
|
| 85 |
|
| 86 |
class ToStringTestCase(unittest.TestCase): |
| 87 |
def test_str(self): |
| 88 |
test_numerals = [ |
| 89 |
'I', 'II', 'III', 'IV', 'V', |
| 90 |
'VI', 'VII', 'VIII', 'IX', 'X', |
| 91 |
'XI', 'XII', 'XIII', 'XIV', 'XV', |
| 92 |
'XVI', 'XVII', 'XVIII', 'XIX', 'XX', |
| 93 |
'XXI', 'XXII', 'XXIII', 'XXIV', |
| 94 |
] |
| 95 |
for n, roman_str in enumerate(test_numerals, start=1): |
| 96 |
with self.subTest(id=n, roman_str=roman_str): |
| 97 |
num = RomanNumeral(n) |
| 98 |
self.assertEqual(f'{num}', roman_str) |
| 99 |
|
| 100 |
def test_uppercase(self): |
| 101 |
test_numerals = [ |
| 102 |
'I', 'II', 'III', 'IV', 'V', |
| 103 |
'VI', 'VII', 'VIII', 'IX', 'X', |
| 104 |
'XI', 'XII', 'XIII', 'XIV', 'XV', |
| 105 |
'XVI', 'XVII', 'XVIII', 'XIX', 'XX', |
| 106 |
'XXI', 'XXII', 'XXIII', 'XXIV', |
| 107 |
] |
| 108 |
for n, roman_str in enumerate(test_numerals, start=1): |
| 109 |
with self.subTest(id=n, roman_str=roman_str): |
| 110 |
num = RomanNumeral(n) |
| 111 |
self.assertEqual(num.to_uppercase(), roman_str) |
| 112 |
|
| 113 |
def test_lowercase(self): |
| 114 |
test_numerals = [ |
| 115 |
'i', 'ii', 'iii', 'iv', 'v', |
| 116 |
'vi', 'vii', 'viii', 'ix', 'x', |
| 117 |
'xi', 'xii', 'xiii', 'xiv', 'xv', |
| 118 |
'xvi', 'xvii', 'xviii', 'xix', 'xx', |
| 119 |
'xxi', 'xxii', 'xxiii', 'xxiv', |
| 120 |
] |
| 121 |
for n, roman_str in enumerate(test_numerals, start=1): |
| 122 |
with self.subTest(id=n, roman_str=roman_str): |
| 123 |
num = RomanNumeral(n) |
| 124 |
self.assertEqual(num.to_lowercase(), roman_str) |
| 125 |
|
| 126 |
def test_minitrue(self): |
| 127 |
|
| 128 |
num = RomanNumeral(1984) |
| 129 |
self.assertEqual(f'{num}', 'MCMLXXXIV') |
| 130 |
self.assertEqual(num.to_uppercase(), 'MCMLXXXIV') |
| 131 |
self.assertEqual(num.to_lowercase(), 'mcmlxxxiv') |
| 132 |
|
| 133 |
|
| 134 |
class FromStringTestCase(unittest.TestCase): |
| 135 |
def test_uppercase(self): |
| 136 |
test_numerals = [ |
| 137 |
'I', 'II', 'III', 'IV', 'V', |
| 138 |
'VI', 'VII', 'VIII', 'IX', 'X', |
| 139 |
'XI', 'XII', 'XIII', 'XIV', 'XV', |
| 140 |
'XVI', 'XVII', 'XVIII', 'XIX', 'XX', |
| 141 |
'XXI', 'XXII', 'XXIII', 'XXIV', |
| 142 |
] |
| 143 |
for n, roman_str in enumerate(test_numerals, start=1): |
| 144 |
with self.subTest(id=n, roman_str=roman_str): |
| 145 |
expected = RomanNumeral(n) |
| 146 |
parsed = RomanNumeral.from_string(roman_str) |
| 147 |
self.assertEqual(expected, parsed) |
| 148 |
|
| 149 |
def test_lowercase(self): |
| 150 |
test_numerals = [ |
| 151 |
'i', 'ii', 'iii', 'iv', 'v', |
| 152 |
'vi', 'vii', 'viii', 'ix', 'x', |
| 153 |
'xi', 'xii', 'xiii', 'xiv', 'xv', |
| 154 |
'xvi', 'xvii', 'xviii', 'xix', 'xx', |
| 155 |
'xxi', 'xxii', 'xxiii', 'xxiv', |
| 156 |
] |
| 157 |
for n, roman_str in enumerate(test_numerals, start=1): |
| 158 |
with self.subTest(id=n, roman_str=roman_str): |
| 159 |
expected = RomanNumeral(n) |
| 160 |
parsed = RomanNumeral.from_string(roman_str) |
| 161 |
self.assertEqual(expected, parsed) |
| 162 |
|
| 163 |
def test_special(self): |
| 164 |
parsed = RomanNumeral.from_string('MDCCCXXIII') |
| 165 |
self.assertEqual(RomanNumeral(1823), parsed) |
| 166 |
|
| 167 |
parsed = RomanNumeral.from_string('mdcccxxiii') |
| 168 |
self.assertEqual(RomanNumeral(1823), parsed) |
| 169 |
|
| 170 |
parsed = RomanNumeral.from_string('MCMLXXXIV') |
| 171 |
self.assertEqual(RomanNumeral(1984), parsed) |
| 172 |
|
| 173 |
parsed = RomanNumeral.from_string('mcmlxxxiv') |
| 174 |
self.assertEqual(RomanNumeral(1984), parsed) |
| 175 |
|
| 176 |
parsed = RomanNumeral.from_string('MM') |
| 177 |
self.assertEqual(RomanNumeral(2000), parsed) |
| 178 |
|
| 179 |
parsed = RomanNumeral.from_string('mm') |
| 180 |
self.assertEqual(RomanNumeral(2000), parsed) |
| 181 |
|
| 182 |
parsed = RomanNumeral.from_string('MMMMCMXCIX') |
| 183 |
self.assertEqual(RomanNumeral(4_999), parsed) |
| 184 |
|
| 185 |
parsed = RomanNumeral.from_string('mmmmcmxcix') |
| 186 |
self.assertEqual(RomanNumeral(4_999), parsed) |
| 187 |
|
| 188 |
def test_invalid(self): |
| 189 |
with self.assertRaises(InvalidRomanNumeralError) as ctx: |
| 190 |
RomanNumeral.from_string('Not a Roman numeral!') |
| 191 |
msg = str(ctx.exception) |
| 192 |
self.assertEqual(msg, 'Invalid Roman numeral: Not a Roman numeral!') |
| 193 |
|
| 194 |
def test_mixed_case(self): |
| 195 |
with self.assertRaises(InvalidRomanNumeralError) as ctx: |
| 196 |
RomanNumeral.from_string('McMlXxXiV') |
| 197 |
msg = str(ctx.exception) |
| 198 |
self.assertEqual(msg, 'Invalid Roman numeral: McMlXxXiV') |
| 199 |
|
| 200 |
|
| 201 |
class RoundTripTestCase(unittest.TestCase): |
| 202 |
def test_round_trip(self): |
| 203 |
for n in range(MIN, MAX + 1, 19): |
| 204 |
num = RomanNumeral(n) |
| 205 |
parsed = RomanNumeral.from_string(str(num)) |
| 206 |
self.assertEqual(num, parsed) |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == '__main__': |
| 210 |
unittest.main() |