diff --git a/news/698.chore b/news/698.chore new file mode 100644 index 000000000..14ddfde28 --- /dev/null +++ b/news/698.chore @@ -0,0 +1 @@ +Added tests for equality and invalid field counts in :class:`~icalendar.prop.adr.vAdr` and :class:`~icalendar.prop.n.vN`. Drafted with OpenAI Codex (GPT-6). :user:`Satorino01` diff --git a/src/icalendar/tests/rfc_6350_vcard/test_vAdr.py b/src/icalendar/tests/rfc_6350_vcard/test_vAdr.py index 044f4b396..22db22d96 100644 --- a/src/icalendar/tests/rfc_6350_vcard/test_vAdr.py +++ b/src/icalendar/tests/rfc_6350_vcard/test_vAdr.py @@ -59,6 +59,23 @@ def test_adr_named_field_access(self): assert adr.fields.country == "USA" +class TestVAdrEquality: + """Test vAdr equality.""" + + @pytest.mark.parametrize( + ("other", "expected"), + [ + (vAdr(";;123 Main St;Springfield;IL;62701;USA"), True), + (vAdr(";;456 Main St;Springfield;IL;62701;USA"), False), + (";;123 Main St;Springfield;IL;62701;USA", False), + ], + ) + def test_adr_equality(self, other, expected): + """Only addresses with matching fields compare equal.""" + adr = vAdr(("", "", "123 Main St", "Springfield", "IL", "62701", "USA")) + assert (adr == other) is expected + + class TestVAdrSerialization: """Test vAdr to_ical and from_ical.""" @@ -76,6 +93,18 @@ def test_adr_from_ical(self): "", "", "123 Main St", "Springfield", "IL", "62701", "USA" ) + @pytest.mark.parametrize( + "ical", + [ + ";;123 Main St;Springfield;IL;62701", + ";;123 Main St;Springfield;IL;62701;USA;extra", + ], + ) + def test_adr_from_ical_requires_7_fields(self, ical): + """Reject addresses with too few or too many fields.""" + with pytest.raises(ValueError, match="ADR must have exactly 7 fields"): + vAdr.from_ical(ical) + def test_adr_round_trip(self): """ADR survives round-trip through to_ical and from_ical.""" original = AdrFields("", "", "123 Main St", "Springfield", "IL", "62701", "USA") diff --git a/src/icalendar/tests/rfc_6350_vcard/test_vN.py b/src/icalendar/tests/rfc_6350_vcard/test_vN.py index 03be4c1de..c6f49726d 100644 --- a/src/icalendar/tests/rfc_6350_vcard/test_vN.py +++ b/src/icalendar/tests/rfc_6350_vcard/test_vN.py @@ -4,6 +4,7 @@ import pytest +from icalendar.error import JCalParsingError from icalendar.prop import vN from icalendar.prop.n import NFields @@ -54,8 +55,25 @@ def test_n_named_field_access(self): assert n.fields.suffix == "Jr." +class TestVNEquality: + """Test vN equality.""" + + @pytest.mark.parametrize( + ("other", "expected"), + [ + (vN("Doe;John;M.;Dr.;Jr."), True), + (vN("Roe;John;M.;Dr.;Jr."), False), + ("Doe;John;M.;Dr.;Jr.", False), + ], + ) + def test_n_equality(self, other, expected): + """Only names with matching fields compare equal.""" + n = vN(("Doe", "John", "M.", "Dr.", "Jr.")) + assert (n == other) is expected + + class TestVNSerialization: - """Test vN to_ical and from_ical.""" + """Test vN serialization.""" def test_n_to_ical(self): """N to_ical generates semicolon-separated format.""" @@ -69,6 +87,24 @@ def test_n_from_ical(self): assert isinstance(result, NFields) assert result == NFields("Doe", "John", "M.", "Dr.", "Jr.") + @pytest.mark.parametrize("ical", ["Doe;John;M.;Dr.", "Doe;John;M.;Dr.;Jr.;extra"]) + def test_n_from_ical_requires_5_fields(self, ical): + """Reject names with too few or too many fields.""" + with pytest.raises(ValueError, match="N must have exactly 5 fields"): + vN.from_ical(ical) + + @pytest.mark.parametrize( + "fields", + [ + ["Doe", "John", "M.", "Dr."], + ["Doe", "John", "M.", "Dr.", "Jr.", "extra"], + ], + ) + def test_n_from_jcal_requires_5_fields(self, fields): + """Reject jCal names with too few or too many fields.""" + with pytest.raises(JCalParsingError, match="N must have 8 elements"): + vN.from_jcal(["n", {}, "text", *fields]) + def test_n_round_trip(self): """N survives round-trip through to_ical and from_ical.""" original = NFields("Doe", "John", "Michael", "Dr.", "Jr.")