Skip to content

Commit

Permalink
Merge pull request #477 from jacadzaca/refactor_test_quoting
Browse files Browse the repository at this point in the history
refactor test_quoting
  • Loading branch information
niccokunzmann committed Oct 29, 2022
2 parents 8729964 + b0cb05e commit 2c93383
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/icalendar/tests/test_property_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def test_parameter_keys_are_uppercase():
parameter = Parameters(parameter1='Value1')
assert list(parameter.keys()) == ['PARAMETER1']

@pytest.mark.parametrize('cn_param, cn_quoted', [
# not double-quoted
('Aramis', 'Aramis'),
# if a space is present - enclose in double quotes
('Aramis Alameda', '"Aramis Alameda"'),
# a single quote in parameter value - double quote the value
('Aramis d\'Alameda', '"Aramis d\'Alameda"'),
('Арамис д\'Аламеда', '"Арамис д\'Аламеда"'),
# double quote is replaced with single quote
('Aramis d\"Alameda', '"Aramis d\'Alameda"'),
])
def test_quoting(cn_param, cn_quoted):
event = Event()
attendee = vCalAddress('test@example.com')
attendee.params['CN'] = cn_param
event.add('ATTENDEE', attendee)
assert f'ATTENDEE;CN={cn_quoted}:test@example.com' in event.to_ical().decode('utf-8')

class TestPropertyParams(unittest.TestCase):

def test_property_params(self):
Expand Down Expand Up @@ -86,32 +104,6 @@ def test_unicode_param(self):
self.assertEqual(vevent['ORGANIZER'].params['CN'],
'Джон Доу')

def test_quoting(self):
# not double-quoted
self._test_quoting("Aramis", 'Aramis')
# if a space is present - enclose in double quotes
self._test_quoting("Aramis Alameda", '"Aramis Alameda"')
# a single quote in parameter value - double quote the value
self._test_quoting("Aramis d'Alameda", '"Aramis d\'Alameda"')
# double quote is replaced with single quote
self._test_quoting("Aramis d\"Alameda", '"Aramis d\'Alameda"')
self._test_quoting("Арамис д'Аламеда", '"Арамис д\'Аламеда"')

def _test_quoting(self, cn_param, cn_quoted):
"""
@param cn_param: CN parameter value to test for quoting
@param cn_quoted: expected quoted parameter in icalendar format
"""
vevent = Event()
attendee = vCalAddress('test@mail.com')
attendee.params['CN'] = cn_param
vevent.add('ATTENDEE', attendee)
self.assertEqual(
vevent.to_ical(),
b'BEGIN:VEVENT\r\nATTENDEE;CN=' + cn_quoted.encode('utf-8') +
b':test@mail.com\r\nEND:VEVENT\r\n'
)

def test_escaping(self):
# verify that escaped non safe chars are decoded correctly
NON_SAFE_CHARS = ',\\;:'
Expand Down

0 comments on commit 2c93383

Please sign in to comment.