Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate tests for each struct #121

Open
DmitrySamoylov opened this issue Mar 11, 2021 · 0 comments
Open

Generate tests for each struct #121

DmitrySamoylov opened this issue Mar 11, 2021 · 0 comments

Comments

@DmitrySamoylov
Copy link
Contributor

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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant