Skip to content

Commit

Permalink
Complete get_forbidden_imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Jan 4, 2025
1 parent 86e41a3 commit cf00d67
Showing 1 changed file with 58 additions and 27 deletions.
85 changes: 58 additions & 27 deletions src/imports_info/queries/internal_imports/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn get_forbidden_imports(layers: &[Layer]) -> Vec<ForbiddenImport> {
}
}
}

if idx >= 2 {
let directly_lower_layer = &layers[idx - 1];
for lower_layer in layers[..idx - 1].iter() {
Expand All @@ -72,6 +73,21 @@ fn get_forbidden_imports(layers: &[Layer]) -> Vec<ForbiddenImport> {
}
}
}

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
Expand All @@ -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<PackageToken, String> = 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(())
}
Expand Down

0 comments on commit cf00d67

Please sign in to comment.