From aa48ea1d3ccc3a4273e7e955aaad99cafc700128 Mon Sep 17 00:00:00 2001 From: Matt Snider Date: Mon, 21 Jan 2019 12:12:49 +0100 Subject: [PATCH] Improve regex and test for days of the week --- tests/test_recur.py | 95 +++++++++++++++++++++++++++++++++--- todoist_taskwarrior/utils.py | 2 +- 2 files changed, 88 insertions(+), 9 deletions(-) diff --git a/tests/test_recur.py b/tests/test_recur.py index b7f6111..6e398dc 100644 --- a/tests/test_recur.py +++ b/tests/test_recur.py @@ -2,6 +2,7 @@ Tests parsing `recur` strings from Todoist `date_string`s """ +import pytest from todoist_taskwarrior import utils @@ -54,17 +55,95 @@ def test_monthly(): assert utils.parse_recur('every 2nd month') == '2 months' assert utils.parse_recur('every 3rd month') == '3 months' +DAYS_OF_WEEK = [ + # Monday + 'mo', + 'mon', + 'monday', -def test_day_of_week(): + # Tuesday + 'tu', + 'tue', + 'tues', + 'tuesday', + + # Wednesday + 'we', + 'wed', + 'weds', + 'wednesday', + + # Thursday + 'th', + 'thu', + 'thurs', + 'thursday', + + # Friday + 'fr', + 'fri', + 'friday', + + # Saturday + 'sa', + 'sat', + 'saturday', + + # Sunday + 'su', + 'sun', + 'sunday', +] + + +@pytest.mark.parametrize('dow', DAYS_OF_WEEK) +def test_every_dow_has_weekly_recurrence(dow): """ The actual day should be indicated in the `due` property, so here - we just need to ensure that the recurrence is correct. + we just need to ensure that the recurrence is correct. """ - assert utils.parse_recur('every mon') == 'weekly' - assert utils.parse_recur('every monday') == 'weekly' - assert utils.parse_recur('every tuesday') == 'weekly' - assert utils.parse_recur('every tues') == 'weekly' - assert utils.parse_recur('every 2nd monday') == '2 weeks' - assert utils.parse_recur('every 3rd friday') == '3 weeks' + assert utils.parse_recur(f'ev {dow}') == 'weekly' + assert utils.parse_recur(f'every {dow}') == 'weekly' + + +@pytest.mark.parametrize('ordinal', [ + ('2', 2), + ('2nd', 2), + ('3', 3), + ('3rd', 3), + ('4', 4), + ('4th', 4), +]) +@pytest.mark.parametrize('dow', DAYS_OF_WEEK) +def test_every_dow_ordinal_recurrence(ordinal, dow): + ordinal, expected = ordinal + assert utils.parse_recur(f'ev {ordinal} {dow}') == f'{expected} weeks' + assert utils.parse_recur(f'every {ordinal} {dow}') == f'{expected} weeks' + + +# def test_day_of_week_short_forms(): +# assert utils.parse_recur('every mo') == 'weekly' +# assert utils.parse_recur('every mon') == 'weekly' + +# assert utils.parse_recur('every tu') == 'weekly' +# assert utils.parse_recur('every tue') == 'weekly' +# assert utils.parse_recur('every tues') == 'weekly' + +# assert utils.parse_recur('every we') == 'weekly' +# assert utils.parse_recur('every wed') == 'weekly' +# assert utils.parse_recur('every weds') == 'weekly' + +# assert utils.parse_recur('every th') == 'weekly' +# assert utils.parse_recur('every thu') == 'weekly' +# assert utils.parse_recur('every thurs') == 'weekly' + +# assert utils.parse_recur('every fr') == 'weekly' +# assert utils.parse_recur('every fri') == 'weekly' + +# assert utils.parse_recur('every sa') == 'weekly' +# assert utils.parse_recur('every sat') == 'weekly' + +# assert utils.parse_recur('every su') == 'weekly' +# assert utils.parse_recur('every sun') == 'weekly' def test_day_of_month(): diff --git a/todoist_taskwarrior/utils.py b/todoist_taskwarrior/utils.py index 3f8b89b..9456274 100644 --- a/todoist_taskwarrior/utils.py +++ b/todoist_taskwarrior/utils.py @@ -65,7 +65,7 @@ _PERIOD = r'(?Phour|day|week|month|year)s?' _EVERY = r'ev(ery)?' _CYCLES = r'((?P\d+)(st|nd|rd|th)?)' _SIMPLE = r'(?Pdaily|weekly|monthly|yearly)' -_DOW = r'((?P(mon|tues|weds|thurs|fri|sat|sun))(day)?)' +_DOW = r'((?P(mo(n(day)?)?|tu(e(s(day)?)?)?|we(d(s|(nes(day)?)?)?)?|th(u(rs(day)?)?)?|fr(i(day)?)?|sa(t(urday)?)?|su(n(day)?)?)))' # A single cycle recurrence is one of: # - daily, weekly, monthly, yearly