From ad10ca57da4e14ce55c1187824281a0990076a8e Mon Sep 17 00:00:00 2001 From: "Eugene Sypachev (@eugene-sy)" Date: Sat, 9 Sep 2023 21:58:39 +0200 Subject: [PATCH] ignore level --- .../com/sksamuel/scapegoat/Feedback.scala | 7 +++--- .../scala/com/sksamuel/scapegoat/Level.scala | 23 ++++++++++++++---- .../com/sksamuel/scapegoat/FeedbackTest.scala | 24 ++++++++++++++++--- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/main/scala/com/sksamuel/scapegoat/Feedback.scala b/src/main/scala/com/sksamuel/scapegoat/Feedback.scala index a2d01775..e7d1b203 100644 --- a/src/main/scala/com/sksamuel/scapegoat/Feedback.scala +++ b/src/main/scala/com/sksamuel/scapegoat/Feedback.scala @@ -93,9 +93,10 @@ final case class Warning( ) { def hasMinimalLevelOf(minimalLevel: Level): Boolean = { minimalLevel match { - case Levels.Info => true - case Levels.Warning => this.level == Levels.Warning || this.level == Levels.Error - case Levels.Error => this.level == Levels.Error + case Levels.Ignore => throw new IllegalArgumentException("Ignore cannot be minimal level") + case Levels.Info => this.level.higherOrEqualTo(Levels.Info) + case Levels.Warning => this.level.higherOrEqualTo(Levels.Warning) + case Levels.Error => this.level.higherOrEqualTo(Levels.Error) } } } diff --git a/src/main/scala/com/sksamuel/scapegoat/Level.scala b/src/main/scala/com/sksamuel/scapegoat/Level.scala index 9996d7bc..2d3ad314 100644 --- a/src/main/scala/com/sksamuel/scapegoat/Level.scala +++ b/src/main/scala/com/sksamuel/scapegoat/Level.scala @@ -4,7 +4,11 @@ package com.sksamuel.scapegoat * @author * Stephen Samuel */ -sealed trait Level +sealed trait Level { + protected def weight: Short + + def higherOrEqualTo(other: Level): Boolean = this.weight >= other.weight +} object Levels { @@ -13,7 +17,9 @@ object Levels { * * An example is use of nulls. Use of nulls can lead to NullPointerExceptions and should be avoided. */ - case object Error extends Level + case object Error extends Level { + override protected def weight: Short = 30 + } /** * Warnings are reserved for code that has bad semantics. This by itself does not necessarily mean the code @@ -26,7 +32,9 @@ object Levels { * Another example is a constant if. You can do things like if (true) { } if you want, but since the block * will always evaluate, the if statement perhaps indicates a mistake. */ - case object Warning extends Level + case object Warning extends Level { + override protected def weight: Short = 20 + } /** * Infos are used for code which is semantically fine, but there exists a more idomatic way of writing it. @@ -43,13 +51,20 @@ object Levels { * * def foo = a */ - case object Info extends Level + case object Info extends Level { + override protected def weight: Short = 10 + } + + case object Ignore extends Level { + override protected def weight: Short = 0 + } def fromName(name: String): Level = name.toLowerCase() match { case "error" => Error case "warning" => Warning case "info" => Info + case "ignore" => Ignore case _ => throw new IllegalArgumentException(s"Unrecognised level '$name'") } } diff --git a/src/test/scala/com/sksamuel/scapegoat/FeedbackTest.scala b/src/test/scala/com/sksamuel/scapegoat/FeedbackTest.scala index 7661c66a..b25ceb4a 100644 --- a/src/test/scala/com/sksamuel/scapegoat/FeedbackTest.scala +++ b/src/test/scala/com/sksamuel/scapegoat/FeedbackTest.scala @@ -132,7 +132,13 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit "This is description.", "This is explanation." ) - val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo) + val inspectionIgnored = new DummyInspection( + "My default is Ignore", + Levels.Ignore, + "This is description.", + "This is explanation." + ) + val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo, inspectionIgnored) val reporter = new StoreReporter(new Settings()) val feedback = new Feedback(reporter, testConfiguration(consoleOutput = true, defaultSourcePrefix, Levels.Info)) @@ -159,7 +165,13 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit "This is description.", "This is explanation." ) - val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo) + val inspectionIgnored = new DummyInspection( + "My default is Ignore", + Levels.Ignore, + "This is description.", + "This is explanation." + ) + val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo, inspectionIgnored) val reporter = new StoreReporter(new Settings()) val feedback = new Feedback( @@ -191,7 +203,13 @@ class FeedbackTest extends AnyFreeSpec with Matchers with OneInstancePerTest wit "This is description.", "This is explanation." ) - val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo) + val inspectionIgnored = new DummyInspection( + "My default is Ignore", + Levels.Ignore, + "This is description.", + "This is explanation." + ) + val inspections = Seq(inspectionError, inspectionWarning, inspectionInfo, inspectionIgnored) val reporter = new StoreReporter(new Settings()) val feedback = new Feedback(reporter, testConfiguration(consoleOutput = false, defaultSourcePrefix, Levels.Error))