Repository: analysis-dev/diktat (kotlin)

Issue:
There should be an option to disable MAGIC_NUMBER rule in test sources

Pull request description:
### What's done:
- Added option
- Added test
- Closes #832

Patch:
diff --git a/diktat-analysis.yml b/diktat-analysis.yml
index 14bfcb612c..1f107ac579 100644
--- a/diktat-analysis.yml
+++ b/diktat-analysis.yml
@@ -304,6 +304,8 @@
 - name: MAGIC_NUMBER
   enabled: true
   configuration:
+    # Ignore numbers from test
+    ignoreTest: "true"
     # Ignore numbers
     ignoreNumbers: "-1, 1, 0, 2"
     # Is ignore override hashCode function
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt
index 643362eb52..549fd4d8e4 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt
@@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3
 
 import org.cqfn.diktat.common.config.rules.RuleConfiguration
 import org.cqfn.diktat.common.config.rules.RulesConfig
+import org.cqfn.diktat.common.config.rules.getCommonConfiguration
 import org.cqfn.diktat.common.config.rules.getRuleConfig
 import org.cqfn.diktat.ruleset.constants.Warnings.MAGIC_NUMBER
 import org.cqfn.diktat.ruleset.rules.DiktatRule
@@ -37,9 +38,14 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
             configRules.getRuleConfig(MAGIC_NUMBER)?.configuration ?: emptyMap()
         )
     }
+    @Suppress("COLLAPSE_IF_STATEMENTS")
     override fun logic(node: ASTNode) {
+        val filePath = node.getRootNode().getFilePath()
+        val config = configRules.getCommonConfiguration()
         if (node.elementType == INTEGER_CONSTANT || node.elementType == FLOAT_CONSTANT) {
-            checkNumber(node, configuration)
+            if (!isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors) || !configuration.isIgnoreTest) {
+                checkNumber(node, configuration)
+            }
         }
     }
 
@@ -76,6 +82,11 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
      * [RuleConfiguration] for configuration
      */
     class MagicNumberConfiguration(config: Map<String, String>) : RuleConfiguration(config) {
+        /**
+         * Flag to ignore numbers from test
+         */
+        val isIgnoreTest = config["ignoreTest"]?.toBoolean() ?: IGNORE_TEST
+
         /**
          * List of ignored numbers
          */
@@ -95,6 +106,7 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
     }
 
     companion object {
+        const val IGNORE_TEST = true
         val ignoreNumbersList = listOf("-1", "1", "0", "2")
         val mapConfiguration = mapOf(
             "ignoreHashCodeFunction" to true,
diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml
index 9354b65640..7ea3341699 100644
--- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml
+++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml
@@ -304,6 +304,8 @@
 - name: MAGIC_NUMBER
   enabled: true
   configuration:
+    # Ignore numbers from test
+    ignoreTest: "true"
     # Ignore numbers
     ignoreNumbers: "-1, 1, 0, 2"
     # Is ignore override hashCode function
diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml
index 3f953a53ed..b6a1f62d55 100644
--- a/diktat-rules/src/main/resources/diktat-analysis.yml
+++ b/diktat-rules/src/main/resources/diktat-analysis.yml
@@ -300,6 +300,8 @@
 - name: MAGIC_NUMBER
   enabled: true
   configuration:
+    # Ignore numbers from test
+    ignoreTest: "true"
     # Ignore numbers
     ignoreNumbers: "-1, 1, 0, 2"
     # Is ignore override hashCode function


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt
index baffac256c..91ee368201 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt
@@ -181,4 +181,30 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) {
             rulesConfigList = rulesConfigMagicNumber
         )
     }
+
+    @Test
+    @Tag(WarningNames.MAGIC_NUMBER)
+    fun `check ignore numbers in test`() {
+        lintMethod(
+            """
+                |fun f1oo() {
+                |   val m = -1
+                |   val a: Byte = 4
+                |   val b = 0xff
+                |}
+                |
+                |enum class A(b:Int) {
+                |   A(-240),
+                |   B(50),
+                |   C(3),
+                |}
+                |@Override                
+                |fun hashCode(): Int {
+                |   return 32
+                |}
+            """.trimMargin(),
+            fileName = "src/test/kotlin/org/cqfn/diktat/test/hehe/MagicNumberTest.kt",
+            rulesConfigList = rulesConfigIgnoreNumbersMagicNumber,
+        )
+    }
 }