diff options
author | Mattori Birnbaum <mattori.birnbaum@gmail.com> | 2021-05-07 21:32:54 +0900 |
---|---|---|
committer | Christian Geier <geier@lostpackets.de> | 2023-10-28 16:40:56 +0200 |
commit | 339d722e9aa1815e6822407538d36c48650a6fd7 (patch) | |
tree | 773fcfc6a1c1d829e87d221dd7bf63e2e1b8de34 | |
parent | 7f2bc5c8c076a1372798eb7ecfdf06534bf5f7a3 (diff) |
fix formatter usage in test
-rw-r--r-- | tests/event_test.py | 107 |
1 files changed, 58 insertions, 49 deletions
diff --git a/tests/event_test.py b/tests/event_test.py index 34e33043..01324de6 100644 --- a/tests/event_test.py +++ b/tests/event_test.py @@ -8,6 +8,7 @@ from hypothesis.strategies import datetimes from icalendar import Parameters, vCalAddress, vRecur, vText from packaging import version +from khal.controllers import human_formatter from khal.khalendar.event import AllDayEvent, Event, FloatingEvent, LocalizedEvent, create_timezone from .utils import ( @@ -25,10 +26,12 @@ from .utils import ( EVENT_KWARGS = {'calendar': 'foobar', 'locale': LOCALE_BERLIN} LIST_FORMAT = '{calendar-color}{cancelled}{start-end-time-style} {title}{repeat-symbol}' +LIST_FORMATTER = human_formatter(LIST_FORMAT) SEARCH_FORMAT = '{calendar-color}{cancelled}{start-long}{to-style}' + \ '{end-necessary-long} {title}{repeat-symbol}' FORMAT_CALENDAR = ('{calendar-color}{cancelled}{start-end-time-style} ({calendar}) ' '{title} [{location}]{repeat-symbol}') +SEARCH_FORMATTER = human_formatter(SEARCH_FORMAT) def test_no_initialization(): @@ -51,8 +54,9 @@ def test_raw_dt(): normalize_component(_get_text('event_dt_simple_inkl_vtimezone')) event = Event.fromString(event_dt, **EVENT_KWARGS) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30-10:30 An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format( + dt.date(2014, 4, 9))) == '09:30-10:30 An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 09:30-10:30 An Event\x1b[0m' assert event.recurring is False assert event.duration == dt.timedelta(hours=1) @@ -99,7 +103,7 @@ def test_no_end(): event = Event.fromString(_get_text('event_dt_no_end'), **EVENT_KWARGS) # TODO make sure the event also gets converted to an all day event, as we # usually do - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 12)) == \ + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 12))) == \ '16.01.2016 08:00-17.01.2016 08:00 Test\x1b[0m' @@ -150,8 +154,8 @@ def test_raw_d(): event_d = _get_text('event_d') event = Event.fromString(event_d, **EVENT_KWARGS) assert event.raw.split('\r\n') == _get_text('cal_d').split('\n') - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == ' An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == '09.04.2014 An Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == ' An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09.04.2014 An Event\x1b[0m' def test_update_sequence(): @@ -177,8 +181,8 @@ def test_transform_event(): end = BERLIN.localize(dt.datetime(2014, 4, 9, 10, 30)) event.update_start_end(start, end) assert isinstance(event, LocalizedEvent) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30-10:30 An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09:30-10:30 An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 09:30-10:30 An Event\x1b[0m' analog_event = Event.fromString(_get_text('event_dt_simple'), **EVENT_KWARGS) assert normalize_component(event.raw) == normalize_component(analog_event.raw) @@ -191,10 +195,10 @@ def test_update_event_d(): event_d = _get_text('event_d') event = Event.fromString(event_d, **EVENT_KWARGS) event.update_start_end(dt.date(2014, 4, 20), dt.date(2014, 4, 22)) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 20)) == '↦ An Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 21)) == '↔ An Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 22)) == '⇥ An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 20)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 20))) == '↦ An Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 21))) == '↔ An Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 22))) == '⇥ An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 20))) == \ '20.04.2014-22.04.2014 An Event\x1b[0m' assert 'DTSTART;VALUE=DATE:20140420' in event.raw.split('\r\n') assert 'DTEND;VALUE=DATE:20140423' in event.raw.split('\r\n') @@ -226,8 +230,8 @@ def test_dt_two_tz(): # local (Berlin) time! assert event.start_local == BERLIN.localize(dt.datetime(2014, 4, 9, 9, 30)) assert event.end_local == BERLIN.localize(dt.datetime(2014, 4, 9, 16, 30)) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30-16:30 An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09:30-16:30 An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 09:30-16:30 An Event\x1b[0m' @@ -237,10 +241,10 @@ def test_event_dt_duration(): event = Event.fromString(event_dt_duration, **EVENT_KWARGS) assert event.start == BERLIN.localize(dt.datetime(2014, 4, 9, 9, 30)) assert event.end == BERLIN.localize(dt.datetime(2014, 4, 9, 10, 30)) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30-10:30 An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09:30-10:30 An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 09:30-10:30 An Event\x1b[0m' - assert event.format('{duration}', relative_to=dt.date.today()) == '1h\x1b[0m' + assert human_formatter('{duration}')(event.format(relative_to=dt.date.today())) == '1h\x1b[0m' def test_event_dt_floating(): @@ -248,9 +252,9 @@ def test_event_dt_floating(): event_str = _get_text('event_dt_floating') event = Event.fromString(event_str, **EVENT_KWARGS) assert isinstance(event, FloatingEvent) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30-10:30 An Event\x1b[0m' - assert event.format('{duration}', relative_to=dt.date.today()) == '1h\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09:30-10:30 An Event\x1b[0m' + assert human_formatter('{duration}')(event.format(relative_to=dt.date.today())) == '1h\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 09:30-10:30 An Event\x1b[0m' assert event.start == dt.datetime(2014, 4, 9, 9, 30) assert event.end == dt.datetime(2014, 4, 9, 10, 30) @@ -272,7 +276,7 @@ def test_event_dt_tz_missing(): assert event.end == BERLIN.localize(dt.datetime(2014, 4, 9, 10, 30)) assert event.start_local == BERLIN.localize(dt.datetime(2014, 4, 9, 9, 30)) assert event.end_local == BERLIN.localize(dt.datetime(2014, 4, 9, 10, 30)) - assert event.format('{duration}', relative_to=dt.date.today()) == '1h\x1b[0m' + assert human_formatter('{duration}')(event.format(relative_to=dt.date.today())) == '1h\x1b[0m' event = Event.fromString(event_str, calendar='foobar', locale=LOCALE_MIXED) assert event.start == BERLIN.localize(dt.datetime(2014, 4, 9, 9, 30)) @@ -286,27 +290,29 @@ def test_event_dt_rr(): event = Event.fromString(event_dt_rr, **EVENT_KWARGS) assert event.recurring is True - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30-10:30 An Event ⟳\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09:30-10:30 An Event ⟳\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 09:30-10:30 An Event ⟳\x1b[0m' - assert event.format('{repeat-pattern}', dt.date(2014, 4, 9)) == 'FREQ=DAILY;COUNT=10\x1b[0m' + assert human_formatter('{repeat-pattern}')(event.format(dt.date(2014, 4, 9)) + ) == 'FREQ=DAILY;COUNT=10\x1b[0m' def test_event_d_rr(): event_d_rr = _get_text('event_d_rr') event = Event.fromString(event_d_rr, **EVENT_KWARGS) assert event.recurring is True - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == ' Another Event ⟳\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 9)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == ' Another Event ⟳\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 9))) == \ '09.04.2014 Another Event ⟳\x1b[0m' - assert event.format('{repeat-pattern}', dt.date(2014, 4, 9)) == 'FREQ=DAILY;COUNT=10\x1b[0m' + assert human_formatter('{repeat-pattern}')(event.format(dt.date(2014, 4, 9)) + ) == 'FREQ=DAILY;COUNT=10\x1b[0m' start = dt.date(2014, 4, 10) end = dt.date(2014, 4, 11) event = Event.fromString(event_d_rr, start=start, end=end, **EVENT_KWARGS) assert event.recurring is True - assert event.format(LIST_FORMAT, dt.date(2014, 4, 10)) == ' Another Event ⟳\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 10)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 10))) == ' Another Event ⟳\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 10))) == \ '10.04.2014 Another Event ⟳\x1b[0m' @@ -319,23 +325,24 @@ def test_event_rd(): def test_event_d_long(): event_d_long = _get_text('event_d_long') event = Event.fromString(event_d_long, **EVENT_KWARGS) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '↦ Another Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 10)) == '↔ Another Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 11)) == '⇥ Another Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 12)) == ' Another Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 16)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '↦ Another Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 10))) == '↔ Another Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 11))) == '⇥ Another Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 12))) == ' Another Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 16))) == \ '09.04.2014-11.04.2014 Another Event\x1b[0m' - assert event.format('{duration}', relative_to=dt.date(2014, 4, 11)) == '3d\x1b[0m' + assert human_formatter('{duration}')(event.format( + relative_to=dt.date(2014, 4, 11))) == '3d\x1b[0m' def test_event_d_two_days(): event_d_long = _get_text('event_d_long') event = Event.fromString(event_d_long, **EVENT_KWARGS) event.update_start_end(dt.date(2014, 4, 9), dt.date(2014, 4, 10)) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '↦ Another Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 10)) == '⇥ Another Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 12)) == ' Another Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 10)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '↦ Another Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 10))) == '⇥ Another Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 12))) == ' Another Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 10))) == \ '09.04.2014-10.04.2014 Another Event\x1b[0m' @@ -343,10 +350,10 @@ def test_event_dt_long(): event_dt_long = _get_text('event_dt_long') event = Event.fromString(event_dt_long, **EVENT_KWARGS) - assert event.format(LIST_FORMAT, dt.date(2014, 4, 9)) == '09:30→ An Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 10)) == '↔ An Event\x1b[0m' - assert event.format(LIST_FORMAT, dt.date(2014, 4, 12)) == '→10:30 An Event\x1b[0m' - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 10)) == \ + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 9))) == '09:30→ An Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 10))) == '↔ An Event\x1b[0m' + assert LIST_FORMATTER(event.format(dt.date(2014, 4, 12))) == '→10:30 An Event\x1b[0m' + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 10))) == \ '09.04.2014 09:30-12.04.2014 10:30 An Event\x1b[0m' @@ -368,7 +375,7 @@ def test_event_no_dst(): ) assert normalize_component(event.raw) == normalize_component(cal_no_dst) - assert event.format(SEARCH_FORMAT, dt.date(2014, 4, 10)) == \ + assert SEARCH_FORMATTER(event.format(dt.date(2014, 4, 10))) == \ '09.04.2014 09:30-10:30 An Event\x1b[0m' @@ -415,10 +422,10 @@ def test_multi_uid(): def test_cancelled_instance(): orig_event_str = _get_text('event_rrule_recuid_cancelled') event = Event.fromString(orig_event_str, ref='1405314000', **EVENT_KWARGS) - assert event.format(SEARCH_FORMAT, dt.date(2014, 7, 14)) == \ + assert SEARCH_FORMATTER(event.format(dt.date(2014, 7, 14))) == \ 'CANCELLED 14.07.2014 07:00-12:00 Arbeit ⟳\x1b[0m' event = Event.fromString(orig_event_str, ref='PROTO', **EVENT_KWARGS) - assert event.format(SEARCH_FORMAT, dt.date(2014, 7, 14)) == \ + assert SEARCH_FORMATTER(event.format(dt.date(2014, 7, 14))) == \ '30.06.2014 07:00-12:00 Arbeit ⟳\x1b[0m' @@ -487,7 +494,8 @@ def test_format_24(): event = Event.fromString(event_dt, **EVENT_KWARGS) event.update_start_end(start, end) format_ = '{start-end-time-style} {title}{repeat-symbol}' - assert event.format(format_, dt.date(2014, 4, 9)) == '19:30-24:00 An Event\x1b[0m' + assert human_formatter(format_)(event.format(dt.date(2014, 4, 9)) + ) == '19:30-24:00 An Event\x1b[0m' def test_invalid_format_string(): @@ -495,14 +503,15 @@ def test_invalid_format_string(): event = Event.fromString(event_dt, **EVENT_KWARGS) format_ = '{start-end-time-style} {title}{foo}' with pytest.raises(KeyError): - event.format(format_, dt.date(2014, 4, 9)) + human_formatter(format_)(event.format(dt.date(2014, 4, 9))) def test_format_colors(): event = Event.fromString(_get_text('event_dt_simple'), **EVENT_KWARGS) format_ = '{red}{title}{reset}' - assert event.format(format_, dt.date(2014, 4, 9)) == '\x1b[31mAn Event\x1b[0m\x1b[0m' - assert event.format(format_, dt.date(2014, 4, 9), colors=False) == 'An Event' + formatter = human_formatter(format_) + assert formatter(event.format(dt.date(2014, 4, 9))) == '\x1b[31mAn Event\x1b[0m\x1b[0m' + assert formatter(event.format(dt.date(2014, 4, 9), colors=False)) == 'An Event' def test_event_alarm(): |