From cf00d67ef9aceb1b268a43ec91d5e8d83b3d8c83 Mon Sep 17 00:00:00 2001 From: Peter Byfield Date: Sat, 4 Jan 2025 07:07:06 +0100 Subject: [PATCH] Complete get_forbidden_imports --- .../queries/internal_imports/layers.rs | 85 +++++++++++++------ 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/src/imports_info/queries/internal_imports/layers.rs b/src/imports_info/queries/internal_imports/layers.rs index e663037..df58ff3 100644 --- a/src/imports_info/queries/internal_imports/layers.rs +++ b/src/imports_info/queries/internal_imports/layers.rs @@ -58,6 +58,7 @@ fn get_forbidden_imports(layers: &[Layer]) -> Vec { } } } + if idx >= 2 { let directly_lower_layer = &layers[idx - 1]; for lower_layer in layers[..idx - 1].iter() { @@ -72,6 +73,21 @@ fn get_forbidden_imports(layers: &[Layer]) -> Vec { } } } + + if layer.siblings_independent { + for layer_sibling1 in layer.siblings.iter() { + for layer_sibling2 in layer.siblings.iter() { + if layer_sibling1 == layer_sibling2 { + continue; + } + forbidden_imports.push(ForbiddenImport::new( + *layer_sibling1, + *layer_sibling2, + hashset! {}, + )); + } + } + } } forbidden_imports @@ -85,46 +101,61 @@ mod tests { use pretty_assertions::assert_eq; use slotmap::SlotMap; - // TODO: Add siblings, both independent and not. #[test] fn test_get_forbidden_imports() -> Result<()> { let mut sm: SlotMap = SlotMap::with_key(); let data: PackageItemToken = sm.insert("data".into()).into(); - let domain: PackageItemToken = sm.insert("domain".into()).into(); - let application: PackageItemToken = sm.insert("application".into()).into(); + let domain1: PackageItemToken = sm.insert("domain1".into()).into(); + let domain2: PackageItemToken = sm.insert("domain2".into()).into(); + let application1: PackageItemToken = sm.insert("application1".into()).into(); + let application2: PackageItemToken = sm.insert("application2".into()).into(); let interfaces: PackageItemToken = sm.insert("interfaces".into()).into(); let layers = vec![ Layer::new([data], true), - Layer::new([domain], true), - Layer::new([application], true), + Layer::new([domain1, domain2], true), + Layer::new([application1, application2], false), Layer::new([interfaces], true), ]; let forbidden_imports = get_forbidden_imports(&layers); - assert_eq!( - forbidden_imports, - vec![ - // data may not import domain, application or interfaces - ForbiddenImport::new(data, domain, hashset! {}), - ForbiddenImport::new(data, application, hashset! {}), - ForbiddenImport::new(data, interfaces, hashset! {}), - // domain may not import application or interfaces - // (domain may import data) - ForbiddenImport::new(domain, application, hashset! {}), - ForbiddenImport::new(domain, interfaces, hashset! {}), - // application may not import interfaces - // application may not import data, except via domain - // (application may import domain) - ForbiddenImport::new(application, interfaces, hashset! {}), - ForbiddenImport::new(application, data, hashset! {domain}), - // interfaces may not import data or domain, except via application - // (application may import application) - ForbiddenImport::new(interfaces, data, hashset! {application}), - ForbiddenImport::new(interfaces, domain, hashset! {application}), - ] - ); + let expected = vec![ + // data may not import domain, application or interfaces + ForbiddenImport::new(data, domain1, hashset! {}), + ForbiddenImport::new(data, domain2, hashset! {}), + ForbiddenImport::new(data, application1, hashset! {}), + ForbiddenImport::new(data, application2, hashset! {}), + ForbiddenImport::new(data, interfaces, hashset! {}), + // domain may not import application or interfaces + // (domain may import data) + ForbiddenImport::new(domain1, application1, hashset! {}), + ForbiddenImport::new(domain1, application2, hashset! {}), + ForbiddenImport::new(domain1, interfaces, hashset! {}), + ForbiddenImport::new(domain2, application1, hashset! {}), + ForbiddenImport::new(domain2, application2, hashset! {}), + ForbiddenImport::new(domain2, interfaces, hashset! {}), + // domain1 and domain2 are independent siblings + ForbiddenImport::new(domain1, domain2, hashset! {}), + ForbiddenImport::new(domain2, domain1, hashset! {}), + // application may not import interfaces + // application may not import data, except via domain + // (application may import domain) + ForbiddenImport::new(application1, interfaces, hashset! {}), + ForbiddenImport::new(application1, data, hashset! {domain1, domain2}), + ForbiddenImport::new(application2, interfaces, hashset! {}), + ForbiddenImport::new(application2, data, hashset! {domain1, domain2}), + // interfaces may not import data or domain, except via application + // (application may import application) + ForbiddenImport::new(interfaces, data, hashset! {application1, application2}), + ForbiddenImport::new(interfaces, domain1, hashset! {application1, application2}), + ForbiddenImport::new(interfaces, domain2, hashset! {application1, application2}), + ]; + + assert_eq!(forbidden_imports.len(), expected.len(),); + for forbidden_import in forbidden_imports.iter() { + assert!(expected.contains(forbidden_import)); + } Ok(()) }