Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Magic Number in test #879

Merged
merged 4 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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
*/
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}