mithril_doc/
test_doc_macro.rs1#[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 version: Option<String>,
12
13 #[example = "dev"]
15 environment: String,
16 }
17
18 #[derive(Debug, Clone, mithril_doc_derive::DocumenterDefault)]
19 struct MyDefaultConfiguration {
20 environment: String,
22 }
23 impl Default for MyDefaultConfiguration {
24 fn default() -> Self {
25 Self {
26 environment: "prod".to_string(),
27 }
28 }
29 }
30
31 impl Source for MyDefaultConfiguration {
32 fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
33 Box::new(self.clone())
34 }
35
36 fn collect(&self) -> Result<Map<String, Value>, config::ConfigError> {
37 let mut result = Map::new();
38 let namespace = "default configuration".to_string();
39
40 let myself = self.clone();
41 register_config_value!(result, &namespace, myself.environment);
42
43 Ok(result)
44 }
45 }
46
47 #[test]
48 fn test_extract_struct_of_default_configuration() {
49 let doc = MyDefaultConfiguration::extract();
50 let field = doc.get_field("environment").unwrap();
51
52 assert_eq!("environment", field.parameter);
53 assert_eq!("ENVIRONMENT", field.environment_variable.as_ref().unwrap());
54 assert_eq!("Execution environment", field.description);
55 assert_eq!("prod", field.default_value.as_ref().unwrap());
56 }
57
58 #[test]
59 fn test_extract_struct_of_configuration() {
60 let doc = MyConfiguration::extract();
61 let field = doc.get_field("environment").unwrap();
62
63 assert_eq!("environment", field.parameter);
64 assert_eq!("ENVIRONMENT", field.environment_variable.as_ref().unwrap());
65 assert_eq!("Execution environment", field.description);
66 assert_eq!(None, field.default_value);
67 }
68
69 #[test]
70 fn test_extract_example_of_configuration() {
71 {
72 let doc_with_example = MyConfiguration::extract();
73 let field = doc_with_example.get_field("environment").unwrap();
74 assert_eq!(Some("dev".to_string()), field.example);
75 }
76 {
77 let doc_without_example = MyDefaultConfiguration::extract();
78 let field = doc_without_example.get_field("environment").unwrap();
79 assert_eq!(None, field.example);
80 }
81 }
82
83 #[test]
84 fn test_extract_configuration_optional_field() {
85 let doc_with_example = MyConfiguration::extract();
86 assert!(!doc_with_example.get_field("version").unwrap().is_mandatory);
87 assert!(doc_with_example.get_field("environment").unwrap().is_mandatory);
88 }
89
90 #[test]
91 fn test_extract_configuration_fields_in_declaration_order() {
92 let doc_with_example = MyConfiguration::extract();
93
94 let fields = doc_with_example.get_ordered_data();
95
96 assert_eq!(fields[0].parameter, "version");
97 assert_eq!(fields[1].parameter, "environment");
98 }
99}