|
20 | 20 | package org.sonar.python.checks.tests;
|
21 | 21 |
|
22 | 22 | import java.util.List;
|
| 23 | +import java.util.Optional; |
23 | 24 | import java.util.Set;
|
| 25 | +import java.util.function.Consumer; |
24 | 26 | import org.sonar.check.Rule;
|
25 | 27 | import org.sonar.plugins.python.api.PythonSubscriptionCheck;
|
26 | 28 | import org.sonar.plugins.python.api.SubscriptionContext;
|
@@ -85,17 +87,21 @@ public void initialize(Context context) {
|
85 | 87 | List<Argument> arguments = call.arguments();
|
86 | 88 |
|
87 | 89 | if (BOOLEAN_ASSERTIONS.contains(name)) {
|
88 |
| - checkBooleanAssertion(ctx, TreeUtils.nthArgumentOrKeyword(0, "expr", arguments)); |
| 90 | + checkAssertion(arg -> checkBooleanAssertion(ctx, arg), 0, "expr", arguments); |
89 | 91 | } else if (NONE_ASSERTIONS.contains(name)) {
|
90 |
| - checkNoneAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(0, "expr", arguments)); |
| 92 | + checkAssertion(arg -> checkNoneAssertion(ctx, call, arg),0, "expr", arguments); |
91 | 93 | } else if (IS_ASSERTIONS.contains(name)) {
|
92 | 94 | String message = "assertIs".equals(name) ? IS_MESSAGE : IS_NOT_MESSAGE;
|
93 |
| - checkIsAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(0, "first", arguments), message); |
94 |
| - checkIsAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(1, "second", arguments), message); |
| 95 | + checkAssertion(arg -> checkIsAssertion(ctx, call, arg, message), 0, "first", arguments); |
| 96 | + checkAssertion(arg -> checkIsAssertion(ctx, call, arg, message),1, "second", arguments); |
95 | 97 | }
|
96 | 98 | });
|
97 | 99 | }
|
98 | 100 |
|
| 101 | + private static void checkAssertion(Consumer<RegularArgument> checkConsumer, int argPosition, String keyword, List<Argument> arguments) { |
| 102 | + Optional.ofNullable(TreeUtils.nthArgumentOrKeyword(argPosition, keyword, arguments)).ifPresent(checkConsumer); |
| 103 | + } |
| 104 | + |
99 | 105 | /**
|
100 | 106 | * `assert False` or `assert 0` is often used to make a test fail.
|
101 | 107 | * Usually it is better to use another assertion or throw an AssertionException.
|
|
0 commit comments