workspacer_crate/
mock_crate_config.rs

1// ---------------- [ File: workspacer-crate/src/mock_crate_config.rs ]
2crate::ix!();
3
4/// Configuration for creating a mock crate in the workspace.
5#[derive(Getters,Clone,Debug)]
6#[getset(get="pub")]
7pub struct CrateConfig {
8    name:           String,
9    add_readme:     bool,
10    add_src_files:  bool,
11    add_test_files: bool,
12
13    /// (new) Optionally store custom content to write into `src/main.rs`
14    src_file_content: Option<String>,
15}
16
17impl CrateConfig {
18
19    /// Create a new CrateConfig with the given crate name.
20    pub fn new(name: &str) -> Self {
21        Self {
22            name: name.to_string(),
23            add_readme: false,
24            add_src_files: false,
25            add_test_files: false,
26            src_file_content: None,
27        }
28    }
29
30    /// (new) Provide custom Rust code for src/main.rs
31    pub fn with_src_files_content<S: Into<String>>(mut self, content: S) -> Self {
32        self.add_src_files = true;        // also mark that we do want a src
33        self.src_file_content = Some(content.into());
34        self
35    }
36
37    /// Set whether to add a README.md file.
38    pub fn with_readme(mut self) -> Self {
39        self.add_readme = true;
40        self
41    }
42
43    /// Set whether to add source files in the src/ directory.
44    pub fn with_src_files(mut self) -> Self {
45        self.add_src_files = true;
46        self
47    }
48
49    /// Set whether to add test files in the tests/ directory.
50    pub fn with_test_files(mut self) -> Self {
51        self.add_test_files = true;
52        self
53    }
54
55    /// Accessor to check if README.md is to be added.
56    pub fn has_readme(&self) -> bool {
57        self.add_readme
58    }
59
60    /// Accessor to check if src/ files are to be added.
61    pub fn has_src_files(&self) -> bool {
62        self.add_src_files
63    }
64
65    /// Accessor to check if test files are to be added.
66    pub fn has_test_files(&self) -> bool {
67        self.add_test_files
68    }
69}
70
71#[cfg(test)]
72mod test_crate_config {
73    use super::*;
74
75    /// Demonstrates that a new `CrateConfig` initialized with a given name
76    /// has default values for readme, src files, and test files set to false.
77    #[test]
78    fn test_new_crate_config_defaults() {
79        let config = CrateConfig::new("example_crate");
80
81        // Verify name
82        assert_eq!(config.name(), "example_crate", "Crate name should match constructor input");
83
84        // Verify defaults
85        assert!(!config.has_readme(), "Expected no README by default");
86        assert!(!config.has_src_files(), "Expected no src files by default");
87        assert!(!config.has_test_files(), "Expected no test files by default");
88    }
89
90    /// Demonstrates that calling `with_readme`, `with_src_files`, or `with_test_files`
91    /// modifies the config accordingly.
92    #[test]
93    fn test_crate_config_with_readme_src_and_tests() {
94        let config = CrateConfig::new("my_crate")
95            .with_readme()
96            .with_src_files()
97            .with_test_files();
98
99        // All flags are now true
100        assert!(config.has_readme(), "Expected README to be set");
101        assert!(config.has_src_files(), "Expected src files to be set");
102        assert!(config.has_test_files(), "Expected test files to be set");
103    }
104
105    /// Demonstrates partial usage: e.g., setting only README and src files but not test files.
106    #[test]
107    fn test_crate_config_partial_flags() {
108        let config = CrateConfig::new("partial_crate")
109            .with_readme()
110            .with_src_files();
111        // We did not call `.with_test_files()`
112
113        assert!(config.has_readme(), "Expected README to be set");
114        assert!(config.has_src_files(), "Expected src files to be set");
115        assert!(!config.has_test_files(), "Did not set test files, should remain false");
116    }
117
118    /// Demonstrates that the name is unaffected by enabling readme/src/tests.
119    #[test]
120    fn test_name_is_unaffected_by_flags() {
121        let original_name = "unchanged_crate_name";
122        let config = CrateConfig::new(original_name)
123            .with_readme()
124            .with_test_files();
125
126        assert_eq!(config.name(), original_name, "Name should remain unchanged");
127    }
128
129    /// Demonstrates that the user can check the underlying booleans
130    /// via both the builder methods and the getters `add_readme()`, `add_src_files()`, etc.
131    #[test]
132    fn test_getter_vs_builder_fields() {
133        let config = CrateConfig::new("getter_vs_builder")
134            .with_readme()
135            .with_src_files();
136
137        assert!(config.has_readme(), "has_readme() should match the builder");
138        assert!(config.add_readme(), "add_readme() raw field should also be true");
139        assert!(config.add_src_files(), "add_src_files() raw field should also be true");
140        assert!(!config.add_test_files(), "didn't enable test files, should be false");
141    }
142}