Skip to content

Commit

Permalink
Adds some tests for project structure verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Tammo0987 committed Jan 25, 2024
1 parent 6ec11df commit b2e8bcb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit b2e8bcb

Please sign in to comment.