mithril_doc/
test_doc_macro.rs

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
#[cfg(test)]
mod tests {
    use crate::{Documenter, DocumenterDefault, StructDoc};
    use config::{Map, Source, Value, ValueKind};

    #[allow(dead_code)]
    #[derive(Debug, Clone, mithril_doc_derive::Documenter)]
    struct MyConfiguration {
        /// Execution environment
        #[example = "dev"]
        environment: String,
    }

    #[derive(Debug, Clone, mithril_doc_derive::DocumenterDefault)]
    struct MyDefaultConfiguration {
        /// Execution environment
        environment: String,
    }
    impl Default for MyDefaultConfiguration {
        fn default() -> Self {
            Self {
                environment: "prod".to_string(),
            }
        }
    }

    impl Source for MyDefaultConfiguration {
        fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
            Box::new(self.clone())
        }

        fn collect(&self) -> Result<Map<String, Value>, config::ConfigError> {
            let mut result = Map::new();
            let namespace = "default configuration".to_string();
            let myself = self.clone();
            result.insert(
                "environment".to_string(),
                Value::new(Some(&namespace), ValueKind::from(myself.environment)),
            );
            Ok(result)
        }
    }

    #[test]
    fn test_extract_struct_of_default_configuration() {
        let doc = MyDefaultConfiguration::extract();
        let field = doc.get_field("environment");

        assert_eq!("environment", field.parameter);
        assert_eq!("ENVIRONMENT", field.environment_variable.as_ref().unwrap());
        assert_eq!("Execution environment", field.description);
        assert_eq!("prod", field.default_value.as_ref().unwrap());
    }

    #[test]
    fn test_extract_struct_of_configuration() {
        let doc = MyConfiguration::extract();
        let field = doc.get_field("environment");

        assert_eq!("environment", field.parameter);
        assert_eq!("ENVIRONMENT", field.environment_variable.as_ref().unwrap());
        assert_eq!("Execution environment", field.description);
        assert_eq!(None, field.default_value);
    }

    #[test]
    fn test_extract_example_of_configuration() {
        {
            let doc_with_example = MyConfiguration::extract();
            let field = doc_with_example.get_field("environment");
            assert_eq!(Some("dev".to_string()), field.example);
        }
        {
            let doc_without_example = MyDefaultConfiguration::extract();
            let field = doc_without_example.get_field("environment");
            assert_eq!(None, field.example);
        }
    }
}