1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using System . Windows . Forms ;
4
+ using Antlr4 . Runtime ;
5
+ using Microsoft . Vbe . Interop ;
6
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
7
+ using Moq ;
8
+ using Rubberduck . Parsing . Symbols ;
9
+ using Rubberduck . UI . Refactorings . Rename ;
10
+ using Parsing = Rubberduck . Parsing ;
11
+
12
+ namespace RubberduckTests . UI . Refactorings . Rename
13
+ {
14
+ [ TestClass ]
15
+ public class RenamePresenterTests
16
+ {
17
+ private static Mock < VBE > _vbe ;
18
+ private static Mock < VBProject > _vbProject ;
19
+ private static Declarations _declarations ;
20
+ private static Parsing . QualifiedModuleName _module ;
21
+ private static Mock < IRenameView > _view ;
22
+ private List < Declaration > _listDeclarations ;
23
+
24
+ [ TestInitialize ]
25
+ public void TestInitialization ( )
26
+ {
27
+ _vbe = new Mock < VBE > ( ) ;
28
+ _vbProject = new Mock < VBProject > ( ) ;
29
+ _declarations = new Declarations ( ) ;
30
+ _module = new Parsing . QualifiedModuleName ( ) ;
31
+ _view = new Mock < IRenameView > ( ) ;
32
+ }
33
+
34
+ /// <summary>Common method for adding declaration with some default values</summary>
35
+ private void AddDeclarationItem ( IMock < ParserRuleContext > context ,
36
+ Parsing . Selection selection ,
37
+ Parsing . QualifiedMemberName ? qualifiedName = null ,
38
+ DeclarationType declarationType = DeclarationType . Project ,
39
+ string identifierName = "identifierName" )
40
+ {
41
+ Declaration declarationItem = null ;
42
+ var qualName = qualifiedName ?? new Parsing . QualifiedMemberName ( _module , "fakeModule" ) ;
43
+
44
+ declarationItem = new Declaration (
45
+ qualifiedName : qualName ,
46
+ parentScope : "module.proc" ,
47
+ identifierName : identifierName ,
48
+ asTypeName : "asTypeName" ,
49
+ isSelfAssigned : false ,
50
+ isWithEvents : false ,
51
+ accessibility : Accessibility . Public ,
52
+ declarationType : declarationType ,
53
+ context : context . Object ,
54
+ selection : selection
55
+ ) ;
56
+
57
+ _declarations . Add ( declarationItem ) ;
58
+ if ( _listDeclarations == null ) _listDeclarations = new List < Declaration > ( ) ;
59
+ _listDeclarations . Add ( declarationItem ) ;
60
+ }
61
+
62
+ /// <summary>Common method for adding a reference to given declaration item</summary>
63
+ private static void AddReference ( Declaration itemToAdd , IdentifierReference reference )
64
+ {
65
+ var declaration = _declarations . Items . ToList ( ) . FirstOrDefault ( x => x . Equals ( itemToAdd ) ) ;
66
+ if ( declaration == null ) return ;
67
+
68
+ declaration . AddReference ( reference ) ;
69
+ }
70
+
71
+ [ TestMethod ]
72
+ public void ConstructorWorks_IsNotNull ( )
73
+ {
74
+ // arange
75
+ var symbolSelection = new Parsing . Selection ( 1 , 1 , 2 , 2 ) ;
76
+ var qualifiedSelection = new Parsing . QualifiedSelection ( _module , symbolSelection ) ;
77
+
78
+ //act
79
+ var presenter = new RenamePresenter ( _vbe . Object , _view . Object , _declarations , qualifiedSelection ) ;
80
+
81
+ //assert
82
+ Assert . IsNotNull ( presenter , "Successfully initialized" ) ;
83
+ }
84
+
85
+ [ TestMethod ]
86
+ public void NoTargetFound ( )
87
+ {
88
+ // arange
89
+ var symbolSelection = new Parsing . Selection ( 1 , 1 , 2 , 2 ) ;
90
+ var qualifiedSelection = new Parsing . QualifiedSelection ( _module , symbolSelection ) ;
91
+
92
+ var context = new Mock < ParserRuleContext > ( ) ;
93
+ AddDeclarationItem ( context , symbolSelection ) ;
94
+ _view . Setup ( form => form . ShowDialog ( ) ) . Returns ( DialogResult . Cancel ) ;
95
+
96
+ //act
97
+ var presenter = new RenamePresenter ( _vbe . Object , _view . Object , _declarations , qualifiedSelection ) ;
98
+ presenter . Show ( ) ;
99
+
100
+ //assert
101
+ Assert . IsNull ( _view . Object . Target , "No Target was found" ) ;
102
+ }
103
+
104
+ [ TestMethod ]
105
+ public void AcquireTarget_ProcedureRenaming_TargetIsNotNull ( )
106
+ {
107
+ // arange
108
+ var symbolSelection = new Parsing . Selection ( 1 , 1 , 2 , 4 ) ;
109
+ var qualifiedSelection = new Parsing . QualifiedSelection ( _module , symbolSelection ) ;
110
+
111
+ // just for passing null reference exception
112
+ var context = new Mock < ParserRuleContext > ( ) ;
113
+ context . SetupGet ( c => c . Start . Line ) . Returns ( 1 ) ;
114
+ context . SetupGet ( c => c . Stop . Line ) . Returns ( 2 ) ;
115
+ context . SetupGet ( c => c . Stop . Text ) . Returns ( "Four" ) ;
116
+
117
+ // setting a declaration item as a procedure that will be renamed
118
+ const string identifierName = "AProcedure" ;
119
+ AddDeclarationItem ( context , symbolSelection , null , DeclarationType . Procedure , identifierName ) ;
120
+
121
+ // allow Moq to set the Target property
122
+ _view . Setup ( view => view . ShowDialog ( ) ) . Returns ( DialogResult . Cancel ) ;
123
+ _view . SetupProperty ( view => view . Target ) ;
124
+
125
+ //act
126
+ var presenter = new RenamePresenter ( _vbe . Object , _view . Object , _declarations , qualifiedSelection ) ;
127
+ presenter . Show ( ) ;
128
+
129
+ //assert
130
+ Assert . IsNotNull ( _view . Object . Target , "A target was found" ) ;
131
+ Assert . AreEqual ( identifierName , _view . Object . Target . IdentifierName ) ;
132
+ }
133
+
134
+ [ TestMethod ]
135
+ public void AcquireTarget_ModuleRenaming_TargetIsNotNull ( )
136
+ {
137
+ // arange
138
+ var symbolSelection = new Parsing . Selection ( 1 , 1 , 2 , 4 ) ;
139
+ var qualifiedSelection = new Parsing . QualifiedSelection ( _module , symbolSelection ) ;
140
+
141
+ // just for passing null reference exception
142
+ var context = new Mock < ParserRuleContext > ( ) ;
143
+ context . SetupGet ( c => c . Start . Line ) . Returns ( 1 ) ;
144
+ context . SetupGet ( c => c . Stop . Line ) . Returns ( 2 ) ;
145
+ context . SetupGet ( c => c . Stop . Text ) . Returns ( "Four" ) ;
146
+
147
+ // setting a declaration item as a module that will be renamed
148
+ const string identifierName = "FakeModule" ;
149
+ AddDeclarationItem ( context , symbolSelection , null , DeclarationType . Module , identifierName ) ;
150
+
151
+ // allow Moq to set the Target property
152
+ _view . Setup ( view => view . ShowDialog ( ) ) . Returns ( DialogResult . Cancel ) ;
153
+ _view . SetupProperty ( view => view . Target ) ;
154
+
155
+ //act
156
+ var presenter = new RenamePresenter ( _vbe . Object , _view . Object , _declarations , qualifiedSelection ) ;
157
+ presenter . Show ( ) ;
158
+
159
+ //assert
160
+ Assert . IsNotNull ( _view . Object . Target , "A target was found" ) ;
161
+ Assert . AreEqual ( identifierName , _view . Object . Target . IdentifierName ) ;
162
+ }
163
+
164
+ [ TestMethod ]
165
+ public void AcquireTarget_MethodRenamingAtSameComponent_CorrectTargetChosen ( )
166
+ {
167
+ // arange
168
+ var symbolSelection = new Parsing . Selection ( 8 , 1 , 8 , 16 ) ;
169
+ var selectedComponent = new Parsing . QualifiedModuleName ( "TestProject" , "TestModule" , _vbProject . Object , 1 ) ;
170
+ var qualifiedSelection = new Parsing . QualifiedSelection ( selectedComponent , symbolSelection ) ;
171
+
172
+ // just for passing null reference exception
173
+ var context = new Mock < ParserRuleContext > ( ) ;
174
+ context . SetupGet ( c => c . Start . Line ) . Returns ( 1 ) ;
175
+ context . SetupGet ( c => c . Stop . Line ) . Returns ( 2 ) ;
176
+ context . SetupGet ( c => c . Stop . Text ) . Returns ( "Four" ) ;
177
+
178
+ // simulate all the components and symbols
179
+ var member = new Parsing . QualifiedMemberName ( selectedComponent , "fakeModule" ) ;
180
+ const string identifierName = "Foo" ;
181
+ AddDeclarationItem ( context , symbolSelection , member , DeclarationType . Procedure , identifierName ) ;
182
+ AddDeclarationItem ( context , new Parsing . Selection ( 1 , 1 , 1 , 16 ) , member , DeclarationType . Procedure ) ;
183
+ AddDeclarationItem ( context , new Parsing . Selection ( 1 , 1 , 1 , 1 ) , member , DeclarationType . Module ) ;
184
+
185
+ // allow Moq to set the Target property
186
+ _view . Setup ( view => view . ShowDialog ( ) ) . Returns ( DialogResult . Cancel ) ;
187
+ _view . SetupProperty ( view => view . Target ) ;
188
+
189
+ //act
190
+ var presenter = new RenamePresenter ( _vbe . Object , _view . Object , _declarations , qualifiedSelection ) ;
191
+ presenter . Show ( ) ;
192
+
193
+ //assert
194
+ var retVal = _view . Object . Target ;
195
+ Assert . AreEqual ( symbolSelection , retVal . Selection , "Returns only the declaration on the desired selection" ) ;
196
+ Assert . AreEqual ( identifierName , retVal . IdentifierName ) ;
197
+ }
198
+
199
+ [ TestMethod ]
200
+ public void AcquireTarget_MethodRenamingMoreComponents_CorrectTargetChosen ( )
201
+ {
202
+ // arange
203
+ // initial selection
204
+ var symbolSelection = new Parsing . Selection ( 4 , 5 , 4 , 8 ) ;
205
+ var selectedComponent = new Parsing . QualifiedModuleName ( "TestProject" , "Module1" , _vbProject . Object , 1 ) ;
206
+ var qualifiedSelection = new Parsing . QualifiedSelection ( selectedComponent , symbolSelection ) ;
207
+
208
+ // just for passing null reference exception
209
+ var context = new Mock < ParserRuleContext > ( ) ;
210
+ context . SetupGet ( c => c . Start . Line ) . Returns ( - 1 ) ;
211
+ context . SetupGet ( c => c . Stop . Line ) . Returns ( - 1 ) ;
212
+ context . SetupGet ( c => c . Stop . Text ) . Returns ( "Fake" ) ;
213
+
214
+ // simulate all the components and symbols
215
+ IdentifierReference reference ;
216
+ var differentComponent = new Parsing . QualifiedModuleName ( "TestProject" , "Module2" , _vbProject . Object , 1 ) ;
217
+ var differentMember = new Parsing . QualifiedMemberName ( differentComponent , "Module2" ) ;
218
+ AddDeclarationItem ( context , new Parsing . Selection ( 4 , 9 , 4 , 16 ) , differentMember , DeclarationType . Variable , "FooTest" ) ;
219
+
220
+ // add references to the Foo declaration item to simulate prod usage
221
+ AddDeclarationItem ( context , new Parsing . Selection ( 3 , 5 , 3 , 8 ) , differentMember , DeclarationType . Procedure , "Foo" ) ;
222
+ var declarationItem = _listDeclarations [ _listDeclarations . Count - 1 ] ;
223
+ reference = new IdentifierReference ( selectedComponent , "Foo" , new Parsing . Selection ( 7 , 5 , 7 , 11 ) , false , context . Object , declarationItem ) ;
224
+ AddReference ( declarationItem , reference ) ;
225
+ reference = new IdentifierReference ( selectedComponent , "Foo" , symbolSelection , false , context . Object , declarationItem ) ;
226
+ AddReference ( declarationItem , reference ) ;
227
+
228
+ AddDeclarationItem ( context , new Parsing . Selection ( 1 , 1 , 1 , 1 ) , differentMember , DeclarationType . Module , "Module2" ) ;
229
+ var member = new Parsing . QualifiedMemberName ( selectedComponent , "fakeModule" ) ;
230
+ AddDeclarationItem ( context , new Parsing . Selection ( 7 , 5 , 7 , 11 ) , member , DeclarationType . Procedure , "RunFoo" ) ;
231
+ AddDeclarationItem ( context , new Parsing . Selection ( 3 , 5 , 3 , 9 ) , member , DeclarationType . Procedure , "Main" ) ;
232
+ AddDeclarationItem ( context , new Parsing . Selection ( 1 , 1 , 1 , 1 ) , member , DeclarationType . Module , "Module1" ) ;
233
+
234
+ _view . Setup ( view => view . ShowDialog ( ) ) . Returns ( DialogResult . Cancel ) ;
235
+ _view . SetupProperty ( view => view . Target ) ;
236
+
237
+ //act
238
+ var presenter = new RenamePresenter ( _vbe . Object , _view . Object , _declarations , qualifiedSelection ) ;
239
+ presenter . Show ( ) ;
240
+
241
+ //assert
242
+ var retVal = _view . Object . Target ;
243
+ Assert . AreEqual ( "Foo" , retVal . IdentifierName , "Selected the correct symbol name" ) ;
244
+ Assert . AreEqual ( declarationItem . References . Count ( ) , retVal . References . Count ( ) ) ;
245
+ }
246
+ }
247
+ }
0 commit comments