Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception handling #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Roman.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def roman(numero):
if numero < 1:
return "Can't convert to roman number!"
raise Exception("Can't convert to roman number!")

if numero > 3999:
return "Not a valid number"
raise Exception("Not a valid number")

if not isinstance(numero, int):
return "Only int numbers"
raise Exception("Only int numbers")

numeros = {
1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50: 'L',
Expand Down
2 changes: 1 addition & 1 deletion roman_to_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def decimal(roman):
'Z',
]
if any(x in roman for x in a):
result = 'Input is not a roman number'
raise Exception('Input is not a roman number')
else:
for letra in cadena:
if len(cadena) != count + 1:
Expand Down
32 changes: 25 additions & 7 deletions test_roman.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ def test_roman_decimal_roman_845(self):
self.assertEqual(resultado, 'DCCCXLV')

def test_roman_decimal_roman_minor(self):
resultado = roman(-1)
self.assertEqual(resultado, "Can't convert to roman number!")
with self.assertRaises(Exception) as e:
roman(-1)
self.assertEqual(
e.exception.message,
"Can't convert to roman number!",
)

def test_roman_decimal_roman_not_type(self):
resultado = roman(10.5)
self.assertEqual(resultado, "Only int numbers")
with self.assertRaises(Exception) as e:
roman(10.5)

self.assertEqual(
e.exception.message,
"Only int numbers",
)

def test_romandecimal_one(self):
self.assertEqual(decimal('I'), 1)
Expand All @@ -62,15 +71,24 @@ def test_romandecimal_3999(self):
self.assertEqual(decimal('MMMCMXCIX'), 3999)

def test_wrong_letter(self):
self.assertEqual(decimal('CCXASDASHDUASBD'), 'Input is not a roman number')
with self.assertRaises(Exception) as e:
decimal('CCXASDASHDUASBD'),
self.assertEqual(
e.exception.message,
'Input is not a roman number',
)

def test_roman_decimal_3999(self):
resultado = roman(3999)
self.assertEqual(resultado, 'MMMCMXCIX')

def test_roman_decimal_4000(self):
resultado = roman(4000)
self.assertEqual(resultado, 'Not a valid number')
with self.assertRaises(Exception) as e:
roman(4000)
self.assertEqual(
e.exception.message,
'Not a valid number',
)

## definir primer test

Expand Down
4 changes: 3 additions & 1 deletion test_roman_to_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def test_romandecimal_3999(self):
self.assertEqual(decimal('MMMCMXCIX'), 3999)

def test_wrong_letter(self):
self.assertEqual(
with self.assertRaises(Exception) as e:
decimal('CCXASDASHDUASBD'),
self.assertEqual(
e.exception.message,
'Input is not a roman number'
)

Expand Down