lumeohq / xsd-parser-rs

A xsd/wsdl => rust code generator written in rust
Apache License 2.0
100 stars 39 forks source link

Generate tests for each struct #121

Open DmitrySamoylov opened 3 years ago

DmitrySamoylov commented 3 years ago

It is useful to have at least a basic test for each generated struct to verify that yaserde works fine for all cases.

#[test]
fn test_profile() {
    ser_de(tt::Profile::default());
}

// mod utils:
fn ser_de<T: YaSerialize + YaDeserialize>(value: T) {
    let ser = yaserde::ser::to_string(&value).unwrap();
    println!("{}", ser);
    let _ = yaserde::de::from_str::<T>(&ser).unwrap();
}

As a bonus point all Option fields should have Some value and all Vecs should not be empty. To accomplish this we can generate something like


#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(prefix = "tt", namespace = "tt: http://www.onvif.org/ver10/schema")]
pub struct Profile {
    // User readable name of the profile.
    #[yaserde(prefix = "tt", rename = "Name")]
    pub name: Name,

    // Optional configuration of the Video input.
    #[yaserde(prefix = "tt", rename = "VideoSourceConfiguration")]
    pub video_source_configuration: Option<VideoSourceConfiguration>,

    ... etc
}

#[cfg(test)]
fn create_test_profile() -> tt::Profile {
    tt::Profile {
        name: create_test_name(),
        video_source_configuration: Some(create_test_video_source_configuration()),
        ... etc
    }
}

#[test]
fn test_profile() {
    ser_de(create_test_profile());
}

#[derive(Default, PartialEq, Debug, UtilsTupleIo, UtilsDefaultSerde)]
pub struct Name(pub String);

#[cfg(test)]
fn create_test_name() -> tt::Name {
    tt::Name("test Name".to_string())
}

#[test]
fn test_name() {
    ser_de(create_test_name());
}