Inheritance: ViewModelBase, IControlViewModel
        public void UnPublishedBranchesAreListed()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //assert
            var expected = new List<string> { "dev" };
            CollectionAssert.AreEqual(expected, vm.UnpublishedBranches.ToList());
        }
        public void SelectedBranchShouldBeCurrentBranchAfterRefresh()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            _provider.SetupGet(git => git.CurrentBranch).Returns(_branches[1]);

            //act
            vm.RefreshView();

            //assert
            Assert.AreEqual(_provider.Object.CurrentBranch.Name, vm.CurrentBranch);
        }
        public void InitializeMocks()
        {
            _window = Mocks.MockFactory.CreateWindowMock();
            _windows = new MockWindowsCollection(new List<Window> { _window.Object });
            _vbe = Mocks.MockFactory.CreateVbeMock(_windows);

            _configService = new Mock<IConfigurationService<SourceControlConfiguration>>();
            _configService.Setup(c => c.LoadConfiguration()).Returns(GetDummyConfig());

            _folderBrowser = new Mock<IFolderBrowser>();
            _folderBrowserFactory = new Mock<IFolderBrowserFactory>();
            _folderBrowserFactory.Setup(f => f.CreateFolderBrowser(It.IsAny<string>())).Returns(_folderBrowser.Object);
            _folderBrowserFactory.Setup(f => f.CreateFolderBrowser(It.IsAny<string>(), false)).Returns(_folderBrowser.Object);


            var masterRemote = new Mock<LibGit2Sharp.Branch>();
            masterRemote.SetupGet(git => git.Tip).Returns(new Mock<LibGit2Sharp.Commit>().Object);
            masterRemote.SetupGet(git => git.FriendlyName).Returns("master");

            var initialBranch = new Branch("master", "refs/Heads/master", false, true, masterRemote.Object);

            _provider = new Mock<ISourceControlProvider>();
            _provider.SetupGet(git => git.CurrentBranch).Returns(initialBranch);
            _provider.SetupGet(git => git.UnsyncedLocalCommits).Returns(new List<ICommit>());
            _provider.SetupGet(git => git.UnsyncedRemoteCommits).Returns(new List<ICommit>());
            _provider.Setup(git => git.InitVBAProject(It.IsAny<string>())).Returns(GetDummyRepo());
            _provider.Setup(git => git.Clone(It.IsAny<string>(), It.IsAny<string>())).Returns(GetDummyRepo());

            _providerFactory = new Mock<ISourceControlProviderFactory>();
            _providerFactory.Setup(f => f.CreateProvider(It.IsAny<VBProject>()))
                .Returns(_provider.Object);
            _providerFactory.Setup(f => f.CreateProvider(It.IsAny<VBProject>(), It.IsAny<IRepository>(), It.IsAny<ICodePaneWrapperFactory>()))
                .Returns(_provider.Object);
            _providerFactory.Setup(f => f.CreateProvider(It.IsAny<VBProject>(), It.IsAny<IRepository>(), It.IsAny<SecureCredentials>(), It.IsAny<ICodePaneWrapperFactory>()))
                .Returns(_provider.Object);

            _changesVM = new ChangesViewViewModel();
            _branchesVM = new BranchesViewViewModel();
            _unsyncedVM = new UnsyncedCommitsViewViewModel();
            _settingsVM = new SettingsViewViewModel(_configService.Object, _folderBrowserFactory.Object);
        }
        public void MergeViewIsShownOnMergeClick()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //act
            vm.MergeBranchCommand.Execute(null);

            //Assert
            Assert.IsTrue(vm.DisplayMergeBranchesGrid);
        }
        public void MergeViewIsNotShownWhenLocal_IsNull()
        {
            //arrange
            var vm = new BranchesViewViewModel();

            //Assert
            Assert.IsFalse(vm.MergeBranchCommand.CanExecute(null));
        }
        public void MergeBranchViewIsShownOnCreateBranch()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //act
            vm.NewBranchCommand.Execute(null);

            //Assert
            Assert.IsFalse(vm.DisplayMergeBranchesGrid);
        }
        public void DeleteBranchDisabled_BranchIsActive()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //Assert
            Assert.IsFalse(vm.DeleteBranchToolbarButtonCommand.CanExecute("master"));
        }
        public void ChangingSelectedBranchChecksOutThatBranch()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                CurrentBranch = "dev"
            };

            //assert
            _provider.Verify(git => git.Checkout("dev"));
        }
        public void OnBranchChange_WhenCheckoutFails_ActionFailedEventIsRaised()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };
            var wasRaised = false;

            _provider.Setup(p => p.Checkout(It.IsAny<string>()))
                .Throws(
                    new SourceControlException("A source control exception was thrown.",
                        new LibGit2Sharp.LibGit2SharpException("With an inner libgit2sharp exception"))
                    );

            vm.ErrorThrown += (sender, error) => wasRaised = true;

            //act
            vm.CurrentBranch = null;

            //assert
            Assert.IsTrue(wasRaised, "ActionFailedEvent was not raised.");
        }
        public void DeleteBranchEnabled_BranchIsNotActive()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //Assert
            Assert.IsTrue(vm.DeleteBranchToolbarButtonCommand.CanExecute("bugbranch"));
        }
        public void MergeViewIsHiddenOnSuccessfulMerge()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                SourceBranch = "fizz",
                DestinationBranch = "buzz",
                DisplayMergeBranchesGrid = true
            };

            //act
            vm.MergeBranchesOkButtonCommand.Execute(null);

            //Assert
            Assert.IsFalse(vm.DisplayMergeBranchesGrid);
        }
        public void CreateBranch_NameIsAtSign()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "@"
            };

            //Assert
            Assert.IsFalse(vm.CreateBranchOkButtonCommand.CanExecute(null));
        }
        public void CreateBranch_NameContainsConsecutiveAtSignAndOpenBrace()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "bug@{branch"
            };

            //Assert
            Assert.IsFalse(vm.CreateBranchOkButtonCommand.CanExecute(null));
        }
        public void CreateBranch_NameStartsWithSlash()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "/bugBranch"
            };

            //Assert
            Assert.IsFalse(vm.CreateBranchOkButtonCommand.CanExecute(null));
        }
        public void CreateBranch_ValidBranchName()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "bugBranch"
            };

            //Assert
            Assert.IsTrue(vm.CreateBranchOkButtonCommand.CanExecute(null));
        }
        public void CreateBranch_BranchExists()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //Assert
            Assert.IsFalse(vm.CreateBranchOkButtonCommand.CanExecute("master"));
        }
        public void MergeViewSelectedDestinationBranchIsCurrentBranch()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //assert 
            Assert.AreEqual(_intialBranch.Name, vm.DestinationBranch);
        }
        public void CreateBranch_NameContainsBackslash()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "bug\\branch"
            };

            //Assert
            Assert.IsFalse(vm.CreateBranchOkButtonCommand.CanExecute(null));
        }
        public void ProviderMergesOnMergeViewSubmit()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                SourceBranch = "dev",
                DestinationBranch = "master"
            };

            //act
            vm.MergeBranchesOkButtonCommand.Execute(null);

            //assert
            _provider.Verify(git => git.Merge("dev", "master"));
        }
        public void CreateBranch_NameContainsSlashSectionEndingWithDotlock()
        {
            //arrange
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "bug/branch.lock"
            };

            //Assert
            Assert.IsFalse(vm.CreateBranchOkButtonCommand.CanExecute(null));
        }
        public void MergeViewIsHiddenOnCancel()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                DisplayMergeBranchesGrid = true
            };

            //act
            vm.MergeBranchesCancelButtonCommand.Execute(null);

            //Assert
            Assert.IsFalse(vm.DisplayMergeBranchesGrid);
        }
        public void OnCreateBranch_WhenCreateFails_ActionFailedEventIsRaised()
        {
            //arrange
            var wasRaised = false;
            var branchName = "dev";

            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = branchName
            };

            _provider.Setup(p => p.CreateBranch(It.Is<string>(b => b == vm.CurrentBranch), It.Is<string>(b => b == branchName)))
                .Throws(
                    new SourceControlException("A source control exception was thrown.",
                        new LibGit2Sharp.LibGit2SharpException("With an inner libgit2sharp exception"))
                    );

            vm.ErrorThrown += (sender, error) => wasRaised = true;

            //act
            vm.CreateBranchOkButtonCommand.Execute(null);

            //assert
            Assert.IsTrue(wasRaised, "ActionFailedEvent was not raised.");
        }
        public void RefreshingViewShouldNotCheckoutBranch()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
            };

            //act
            vm.RefreshView();

            //assert
            _provider.Verify(git => git.Checkout(It.IsAny<string>()), Times.Once);  //checkout when we first set provider
        }
        public void UnpublishBranch_ActionFailedEventIsRaised()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };
            var wasRaised = false;

            _provider.Setup(p => p.Unpublish(It.IsAny<string>()))
                .Throws(
                    new SourceControlException("A source control exception was thrown.",
                        new LibGit2Sharp.LibGit2SharpException("With an inner libgit2sharp exception"))
                    );

            vm.ErrorThrown += (sender, error) => wasRaised = true;

            //act
            vm.UnpublishBranchToolbarButtonCommand.Execute("master");

            //assert
            Assert.IsTrue(wasRaised, "ActionFailedEvent was not raised.");
        }
        public void ProviderCallsCreateBranchOnCreateBranchConfirm()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "bugBranch"
            };

            //act
            vm.CreateBranchOkButtonCommand.Execute(null);

            //assert
            _provider.Verify(git => git.CreateBranch(It.Is<string>(s => s == vm.CurrentBranch), It.Is<string>(s => s == "bugBranch")));
        }
        public void DeleteBranch_BranchIsNotActive_BranchIsRemoved()
        {
            //arrange
            var firstBranchName = "master";
            var secondBranchName = "bugBranch";

            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //Assert
            Assert.IsFalse(vm.DeleteBranchToolbarButtonCommand.CanExecute("master"));

            _provider.SetupGet(p => p.Branches).Returns(
                new List<IBranch>
                {
                    new Branch(firstBranchName, "ref/Heads/" + firstBranchName, false, true, null),
                    new Branch(secondBranchName, "ref/Heads/" + secondBranchName, false, false, null)
                });
            _provider.Setup(p => p.DeleteBranch(It.IsAny<string>()));

            //act
            vm.DeleteBranchToolbarButtonCommand.Execute(secondBranchName);

            //Assert
            _provider.Verify(p => p.DeleteBranch(secondBranchName));
        }
        public void UnpublishUnpublishesBranch()
        {
            //arrange
            var branch = "master";
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //act
            vm.UnpublishBranchToolbarButtonCommand.Execute(branch);

            //Assert
            _provider.Verify(git => git.Unpublish(branch));
        }
        public void CreateBranchViewIshiddenAfterCancel()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                DisplayCreateBranchGrid = true
            };

            //act
            vm.CreateBranchCancelButtonCommand.Execute(null);

            //Assert
            Assert.IsFalse(vm.DisplayCreateBranchGrid);
        }
        public void OnlyLocalBranchesInBranches()
        {
            //arrange 
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object
            };

            //assert
            var expected = new List<string> { "master", "dev" };
            CollectionAssert.AreEquivalent(expected, vm.LocalBranches.ToList());
        }
        public void CreateBranchUserInputIsClearedAfterCancel()
        {
            //arrange
            var vm = new BranchesViewViewModel
            {
                Provider = _provider.Object,
                NewBranchName = "newBranch"
            };

            //act
            vm.CreateBranchCancelButtonCommand.Execute(null);

            //assert
            Assert.AreEqual(string.Empty, vm.NewBranchName);
        }