1+ using System . Linq ;
2+ using System . Threading ;
3+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
4+ using Rubberduck . Inspections . Concrete ;
5+ using RubberduckTests . Mocks ;
6+
7+ namespace RubberduckTests . Inspections
8+ {
9+ [ TestClass ]
10+ public class BooleanAssignedInIfElseInspectionTests
11+ {
12+ [ TestMethod ]
13+ [ TestCategory ( "Inspections" ) ]
14+ public void Simple ( )
15+ {
16+ const string inputcode =
17+ @"Sub Foo()
18+ Dim d As Boolean
19+ If True Then
20+ d = True
21+ Else
22+ d = False
23+ EndIf
24+ End Sub" ;
25+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
26+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
27+
28+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
29+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
30+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
31+
32+ Assert . AreEqual ( 1 , results . Count ( ) ) ;
33+ }
34+
35+ [ TestMethod ]
36+ [ TestCategory ( "Inspections" ) ]
37+ public void MultipleResults ( )
38+ {
39+ const string inputcode =
40+ @"Sub Foo()
41+ Dim d As Boolean
42+ If True Then
43+ d = True
44+ Else
45+ d = False
46+ EndIf
47+
48+ If True Then
49+ d = False
50+ Else
51+ d = True
52+ EndIf
53+ End Sub" ;
54+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
55+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
56+
57+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
58+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
59+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
60+
61+ Assert . AreEqual ( 2 , results . Count ( ) ) ;
62+ }
63+
64+ [ TestMethod ]
65+ [ TestCategory ( "Inspections" ) ]
66+ public void AssignsInteger ( )
67+ {
68+ const string inputcode =
69+ @"Sub Foo()
70+ Dim d
71+ If True Then
72+ d = 0
73+ Else
74+ d = 1
75+ EndIf
76+ End Sub" ;
77+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
78+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
79+
80+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
81+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
82+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
83+
84+ Assert . IsFalse ( results . Any ( ) ) ;
85+ }
86+
87+ [ TestMethod ]
88+ [ TestCategory ( "Inspections" ) ]
89+ public void AssignsTheSameValue ( ) // worthy of an inspection in its own right
90+ {
91+ const string inputcode =
92+ @"Sub Foo()
93+ Dim d
94+ If True Then
95+ d = True
96+ Else
97+ d = True
98+ EndIf
99+ End Sub" ;
100+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
101+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
102+
103+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
104+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
105+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
106+
107+ Assert . IsFalse ( results . Any ( ) ) ;
108+ }
109+
110+ [ TestMethod ]
111+ [ TestCategory ( "Inspections" ) ]
112+ public void AssignsToDifferentVariables ( )
113+ {
114+ const string inputcode =
115+ @"Sub Foo()
116+ Dim d1, d2
117+ If True Then
118+ d1 = True
119+ Else
120+ d2 = False
121+ EndIf
122+ End Sub" ;
123+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
124+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
125+
126+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
127+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
128+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
129+
130+ Assert . IsFalse ( results . Any ( ) ) ;
131+ }
132+
133+ [ TestMethod ]
134+ [ TestCategory ( "Inspections" ) ]
135+ public void ConditionalContainsElseIfBlock ( )
136+ {
137+ const string inputcode =
138+ @"Sub Foo()
139+ Dim d
140+ If True Then
141+ d = True
142+ ElseIf False Then
143+ d = True
144+ Else
145+ d = False
146+ EndIf
147+ End Sub" ;
148+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
149+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
150+
151+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
152+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
153+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
154+
155+ Assert . IsFalse ( results . Any ( ) ) ;
156+ }
157+
158+ [ TestMethod ]
159+ [ TestCategory ( "Inspections" ) ]
160+ public void ConditionalDoesNotContainElseBlock ( )
161+ {
162+ const string inputcode =
163+ @"Sub Foo()
164+ Dim d
165+ If True Then
166+ d = True
167+ EndIf
168+ End Sub" ;
169+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
170+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
171+
172+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
173+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
174+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
175+
176+ Assert . IsFalse ( results . Any ( ) ) ;
177+ }
178+
179+ [ TestMethod ]
180+ [ TestCategory ( "Inspections" ) ]
181+ public void IsIgnored ( )
182+ {
183+ const string inputcode =
184+ @"Sub Foo()
185+ Dim d
186+ '@Ignore BooleanAssignedInIfElse
187+ If True Then
188+ d = True
189+ Else
190+ d = False
191+ EndIf
192+ End Sub" ;
193+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputcode , out _ ) ;
194+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
195+
196+ var inspection = new BooleanAssignedInIfElseInspection ( state ) ;
197+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
198+ var results = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
199+
200+ Assert . IsFalse ( results . Any ( ) ) ;
201+ }
202+ }
203+ }
0 commit comments