|
1 | | -use crate::hierarchy::ModuleHierarchy; |
| 1 | +use crate::hierarchy::{ModuleHierarchy, ModuleIterator}; |
2 | 2 | use crate::imports::{ImportDetails, ModuleImports}; |
3 | 3 | use anyhow::Result; |
4 | 4 | use std::collections::HashSet; |
@@ -46,7 +46,11 @@ impl Graph { |
46 | 46 | } |
47 | 47 |
|
48 | 48 | pub fn get_modules(&self) -> HashSet<&str> { |
49 | | - todo!() |
| 49 | + self.hierarchy |
| 50 | + .all() |
| 51 | + .visible() |
| 52 | + .map(|m| m.name().as_str()) |
| 53 | + .collect() |
50 | 54 | } |
51 | 55 |
|
52 | 56 | pub fn count_imports(&self) -> usize { |
@@ -150,3 +154,45 @@ impl Graph { |
150 | 154 | todo!() |
151 | 155 | } |
152 | 156 | } |
| 157 | + |
| 158 | +#[cfg(test)] |
| 159 | +mod tests { |
| 160 | + use super::*; |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn add_module() -> Result<()> { |
| 164 | + let mut graph = Graph::default(); |
| 165 | + |
| 166 | + graph.add_module("mypackage")?; |
| 167 | + |
| 168 | + let result = graph.get_modules(); |
| 169 | + |
| 170 | + assert_eq!(result, HashSet::from(["mypackage"])); |
| 171 | + Ok(()) |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn add_module_doesnt_add_parent() -> Result<()> { |
| 176 | + let mut graph = Graph::default(); |
| 177 | + |
| 178 | + graph.add_module("mypackage.foo")?; |
| 179 | + |
| 180 | + let result = graph.get_modules(); |
| 181 | + |
| 182 | + assert_eq!(result, HashSet::from(["mypackage.foo"])); |
| 183 | + Ok(()) |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn add_modules() -> Result<()> { |
| 188 | + let mut graph = Graph::default(); |
| 189 | + |
| 190 | + graph.add_module("mypackage")?; |
| 191 | + graph.add_module("mypackage.foo")?; |
| 192 | + |
| 193 | + let result = graph.get_modules(); |
| 194 | + |
| 195 | + assert_eq!(result, HashSet::from(["mypackage", "mypackage.foo"])); |
| 196 | + Ok(()) |
| 197 | + } |
| 198 | +} |
0 commit comments