80 lines
2.1 KiB
Python
80 lines
2.1 KiB
Python
# SPDX-License-Identifier: CC0-1.0
|
|
import unittest
|
|
|
|
|
|
class MonthNumConvert:
|
|
__months = (
|
|
"january",
|
|
"february",
|
|
"march",
|
|
"april",
|
|
"may",
|
|
"june",
|
|
"july",
|
|
"august",
|
|
"september",
|
|
"october",
|
|
"november",
|
|
"december",
|
|
)
|
|
|
|
# https://example.com/1 -> January
|
|
def __num_to_name(self, num):
|
|
if (num > 12) or (num < 1):
|
|
return None
|
|
return self.__months[num - 1].title()
|
|
|
|
def __name_to_num(self, name):
|
|
for i, month in enumerate(self.__months):
|
|
if name[0:3].lower() == month[0:3]:
|
|
return i + 1
|
|
|
|
# a is a string
|
|
def convert(self, a):
|
|
try:
|
|
return self.__num_to_name(int(a))
|
|
except ValueError:
|
|
pass
|
|
|
|
# So it's not a number
|
|
return self.__name_to_num(a)
|
|
|
|
|
|
class TestMonthNumConvert(unittest.TestCase):
|
|
|
|
def test_convert_number(self):
|
|
"https://example.com/1 -> January"
|
|
self.assertEqual(MonthNumConvert().convert("8"), "August")
|
|
|
|
def test_convert_name_lower_shortened(self):
|
|
"https://example.com/jan -> 1"
|
|
self.assertEqual(MonthNumConvert().convert("jan"), 1)
|
|
|
|
def test_convert_name_lower(self):
|
|
"https://example.com/january -> 1"
|
|
self.assertEqual(MonthNumConvert().convert("march"), 3)
|
|
|
|
def test_convert_name(self):
|
|
"https://example.com/January -> 1"
|
|
self.assertEqual(MonthNumConvert().convert("April"), 4)
|
|
|
|
def test_convert_name_shortened(self):
|
|
"https://example.com/Jan -> 1"
|
|
self.assertEqual(MonthNumConvert().convert("Oct"), 10)
|
|
|
|
def test_out_of_bounds_num(self):
|
|
"https://example.com/13 -> (Error 404)"
|
|
self.assertEqual(MonthNumConvert().convert("13"), None)
|
|
|
|
def test_consistency(self):
|
|
self.assertEqual(
|
|
MonthNumConvert().convert("November"), MonthNumConvert().convert("nov")
|
|
)
|
|
|
|
def test_invalid_string(self):
|
|
"https://example.com/audwhb -> (Error 404)"
|
|
self.assertEqual(MonthNumConvert().convert("audwhb"), None)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|