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

fix: right tests for value errors and zero len argument #14

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
16 changes: 16 additions & 0 deletions excel2py/excel_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numbers
from collections import Iterable
from functools import reduce
import excel2py.excel_functions as ef
from excel2py.ex_datetime import ex_datetime, to_excel_number


Expand Down Expand Up @@ -202,6 +203,21 @@ def VLOOKUP(value, table, column, range_lookup=True):
return table[-1][column-1]
return None


def RIGHT(x, len=1):
"""
RIGHT returns the last character or characters in a text string,
based on the number of characters you specify.
:param x: the text
:param len: the number of character specified, default is 1
:return : the last character or characters
"""
if isinstance(len, (str, list, tuple, bool)) or len < 0:
raise ValueError("ValueError exception thrown")
elif len != 0:
return x[-len:]
return ''

#
# dt = ex_datetime(2018, 7, 5)
# print(MIN(dt))
43 changes: 43 additions & 0 deletions tests/test_excel_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,48 @@ def test_ok(self):
self.assertEqual(ef.INT(val), expect)


# RIGHT test
class TestRight(unittest.TestCase):
def test_with_optional_value(self):
"""
Test to extract characters based on the second argument
"""
expect = "cel"
result = ef.RIGHT("Excel", 3)
self.assertEqual(expect, result)

def test_with_no_optional_value(self):
"""
Test to extract characters if no second argument is passed
"""
expect = 'l'
result = ef.RIGHT("Excel")
self.assertEqual(expect, result)

def test_with_zero_as_len(self):
"""
Test to return any empty string if 0 is passed as the len
"""
result = ef.RIGHT("ABC",0)
self.assertEqual('',result)

def test_value_errors(self):
"""
Throw a value error if the len is a string or a negative value
"""
with self.assertRaises(ValueError):
ef.RIGHT("ABC","len")
with self.assertRaises(ValueError):
ef.RIGHT("ABC",-1)
with self.assertRaises(ValueError):
ef.RIGHT("ABC",True)
with self.assertRaises(ValueError):
ef.RIGHT("ABC",[5,10,15])
with self.assertRaises(ValueError):
ef.RIGHT("ABC",(25,30,35))




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