1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# pylint: disable=missing-module-docstring, missing-function-docstring
def triggered_if_if_block_ends_with_elif(machine, old_conf, new_conf):
"""Example code that will trigger the message
Given an if-elif construct
When the body of the if ends with an elif
Then the message confusing-consecutive-elif must be triggered.
"""
if old_conf:
if not new_conf:
machine.disable()
elif old_conf.value != new_conf.value:
machine.disable()
machine.enable(new_conf.value)
elif new_conf: # [confusing-consecutive-elif]
machine.enable(new_conf.value)
def not_triggered_if_indented_block_ends_with_else(machine, old_conf, new_conf):
"""Example code must not trigger the message, because the inner block ends with else.
Given an if-elif construct
When the body of the if ends with an else
Then no message shall be triggered.
"""
if old_conf:
if not new_conf:
machine.disable()
elif old_conf.value != new_conf.value:
machine.disable()
machine.enable(new_conf.value)
else:
pass
elif new_conf:
machine.enable(new_conf.value)
def not_triggered_if_indentend_block_ends_with_call(machine, old_conf, new_conf):
"""
Example code must not trigger the message,
Given an if-elif construct
When the body of the if ends with a function call
Then no message shall be triggered.
Note: There is nothing special about the body ending with a function call.
This is just taken as a representative value for the equivalence class of
"every node class unrelated to if/elif/else".
"""
if old_conf:
if not new_conf:
machine.disable()
elif old_conf.value != new_conf.value:
machine.disable()
machine.enable(new_conf.value)
print("Processed old configuration...")
elif new_conf:
machine.enable(new_conf.value)
def triggered_if_elif_block_ends_with_elif(machine, old_conf, new_conf, new_new_conf):
"""Example code that will trigger the message
Given an if-elif-elif construct
When the body of the first elif ends with an elif
Then the message confusing-consecutive-elif must be triggered.
"""
if old_conf:
machine.disable()
elif not new_conf:
if new_new_conf:
machine.disable()
elif old_conf.value != new_conf.value:
machine.disable()
machine.enable(new_conf.value)
elif new_conf: # [confusing-consecutive-elif]
machine.enable(new_conf.value)
def triggered_if_block_ends_with_if(machine, old_conf, new_conf, new_new_conf):
"""Example code that will trigger the message
Given an if-elif construct
When the body of the if ends with an if
Then the message confusing-consecutive-elif must be triggered.
"""
if old_conf:
if new_new_conf:
machine.disable()
elif new_conf: # [confusing-consecutive-elif]
machine.enable(new_conf.value)
def not_triggered_if_indented_block_ends_with_ifexp(machine, old_conf, new_conf):
"""
Example code must not trigger the message,
Given an if-elif construct
When the body of the if ends with an if expression
Then no message shall be triggered.
"""
if old_conf:
if not new_conf:
machine.disable()
print("Processed old configuration...")
elif new_conf:
machine.enable(new_conf.value)
def not_triggered_if_outer_block_does_not_have_elif(machine, old_conf, new_conf):
"""Example code must not trigger the message
Given an if construct without an elif
When the body of the if ends with an if
Then no message shall be triggered.
"""
if old_conf:
if not new_conf:
machine.disable()
elif old_conf.value != new_conf.value:
machine.disable()
machine.enable(new_conf.value)
else:
pass
def not_triggered_if_outer_block_continues_with_if(machine, old_conf, new_conf, new_new_conf):
"""Example code that will trigger the message
Given an if construct which continues with a new if construct
When the body of the first if ends with an if expression
Then no message shall be triggered.
"""
if old_conf:
if new_new_conf:
machine.disable()
elif old_conf.value != new_conf.value:
machine.disable()
machine.enable(new_conf.value)
if new_conf:
machine.enable(new_conf.value)
|