context: using System.Linq;
  using System.Threading;
  using System.Windows.Forms;
  using Microsoft.Vbe.Interop;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using Moq;
  using Rubberduck.Parsing;
  using Rubberduck.Parsing.Symbols;
  using Rubberduck.Parsing.VBA;
  using Rubberduck.Refactorings;
  using Rubberduck.Refactorings.Rename;
  using Rubberduck.UI;
  using Rubberduck.VBEditor;
  using Rubberduck.VBEditor.Extensions;
  using Rubberduck.VBEditor.VBEHost;
  using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
  using RubberduckTests.Mocks;
  
  namespace RubberduckTests.Refactoring
  {
      [TestClass]
      public class RenameTests : VbeTestBase
      {
          [TestMethod]
          public void RenameRefactoring_RenameSub()
          {
              //Input
              const string inputCode =
  @"Private Sub Foo()
  End Sub";
              var selection = new Selection(1, 15, 1, 15);
  
              //Expectation
              const string expectedCode =
  @"Private Sub Goo()
  End Sub";
  
              //Arrange
              var builder = new MockVbeBuilder();
              VBComponent component;
              var vbe = builder.BuildFromSingleStandardModule(inputCode, out component, selection);
              var project = vbe.Object.VBProjects.Item(0);
              var module = project.VBComponents.Item(0).CodeModule;
              var mockHost = new Mock<IHostApplication>();
              mockHost.SetupAllProperties();
              var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
  
              parser.Parse(new CancellationTokenSource());
              if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
  
              var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);
  
              var msgbox = new Mock<IMessageBox>();
              msgbox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.YesNo, It.IsAny<MessageBoxIcon>()))
                    .Returns(DialogResult.Yes);
  
              var model = new RenameModel(vbe.Object, parser.State, qualifiedSelection, msgbox.Object) { NewName = "Goo" };
  
              //SetupFactory
              var factory = SetupFactory(model);
  
              //Act
              var refactoring = new RenameRefactoring(vbe.Object, factory.Object, msgbox.Object, parser.State);
              refactoring.Refactor(qualifiedSelection);
  
              //Assert
              var actual = module.Lines();
              Assert.AreEqual(expectedCode, actual);
          }
  
          [TestMethod]
          public void RenameRefactoring_RenameVariable()
          {
              //Input
              const string inputCode =
  @"Private Sub Foo()
      Dim val1 As Integer
  End Sub";
              var selection = new Selection(2, 12, 2, 12);
  
              //Expectation
              const string expectedCode =
  @"Private Sub Foo()
      Dim val2 As Integer
  End Sub";
  
              //Arrange
              var builder = new MockVbeBuilder();
              VBComponent component;
              var vbe = builder.BuildFromSingleStandardModule(inputCode, out component, selection);
              var project = vbe.Object.VBProjects.Item(0);
              var module = project.VBComponents.Item(0).CodeModule;
              var mockHost = new Mock<IHostApplication>();
              mockHost.SetupAllProperties();
              var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
  
              parser.Parse(new CancellationTokenSource());
              if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
  
              var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);
  
              var msgbox = new Mock<IMessageBox>();
              msgbox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.YesNo, It.IsAny<MessageBoxIcon>()))
                    .Returns(DialogResult.Yes);
  
              var model = new RenameModel(vbe.Object, parser.State, qualifiedSelection, msgbox.Object) { NewName = "val2" };
  
              //SetupFactory
              var factory = SetupFactory(model);
  
              //Act
              var refactoring = new RenameRefactoring(vbe.Object, factory.Object, msgbox.Object, parser.State);
              refactoring.Refactor(qualifiedSelection);
  
              //Assert
              Assert.AreEqual(expectedCode, module.Lines());
          }
  
          [TestMethod]
          public void RenameRefactoring_RenameParameter()
          {
              //Input
              const string inputCode =
  @"Private Sub Foo(ByVal arg1 As String)
  End Sub";
              var selection = new Selection(1, 25, 1, 25);
  
              //Expectation
              const string expectedCode =
  @"Private Sub Foo(ByVal arg2 As String)
  End Sub";
  
              //Arrange
              var builder = new MockVbeBuilder();
              VBComponent component;
              var vbe = builder.BuildFromSingleStandardModule(inputCode, out component, selection);
              var project = vbe.Object.VBProjects.Item(0);
              var module = project.VBComponents.Item(0).CodeModule;
              var mockHost = new Mock<IHostApplication>();
              mockHost.SetupAllProperties();
              var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
  
              parser.Parse(new CancellationTokenSource());
              if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
  
              var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);
  
              var msgbox = new Mock<IMessageBox>();
              msgbox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.YesNo, It.IsAny<MessageBoxIcon>()))
                    .Returns(DialogResult.Yes);
  
              var model = new RenameModel(vbe.Object, parser.State, qualifiedSelection, msgbox.Object) { NewName = "arg2" };
  
              //SetupFactory
              var factory = SetupFactory(model);
  
              //Act
              var refactoring = new RenameRefactoring(vbe.Object, factory.Object, msgbox.Object, parser.State);
              refactoring.Refactor(qualifiedSelection);
  
              //Assert
              Assert.AreEqual(expectedCode, module.Lines());
          }
  
          [TestMethod]
          public void RenameRefactoring_RenameSub_UpdatesReferences()
          {
              //Input
              const string inputCode =
  @"Private Sub Foo()
  End Sub
  
  Private Sub Goo()
      Foo
  End Sub
  ";
              var selection = new Selection(1, 15, 1, 15);
  
              //Expectation
              const string expectedCode =
  @"Private Sub Hoo()
  End Sub
  
  Private Sub Goo()
      Hoo
  End Sub
  ";
  
              //Arrange
              var builder = new MockVbeBuilder();
              VBComponent component;
              var vbe = builder.BuildFromSingleStandardModule(inputCode, out component, selection);
              var project = vbe.Object.VBProjects.Item(0);
              var module = project.VBComponents.Item(0).CodeModule;
              var mockHost = new Mock<IHostApplication>();
              mockHost.SetupAllProperties();
              var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
  
              parser.Parse(new CancellationTokenSource());
              if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
  
              var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);
  
              var msgbox = new Mock<IMessageBox>();
              msgbox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.YesNo, It.IsAny<MessageBoxIcon>()))
                    .Returns(DialogResult.Yes);
  
              var model = new RenameModel(vbe.Object, parser.State, qualifiedSelection, msgbox.Object) { NewName = "Hoo" };
  
              //SetupFactory
  
gt:             var factory = SetupFactory(model);