o
    .j*                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ dd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- ZdS ).z4Tests for error-tolerant parsing of property values.    N)Calendar)BrokenCalendarProperty)vBroken	vDDDLists	vDDDTypesvRecurvTextc                  C   sZ   d} t | }|dd }|d }t|tsJ |d dgks"J |d dgks+J d	S )
z<Verify properties are parsed immediately during from_ical().   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:20250101T100000Z
RRULE:FREQ=DAILY;COUNT=10
SUMMARY:Test Event
END:VEVENT
END:VCALENDARVEVENTr   RRULEFREQDAILYCOUNT
   N)r   	from_icalwalk
isinstancer   ical_strcaleventrrule r   `/home/thesage/.local/lib/python3.10/site-packages/icalendar/tests/test_error_tolerant_parsing.py"test_properties_parsed_immediately
   s   
r   c                  C   s:   d} t | }|dd }|d }|jd dksJ dS )z2Verify params are accessible on parsed properties.   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART;TZID=America/New_York:20250101T100000
SUMMARY:Test Event
END:VEVENT
END:VCALENDARr
   r   DTSTARTTZIDzAmerica/New_YorkN)r   r   r   paramsr   r   r   dtstartr   r   r   test_params_accessible!   s
   

r!   c                 C   s   | d }| dd }|d }t|tsJ t|tsJ t|dks%J |jdks,J |jdus3J |jdus:J t|j	dksCJ |j	d d dksNJ dS )z.Verify broken properties fall back to vBroken.broken_dtstartr
   r   r   INVALID-DATEN   )
r   r   r   r   strproperty_nameexpected_typeparse_errorlenerrors	calendarsr   r   r    r   r   r   %test_broken_property_vbroken_fallback7   s   r-   c                  C   st   d} t | }|dd }|d }t|tsJ |jjdks!J |d }t|ts,J |d }t|dks8J d	S )
z<Verify one broken property doesn't prevent accessing others.s   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:INVALID-DATE
DTEND:20250102T120000Z
SUMMARY:Test Event
END:VEVENT
END:VCALENDARr
   r   DTEND  r   SUMMARY
Test EventN)	r   r   r   r   r   dtyearr   r%   )r   r   r   dtendr    summaryr   r   r   (test_broken_property_doesnt_block_othersN   s   
r6   c                  C   s\   d} t | }t | }|dd }|dd }|d |d ks$J |d dks,J dS )z1Verify equality comparisons work with properties.m   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
SUMMARY:Test Event
END:VEVENT
END:VCALENDARr
   r   r0   r1   Nr   r   r   )r   cal1cal2event1event2r   r   r   test_property_equalityl   s   
	
r=   c                  C   s>   d} t | }| }d|v sJ d|v sJ d|v sJ dS )z1Verify to_ical() works correctly with properties.r	   s   DTSTART:20250101T100000Zs   RRULE:FREQ=DAILY;COUNT=10s   SUMMARY:Test EventN)r   r   to_ical)r   r   outputr   r   r   test_property_to_ical   s   
r@   c                  C   s\   d} t | }|dd }|d }t|tsJ t|dks!J tdd |D s,J dS )	z*Verify lists of properties work correctly.s   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:20250101T100000Z
EXDATE:20250105T100000Z
EXDATE:20250106T100000Z
SUMMARY:Test Event
END:VEVENT
END:VCALENDARr
   r   EXDATE   c                 s   s    | ]}t |tV  qd S )N)r   r   ).0r2   r   r   r   	<genexpr>   s    z3test_multiple_properties_in_list.<locals>.<genexpr>N)r   r   r   r   listr)   all)r   r   r   exdatesr   r   r    test_multiple_properties_in_list   s   
rH   c                  C   sf   d} t | }|dd }|d }t|tsJ |jjdks!J |jjdks)J |jjdks1J dS )z+Verify TZID parameter is handled correctly.r   r
   r   r   r/   r$   N)	r   r   r   r   r   r2   r3   monthdayr   r   r   r   test_property_with_tzid   s   

rK   c                  C   sF   d} t | }|dd }|d }t|tsJ t|dks!J dS )zHVerify FREEBUSY comma-separated values work with error-tolerant parsing.s   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VFREEBUSY
UID:test-123
DTSTART:20250101T000000Z
DTEND:20250102T000000Z
FREEBUSY:20250101T100000Z/20250101T120000Z,20250101T140000Z/20250101T160000Z
END:VFREEBUSY
END:VCALENDAR	VFREEBUSYr   FREEBUSYrB   N)r   r   r   r   rE   r)   )r   r   fbfreebusyr   r   r   $test_freebusy_comma_separated_values   s   
rP   c                  C   s,   d} t | }|dd }d|vsJ dS )z0Verify empty RDATE values are handled correctly.s   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:20250101T100000Z
RDATE:
SUMMARY:Test Event
END:VEVENT
END:VCALENDARr
   r   RDATENr8   r   r   r   r   r   r   test_empty_rdate_workaround   s   
rS   c                 C   sb   | d }| dd }|d }dt|v sJ dt|v sJ dt|v s'J dt|v s/J d	S )
z&Verify vBroken repr includes metadata.r"   r
   r   r   r   r#   r'   r&   N)r   reprr+   r   r   r   test_vbroken_repr   s   rU   c                  C   sH   d} t | }|dd }|d }t|tsJ t|jdks"J dS )z'Verify ignore_exceptions flag behavior.sw   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:INVALID
SUMMARY:Test
END:VEVENT
END:VCALENDARr
   r   r   N)r   r   r   r   r   r)   r*   )ical_str_eventr   r   r    r   r   r   %test_ignore_exceptions_flag_respected  s   

rW   c                  C   s8   d} t | }|dd }|d }t|dksJ dS )zVerify __str__ works.r7   r
   r   r0   r1   N)r   r   r   r%   )r   r   r   r5   r   r   r   test_property_str  s
   
	rX   c                 C   sZ   | d }| dd }|d }t|tsJ |jdksJ |jdus$J |jdus+J dS )z+Verify vBroken stores parse error metadata.r"   r
   r   r   N)r   r   r   r&   r'   r(   r+   r   r   r   test_vbroken_metadata0  s   rY   c                  C   s^   d} t | }|dd }t|jdksJ |jd d dks"J |jd d dks-J dS )	z@Verify errors available after from_ical without property access.s   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:INVALID-DATE
DTEND:ALSO-INVALID
SUMMARY:Test
END:VEVENT
END:VCALENDARr
   r   rB   r   r$   r.   N)r   r   r   r)   r*   rR   r   r   r   !test_errors_populated_immediately<  s   
rZ   c                  C   s`   d} t | }|dd }|d }t|tsJ t|jdks"J tdd |jD s.J dS )	zVerify TypeError exceptions are caught in error-tolerant mode.

    This test addresses the concern raised in PR #1044 review that
    TypeError exceptions should be handled consistently with ValueError.
    s   BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
BEGIN:VEVENT
UID:test-123
DTSTART:20250101T100000Z
RRULE:FREQ=INVALID_TYPE_CAUSES_ERROR
SUMMARY:Test Event
END:VEVENT
END:VCALENDARr
   r   r   r$   c                 s   s    | ]	}d |d v V  qdS )r   r   Nr   )rC   errorr   r   r   rD   o  s    z;test_typeerror_handling_in_tolerant_mode.<locals>.<genexpr>N)r   r   r   r   r   r)   r*   anyr   r   r   r   (test_typeerror_handling_in_tolerant_modeR  s   
r]   c                 C   s@   | d }| dd }|d }t|tsJ t|jtsJ dS )z=Verify parse_error stores the actual exception, not a string.r"   r
   r   r   N)r   r   r   r(   	Exceptionr+   r   r   r   $test_parse_error_is_exception_objectr  s
   r_   c                  C   s~   t dddtdd} tt}| j W d   n1 sw   Y  |jj| ju s+J dt	|jv s4J dt	|jv s=J dS )zBVerify accessing missing attributes raises BrokenCalendarProperty.INVALIDr   r   	bad valuer&   r'   r(   N)
r   
ValueErrorpytestraisesr   r2   value	__cause__r(   r%   )brokenexc_infor   r   r   4test_vbroken_getattr_raises_broken_calendar_property|  s   rj   c                  C   sb   t dddtdd} | jdksJ | jdksJ t| jts J t| jts(J | jdus/J dS )z/Verify normal attributes still work on vBroken.r`   r   r   ra   rb   N)	r   rc   r&   r'   r   r(   r   dictencoding)rh   r   r   r   2test_vbroken_getattr_preserves_existing_attributes  s   rm   c                 C   sZ   | d }| dd }tt}|j W d   n1 sw   Y  |jjdus+J dS )zEVerify event.DTSTART raises BrokenCalendarProperty for broken values.r"   r
   r   N)r   rd   re   r   r   rf   rg   )r,   r   r   ri   r   r   r   2test_event_dtstart_raises_broken_calendar_property  s   rn   ) __doc__rd   	icalendarr   icalendar.errorr   icalendar.propr   r   r   r   r   r   r!   r-   r6   r=   r@   rH   rK   rP   rS   rU   rW   rX   rY   rZ   r]   r_   rj   rm   rn   r   r   r   r   <module>   s2     
