1
+ using System . Linq ;
2
+ using Microsoft . Vbe . Interop ;
3
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
4
+ using Rubberduck . Inspections ;
5
+ using Rubberduck . Parsing . VBA ;
6
+ using Rubberduck . VBEditor . Extensions ;
7
+ using Rubberduck . VBEditor . VBEInterfaces . RubberduckCodePane ;
8
+ using RubberduckTests . Mocks ;
9
+
10
+ namespace RubberduckTests . Inspections
11
+ {
12
+ [ TestClass ]
13
+ public class NonReturningFunctionInspectionTests : VbeTestBase
14
+ {
15
+ [ TestMethod ]
16
+ public void NonReturningFunction_ReturnsResult ( )
17
+ {
18
+ const string inputCode =
19
+ @"Function Foo() As Boolean
20
+ End Function" ;
21
+
22
+ //Arrange
23
+ var builder = new MockVbeBuilder ( ) ;
24
+ var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
25
+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode )
26
+ . Build ( ) . Object ;
27
+
28
+ var codePaneFactory = new RubberduckCodePaneFactory ( ) ;
29
+ var parseResult = new RubberduckParser ( codePaneFactory ) . Parse ( project ) ;
30
+
31
+ var inspection = new NonReturningFunctionInspection ( ) ;
32
+ var inspectionResults = inspection . GetInspectionResults ( parseResult ) ;
33
+
34
+ Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
35
+ }
36
+
37
+ [ TestMethod ]
38
+ public void NonReturningFunction_ReturnsResult_MultipleFunctions ( )
39
+ {
40
+ const string inputCode =
41
+ @"Function Foo() As Boolean
42
+ End Function
43
+
44
+ Function Goo() As String
45
+ End Function" ;
46
+
47
+ //Arrange
48
+ var builder = new MockVbeBuilder ( ) ;
49
+ var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
50
+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode )
51
+ . Build ( ) . Object ;
52
+
53
+ var codePaneFactory = new RubberduckCodePaneFactory ( ) ;
54
+ var parseResult = new RubberduckParser ( codePaneFactory ) . Parse ( project ) ;
55
+
56
+ var inspection = new NonReturningFunctionInspection ( ) ;
57
+ var inspectionResults = inspection . GetInspectionResults ( parseResult ) ;
58
+
59
+ Assert . AreEqual ( 2 , inspectionResults . Count ( ) ) ;
60
+ }
61
+
62
+ [ TestMethod ]
63
+ public void NonReturningFunction_DoesNotReturnResult ( )
64
+ {
65
+ const string inputCode =
66
+ @"Function Foo() As Boolean
67
+ Foo = True
68
+ End Function" ;
69
+
70
+ //Arrange
71
+ var builder = new MockVbeBuilder ( ) ;
72
+ var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
73
+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode )
74
+ . Build ( ) . Object ;
75
+
76
+ var codePaneFactory = new RubberduckCodePaneFactory ( ) ;
77
+ var parseResult = new RubberduckParser ( codePaneFactory ) . Parse ( project ) ;
78
+
79
+ var inspection = new NonReturningFunctionInspection ( ) ;
80
+ var inspectionResults = inspection . GetInspectionResults ( parseResult ) ;
81
+
82
+ Assert . AreEqual ( 0 , inspectionResults . Count ( ) ) ;
83
+ }
84
+
85
+ [ TestMethod ]
86
+ public void NonReturningFunction_ReturnsResult_MultipleSubs_SomeNoneReturning ( )
87
+ {
88
+ const string inputCode =
89
+ @"Function Foo() As Boolean
90
+ Foo = True
91
+ End Function
92
+
93
+ Function Goo() As String
94
+ End Function" ;
95
+
96
+ //Arrange
97
+ var builder = new MockVbeBuilder ( ) ;
98
+ var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
99
+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode )
100
+ . Build ( ) . Object ;
101
+
102
+ var codePaneFactory = new RubberduckCodePaneFactory ( ) ;
103
+ var parseResult = new RubberduckParser ( codePaneFactory ) . Parse ( project ) ;
104
+
105
+ var inspection = new NonReturningFunctionInspection ( ) ;
106
+ var inspectionResults = inspection . GetInspectionResults ( parseResult ) ;
107
+
108
+ Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
109
+ }
110
+
111
+ [ TestMethod ]
112
+ public void NonReturningFunction_ReturnsResult_InterfaceImplementation ( )
113
+ {
114
+ //Input
115
+ const string inputCode1 =
116
+ @"Function Foo() As Boolean
117
+ End Function" ;
118
+ const string inputCode2 =
119
+ @"Implements IClass1
120
+
121
+ Function IClass1_Foo() As Boolean
122
+ End Function" ;
123
+
124
+ //Arrange
125
+ var builder = new MockVbeBuilder ( ) ;
126
+ var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
127
+ . AddComponent ( "IClass1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode1 )
128
+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode2 )
129
+ . Build ( ) . Object ;
130
+
131
+ var codePaneFactory = new RubberduckCodePaneFactory ( ) ;
132
+ var parseResult = new RubberduckParser ( codePaneFactory ) . Parse ( project ) ;
133
+
134
+ var inspection = new NonReturningFunctionInspection ( ) ;
135
+ var inspectionResults = inspection . GetInspectionResults ( parseResult ) ;
136
+
137
+ Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
138
+ }
139
+
140
+ [ TestMethod ]
141
+ public void NonReturningFunction_QuickFixWorks ( )
142
+ {
143
+ const string inputCode =
144
+ @"Function Foo() As Boolean
145
+ End Function" ;
146
+
147
+ const string expectedCode =
148
+ @"Sub Foo()
149
+ End Sub" ;
150
+
151
+ //Arrange
152
+ var builder = new MockVbeBuilder ( ) ;
153
+ var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
154
+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode )
155
+ . Build ( ) . Object ;
156
+ var module = project . VBComponents . Item ( 0 ) . CodeModule ;
157
+
158
+ var codePaneFactory = new RubberduckCodePaneFactory ( ) ;
159
+ var parseResult = new RubberduckParser ( codePaneFactory ) . Parse ( project ) ;
160
+
161
+ var inspection = new NonReturningFunctionInspection ( ) ;
162
+ var inspectionResults = inspection . GetInspectionResults ( parseResult ) ;
163
+
164
+ inspectionResults . First ( ) . GetQuickFixes ( ) . First ( ) . Value ( ) ;
165
+
166
+ Assert . AreEqual ( expectedCode , module . Lines ( ) ) ;
167
+ }
168
+
169
+ [ TestMethod ]
170
+ public void InspectionType ( )
171
+ {
172
+ var inspection = new NonReturningFunctionInspection ( ) ;
173
+ Assert . AreEqual ( CodeInspectionType . CodeQualityIssues , inspection . InspectionType ) ;
174
+ }
175
+
176
+ [ TestMethod ]
177
+ public void InspectionName ( )
178
+ {
179
+ const string inspectionName = "NonReturningFunctionInspection" ;
180
+ var inspection = new NonReturningFunctionInspection ( ) ;
181
+
182
+ Assert . AreEqual ( inspectionName , inspection . Name ) ;
183
+ }
184
+ }
185
+ }
0 commit comments