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