|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Runtime.ExceptionServices; |
| 4 | +using System.Threading; |
| 5 | +using NUnit.Framework; |
| 6 | +using Rubberduck.Parsing.Symbols; |
| 7 | +using Rubberduck.VBEditor; |
| 8 | +using Rubberduck.VBEditor.SafeComWrappers; |
| 9 | +using RubberduckTests.Mocks; |
| 10 | + |
| 11 | +namespace RubberduckTests.Symbols |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class VariableDeclarationTests |
| 15 | + { |
| 16 | + [Test] |
| 17 | + [TestCase("Private WithEvents foo As EventSource", true)] |
| 18 | + [TestCase("Private foo As EventSource", false)] |
| 19 | + [Category("Resolver")] |
| 20 | + public void WithEventsIsResolvedCorrectly(string declaration, bool withEvents) |
| 21 | + { |
| 22 | + var variables = ArrangeAndGetVariableDeclarations(ComponentType.ClassModule, declaration); |
| 23 | + var foo = variables.Single(); |
| 24 | + Assert.AreEqual(withEvents, foo.IsWithEvents); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + [TestCase("Private WithEvents {0} As EventSource, WithEvents {1} As EventSource", "foo", "bar", true, true)] |
| 29 | + [TestCase("Private {0} As EventSource, WithEvents {1} As EventSource", "foo", "bar", false, true)] |
| 30 | + [TestCase("Private WithEvents {0} As EventSource, {1} As EventSource", "foo", "bar", true, false)] |
| 31 | + [TestCase("Private {0} As EventSource, {1} As EventSource", "foo", "bar", false, false)] |
| 32 | + [Category("Resolver")] |
| 33 | + public void WithEventsIsResolvedCorrectlyVariableList(string template, string first, string second, bool firstEvents, bool secondEvents) |
| 34 | + { |
| 35 | + var variables = ArrangeAndGetVariableDeclarations(ComponentType.ClassModule, string.Format(template, first, second)); |
| 36 | + Assert.AreEqual(2, variables.Count); |
| 37 | + Assert.AreEqual(firstEvents, variables.Single(variable => variable.IdentifierName.Equals(first)).IsWithEvents); |
| 38 | + Assert.AreEqual(secondEvents, variables.Single(variable => variable.IdentifierName.Equals(second)).IsWithEvents); |
| 39 | + } |
| 40 | + |
| 41 | + private List<VariableDeclaration> ArrangeAndGetVariableDeclarations(ComponentType moduleType, string code) |
| 42 | + { |
| 43 | + var vbe = new MockVbeBuilder() |
| 44 | + .ProjectBuilder("TestProject", ProjectProtection.Unprotected) |
| 45 | + .AddComponent("UnderTest", moduleType, code, new Selection(1, 1)) |
| 46 | + .AddProjectToVbeBuilder() |
| 47 | + .Build(); |
| 48 | + |
| 49 | + using (var parser = MockParser.Create(vbe.Object)) |
| 50 | + { |
| 51 | + parser.Parse(new CancellationTokenSource()); |
| 52 | + |
| 53 | + return parser.State.DeclarationFinder.UserDeclarations(DeclarationType.Variable) |
| 54 | + .Cast<VariableDeclaration>() |
| 55 | + .ToList(); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments