Skip to content
Closed
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
28 changes: 28 additions & 0 deletions src/icalendar/prop/recur/weekday.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ def __new__(
self.params = Parameters(params)
return self

@property
def ical_value(self) -> str:
"""Return the Python string value.

This property provides access to the underlying weekday string value,
which may be a simple weekday (e.g., "MO") or a weekday with relative
position (e.g., "2FR" for second Friday, "-1SU" for last Sunday).

Returns:
str: The weekday string value.

Example:
>>> from icalendar.prop import vWeekday
>>> wd = vWeekday("MO")
>>> wd.ical_value
'MO'
>>> wd2 = vWeekday("2FR")
>>> wd2.ical_value
'2FR'
>>> wd3 = vWeekday("-1SU")
>>> wd3.ical_value
'-1SU'

See Also:
:rfc:`5545#section-3.3.10` for the weekday value type specification.
"""
return str(self)

def to_ical(self):
return self.encode(DEFAULT_ENCODING).upper()

Expand Down
68 changes: 51 additions & 17 deletions src/icalendar/tests/prop/test_vWeekday.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
import pytest
"""Test vWeekday ical_value property."""

from icalendar.prop import vWeekday


def test_simple():
weekday = vWeekday("SU")
assert weekday.to_ical() == b"SU"
assert weekday.weekday == "SU"
assert weekday.relative is None
def test_ical_value_simple_weekday():
"""ical_value property returns string for simple weekday."""
wd = vWeekday("MO")
assert wd.ical_value == "MO"
assert isinstance(wd.ical_value, str)


def test_relative():
weekday = vWeekday("-1MO")
assert weekday.to_ical() == b"-1MO"
assert weekday.weekday == "MO"
assert weekday.relative == -1
def test_ical_value_all_weekdays():
"""ical_value property works for all weekday abbreviations."""
weekdays = ["SU", "MO", "TU", "WE", "TH", "FR", "SA"]
for day in weekdays:
wd = vWeekday(day)
assert wd.ical_value == day


def test_roundtrip():
assert vWeekday.from_ical(vWeekday("+2TH").to_ical()) == "+2TH"
def test_ical_value_positive_relative():
"""ical_value property returns string with positive relative position."""
wd = vWeekday("2FR")
assert wd.ical_value == "2FR"
assert wd.relative == 2
assert wd.weekday == "FR"


def test_error():
"""Error: Expected weekday abbrevation, got: -100MO"""
with pytest.raises(ValueError):
vWeekday.from_ical("-100MO")
def test_ical_value_negative_relative():
"""ical_value property returns string with negative relative position."""
wd = vWeekday("-1SU")
assert wd.ical_value == "-1SU"
assert wd.relative == -1
assert wd.weekday == "SU"


def test_ical_value_from_ical():
"""ical_value property works with weekday parsed from ical string."""
wd = vWeekday.from_ical("MO")
assert wd.ical_value == "MO"

wd2 = vWeekday.from_ical("3TH")
assert wd2.ical_value == "3TH"
assert wd2.relative == 3


def test_ical_value_case_insensitive():
"""ical_value property handles case-insensitive input."""
wd_lower = vWeekday("mo")
wd_upper = vWeekday("MO")
# Both should normalize to uppercase
assert wd_lower.ical_value.upper() == "MO"
assert wd_upper.ical_value == "MO"


def test_ical_value_with_plus_sign():
"""ical_value property handles explicit plus sign."""
wd = vWeekday("+2WE")
assert wd.ical_value == "+2WE"
assert wd.relative == 2
assert wd.weekday == "WE"