summaryrefslogtreecommitdiff
path: root/tests/functional/s/simplifiable
diff options
context:
space:
mode:
Diffstat (limited to 'tests/functional/s/simplifiable')
-rw-r--r--tests/functional/s/simplifiable/simplifiable_condition.py36
-rw-r--r--tests/functional/s/simplifiable/simplifiable_condition.txt12
-rw-r--r--tests/functional/s/simplifiable/simplifiable_if_expression.py27
-rw-r--r--tests/functional/s/simplifiable/simplifiable_if_expression.txt4
-rw-r--r--tests/functional/s/simplifiable/simplifiable_if_statement.py146
-rw-r--r--tests/functional/s/simplifiable/simplifiable_if_statement.txt4
-rw-r--r--tests/functional/s/simplifiable/simplify_chained_comparison.py89
-rw-r--r--tests/functional/s/simplifiable/simplify_chained_comparison.txt18
8 files changed, 336 insertions, 0 deletions
diff --git a/tests/functional/s/simplifiable/simplifiable_condition.py b/tests/functional/s/simplifiable/simplifiable_condition.py
new file mode 100644
index 000000000..32bf36aca
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplifiable_condition.py
@@ -0,0 +1,36 @@
+"""Test that boolean conditions can be simplified"""
+# pylint: disable=pointless-statement
+
+
+def func(_):
+ """Pointless function"""
+
+
+CONSTANT = 100
+OTHER = 200
+
+# Simplifies any boolean expression that is coerced into a True/False value
+bool(CONSTANT or False) # [simplifiable-condition]
+assert CONSTANT or False # [simplifiable-condition]
+if CONSTANT and True: # [simplifiable-condition]
+ pass
+elif CONSTANT and True: # [simplifiable-condition]
+ pass
+while CONSTANT and True: # [simplifiable-condition]
+ break
+1 if CONSTANT or False else 2 # [simplifiable-condition]
+z = [x for x in range(10) if x or False] # [simplifiable-condition]
+
+# Simplifies recursively
+assert CONSTANT or (True and False) # [simplifiable-condition]
+assert True and CONSTANT and OTHER # [simplifiable-condition]
+assert (CONSTANT or False) and (OTHER or True) # [simplifiable-condition]
+
+# Will try to infer the truthiness of an expression as long as it doesn't contain any variables
+assert [] or CONSTANT # [simplifiable-condition]
+assert {} or CONSTANT # [simplifiable-condition]
+
+# Expressions not in one of the above situations will not emit a message
+CONSTANT or True
+bool(CONSTANT or OTHER)
+bool(func(CONSTANT or True))
diff --git a/tests/functional/s/simplifiable/simplifiable_condition.txt b/tests/functional/s/simplifiable/simplifiable_condition.txt
new file mode 100644
index 000000000..b3552b50f
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplifiable_condition.txt
@@ -0,0 +1,12 @@
+simplifiable-condition:13:5::Boolean condition 'CONSTANT or False' may be simplified to 'CONSTANT'
+simplifiable-condition:14:7::Boolean condition 'CONSTANT or False' may be simplified to 'CONSTANT'
+simplifiable-condition:15:3::Boolean condition 'CONSTANT and True' may be simplified to 'CONSTANT'
+simplifiable-condition:17:5::Boolean condition 'CONSTANT and True' may be simplified to 'CONSTANT'
+simplifiable-condition:19:6::Boolean condition 'CONSTANT and True' may be simplified to 'CONSTANT'
+simplifiable-condition:21:5::Boolean condition 'CONSTANT or False' may be simplified to 'CONSTANT'
+simplifiable-condition:22:29::Boolean condition 'x or False' may be simplified to 'x'
+simplifiable-condition:25:7::Boolean condition 'CONSTANT or True and False' may be simplified to 'CONSTANT'
+simplifiable-condition:26:7::Boolean condition 'True and CONSTANT and OTHER' may be simplified to 'CONSTANT and OTHER'
+simplifiable-condition:27:7::Boolean condition '(CONSTANT or False) and (OTHER or True)' may be simplified to 'CONSTANT'
+simplifiable-condition:30:7::Boolean condition '[] or CONSTANT' may be simplified to 'CONSTANT'
+simplifiable-condition:31:7::Boolean condition '{} or CONSTANT' may be simplified to 'CONSTANT'
diff --git a/tests/functional/s/simplifiable/simplifiable_if_expression.py b/tests/functional/s/simplifiable/simplifiable_if_expression.py
new file mode 100644
index 000000000..bcfdf890b
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplifiable_if_expression.py
@@ -0,0 +1,27 @@
+"""Test that some if expressions can be simplified."""
+
+# pylint: disable=missing-docstring, invalid-name
+
+
+def test_simplifiable_1(arg):
+ # Simple test that can be replaced by bool(arg)
+ return True if arg else False # [simplifiable-if-expression]
+
+def test_simplifiable_2(arg):
+ # Simple test that can be replaced by not arg
+ return False if arg else True # [simplifiable-if-expression]
+
+def test_simplifiable_3(arg):
+ # Simple test that can be replaced by arg == 1
+ return True if arg == 1 else False # [simplifiable-if-expression]
+
+def test_simplifiable_4(arg):
+ # Simple test that can be replaced by not (arg == 1)
+ return False if arg == 1 else True # [simplifiable-if-expression]
+
+def test_not_simplifiable(arg):
+ x = True if arg else True
+ y = 0 if arg else 1
+ t = False if arg != 1 else False
+ t2 = None if arg > 3 else False
+ return x, y, t, t2
diff --git a/tests/functional/s/simplifiable/simplifiable_if_expression.txt b/tests/functional/s/simplifiable/simplifiable_if_expression.txt
new file mode 100644
index 000000000..872df8c25
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplifiable_if_expression.txt
@@ -0,0 +1,4 @@
+simplifiable-if-expression:8:11:test_simplifiable_1:The if expression can be replaced with 'bool(test)'
+simplifiable-if-expression:12:11:test_simplifiable_2:The if expression can be replaced with 'not test'
+simplifiable-if-expression:16:11:test_simplifiable_3:The if expression can be replaced with 'test'
+simplifiable-if-expression:20:11:test_simplifiable_4:The if expression can be replaced with 'not test'
diff --git a/tests/functional/s/simplifiable/simplifiable_if_statement.py b/tests/functional/s/simplifiable/simplifiable_if_statement.py
new file mode 100644
index 000000000..4d4c8b5d4
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplifiable_if_statement.py
@@ -0,0 +1,146 @@
+"""Test that some if statement tests can be simplified."""
+
+# pylint: disable=missing-docstring, invalid-name, no-else-return, arguments-out-of-order
+
+
+def test_simplifiable_1(arg):
+ # Simple test that can be replaced by bool(arg)
+ if arg: # [simplifiable-if-statement]
+ return True
+ else:
+ return False
+
+
+def test_simplifiable_2(arg, arg2):
+ # Can be reduced to bool(arg and not arg2)
+ if arg and not arg2: # [simplifiable-if-statement]
+ return True
+ else:
+ return False
+
+
+def test_simplifiable_3(arg, arg2):
+ # Can be reduced to bool(arg and not arg2)
+ if arg and not arg2: # [simplifiable-if-statement]
+ var = True
+ else:
+ var = False
+ return var
+
+
+def test_simplifiable_4(arg):
+ if arg:
+ var = True
+ else:
+ if arg == "arg1": # [simplifiable-if-statement]
+ return True
+ else:
+ return False
+ return var
+
+
+def test_not_necessarily_simplifiable_1(arg, arg2):
+ # Can be reduced to bool(not arg and not arg2) or to
+ # `not all(N)`, which is a bit harder to understand
+ # than `any(N)` when var should be False.
+ if arg or arg2:
+ var = False
+ else:
+ var = True
+ return var
+
+
+def test_not_necessarily_simplifiabile_2(arg):
+ # This could theoretically be reduced to `not arg or arg > 3`
+ # but the net result is that now the condition is harder to understand,
+ # because it requires understanding of an extra clause:
+ # * first, there is the negation of truthness with `not arg`
+ # * the second clause is `arg > 3`, which occurs when arg has a
+ # a truth value, but it implies that `arg > 3` is equivalent
+ # with `arg and arg > 3`, which means that the user must
+ # think about this assumption when evaluating `arg > 3`.
+ # The original form is easier to grasp.
+ if arg and arg <= 3:
+ return False
+ else:
+ return True
+
+
+def test_not_simplifiable_3(arg):
+ if arg:
+ test_not_necessarily_simplifiabile_2(arg)
+ test_not_necessarily_simplifiable_1(arg, arg)
+ return False
+ else:
+ if arg < 3:
+ test_simplifiable_3(arg, 42)
+ return True
+
+
+def test_not_simplifiable_4(arg):
+ # Not interested in multiple elifs
+ if arg == "any":
+ return True
+ elif test_not_simplifiable_3(arg) == arg:
+ return True
+ else:
+ return False
+
+
+def test_not_simplifiable_5(arg):
+ # Different actions in each branch
+ if arg == "any":
+ return True
+ else:
+ var = 42
+ return var
+
+
+def test_not_simplifiable_6(arg):
+ # Different actions in each branch
+ if arg == "any":
+ var = 42
+ else:
+ return True
+ return var
+
+def test_not_simplifiable_7(arg):
+ # Returning something different
+ if arg == "any":
+ return 4
+ else:
+ return 5
+
+
+def test_not_simplifiable_8(arg):
+ # Only one of the branch returns something boolean
+ if arg == "any":
+ return True
+ else:
+ return 0
+
+
+def test_not_simplifiable_9():
+ # Not the same targets
+ first = True
+ second = 1
+ third = [1]
+ fourth = False
+ fifth = False
+
+ if first and second in third:
+ fourth = True
+ else:
+ fifth = True
+ return fourth + fifth
+
+
+def test_not_simplifiable_10():
+ # Subscripts are not considered
+ object_type = 'read'
+ filter_kwargs = {}
+ if object_type == 'read':
+ filter_kwargs['a'] = True
+ else:
+ filter_kwargs['b'] = True
+ return filter_kwargs
diff --git a/tests/functional/s/simplifiable/simplifiable_if_statement.txt b/tests/functional/s/simplifiable/simplifiable_if_statement.txt
new file mode 100644
index 000000000..aa61f4993
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplifiable_if_statement.txt
@@ -0,0 +1,4 @@
+simplifiable-if-statement:8:4:test_simplifiable_1:The if statement can be replaced with 'return bool(test)'
+simplifiable-if-statement:16:4:test_simplifiable_2:The if statement can be replaced with 'return bool(test)'
+simplifiable-if-statement:24:4:test_simplifiable_3:The if statement can be replaced with 'var = bool(test)'
+simplifiable-if-statement:35:8:test_simplifiable_4:The if statement can be replaced with 'return bool(test)'
diff --git a/tests/functional/s/simplifiable/simplify_chained_comparison.py b/tests/functional/s/simplifiable/simplify_chained_comparison.py
new file mode 100644
index 000000000..4be9fbe3d
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplify_chained_comparison.py
@@ -0,0 +1,89 @@
+"""Test that some boolean expression statement tests can be simplified."""
+
+# pylint: disable=missing-docstring, invalid-name, no-else-return, too-many-branches
+
+
+def test_simplify_chained_comparison_1():
+ a = 1
+ b = 2
+ c = 3
+ return a < b and b < c # [chained-comparison]
+
+
+def test_simplify_chained_comparison_2():
+ a = 1
+ return a < 10 and a > 1 # [chained-comparison]
+
+
+def test_simplify_chained_comparison_3():
+ a = 1
+ b = 2
+ c = 3
+ d = 4
+ if a < 10 and a > 1: # [chained-comparison]
+ pass
+ elif a > 1 and a < 10: # [chained-comparison]
+ pass
+ elif a > 1 and a <= 10: # [chained-comparison]
+ pass
+ elif a > 1 and a < 10 and b == 2: # [chained-comparison]
+ pass
+ elif a > 1 and c == b and a < 10: # [chained-comparison]
+ pass
+ elif a > 100 and c == b and a < 10: # [chained-comparison]
+ # In this comparison we are not checking whether left bound is actually
+ # lower than right bound or not.
+ pass
+ elif a < b and b < c: # [chained-comparison]
+ pass
+ elif a > b and b > c: # [chained-comparison]
+ pass
+ elif a < b and a == 1 and b < c: # [chained-comparison]
+ pass
+ elif a < b and b < c and c == 786: # [chained-comparison]
+ pass
+ elif a < b and b < 0 and c == 786: # [chained-comparison]
+ pass
+ elif a < b and c == 786 and b < 0: # [chained-comparison]
+ pass
+ elif c == 786 and b < 0 and a < b: # [chained-comparison]
+ pass
+ elif a < b < c and c < d: # [chained-comparison]
+ pass
+ elif b < c < d and a < b: # [chained-comparison]
+ pass
+ elif a < b < c and a < b and b < c: # [chained-comparison]
+ pass
+
+
+def test_not_simplify_chained_comparison_1():
+ a = 1
+ b = 2
+ c = 3
+ d = 4
+ if a < 10 and b > 1:
+ pass
+ elif a > 1 and b < 10:
+ pass
+ elif a > 1 and a == 10:
+ pass
+ elif a == 1 and b == 2:
+ pass
+ elif a > 1 and a > 10:
+ pass
+ elif a < 100 and a < 10:
+ pass
+ elif a < b and a < c:
+ pass
+ elif a < b and a == 1 and a < c:
+ pass
+ elif a < b and a < c and c == 786:
+ pass
+ elif a < b < c and b < d:
+ pass
+ elif a < b < c and a < d:
+ pass
+ elif b < c < d and a < c:
+ pass
+ elif b < c < d and a < d:
+ pass
diff --git a/tests/functional/s/simplifiable/simplify_chained_comparison.txt b/tests/functional/s/simplifiable/simplify_chained_comparison.txt
new file mode 100644
index 000000000..fe8d3c384
--- /dev/null
+++ b/tests/functional/s/simplifiable/simplify_chained_comparison.txt
@@ -0,0 +1,18 @@
+chained-comparison:10:11:test_simplify_chained_comparison_1:Simplify chained comparison between the operands
+chained-comparison:15:11:test_simplify_chained_comparison_2:Simplify chained comparison between the operands
+chained-comparison:23:7:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:25:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:27:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:29:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:31:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:33:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:37:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:39:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:41:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:43:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:45:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:47:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:49:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:51:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:53:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands
+chained-comparison:55:9:test_simplify_chained_comparison_3:Simplify chained comparison between the operands