|
| 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.VBEHost; |
| 13 | +using RubberduckTests.Mocks; |
| 14 | + |
| 15 | +namespace RubberduckTests.FindCommands |
| 16 | +{ |
| 17 | + [TestClass] |
| 18 | + public class FindAllReferencesTests |
| 19 | + { |
| 20 | + [TestMethod] |
| 21 | + public void FindAllReferences_ReturnsCorrectNumber() |
| 22 | + { |
| 23 | + const string inputCode = |
| 24 | +@"Public Sub Foo() |
| 25 | +End Sub |
| 26 | +
|
| 27 | +Private Sub Bar() |
| 28 | + Foo: Foo |
| 29 | + Foo |
| 30 | + Foo |
| 31 | +End Sub"; |
| 32 | + |
| 33 | + //Arrange |
| 34 | + var builder = new MockVbeBuilder(); |
| 35 | + VBComponent component; |
| 36 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 37 | + var mockHost = new Mock<IHostApplication>(); |
| 38 | + mockHost.SetupAllProperties(); |
| 39 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 40 | + |
| 41 | + parser.Parse(new CancellationTokenSource()); |
| 42 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 43 | + |
| 44 | + var vm = new SearchResultsWindowViewModel(); |
| 45 | + var command = new FindAllReferencesCommand(null, null, parser.State, vbe.Object, vm, null); |
| 46 | + |
| 47 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 48 | + |
| 49 | + Assert.AreEqual(4, vm.Tabs[0].SearchResults.Count); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void FindAllReferences_NoResults_DisplayMessageBox() |
| 54 | + { |
| 55 | + const string inputCode = |
| 56 | +@"Public Sub Foo() |
| 57 | +End Sub"; |
| 58 | + |
| 59 | + //Arrange |
| 60 | + var builder = new MockVbeBuilder(); |
| 61 | + VBComponent component; |
| 62 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 63 | + var mockHost = new Mock<IHostApplication>(); |
| 64 | + mockHost.SetupAllProperties(); |
| 65 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 66 | + |
| 67 | + parser.Parse(new CancellationTokenSource()); |
| 68 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 69 | + |
| 70 | + var messageBox = new Mock<IMessageBox>(); |
| 71 | + messageBox.Setup(m => |
| 72 | + m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), |
| 73 | + It.IsAny<MessageBoxIcon>())).Returns(DialogResult.OK); |
| 74 | + |
| 75 | + var vm = new SearchResultsWindowViewModel(); |
| 76 | + var command = new FindAllReferencesCommand(null, messageBox.Object, parser.State, vbe.Object, vm, null); |
| 77 | + |
| 78 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 79 | + |
| 80 | + messageBox.Verify(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), |
| 81 | + It.IsAny<MessageBoxIcon>()), Times.Once); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void FindAllReferences_SingleResult_Navigates() |
| 86 | + { |
| 87 | + const string inputCode = |
| 88 | +@"Public Sub Foo() |
| 89 | +End Sub |
| 90 | +
|
| 91 | +Private Sub Bar() |
| 92 | + Foo |
| 93 | +End Sub"; |
| 94 | + |
| 95 | + //Arrange |
| 96 | + var builder = new MockVbeBuilder(); |
| 97 | + VBComponent component; |
| 98 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 99 | + var mockHost = new Mock<IHostApplication>(); |
| 100 | + mockHost.SetupAllProperties(); |
| 101 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 102 | + |
| 103 | + parser.Parse(new CancellationTokenSource()); |
| 104 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 105 | + |
| 106 | + var navigateCommand = new Mock<INavigateCommand>(); |
| 107 | + |
| 108 | + var vm = new SearchResultsWindowViewModel(); |
| 109 | + var command = new FindAllReferencesCommand(navigateCommand.Object, null, parser.State, vbe.Object, vm, null); |
| 110 | + |
| 111 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 112 | + |
| 113 | + navigateCommand.Verify(n => n.Execute(It.IsAny<object>()), Times.Once); |
| 114 | + } |
| 115 | + |
| 116 | + [TestMethod] |
| 117 | + public void FindAllReferences_NullTarget_Aborts() |
| 118 | + { |
| 119 | + //Arrange |
| 120 | + var builder = new MockVbeBuilder(); |
| 121 | + VBComponent component; |
| 122 | + var vbe = builder.BuildFromSingleStandardModule(string.Empty, out component); |
| 123 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 124 | + var mockHost = new Mock<IHostApplication>(); |
| 125 | + mockHost.SetupAllProperties(); |
| 126 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 127 | + |
| 128 | + parser.Parse(new CancellationTokenSource()); |
| 129 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 130 | + |
| 131 | + var vm = new SearchResultsWindowViewModel(); |
| 132 | + var command = new FindAllReferencesCommand(null, null, parser.State, vbe.Object, vm, null); |
| 133 | + |
| 134 | + command.Execute(null); |
| 135 | + |
| 136 | + Assert.IsFalse(vm.Tabs.Any()); |
| 137 | + } |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void FindAllReferences_StateNotReady_Aborts() |
| 141 | + { |
| 142 | + const string inputCode = |
| 143 | +@"Public Sub Foo() |
| 144 | +End Sub |
| 145 | +
|
| 146 | +Private Sub Bar() |
| 147 | + Foo: Foo |
| 148 | + Foo |
| 149 | + Foo |
| 150 | +End Sub"; |
| 151 | + |
| 152 | + //Arrange |
| 153 | + var builder = new MockVbeBuilder(); |
| 154 | + VBComponent component; |
| 155 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 156 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 157 | + var mockHost = new Mock<IHostApplication>(); |
| 158 | + mockHost.SetupAllProperties(); |
| 159 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 160 | + |
| 161 | + parser.Parse(new CancellationTokenSource()); |
| 162 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 163 | + |
| 164 | + parser.State.SetStatusAndFireStateChanged(ParserState.ResolvedDeclarations); |
| 165 | + |
| 166 | + var vm = new SearchResultsWindowViewModel(); |
| 167 | + var command = new FindAllReferencesCommand(null, null, parser.State, vbe.Object, vm, null); |
| 168 | + |
| 169 | + command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo")); |
| 170 | + |
| 171 | + Assert.IsFalse(vm.Tabs.Any()); |
| 172 | + } |
| 173 | + |
| 174 | + [TestMethod] |
| 175 | + public void FindAllReferences_CanExecute_NullTarget() |
| 176 | + { |
| 177 | + //Arrange |
| 178 | + var builder = new MockVbeBuilder(); |
| 179 | + VBComponent component; |
| 180 | + var vbe = builder.BuildFromSingleStandardModule(string.Empty, out component); |
| 181 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 182 | + var mockHost = new Mock<IHostApplication>(); |
| 183 | + mockHost.SetupAllProperties(); |
| 184 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 185 | + |
| 186 | + parser.Parse(new CancellationTokenSource()); |
| 187 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 188 | + |
| 189 | + var vm = new SearchResultsWindowViewModel(); |
| 190 | + var command = new FindAllReferencesCommand(null, null, parser.State, vbe.Object, vm, null); |
| 191 | + |
| 192 | + Assert.IsFalse(command.CanExecute(null)); |
| 193 | + } |
| 194 | + |
| 195 | + [TestMethod] |
| 196 | + public void FindAllReferences_CanExecute_StateNotReady() |
| 197 | + { |
| 198 | + const string inputCode = |
| 199 | +@"Public Sub Foo() |
| 200 | +End Sub |
| 201 | +
|
| 202 | +Private Sub Bar() |
| 203 | + Foo: Foo |
| 204 | + Foo |
| 205 | + Foo |
| 206 | +End Sub"; |
| 207 | + |
| 208 | + //Arrange |
| 209 | + var builder = new MockVbeBuilder(); |
| 210 | + VBComponent component; |
| 211 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 212 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 213 | + var mockHost = new Mock<IHostApplication>(); |
| 214 | + mockHost.SetupAllProperties(); |
| 215 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 216 | + |
| 217 | + parser.Parse(new CancellationTokenSource()); |
| 218 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 219 | + |
| 220 | + parser.State.SetStatusAndFireStateChanged(ParserState.ResolvedDeclarations); |
| 221 | + |
| 222 | + var vm = new SearchResultsWindowViewModel(); |
| 223 | + var command = new FindAllReferencesCommand(null, null, parser.State, vbe.Object, vm, null); |
| 224 | + |
| 225 | + Assert.IsFalse(command.CanExecute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo"))); |
| 226 | + } |
| 227 | + |
| 228 | + [TestMethod] |
| 229 | + public void FindAllReferences_CanExecute_NullActiveCodePane() |
| 230 | + { |
| 231 | + //Arrange |
| 232 | + var builder = new MockVbeBuilder(); |
| 233 | + VBComponent component; |
| 234 | + var vbe = builder.BuildFromSingleStandardModule(string.Empty, out component); |
| 235 | + vbe.Setup(s => s.ActiveCodePane).Returns(value: null); |
| 236 | + var mockHost = new Mock<IHostApplication>(); |
| 237 | + mockHost.SetupAllProperties(); |
| 238 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 239 | + |
| 240 | + parser.Parse(new CancellationTokenSource()); |
| 241 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 242 | + |
| 243 | + var vm = new SearchResultsWindowViewModel(); |
| 244 | + var command = new FindAllReferencesCommand(null, null, parser.State, vbe.Object, vm, null); |
| 245 | + |
| 246 | + Assert.IsFalse(command.CanExecute(null)); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments