|
| 1 | +using System.Linq; |
| 2 | +using System.Threading; |
| 3 | +using System.Windows.Forms; |
| 4 | +using Microsoft.Vbe.Interop; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | +using Moq; |
| 7 | +using Rubberduck.Parsing; |
| 8 | +using Rubberduck.Parsing.VBA; |
| 9 | +using Rubberduck.UI; |
| 10 | +using Rubberduck.UI.Command; |
| 11 | +using Rubberduck.UI.Controls; |
| 12 | +using Rubberduck.VBEditor; |
| 13 | +using Rubberduck.VBEditor.VBEHost; |
| 14 | +using RubberduckTests.Mocks; |
| 15 | + |
| 16 | +namespace RubberduckTests.FindCommands |
| 17 | +{ |
| 18 | + [TestClass] |
| 19 | + public class FindAllImplementationsTests |
| 20 | + { |
| 21 | + [TestMethod] |
| 22 | + public void FindAllImplementations_ReturnsCorrectNumber() |
| 23 | + { |
| 24 | + const string inputClass = |
| 25 | +@"Implements IClass1 |
| 26 | +
|
| 27 | +Public Sub IClass1_Foo() |
| 28 | +End Sub"; |
| 29 | + |
| 30 | + const string inputInterface = |
| 31 | +@"Public Sub Foo() |
| 32 | +End Sub"; |
| 33 | + |
| 34 | + //Arrange |
| 35 | + var builder = new MockVbeBuilder(); |
| 36 | + var project = builder.ProjectBuilder("TestProject", vbext_ProjectProtection.vbext_pp_none) |
| 37 | + .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputClass) |
| 38 | + .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputClass) |
| 39 | + .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputInterface) |
| 40 | + .Build(); |
| 41 | + |
| 42 | + var vbe = builder.AddProject(project).Build(); |
| 43 | + var mockHost = new Mock<IHostApplication>(); |
| 44 | + mockHost.SetupAllProperties(); |
| 45 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 46 | + |
| 47 | + parser.Parse(new CancellationTokenSource()); |
| 48 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 49 | + |
| 50 | + var vm = new SearchResultsWindowViewModel(); |
| 51 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 52 | + |
| 53 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 54 | + |
| 55 | + Assert.AreEqual(2, vm.Tabs[0].SearchResults.Count); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void FindAllImplementations_SelectedImplementation_ReturnsCorrectNumber() |
| 60 | + { |
| 61 | + const string inputClass = |
| 62 | +@"Implements IClass1 |
| 63 | +
|
| 64 | +Public Sub IClass1_Foo() |
| 65 | +End Sub"; |
| 66 | + |
| 67 | + const string inputInterface = |
| 68 | +@"Public Sub Foo() |
| 69 | +End Sub"; |
| 70 | + |
| 71 | + //Arrange |
| 72 | + var builder = new MockVbeBuilder(); |
| 73 | + var project = builder.ProjectBuilder("TestProject", vbext_ProjectProtection.vbext_pp_none) |
| 74 | + .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputClass) |
| 75 | + .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputClass) |
| 76 | + .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputInterface) |
| 77 | + .Build(); |
| 78 | + |
| 79 | + var vbe = builder.AddProject(project).Build(); |
| 80 | + var mockHost = new Mock<IHostApplication>(); |
| 81 | + mockHost.SetupAllProperties(); |
| 82 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 83 | + |
| 84 | + parser.Parse(new CancellationTokenSource()); |
| 85 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 86 | + |
| 87 | + var vm = new SearchResultsWindowViewModel(); |
| 88 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 89 | + |
| 90 | + command.Execute(parser.State.AllUserDeclarations.First(s => s.IdentifierName == "IClass1_Foo")); |
| 91 | + |
| 92 | + Assert.AreEqual(2, vm.Tabs[0].SearchResults.Count); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void FindAllImplementations_SelectedReference_ReturnsCorrectNumber() |
| 97 | + { |
| 98 | + const string inputClass = |
| 99 | +@"Implements IClass1 |
| 100 | +
|
| 101 | +Public Sub IClass1_Foo() |
| 102 | +End Sub |
| 103 | +
|
| 104 | +Public Sub Buzz() |
| 105 | + IClass1_Foo |
| 106 | +End Sub"; |
| 107 | + |
| 108 | + const string inputInterface = |
| 109 | +@"Public Sub Foo() |
| 110 | +End Sub"; |
| 111 | + |
| 112 | + //Arrange |
| 113 | + var builder = new MockVbeBuilder(); |
| 114 | + var project = builder.ProjectBuilder("TestProject", vbext_ProjectProtection.vbext_pp_none) |
| 115 | + .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputClass, new Selection(7, 5, 7, 5)) |
| 116 | + .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputClass) |
| 117 | + .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputInterface) |
| 118 | + .Build(); |
| 119 | + |
| 120 | + var vbe = builder.AddProject(project).Build(); |
| 121 | + vbe.Setup(v => v.ActiveCodePane).Returns(project.Object.VBComponents.Item("Class1").CodeModule.CodePane); |
| 122 | + |
| 123 | + var mockHost = new Mock<IHostApplication>(); |
| 124 | + mockHost.SetupAllProperties(); |
| 125 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 126 | + |
| 127 | + parser.Parse(new CancellationTokenSource()); |
| 128 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 129 | + |
| 130 | + var vm = new SearchResultsWindowViewModel(); |
| 131 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 132 | + |
| 133 | + command.Execute(null); |
| 134 | + |
| 135 | + Assert.AreEqual(2, vm.Tabs[0].SearchResults.Count); |
| 136 | + } |
| 137 | + |
| 138 | + [TestMethod] |
| 139 | + public void FindAllImplementations_NoResults_DisplayMessageBox() |
| 140 | + { |
| 141 | + const string inputCode = |
| 142 | +@"Public Sub Foo() |
| 143 | +End Sub"; |
| 144 | + |
| 145 | + //Arrange |
| 146 | + var builder = new MockVbeBuilder(); |
| 147 | + VBComponent component; |
| 148 | + var vbe = builder.BuildFromSingleModule(inputCode, vbext_ComponentType.vbext_ct_ClassModule, out component, default(Selection)); |
| 149 | + var mockHost = new Mock<IHostApplication>(); |
| 150 | + mockHost.SetupAllProperties(); |
| 151 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 152 | + |
| 153 | + parser.Parse(new CancellationTokenSource()); |
| 154 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 155 | + |
| 156 | + var messageBox = new Mock<IMessageBox>(); |
| 157 | + messageBox.Setup(m => |
| 158 | + m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), |
| 159 | + It.IsAny<MessageBoxIcon>())).Returns(DialogResult.OK); |
| 160 | + |
| 161 | + var vm = new SearchResultsWindowViewModel(); |
| 162 | + var command = new FindAllImplementationsCommand(null, messageBox.Object, parser.State, vbe.Object, vm, null); |
| 163 | + |
| 164 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 165 | + |
| 166 | + messageBox.Verify(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), |
| 167 | + It.IsAny<MessageBoxIcon>()), Times.Once); |
| 168 | + } |
| 169 | + |
| 170 | + [TestMethod] |
| 171 | + public void FindAllImplementations_SingleResult_Navigates() |
| 172 | + { |
| 173 | + const string inputClass = |
| 174 | +@"Implements IClass1 |
| 175 | +
|
| 176 | +Public Sub IClass1_Foo() |
| 177 | +End Sub"; |
| 178 | + |
| 179 | + const string inputInterface = |
| 180 | +@"Public Sub Foo() |
| 181 | +End Sub"; |
| 182 | + |
| 183 | + //Arrange |
| 184 | + var builder = new MockVbeBuilder(); |
| 185 | + var project = builder.ProjectBuilder("TestProject", vbext_ProjectProtection.vbext_pp_none) |
| 186 | + .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputClass) |
| 187 | + .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputInterface) |
| 188 | + .Build(); |
| 189 | + |
| 190 | + var vbe = builder.AddProject(project).Build(); |
| 191 | + var mockHost = new Mock<IHostApplication>(); |
| 192 | + mockHost.SetupAllProperties(); |
| 193 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 194 | + |
| 195 | + parser.Parse(new CancellationTokenSource()); |
| 196 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 197 | + |
| 198 | + var navigateCommand = new Mock<INavigateCommand>(); |
| 199 | + |
| 200 | + var vm = new SearchResultsWindowViewModel(); |
| 201 | + var command = new FindAllImplementationsCommand(navigateCommand.Object, null, parser.State, vbe.Object, vm, null); |
| 202 | + |
| 203 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 204 | + |
| 205 | + navigateCommand.Verify(n => n.Execute(It.IsAny<object>()), Times.Once); |
| 206 | + } |
| 207 | + |
| 208 | + [TestMethod] |
| 209 | + public void FindAllImplementations_NullTarget_Aborts() |
| 210 | + { |
| 211 | + //Arrange |
| 212 | + var builder = new MockVbeBuilder(); |
| 213 | + VBComponent component; |
| 214 | + var vbe = builder.BuildFromSingleStandardModule(string.Empty, out component); |
| 215 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 216 | + var mockHost = new Mock<IHostApplication>(); |
| 217 | + mockHost.SetupAllProperties(); |
| 218 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 219 | + |
| 220 | + parser.Parse(new CancellationTokenSource()); |
| 221 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 222 | + |
| 223 | + var vm = new SearchResultsWindowViewModel(); |
| 224 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 225 | + |
| 226 | + command.Execute(null); |
| 227 | + |
| 228 | + Assert.IsFalse(vm.Tabs.Any()); |
| 229 | + } |
| 230 | + |
| 231 | + [TestMethod] |
| 232 | + public void FindAllImplementations_StateNotReady_Aborts() |
| 233 | + { |
| 234 | + const string inputCode = |
| 235 | +@"Public Sub Foo() |
| 236 | +End Sub |
| 237 | +
|
| 238 | +Private Sub Bar() |
| 239 | + Foo: Foo |
| 240 | + Foo |
| 241 | + Foo |
| 242 | +End Sub"; |
| 243 | + |
| 244 | + //Arrange |
| 245 | + var builder = new MockVbeBuilder(); |
| 246 | + VBComponent component; |
| 247 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 248 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 249 | + var mockHost = new Mock<IHostApplication>(); |
| 250 | + mockHost.SetupAllProperties(); |
| 251 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 252 | + |
| 253 | + parser.Parse(new CancellationTokenSource()); |
| 254 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 255 | + |
| 256 | + parser.State.SetStatusAndFireStateChanged(ParserState.ResolvedDeclarations); |
| 257 | + |
| 258 | + var vm = new SearchResultsWindowViewModel(); |
| 259 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 260 | + |
| 261 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 262 | + |
| 263 | + Assert.IsFalse(vm.Tabs.Any()); |
| 264 | + } |
| 265 | + |
| 266 | + [TestMethod] |
| 267 | + public void FindAllImplementations_CanExecute_NullTarget() |
| 268 | + { |
| 269 | + //Arrange |
| 270 | + var builder = new MockVbeBuilder(); |
| 271 | + VBComponent component; |
| 272 | + var vbe = builder.BuildFromSingleStandardModule(string.Empty, out component); |
| 273 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 274 | + var mockHost = new Mock<IHostApplication>(); |
| 275 | + mockHost.SetupAllProperties(); |
| 276 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 277 | + |
| 278 | + parser.Parse(new CancellationTokenSource()); |
| 279 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 280 | + |
| 281 | + var vm = new SearchResultsWindowViewModel(); |
| 282 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 283 | + |
| 284 | + Assert.IsFalse(command.CanExecute(null)); |
| 285 | + } |
| 286 | + |
| 287 | + [TestMethod] |
| 288 | + public void FindAllImplementations_CanExecute_StateNotReady() |
| 289 | + { |
| 290 | + const string inputCode = |
| 291 | +@"Public Sub Foo() |
| 292 | +End Sub |
| 293 | +
|
| 294 | +Private Sub Bar() |
| 295 | + Foo: Foo |
| 296 | + Foo |
| 297 | + Foo |
| 298 | +End Sub"; |
| 299 | + |
| 300 | + //Arrange |
| 301 | + var builder = new MockVbeBuilder(); |
| 302 | + VBComponent component; |
| 303 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 304 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 305 | + var mockHost = new Mock<IHostApplication>(); |
| 306 | + mockHost.SetupAllProperties(); |
| 307 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 308 | + |
| 309 | + parser.Parse(new CancellationTokenSource()); |
| 310 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 311 | + |
| 312 | + parser.State.SetStatusAndFireStateChanged(ParserState.ResolvedDeclarations); |
| 313 | + |
| 314 | + var vm = new SearchResultsWindowViewModel(); |
| 315 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 316 | + |
| 317 | + Assert.IsFalse(command.CanExecute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo"))); |
| 318 | + } |
| 319 | + |
| 320 | + [TestMethod] |
| 321 | + public void FindAllImplementations_CanExecute_NullActiveCodePane() |
| 322 | + { |
| 323 | + //Arrange |
| 324 | + var builder = new MockVbeBuilder(); |
| 325 | + VBComponent component; |
| 326 | + var vbe = builder.BuildFromSingleStandardModule(string.Empty, out component); |
| 327 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 328 | + var mockHost = new Mock<IHostApplication>(); |
| 329 | + mockHost.SetupAllProperties(); |
| 330 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 331 | + |
| 332 | + parser.Parse(new CancellationTokenSource()); |
| 333 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 334 | + |
| 335 | + var vm = new SearchResultsWindowViewModel(); |
| 336 | + var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null); |
| 337 | + |
| 338 | + Assert.IsFalse(command.CanExecute(null)); |
| 339 | + } |
| 340 | + } |
| 341 | +} |
0 commit comments