|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.checks; |
| 18 | + |
| 19 | +import java.util.EnumSet; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.sonar.plugins.python.api.ProjectPythonVersion; |
| 23 | +import org.sonar.plugins.python.api.PythonVersionUtils; |
| 24 | +import org.sonar.python.checks.utils.PythonCheckVerifier; |
| 25 | +import org.sonar.python.checks.quickfix.PythonQuickFixVerifier; |
| 26 | +import org.sonar.python.types.TypeShed; |
| 27 | + |
| 28 | +class SentinelValuesIdentityComparisonCheckTest { |
| 29 | + |
| 30 | + @AfterEach |
| 31 | + void resetPythonVersion() { |
| 32 | + ProjectPythonVersion.setCurrentVersions(PythonVersionUtils.allVersions()); |
| 33 | + TypeShed.resetBuiltinSymbols(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void test() { |
| 38 | + ProjectPythonVersion.setCurrentVersions(EnumSet.of(PythonVersionUtils.Version.V_315)); |
| 39 | + PythonCheckVerifier.verify("src/test/resources/checks/sentinelValuesIdentityComparison.py", new SentinelValuesIdentityComparisonCheck()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void typing_extensions_sentinel() { |
| 44 | + PythonCheckVerifier.verify("src/test/resources/checks/typingExtensionsSentinelValuesIdentityComparison.py", new SentinelValuesIdentityComparisonCheck()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void equality_comparison_quick_fix() { |
| 49 | + var check = new SentinelValuesIdentityComparisonCheck(); |
| 50 | + String codeWithIssue = """ |
| 51 | + from typing_extensions import Sentinel |
| 52 | + missing = Sentinel("MISSING") |
| 53 | + if value == missing: |
| 54 | + pass |
| 55 | + """; |
| 56 | + String fixedCode = """ |
| 57 | + from typing_extensions import Sentinel |
| 58 | + missing = Sentinel("MISSING") |
| 59 | + if value is missing: |
| 60 | + pass |
| 61 | + """; |
| 62 | + PythonQuickFixVerifier.verify(check, codeWithIssue, fixedCode); |
| 63 | + PythonQuickFixVerifier.verifyQuickFixMessages(check, codeWithIssue, SentinelValuesIdentityComparisonCheck.IS_QUICK_FIX_MESSAGE); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void equality_comparison_without_whitespace_quick_fix() { |
| 68 | + var check = new SentinelValuesIdentityComparisonCheck(); |
| 69 | + String codeWithIssue = """ |
| 70 | + from typing_extensions import Sentinel |
| 71 | + missing = Sentinel("MISSING") |
| 72 | + if value==missing: |
| 73 | + pass |
| 74 | + """; |
| 75 | + String fixedCode = """ |
| 76 | + from typing_extensions import Sentinel |
| 77 | + missing = Sentinel("MISSING") |
| 78 | + if value is missing: |
| 79 | + pass |
| 80 | + """; |
| 81 | + PythonQuickFixVerifier.verify(check, codeWithIssue, fixedCode); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void inequality_comparison_quick_fix() { |
| 86 | + var check = new SentinelValuesIdentityComparisonCheck(); |
| 87 | + String codeWithIssue = """ |
| 88 | + from typing_extensions import Sentinel |
| 89 | + missing = Sentinel("MISSING") |
| 90 | + if value != missing: |
| 91 | + pass |
| 92 | + """; |
| 93 | + String fixedCode = """ |
| 94 | + from typing_extensions import Sentinel |
| 95 | + missing = Sentinel("MISSING") |
| 96 | + if value is not missing: |
| 97 | + pass |
| 98 | + """; |
| 99 | + PythonQuickFixVerifier.verify(check, codeWithIssue, fixedCode); |
| 100 | + PythonQuickFixVerifier.verifyQuickFixMessages(check, codeWithIssue, SentinelValuesIdentityComparisonCheck.IS_NOT_QUICK_FIX_MESSAGE); |
| 101 | + } |
| 102 | +} |
0 commit comments