turner-townsend-backend-ass.../roman/test_roman.py

89 lines
2.8 KiB
Python
Raw Permalink Normal View History

2025-08-30 12:29:05 +01:00
import pytest
2025-08-31 11:34:20 +01:00
from roman import RomanNumeralConverter
ERROR_MESSAGE = "ERROR: Invalid input"
2025-08-30 12:29:05 +01:00
def test_valid_single_numerals():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number("I") == "I = 1"
assert converter.convert_to_number("V") == "V = 5"
assert converter.convert_to_number("X") == "X = 10"
assert converter.convert_to_number("C") == "C = 100"
assert converter.convert_to_number("M") == "M = 1000"
2025-08-30 12:29:05 +01:00
def test_valid_multiple_numerals():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number("IV") == "IV = 1 + 5 = 6"
assert converter.convert_to_number("XCM") == "XCM = 10 + 100 + 1000 = 1110"
assert (
converter.convert_to_number("MCMXCIV")
== "MCMXCIV = 1000 + 100 + 1000 + 10 + 100 + 1 + 5 = 2216"
)
2025-08-30 12:29:05 +01:00
def test_lowercase_input():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert (
converter.convert_to_number("ivxcm") == "IVXCM = 1 + 5 + 10 + 100 + 1000 = 1116"
)
2025-08-30 12:29:05 +01:00
def test_mixed_case_input():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert (
converter.convert_to_number("iVxCm") == "IVXCM = 1 + 5 + 10 + 100 + 1000 = 1116"
)
2025-08-30 12:29:05 +01:00
def test_invalid_characters():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number("EFGH") == ERROR_MESSAGE
assert converter.convert_to_number("123") == ERROR_MESSAGE
assert converter.convert_to_number("!@#") == ERROR_MESSAGE
2025-08-30 12:29:05 +01:00
def test_mixed_valid_and_invalid_characters():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number("AIXB") == "IX = 1 + 10 = 11"
assert converter.convert_to_number("M1C2") == "MC = 1000 + 100 = 1100"
2025-08-30 12:29:05 +01:00
def test_empty_string():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number("") == ERROR_MESSAGE
2025-08-30 12:29:05 +01:00
def test_only_spaces():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number(" ") == ERROR_MESSAGE
2025-08-30 12:29:05 +01:00
def test_spaces_and_valid_numerals():
2025-08-31 11:34:20 +01:00
converter = RomanNumeralConverter()
assert converter.convert_to_number(" I V ") == "IV = 1 + 5 = 6"
def test_filter_valid_numerals():
converter = RomanNumeralConverter()
assert converter._filter_valid_numerals("M1X2V") == "MXV"
assert converter._filter_valid_numerals("def") == ""
assert converter._filter_valid_numerals(" I V ") == "IV"
def test_convert_numerals_to_values():
converter = RomanNumeralConverter()
assert converter._convert_numerals_to_values("IV") == [1, 5]
assert converter._convert_numerals_to_values("MXV") == [1000, 10, 5]
assert converter._convert_numerals_to_values("C") == [100]
def test_format_output():
converter = RomanNumeralConverter()
assert converter._format_output("I", [1]) == "I = 1"
assert converter._format_output("IV", [1, 5]) == "IV = 1 + 5 = 6"
assert (
converter._format_output("MXV", [1000, 10, 5]) == "MXV = 1000 + 10 + 5 = 1015"
)