From b2e8bcb7828487be65ed0e6ffd08778a680567b5 Mon Sep 17 00:00:00 2001 From: Tammo Steffens Date: Thu, 25 Jan 2024 20:23:43 +0100 Subject: [PATCH] Adds some tests for project structure verifier --- build.sbt | 3 +- .../ProjectStructureVerifier.scala | 8 +- .../ProjectStructureVerifierTest.scala | 75 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/test/scala/com/github/tammo/verification/ProjectStructureVerifierTest.scala diff --git a/build.sbt b/build.sbt index 2f5a27d..d9d02c2 100644 --- a/build.sbt +++ b/build.sbt @@ -5,5 +5,6 @@ lazy val root = project name := "module-layers", organization := "com.github.tammo", version := "0.1.0-SNAPSHOT", - scalaVersion := "2.12.18" + scalaVersion := "2.12.18", + libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % Test ) diff --git a/src/main/scala/com/github/tammo/verification/ProjectStructureVerifier.scala b/src/main/scala/com/github/tammo/verification/ProjectStructureVerifier.scala index b980e11..2ed7379 100644 --- a/src/main/scala/com/github/tammo/verification/ProjectStructureVerifier.scala +++ b/src/main/scala/com/github/tammo/verification/ProjectStructureVerifier.scala @@ -7,9 +7,11 @@ object ProjectStructureVerifier { def verify( module: Module, architectureStyle: ArchitectureStyle - ): Iterable[Violation] = { + ): Set[Violation] = { val potentiallyAllowedLayers = - architectureStyle.permits.foldLeft[Option[Set[Layer]]](Some(Set.empty)) { + architectureStyle.permits.foldLeft[Option[Set[Layer]]]( + Some(Set.empty) + ) { case (None, _) => None case (result, PermitAll(layer)) => if (module.layer == layer) { @@ -28,7 +30,7 @@ object ProjectStructureVerifier { potentiallyAllowedLayers match { case None => // None expresses the absence of forbidden layers - Iterable.empty + Set.empty case Some(allowedLayers) => module.dependencies.flatMap( isDependencyViolated(module, _, allowedLayers) diff --git a/src/test/scala/com/github/tammo/verification/ProjectStructureVerifierTest.scala b/src/test/scala/com/github/tammo/verification/ProjectStructureVerifierTest.scala new file mode 100644 index 0000000..12eb3e1 --- /dev/null +++ b/src/test/scala/com/github/tammo/verification/ProjectStructureVerifierTest.scala @@ -0,0 +1,75 @@ +package com.github.tammo.verification + +import com.github.tammo.ModuleLayersPlugin.{Aggregate, NoArchitectureStyle} +import com.github.tammo.domain.ModuleLayersDomain.* +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers + +class ProjectStructureVerifierTest extends AnyFlatSpecLike with Matchers { + + private val emptyModule = Module("empty", DependencyLayer, Set.empty) + + private val moduleWithDependency = + Module("with-dependency", Aggregate, Set(emptyModule)) + + private case object DependencyLayer extends Layer + + private def architectureStyleForTest( + testPermits: Permit* + ): ArchitectureStyle = new ArchitectureStyle { + override def styleName: String = "test-style" + + override def permits: Iterable[Permit] = testPermits + } + + it should "have no violation if module has no dependencies" in { + val violations = + ProjectStructureVerifier.verify(emptyModule, NoArchitectureStyle) + + violations shouldBe Set.empty + } + + behavior of "explicit permit" + + it should "have no violations if module with dependency has explicit permit" in { + val architectureStyle: ArchitectureStyle = + architectureStyleForTest(Aggregate --> DependencyLayer) + + val violations = + ProjectStructureVerifier.verify( + moduleWithDependency, + architectureStyle + ) + + violations shouldBe Set.empty + } + + it should "have violation if module with dependency has explicit permit for other layer" in { + case object OtherLayer extends Layer + + val architectureStyle: ArchitectureStyle = + architectureStyleForTest(Aggregate --> OtherLayer) + + val violations = + ProjectStructureVerifier.verify( + moduleWithDependency, + architectureStyle + ) + + violations shouldBe Set( + Violation(moduleWithDependency, emptyModule, Set(OtherLayer)) + ) + } + + behavior of "permit all" + + it should "have no violations if module with dependency has permit all" in { + val architectureStyle = architectureStyleForTest(PermitAll(Aggregate)) + + val violations = + ProjectStructureVerifier.verify(moduleWithDependency, architectureStyle) + + violations shouldBe Set.empty + } + +}