mithril_doc/
test_doc_macro.rs

1#[cfg(test)]
2mod tests {
3    use crate::{Documenter, DocumenterDefault, StructDoc};
4    use config::{Map, Source, Value};
5    use mithril_cli_helper::register_config_value;
6
7    #[allow(dead_code)]
8    #[derive(Debug, Clone, mithril_doc_derive::Documenter)]
9    struct MyConfiguration {
10        /// Execution environment
11        #[example = "dev"]
12        environment: String,
13    }
14
15    #[derive(Debug, Clone, mithril_doc_derive::DocumenterDefault)]
16    struct MyDefaultConfiguration {
17        /// Execution environment
18        environment: String,
19    }
20    impl Default for MyDefaultConfiguration {
21        fn default() -> Self {
22            Self {
23                environment: "prod".to_string(),
24            }
25        }
26    }
27
28    impl Source for MyDefaultConfiguration {
29        fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
30            Box::new(self.clone())
31        }
32
33        fn collect(&self) -> Result<Map<String, Value>, config::ConfigError> {
34            let mut result = Map::new();
35            let namespace = "default configuration".to_string();
36
37            let myself = self.clone();
38            register_config_value!(result, &namespace, myself.environment);
39
40            Ok(result)
41        }
42    }
43
44    #[test]
45    fn test_extract_struct_of_default_configuration() {
46        let doc = MyDefaultConfiguration::extract();
47        let field = doc.get_field("environment");
48
49        assert_eq!("environment", field.parameter);
50        assert_eq!("ENVIRONMENT", field.environment_variable.as_ref().unwrap());
51        assert_eq!("Execution environment", field.description);
52        assert_eq!("prod", field.default_value.as_ref().unwrap());
53    }
54
55    #[test]
56    fn test_extract_struct_of_configuration() {
57        let doc = MyConfiguration::extract();
58        let field = doc.get_field("environment");
59
60        assert_eq!("environment", field.parameter);
61        assert_eq!("ENVIRONMENT", field.environment_variable.as_ref().unwrap());
62        assert_eq!("Execution environment", field.description);
63        assert_eq!(None, field.default_value);
64    }
65
66    #[test]
67    fn test_extract_example_of_configuration() {
68        {
69            let doc_with_example = MyConfiguration::extract();
70            let field = doc_with_example.get_field("environment");
71            assert_eq!(Some("dev".to_string()), field.example);
72        }
73        {
74            let doc_without_example = MyDefaultConfiguration::extract();
75            let field = doc_without_example.get_field("environment");
76            assert_eq!(None, field.example);
77        }
78    }
79}