1
use aetolia::prelude::*;
2

            
3
mod common;
4
use common::make_test_object;
5

            
6
/// Create an ical object with as much coverage as possible, using the builder interface
7
/// Then that is built, serialized, and parsed to be compared with the original object.
8
///
9
/// Just to check that the content should be expected to be processed correctly, the object is
10
/// validated before being serialized and parsed.
11
#[test]
12
2
fn round_trip() {
13
2
    let object = make_test_object();
14
2

            
15
2
    let validation_errors = validate_model(&object).unwrap();
16
2

            
17
2
    if !validation_errors.is_empty() {
18
        validation_errors.iter().for_each(|e| {
19
            eprintln!("{}", e);
20
        })
21
2
    }
22

            
23
2
    assert!(
24
2
        validation_errors.is_empty(),
25
        "Didn't expect any validation errors, see errors above"
26
    );
27

            
28
2
    let mut target = Vec::new();
29
2
    object.write_model(&mut target).unwrap();
30
2

            
31
2
    std::fs::write("tests/round_trip.ics", &target).unwrap();
32
2

            
33
2
    let parsed = load_ical(&target[..]).unwrap();
34
2

            
35
2
    assert_eq!(1, parsed.len());
36
2
    similar_asserts::assert_eq!(object, parsed[0]);
37
2
}
38

            
39
macro_rules! assert_model {
40
    ($equals:literal, $v:expr) => {
41
        let mut target = Vec::new();
42
        $v.write_model(&mut target).unwrap();
43
        let actual = String::from_utf8(target).unwrap();
44
        assert_eq!($equals, actual);
45
    };
46
}
47

            
48
#[test]
49
2
fn accessors() {
50
2
    let object = make_test_object();
51

            
52
16
    for component in object.components {
53
14
        match component {
54
2
            CalendarComponent::Event(event) => {
55
2
                let dt_stamp = event.get_property::<DateTimeStampProperty>().unwrap();
56
2
                assert_eq!(1, dt_stamp.get_iana_params("date-time-stamp-test").len());
57
2
                assert_eq!(1, dt_stamp.get_x_params("x-date-time-stamp-test").len());
58
2
                assert_model!("20240808T150000Z", dt_stamp.value());
59

            
60
2
                let uid = event.get_property::<UniqueIdentifierProperty>().unwrap();
61
2
                assert_eq!(1, uid.get_iana_params("unique-identifier-test").len());
62
2
                assert_eq!(1, uid.get_x_params("x-unique-identifier-test").len());
63
2
                assert_model!("test-id", uid.value());
64

            
65
2
                let dt_start = event.get_property::<DateTimeStartProperty>().unwrap();
66
2
                let tz_id_param = dt_start.get_param::<TimeZoneIdParam>().unwrap();
67
2
                assert_eq!("test", tz_id_param.tz_id);
68
2
                assert!(tz_id_param.unique);
69
2
                assert_eq!(1, dt_start.get_iana_params("date-time-start-test").len());
70
2
                assert_eq!(1, dt_start.get_x_params("x-date-time-start-test").len());
71
2
                assert_model!("20240808T150000", dt_start.value());
72

            
73
2
                let class = event.get_property::<ClassificationProperty>().unwrap();
74
2
                assert_eq!(1, class.get_iana_params("classification-test").len());
75
2
                assert_eq!(1, class.get_x_params("x-classification-test").len());
76
2
                assert_model!("PUBLIC", class.value());
77

            
78
2
                let attendees = event.get_properties::<AttendeeProperty>();
79
2
                assert_eq!(1, attendees.len());
80

            
81
2
                let alarms = event.alarms();
82
2
                assert_eq!(3, alarms.len());
83

            
84
2
                let trigger = alarms[0].get_property::<TriggerProperty>().unwrap();
85
2
                let trigger_relationship = trigger.get_param::<TriggerRelationshipParam>().unwrap();
86
2
                assert_eq!(
87
2
                    TriggerRelationship::Start,
88
2
                    trigger_relationship.trigger_relationship
89
2
                );
90
2
                assert_eq!(1, trigger.get_iana_params("trigger-test").len());
91
2
                assert_eq!(1, trigger.get_x_params("x-trigger-test").len());
92
2
                match trigger.value() {
93
2
                    TriggerValue::Relative(d) => {
94
2
                        assert_model!("P1D", d);
95
                    }
96
                    _ => {
97
                        panic!("Expected duration");
98
                    }
99
                }
100

            
101
2
                let iana_prop = event.get_iana_properties("other");
102
2
                assert_eq!(1, iana_prop.len());
103

            
104
2
                let x_prop = event.get_x_properties("x-other");
105
2
                assert_eq!(1, x_prop.len());
106
            }
107
2
            CalendarComponent::TimeZone(timezone) => {
108
2
                let tz_id = timezone.get_property::<TimeZoneIdProperty>().unwrap();
109
2
                assert_eq!(1, tz_id.get_iana_params("time-zone-id-test").len());
110
2
                assert_eq!(1, tz_id.get_x_params("x-time-zone-id-test").len());
111
2
                assert_eq!("test", tz_id.value().id);
112

            
113
2
                let nested_components = timezone.nested_components();
114
2
                assert_eq!(2, nested_components.len());
115

            
116
2
                let tz_name = nested_components[0].get_properties::<TimeZoneNameProperty>();
117
2
                assert_eq!(1, tz_name.len());
118
            }
119
10
            _ => {
120
10
                // More test coverage could be added here
121
10
            }
122
        }
123
    }
124
2
}