ical_rust 0.8.12

A lightweight, efficient, and robust iCalendar (ics) parser and recurrence expander library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
use super::*;

#[test]
fn test_readme_example() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome:20230101T090000\r\n\
LAST-MODIFIED:20230101T170000Z\r\n\
CREATED:20230101T170000Z\r\n\
DTSTAMP:20230101T170000Z\r\n\
SUMMARY:Rust Pair Programming\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=WEEKLY;BYDAY=MO,WE,FR;COUNT=10\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    let event = &cal.events[0];
    assert_eq!(event.summary, "Rust Pair Programming");

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 10);
}

#[test]
fn test_bug1_monthly_recurrence_december() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20221105\r\n\
LAST-MODIFIED:20221105T170000Z\r\n\
CREATED:20221105T170000Z\r\n\
DTSTAMP:20221105T170000Z\r\n\
SUMMARY:Monthly Team Sync\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=MONTHLY;BYMONTHDAY=5;COUNT=3\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let event = &cal.events[0];

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 3);

    // Occurrence 1: Nov 5, 2022
    assert_eq!(occurrences[0].start.year(), 2022);
    assert_eq!(occurrences[0].start.month(), 11);

    // Occurrence 2: Dec 5, 2022
    assert_eq!(
        occurrences[1].start.year(),
        2022,
        "Second occurrence should be in 2022 (December)"
    );
    assert_eq!(
        occurrences[1].start.month(),
        12,
        "Second occurrence should be December (12)"
    );

    // Occurrence 3: Jan 5, 2023
    assert_eq!(occurrences[2].start.year(), 2023);
    assert_eq!(occurrences[2].start.month(), 1);
}

#[test]
fn test_bug2_double_interval_monthly() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20221105\r\n\
LAST-MODIFIED:20221105T170000Z\r\n\
CREATED:20221105T170000Z\r\n\
DTSTAMP:20221105T170000Z\r\n\
SUMMARY:Bi-Monthly Team Sync\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=MONTHLY;INTERVAL=2;BYMONTHDAY=5;COUNT=3\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let event = &cal.events[0];

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 3);

    // Occurrence 1: Nov 5, 2022
    assert_eq!(occurrences[0].start.year(), 2022);
    assert_eq!(occurrences[0].start.month(), 11);

    // Occurrence 2: Jan 5, 2023 (+2 months)
    assert_eq!(
        occurrences[1].start.year(),
        2023,
        "Second occurrence should be in 2023 (January)"
    );
    assert_eq!(
        occurrences[1].start.month(),
        1,
        "Second occurrence should be January (1)"
    );

    // Occurrence 3: Mar 5, 2023 (+2 months)
    assert_eq!(occurrences[2].start.year(), 2023);
    assert_eq!(
        occurrences[2].start.month(),
        3,
        "Third occurrence should be March (3)"
    );
}

#[test]
fn test_bug3_invalid_timezone_fails_safely() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Invalid/Timezone:20230101T090000\r\n\
LAST-MODIFIED:20230101T170000Z\r\n\
CREATED:20230101T170000Z\r\n\
DTSTAMP:20230101T170000Z\r\n\
SUMMARY:Invalid TZID test\r\n\
SEQUENCE:0\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let result = VCalendar::try_from(ics_content);
    assert!(
        result.is_err(),
        "Expected an error instead of panic or successful parse"
    );
}

#[test]
fn test_bug3_missing_datetime_fails_safely() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome\r\n\
LAST-MODIFIED:20230101T170000Z\r\n\
CREATED:20230101T170000Z\r\n\
DTSTAMP:20230101T170000Z\r\n\
SUMMARY:Missing datetime test\r\n\
SEQUENCE:0\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let result = VCalendar::try_from(ics_content);
    assert!(
        result.is_err(),
        "Expected an error instead of panic or successful parse"
    );
}

#[test]
fn test_bug4_yearly_by_month_by_day() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome:20221124T090000\r\n\
LAST-MODIFIED:20221124T170000Z\r\n\
CREATED:20221124T170000Z\r\n\
DTSTAMP:20221124T170000Z\r\n\
SUMMARY:Thanksgiving Dinner\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=4TH;COUNT=3\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    let event = &cal.events[0];

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 3);

    // Occurrence 1: Nov 24, 2022
    assert_eq!(occurrences[0].start.year(), 2022);
    assert_eq!(occurrences[0].start.month(), 11);
    assert_eq!(occurrences[0].start.day(), 24);

    // Occurrence 2: Nov 23, 2023
    assert_eq!(occurrences[1].start.year(), 2023);
    assert_eq!(occurrences[1].start.month(), 11);
    assert_eq!(occurrences[1].start.day(), 23);

    // Occurrence 3: Nov 28, 2024
    assert_eq!(occurrences[2].start.year(), 2024);
    assert_eq!(occurrences[2].start.month(), 11);
    assert_eq!(occurrences[2].start.day(), 28);
}

#[test]
fn test_bug5_exdate_multi_value() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome:20230101T090000\r\n\
LAST-MODIFIED:20230101T170000Z\r\n\
CREATED:20230101T170000Z\r\n\
DTSTAMP:20230101T170000Z\r\n\
SUMMARY:Daily Standup\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=DAILY;COUNT=5\r\n\
EXDATE;TZID=Europe/Rome:20230102T090000,20230104T090000\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    let event = &cal.events[0];

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 5, "Expected exactly 5 occurrences total");

    // Occurrence 1: Jan 1, 2023
    assert_eq!(occurrences[0].start.year(), 2023);
    assert_eq!(occurrences[0].start.month(), 1);
    assert_eq!(occurrences[0].start.day(), 1);

    // Occurrence 2: Jan 3, 2023 (Jan 2 is excluded)
    assert_eq!(occurrences[1].start.year(), 2023);
    assert_eq!(occurrences[1].start.month(), 1);
    assert_eq!(occurrences[1].start.day(), 3);

    // Occurrence 3: Jan 5, 2023 (Jan 4 is excluded)
    assert_eq!(occurrences[2].start.year(), 2023);
    assert_eq!(occurrences[2].start.month(), 1);
    assert_eq!(occurrences[2].start.day(), 5);

    // Occurrence 4: Jan 6, 2023
    assert_eq!(occurrences[3].start.year(), 2023);
    assert_eq!(occurrences[3].start.month(), 1);
    assert_eq!(occurrences[3].start.day(), 6);

    // Occurrence 5: Jan 7, 2023
    assert_eq!(occurrences[4].start.year(), 2023);
    assert_eq!(occurrences[4].start.month(), 1);
    assert_eq!(occurrences[4].start.day(), 7);
}

#[test]
fn test_bug6_missing_begin_tag_fails_safely() {
    let ics_content = "INVALID CALENDAR CONTENT\r\nSUMMARY:Test";
    let result = VCalendar::try_from(ics_content);
    assert!(
        result.is_err(),
        "Expected an error instead of panic or successful parse"
    );
}

#[test]
fn test_bug7_lf_line_endings_supported() {
    let ics_content = "BEGIN:VCALENDAR\n\
BEGIN:VEVENT\n\
DTSTART;TZID=Europe/Rome:20230101T090000\n\
LAST-MODIFIED:20230101T170000Z\n\
CREATED:20230101T170000Z\n\
DTSTAMP:20230101T170000Z\n\
SUMMARY:LF Line Ending Test\n\
SEQUENCE:0\n\
END:VEVENT\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    assert_eq!(cal.events[0].summary, "LF Line Ending Test");
}

#[test]
fn test_bug8_tab_line_folding_supported() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome:20230101T090000\r\n\
LAST-MODIFIED:20230101T170000Z\r\n\
CREATED:20230101T170000Z\r\n\
DTSTAMP:20230101T170000Z\r\n\
SUMMARY:LF Line Ending \r\n\
\twith Tab Folding\r\n\
SEQUENCE:0\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    assert_eq!(cal.events[0].summary, "LF Line Ending with Tab Folding");
}

#[test]
fn test_bug9_month_skipping_for_end_of_month_dates() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230131\r\n\
LAST-MODIFIED:20230131T170000Z\r\n\
CREATED:20230131T170000Z\r\n\
DTSTAMP:20230131T170000Z\r\n\
SUMMARY:End of Month Recurrence\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=MONTHLY;BYMONTHDAY=31;COUNT=5\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    let event = &cal.events[0];

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 5);

    // Occurrence 1: Jan 31, 2023
    assert_eq!(occurrences[0].start.year(), 2023);
    assert_eq!(occurrences[0].start.month(), 1);
    assert_eq!(occurrences[0].start.day(), 31);

    // Occurrence 2: Feb 28, 2023 (clamped)
    assert_eq!(occurrences[1].start.year(), 2023);
    assert_eq!(occurrences[1].start.month(), 2);
    assert_eq!(occurrences[1].start.day(), 28);

    // Occurrence 3: Mar 31, 2023
    assert_eq!(occurrences[2].start.year(), 2023);
    assert_eq!(occurrences[2].start.month(), 3);
    assert_eq!(occurrences[2].start.day(), 31);

    // Occurrence 4: Apr 30, 2023 (clamped)
    assert_eq!(occurrences[3].start.year(), 2023);
    assert_eq!(occurrences[3].start.month(), 4);
    assert_eq!(occurrences[3].start.day(), 30);

    // Occurrence 5: May 31, 2023
    assert_eq!(occurrences[4].start.year(), 2023);
    assert_eq!(occurrences[4].start.month(), 5);
    assert_eq!(occurrences[4].start.day(), 31);
}

#[test]
fn test_bug10_weekly_by_day_interval_skipping() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230103\r\n\
LAST-MODIFIED:20230103T170000Z\r\n\
CREATED:20230103T170000Z\r\n\
DTSTAMP:20230103T170000Z\r\n\
SUMMARY:Bi-Weekly Multi-Day Meeting\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,TH;COUNT=4\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    let event = &cal.events[0];

    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 4);

    // Occurrence 1: Tue Jan 3, 2023
    assert_eq!(occurrences[0].start.year(), 2023);
    assert_eq!(occurrences[0].start.month(), 1);
    assert_eq!(occurrences[0].start.day(), 3);

    // Occurrence 2: Thu Jan 5, 2023
    assert_eq!(occurrences[1].start.year(), 2023);
    assert_eq!(occurrences[1].start.month(), 1);
    assert_eq!(occurrences[1].start.day(), 5);

    // Occurrence 3: Tue Jan 17, 2023 (skipped week of Jan 9-15)
    assert_eq!(occurrences[2].start.year(), 2023);
    assert_eq!(occurrences[2].start.month(), 1);
    assert_eq!(occurrences[2].start.day(), 17);

    // Occurrence 4: Thu Jan 19, 2023
    assert_eq!(occurrences[3].start.year(), 2023);
    assert_eq!(occurrences[3].start.month(), 1);
    assert_eq!(occurrences[3].start.day(), 19);
}

#[test]
fn test_issue7_dst_transition_preserves_local_wall_clock_time() {
    // Europe/Rome transitions from CET (UTC+1) to CEST (UTC+2) on 2023-03-26
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome:20230325T090000\r\n\
LAST-MODIFIED:20230325T080000Z\r\n\
CREATED:20230325T080000Z\r\n\
DTSTAMP:20230325T080000Z\r\n\
SUMMARY:Morning Standup Across DST\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=DAILY;COUNT=2\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let event = &cal.events[0];
    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 2);

    // 2023-03-25 09:00 CET (UTC+1) == 08:00 UTC
    assert_eq!(occurrences[0].start.hour(), 8);
    // 2023-03-26 09:00 CEST (UTC+2) == 07:00 UTC
    assert_eq!(occurrences[1].start.hour(), 7);
}

#[test]
fn test_issue8_monthly_by_day_same_month_and_day1() {
    // Jan 2023: Jan 30 is Monday. Feb 2023: Feb 1 is Wednesday.
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230127\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY:Monthly Mon/Wed/Fri\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=MONTHLY;BYDAY=MO,WE,FR;COUNT=3\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let occurrences: Vec<_> = cal.events[0].into_iter().collect();
    assert_eq!(occurrences.len(), 3);

    // 1st: Fri Jan 27, 2023
    assert_eq!(
        (occurrences[0].start.month(), occurrences[0].start.day()),
        (1, 27)
    );
    // 2nd: Mon Jan 30, 2023 (same month!)
    assert_eq!(
        (occurrences[1].start.month(), occurrences[1].start.day()),
        (1, 30)
    );
    // 3rd: Wed Feb 1, 2023 (day 1 of next month!)
    assert_eq!(
        (occurrences[2].start.month(), occurrences[2].start.day()),
        (2, 1)
    );
}

#[test]
fn test_issue9_yearly_by_month_month_day_and_leap_year() {
    // Leap day event: Feb 29, 2024 with YearlyByMonthByMonthDay
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20240229\r\n\
LAST-MODIFIED:20240101T000000Z\r\n\
CREATED:20240101T000000Z\r\n\
DTSTAMP:20240101T000000Z\r\n\
SUMMARY:Leap Day Event\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29;COUNT=5\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let occurrences: Vec<_> = cal.events[0].into_iter().collect();
    assert_eq!(occurrences.len(), 5);
    // 2024 is leap year -> Feb 29
    assert_eq!(
        (
            occurrences[0].start.year(),
            occurrences[0].start.month(),
            occurrences[0].start.day()
        ),
        (2024, 2, 29)
    );
    // 2028 is leap year -> should return to Feb 29, not drift to Feb 28!
    assert_eq!(
        (
            occurrences[4].start.year(),
            occurrences[4].start.month(),
            occurrences[4].start.day()
        ),
        (2028, 2, 29)
    );
}

#[test]
fn test_issue10_exdate_date_comparison_ignores_time() {
    // Event with DateTime at 09:00 UTC, and EXDATE;VALUE=DATE:20230102 (midnight 00:00 UTC)
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART:20230101T090000Z\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY:Daily Meeting with Date Exdate\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=DAILY;COUNT=2\r\n\
EXDATE;VALUE=DATE:20230102\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let occurrences: Vec<_> = cal.events[0].into_iter().collect();
    assert_eq!(occurrences.len(), 2);
    // Jan 2 should be excluded, so the 2nd occurrence must be Jan 3
    assert_eq!(occurrences[0].start.day(), 1);
    assert_eq!(occurrences[1].start.day(), 3);
}

#[test]
fn test_issue11_vevent_property_parameters_and_plain_tags() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART:20230101\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY;LANGUAGE=en-US:Team Sync; Planning\r\n\
DESCRIPTION;LANGUAGE=en-US:Notes for the meeting\r\n\
ORGANIZER:mailto:boss@example.com\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=DAILY;COUNT=2\r\n\
EXDATE:20230102\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let event = &cal.events[0];
    assert_eq!(event.summary, "Team Sync; Planning");
    assert_eq!(event.description.as_deref(), Some("Notes for the meeting"));
    assert_eq!(event.organizer.as_deref(), Some("mailto:boss@example.com"));
    let occurrences: Vec<_> = event.into_iter().collect();
    assert_eq!(occurrences.len(), 2);
    assert_eq!(occurrences[0].start.day(), 1);
    assert_eq!(occurrences[1].start.day(), 3);
}

#[test]
fn test_issue13_rrule_flexible_token_order_and_monthly_defaults() {
    // 1. FREQ not first
    let rrule1: crate::rrule::RRule = "COUNT=3;FREQ=DAILY".parse().unwrap();
    assert_eq!(rrule1.frequency, crate::Frequency::Daily);

    // 2. FREQ=YEARLY;BYMONTH=6 without BYDAY/BYMONTHDAY
    let rrule2: crate::rrule::RRule = "FREQ=YEARLY;BYMONTH=6;COUNT=2".parse().unwrap();
    assert_eq!(rrule2.frequency, crate::Frequency::Yearly);
    assert_eq!(rrule2.by_month, vec![6]);

    // 3. FREQ=MONTHLY without BYDAY/BYMONTHDAY and negative BYMONTHDAY=-1
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230115\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY:Plain Monthly\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=MONTHLY;COUNT=3\r\n\
END:VEVENT\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230131\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY:Last Day of Month\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=MONTHLY;BYMONTHDAY=-1;COUNT=3\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let occ0: Vec<_> = cal.events[0].into_iter().collect();
    assert_eq!(occ0.len(), 3);
    assert_eq!((occ0[1].start.month(), occ0[1].start.day()), (2, 15));
    assert_eq!((occ0[2].start.month(), occ0[2].start.day()), (3, 15));

    let occ1: Vec<_> = cal.events[1].into_iter().collect();
    assert_eq!(occ1.len(), 3);
    assert_eq!((occ1[0].start.month(), occ1[0].start.day()), (1, 31));
    assert_eq!((occ1[1].start.month(), occ1[1].start.day()), (2, 28));
    assert_eq!((occ1[2].start.month(), occ1[2].start.day()), (3, 31));
}

#[test]
fn test_issue14_first_occurrence_respects_until_and_count() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART:20230110T090000Z\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY:Zero Count Event\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=DAILY;COUNT=0\r\n\
END:VEVENT\r\n\
BEGIN:VEVENT\r\n\
DTSTART:20230110T090000Z\r\n\
LAST-MODIFIED:20230101T000000Z\r\n\
CREATED:20230101T000000Z\r\n\
DTSTAMP:20230101T000000Z\r\n\
SUMMARY:Expired Until Event\r\n\
SEQUENCE:0\r\n\
RRULE:FREQ=DAILY;UNTIL=20230101T000000Z\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let occ0: Vec<_> = cal.events[0].into_iter().collect();
    assert!(occ0.is_empty());

    let occ1: Vec<_> = cal.events[1].into_iter().collect();
    assert!(occ1.is_empty());
}

#[test]
fn test_issue16_vevent_optional_created_last_modified_sequence() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART:20230101T090000Z\r\n\
DTSTAMP:20230101T080000Z\r\n\
SUMMARY:Minimal Event\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 1);
    let event = &cal.events[0];
    assert_eq!(event.summary, "Minimal Event");
    assert_eq!(event.sequence, 0);
    assert_eq!(event.dt_created, event.dt_stamp);
    assert_eq!(event.dt_last_modified, event.dt_stamp);
}

#[test]
fn test_issue17_vcalendar_validates_root_tag_and_ignores_other_components() {
    // Non-VCALENDAR root block must be rejected
    let not_a_calendar = "BEGIN:VEVENT\r\nDTSTART:20230101T090000Z\r\nSUMMARY:Test\r\nEND:VEVENT";
    assert!(VCalendar::try_from(not_a_calendar).is_err());

    // VCALENDAR containing VTODO alongside VEVENT should succeed and ignore VTODO
    let with_vtodo = "BEGIN:VCALENDAR\r\n\
BEGIN:VTODO\r\n\
SUMMARY:Buy milk\r\n\
END:VTODO\r\n\
BEGIN:VEVENT\r\n\
DTSTART:20230101T090000Z\r\n\
SUMMARY:Actual Event\r\n\
END:VEVENT\r\n\
END:VCALENDAR";
    let cal = VCalendar::try_from(with_vtodo).unwrap();
    assert_eq!(cal.events.len(), 1);
    assert_eq!(cal.events[0].summary, "Actual Event");
}

#[test]
fn test_improvement1_unified_date_or_date_time_preserves_tz_and_naive_date() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=Europe/Rome:20230101T090000\r\n\
DTEND;TZID=America/New_York:20230101T050000\r\n\
SUMMARY:Cross-Timezone Event\r\n\
EXDATE;TZID=Europe/Rome:20230102T090000\r\n\
END:VEVENT\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230510\r\n\
SUMMARY:All Day Event\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    assert_eq!(cal.events.len(), 2);

    let ev0 = &cal.events[0];
    assert_eq!(ev0.dt_start.timezone(), Some(chrono_tz::Europe::Rome));
    assert_eq!(ev0.dt_end.timezone(), Some(chrono_tz::America::New_York));
    assert_eq!(ev0.exdates[0].timezone(), Some(chrono_tz::Europe::Rome));

    let ev1 = &cal.events[1];
    assert_eq!(ev1.dt_start.timezone(), None);
    assert_eq!(
        ev1.dt_start,
        crate::DateOrDateTime::WholeDay(chrono::NaiveDate::from_ymd_opt(2023, 5, 10).unwrap())
    );
}

#[test]
fn test_improvement2_composable_rrule_multi_ordinal_byday_and_interval_validation() {
    // INTERVAL=0 must be rejected at parse time
    assert!(
        "FREQ=WEEKLY;INTERVAL=0;BYDAY=MO"
            .parse::<crate::RRule>()
            .is_err()
    );

    // Multi-ordinal BYDAY=1MO,3MO in a monthly rule
    // Jan 2023 Mondays: Jan 2 (1st), Jan 9 (2nd), Jan 16 (3rd), Jan 23 (4th), Jan 30 (5th)
    // Feb 2023 Mondays: Feb 6 (1st), Feb 13 (2nd), Feb 20 (3rd), Feb 27 (4th)
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230102\r\n\
SUMMARY:1st and 3rd Monday\r\n\
RRULE:FREQ=MONTHLY;BYDAY=1MO,3MO;COUNT=4\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let occ: Vec<_> = cal.events[0].into_iter().collect();
    assert_eq!(occ.len(), 4);
    assert_eq!((occ[0].start.month(), occ[0].start.day()), (1, 2));
    assert_eq!((occ[1].start.month(), occ[1].start.day()), (1, 16));
    assert_eq!((occ[2].start.month(), occ[2].start.day()), (2, 6));
    assert_eq!((occ[3].start.month(), occ[3].start.day()), (2, 20));
}

#[test]
fn test_improvement4_content_line_multi_params_and_text_unescaping() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE-TIME;TZID=\"Europe/Rome\":20230101T090000\r\n\
SUMMARY;LANGUAGE=en-US:Meeting\\, Planning\\; Phase 1\\nAgenda\r\n\
DESCRIPTION:Line 1\\NLine 2\\\\End\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let ev = &cal.events[0];
    assert_eq!(ev.dt_start.timezone(), Some(chrono_tz::Europe::Rome));
    assert_eq!(ev.summary, "Meeting, Planning; Phase 1\nAgenda");
    assert_eq!(ev.description.as_deref(), Some("Line 1\nLine 2\\End"));
}

#[test]
fn test_improvement5_calendar_query_api() {
    let ics_content = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230101\r\n\
SUMMARY:Daily Standup\r\n\
RRULE:FREQ=DAILY\r\n\
END:VEVENT\r\n\
BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230102\r\n\
SUMMARY:One-Off Review\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal = VCalendar::try_from(ics_content).unwrap();
    let start = "20230102".parse::<DateOrDateTime>().unwrap();
    let end = "20230104".parse::<DateOrDateTime>().unwrap();

    let between = cal.occurrences_between(start, end).unwrap();
    assert_eq!(between.len(), 4);
    assert_eq!(between[0].1.start.day(), 2);
    assert_eq!(between[1].1.start.day(), 2);
    assert_eq!(between[2].1.start.day(), 3);
    assert_eq!(between[3].1.start.day(), 4);

    let next = cal.next_occurrences_since(start).unwrap();
    assert_eq!(next.len(), 2);
    assert_eq!(next[0].1.occurrence.start.day(), 2);
    assert_eq!(next[1].1.occurrence.start.day(), 2);
}

#[test]
fn test_improvement6_from_str_eq_and_lightweight_errors() {
    let ev_str = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230101\r\n\
SUMMARY:Direct Event Parse\r\n\
END:VEVENT";

    let ev1: VEvent = ev_str.parse().unwrap();
    let ev2 = VEvent::try_from(ev_str).unwrap();
    assert_eq!(ev1, ev2);

    let cal_str = format!("BEGIN:VCALENDAR\r\n{ev_str}\r\nEND:VCALENDAR");
    let cal1: VCalendar = cal_str.parse().unwrap();
    let cal2 = VCalendar::try_from(cal_str.as_str()).unwrap();
    assert_eq!(cal1, cal2);

    // Verify lightweight error equality and clone
    let missing_dtstart = "BEGIN:VEVENT\r\nSUMMARY:No Start\r\nEND:VEVENT";
    let err = missing_dtstart.parse::<VEvent>().unwrap_err();
    assert_eq!(
        err.clone(),
        crate::errors::VEventParseError::MissingMandatoryField {
            field: "DTSTART".to_string()
        }
    );
}

#[test]
fn test_rfc1_vevent_duration_default_date_end_optional_summary_and_uid() {
    // 1. VEVENT with DURATION (PT1H30M), UID, and no SUMMARY
    let ev_with_duration = "BEGIN:VEVENT\r\n\
UID:event-123@example.com\r\n\
DTSTART:20230101T090000Z\r\n\
DURATION:PT1H30M\r\n\
END:VEVENT";
    let ev: VEvent = ev_with_duration.parse().unwrap();
    assert_eq!(ev.uid.as_deref(), Some("event-123@example.com"));
    assert_eq!(ev.summary, "");
    assert_eq!(ev.dt_end - ev.dt_start, chrono::Duration::minutes(90));

    // 2. VEVENT with WholeDay DTSTART and no DTEND/DURATION defaults to 1 day
    let all_day_ev = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230101\r\n\
SUMMARY:New Year\r\n\
END:VEVENT";
    let ev_day: VEvent = all_day_ev.parse().unwrap();
    assert_eq!(
        ev_day.dt_end,
        DateOrDateTime::WholeDay(chrono::NaiveDate::from_ymd_opt(2023, 1, 2).unwrap())
    );

    // 3. VEVENT with both DTEND and DURATION must fail per RFC 5545 §3.6.1
    let invalid_both = "BEGIN:VEVENT\r\n\
DTSTART:20230101T090000Z\r\n\
DTEND:20230101T100000Z\r\n\
DURATION:PT1H\r\n\
SUMMARY:Conflict\r\n\
END:VEVENT";
    assert!(invalid_both.parse::<VEvent>().is_err());
}

#[test]
fn test_rfc2_rdate_and_recurrence_id_override() {
    // 1. RDATE adds additional recurrence instances in chronological order
    let ev_rdate: VEvent = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230101\r\n\
SUMMARY:With RDATE\r\n\
RRULE:FREQ=DAILY;COUNT=2\r\n\
RDATE;VALUE=DATE:20230110,20230105\r\n\
EXDATE;VALUE=DATE:20230105\r\n\
END:VEVENT"
        .parse()
        .unwrap();
    let occs: Vec<_> = ev_rdate.into_iter().collect();
    assert_eq!(occs.len(), 3);
    assert_eq!(occs[0].start.day(), 1);
    assert_eq!(occs[1].start.day(), 2);
    assert_eq!(occs[2].start.day(), 10);

    // 2. RECURRENCE-ID overrides a specific instance of a recurring VEVENT sharing the same UID
    let cal_str = "BEGIN:VCALENDAR\r\n\
BEGIN:VEVENT\r\n\
UID:team-sync@example.com\r\n\
DTSTART;VALUE=DATE:20230101\r\n\
SUMMARY:Daily Sync\r\n\
RRULE:FREQ=DAILY;COUNT=3\r\n\
END:VEVENT\r\n\
BEGIN:VEVENT\r\n\
UID:team-sync@example.com\r\n\
RECURRENCE-ID;VALUE=DATE:20230102\r\n\
DTSTART;VALUE=DATE:20230105\r\n\
SUMMARY:Moved Daily Sync\r\n\
END:VEVENT\r\n\
END:VCALENDAR";
    let cal: VCalendar = cal_str.parse().unwrap();
    let start = "20230101".parse::<DateOrDateTime>().unwrap();
    let end = "20230110".parse::<DateOrDateTime>().unwrap();
    let all_occs = cal.occurrences_between(start, end).unwrap();
    assert_eq!(all_occs.len(), 3);
    assert_eq!(all_occs[0].1.start.day(), 1);
    assert_eq!(all_occs[1].1.start.day(), 3);
    assert_eq!(all_occs[2].1.start.day(), 5);
    assert_eq!(all_occs[2].0.summary, "Moved Daily Sync");
}

#[test]
fn test_rfc3_rrule_count_until_exclusivity_until_display_and_wkst() {
    use crate::RRule;
    // 1. COUNT and UNTIL are mutually exclusive per RFC 5545 §3.3.10
    assert!(
        "FREQ=DAILY;COUNT=5;UNTIL=20230110T000000Z"
            .parse::<RRule>()
            .is_err()
    );

    // 2. RRule Display preserves UNTIL and WKST
    let rrule_until: RRule = "FREQ=DAILY;UNTIL=20230110;WKST=SU".parse().unwrap();
    let formatted = rrule_until.to_string();
    assert!(formatted.contains("UNTIL=20230110"), "got: {formatted}");
    assert!(formatted.contains("WKST=SU"), "got: {formatted}");

    // 3. WKST=SU changes week boundaries for INTERVAL > 1 in WEEKLY recurrence
    // Saturday 2023-01-07:
    // - Under WKST=MO (default), Sat Jan 7 and Sun Jan 8 are in the same week (Jan 2..Jan 8).
    // - Under WKST=SU, Sat Jan 7 is the end of week 1 (Jan 1..Jan 7), Sun Jan 8 is start of week 2 (skipped by INTERVAL=2!),
    //   and week 3 (Jan 15..Jan 21) has Sun Jan 15 and Sat Jan 21.
    let ev_wkst_su: VEvent = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230107\r\n\
SUMMARY:Biweekly Weekend\r\n\
RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=SU;BYDAY=SA,SU;COUNT=4\r\n\
END:VEVENT"
        .parse()
        .unwrap();
    let days: Vec<u32> = ev_wkst_su.into_iter().map(|o| o.start.day()).collect();
    assert_eq!(days, vec![7, 15, 21, 29]);
}

#[test]
fn test_rfc4_multi_bymonth_bymonthday_and_bysetpos() {
    // 1. Multi-value BYMONTHDAY=1,15,-1 in MONTHLY recurrence
    let ev_bmd: VEvent = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230101\r\n\
SUMMARY:Paydays\r\n\
RRULE:FREQ=MONTHLY;BYMONTHDAY=1,15,-1;COUNT=4\r\n\
END:VEVENT"
        .parse()
        .unwrap();
    let bmd_dates: Vec<(u32, u32)> = ev_bmd
        .into_iter()
        .map(|o| (o.start.month(), o.start.day()))
        .collect();
    assert_eq!(bmd_dates, vec![(1, 1), (1, 15), (1, 31), (2, 1)]);

    // 2. Multi-value BYMONTH=6,12 in YEARLY recurrence
    let ev_bm: VEvent = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230610\r\n\
SUMMARY:Semi-Annual Review\r\n\
RRULE:FREQ=YEARLY;BYMONTH=6,12;BYMONTHDAY=10;COUNT=3\r\n\
END:VEVENT"
        .parse()
        .unwrap();
    let bm_dates: Vec<(i32, u32, u32)> = ev_bm
        .into_iter()
        .map(|o| (o.start.year(), o.start.month(), o.start.day()))
        .collect();
    assert_eq!(bm_dates, vec![(2023, 6, 10), (2023, 12, 10), (2024, 6, 10)]);

    // 3. BYSETPOS=-1 with BYDAY=MO,TU,WE,TH,FR ("last weekday of the month")
    let ev_setpos: VEvent = "BEGIN:VEVENT\r\n\
DTSTART;VALUE=DATE:20230131\r\n\
SUMMARY:Last Weekday of Month\r\n\
RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1;COUNT=3\r\n\
END:VEVENT"
        .parse()
        .unwrap();
    let setpos_dates: Vec<(u32, u32)> = ev_setpos
        .into_iter()
        .map(|o| (o.start.month(), o.start.day()))
        .collect();
    // Jan 31 2023 (Tue), Feb 28 2023 (Tue), Mar 31 2023 (Fri)
    assert_eq!(setpos_dates, vec![(1, 31), (2, 28), (3, 31)]);
}

#[test]
fn test_rfc5_custom_vtimezone_non_iana_tzid_resolution() {
    let cal_str = "BEGIN:VCALENDAR\r\n\
BEGIN:VTIMEZONE\r\n\
TZID:W. Europe Standard Time\r\n\
BEGIN:STANDARD\r\n\
DTSTART:19701025T030000\r\n\
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\n\
TZOFFSETFROM:+0200\r\n\
TZOFFSETTO:+0100\r\n\
END:STANDARD\r\n\
BEGIN:DAYLIGHT\r\n\
DTSTART:19700329T020000\r\n\
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\n\
TZOFFSETFROM:+0100\r\n\
TZOFFSETTO:+0200\r\n\
END:DAYLIGHT\r\n\
END:VTIMEZONE\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=\"W. Europe Standard Time\":20230115T100000\r\n\
DTEND;TZID=\"W. Europe Standard Time\":20230115T110000\r\n\
SUMMARY:Winter Event (+01:00)\r\n\
END:VEVENT\r\n\
BEGIN:VEVENT\r\n\
DTSTART;TZID=\"W. Europe Standard Time\":20230715T100000\r\n\
DTEND;TZID=\"W. Europe Standard Time\":20230715T110000\r\n\
SUMMARY:Summer Event (+02:00)\r\n\
END:VEVENT\r\n\
END:VCALENDAR";

    let cal: VCalendar = cal_str.parse().unwrap();
    assert_eq!(cal.events.len(), 2);
    // Winter: 10:00 local at +01:00 == 09:00 UTC
    assert_eq!(cal.events[0].dt_start.hour(), 9);
    // Summer: 10:00 local at +02:00 == 08:00 UTC
    assert_eq!(cal.events[1].dt_start.hour(), 8);
}

#[test]
fn test_rfc6_case_insensitivity_rfc6868_and_vcalendar_metadata() {
    // 1. Case-insensitive BEGIN/END, property names, parameters, frequencies, and weekdays
    let cal_str = "begin:vcalendar\r\n\
version:2.0\r\n\
prodid:-//Example Corp//CalClient//EN\r\n\
calscale:GREGORIAN\r\n\
method:PUBLISH\r\n\
begin:vevent\r\n\
uid:case-test@example.com\r\n\
dtstart;tzid=\"Europe/Rome\":20230102T090000\r\n\
duration:pt1h\r\n\
summary;cn=\"John ^'Doc^' Doe^nJr.\":Case Insensitive Event\r\n\
rrule:freq=weekly;byday=mo,we;count=2\r\n\
end:vevent\r\n\
end:vcalendar";

    let cal: VCalendar = cal_str.parse().unwrap();
    assert_eq!(cal.version.as_deref(), Some("2.0"));
    assert_eq!(
        cal.prod_id.as_deref(),
        Some("-//Example Corp//CalClient//EN")
    );
    assert_eq!(cal.calscale.as_deref(), Some("GREGORIAN"));
    assert_eq!(cal.method.as_deref(), Some("PUBLISH"));
    assert_eq!(cal.events.len(), 1);
    assert_eq!(cal.events[0].summary, "Case Insensitive Event");
    let occs: Vec<_> = cal.events[0].into_iter().collect();
    assert_eq!(occs.len(), 2);

    // 2. RFC 6868 parameter value caret-unescaping (^' -> ", ^n -> \n, ^^ -> ^)
    let line = crate::property::ContentLine::parse(
        " ATTENDEE;CN=\"John ^'Doc^' Doe^nLine2^^End\":mailto:john@example.com",
    )
    .unwrap();
    assert_eq!(line.param("cn"), Some("John \"Doc\" Doe\nLine2^End"));
}