<commit_before>﻿using Moq;
using NUnit.Framework;
using RepoMonitor.Core;
using System;
using System.IO;
using System.Collections.Generic;

namespace RepoMonitor.Core.UnitTests {
  /// <summary>
  /// Class under test: <see cref="RepositoryFinder"/>
  /// </summary>
  [TestFixture]
  public class RepositoryFinderTest {
    #region Test data
    private const String HG_REPO_1 = "test-repo";
    private const String HG_REPO_2 = "test-repo-clone";
    private const String GIT_REPO_1 = "git-repo";
    private const String GIT_REPO_2 = "git-repo-clone";
    #endregion

    #region SCM mocks
    private Mock<SCM> hgMock;
    private Mock<SCM> gitMock;

    /// <summary>
    /// Configures the specified mock to respond to the "hg --version" command
    /// and returns the mock.
    /// </summary>
    [TestFixtureSetUp]
    public void MockSCMs() {
      hgMock = new Mock<SCM>();
      hgMock.Setup(scm => scm.IsRepository(It.Is<String>(
          s => s.EndsWith(HG_REPO_1) || s.EndsWith(HG_REPO_2)))).Returns(true);

      gitMock = new Mock<SCM>();
      gitMock.Setup(scm => scm.IsRepository(It.Is<String>(
          s => s.EndsWith(GIT_REPO_1) || s.EndsWith(GIT_REPO_2)))).Returns(true);
    }

    private SCM[] CreateArrayOfMockedSCMs() {
      SCM[] result = new SCM[2];
      result[0] = hgMock.Object;
      result[1] = gitMock.Object;

      return result;
    }
    #endregion

    #region Instantiation test cases

    /// <summary>
    /// Confirm the newly created <see cref="RepositoryFinder"/> is
    /// correctly initialized, i.e. SCMs property equals passed SCMs.
    /// </summary>
    [Test]
    public void NewRepositoryFinderIsInitialized() {
      SCM[] scms = CreateArrayOfMockedSCMs();
      RepositoryFinder finder = new RepositoryFinder(scms);

      Assert.IsNotNull(finder);
      Assert.AreEqual(scms, finder.SCMs);
    }

    /// <summary>
    /// Confirm an exception is thrown when trying to create
    /// <see cref="RepositoryFinder"/> without SCMs.
    /// </summary>
    [Test]
    public void InitializingRepositoryFinderWithNoSCMsThrowsExeption() {
      Assert.Throws<ArgumentException>(() => new RepositoryFinder(null));
      Assert.Throws<ArgumentException>(() => new RepositoryFinder(new SCM[0]));
    }

    #endregion

    /// <summary>
    /// Confirm an exception is thrown when passing invalid values to
    /// <see cref="RepositoryFinder.FindRepositories(String)"/> or
    /// <see cref="RepositoryFinder.FindRepositories(DirectoryInfo)"/>.
    /// </summary>
    [Test]
    public void InvalidParametersToFindRepositoriesThrowExeption() {
      String testResPath = TestUtil.TestResourcesPath;
      RepositoryFinder finder = new RepositoryFinder(CreateArrayOfMockedSCMs());

      Assert.Throws<ArgumentNullException>(() =>
          finder.FindRepositories((String) null));
      Assert.Throws<ArgumentException>(() =>
          finder.FindRepositories(""));
      Assert.Throws<DirectoryNotFoundException>(() =>
          finder.FindRepositories(Path.Combine(testResPath, "unknown")));

      Assert.Throws<ArgumentNullException>(() =>
          finder.FindRepositories((DirectoryInfo) null));
    }

    /// <summary>
    /// Confirm the <see cref="RepositoryFinder"/> has found the test repositories.
    /// </summary>
    [Test]
    public void FindRepositoriesOnTestReposReturnsCorrectList() {
      String testResPath = TestUtil.TestResourcesPath;
      RepositoryFinder finder = new RepositoryFinder(CreateArrayOfMockedSCMs());

      IDictionary<String, SCM> repos = finder.FindRepositories(testResPath);

      Assert.AreEqual(2, repos.Count);
      Assert.IsTrue(repos.ContainsKey(Path.Combine(testResPath, HG_REPO_1)));
      Assert.IsTrue(repos.ContainsKey(Path.Combine(testResPath, HG_REPO_2)));
      hgMock.Verify(m => m.IsRepository(It.IsAny<String>()));

      // TODO Add checks for Git: GIT_REPO_1 & GIT_REPO_2
    }
  }
}
<commit_msg>Listen to code coverage :smile:<commit_after>﻿using Moq;
using NUnit.Framework;
using RepoMonitor.Core;
using System;
using System.IO;
using System.Collections.Generic;

namespace RepoMonitor.Core.UnitTests {
  /// <summary>
  /// Class under test: <see cref="RepositoryFinder"/>
  /// </summary>
  [TestFixture]
  public class RepositoryFinderTest {
    #region Test data
    private const String HG_REPO_1 = "test-repo";
    private const String HG_REPO_2 = "test-repo-clone";
    private const String GIT_REPO_1 = "git-repo";
    private const String GIT_REPO_2 = "git-repo-clone";
    #endregion

    #region SCM mocks
    private Mock<SCM> hgMock;
    private Mock<SCM> gitMock;

    /// <summary>
    /// Configures the specified mock to respond to the "hg --version" command
    /// and returns the mock.
    /// </summary>
    [TestFixtureSetUp]
    public void MockSCMs() {
      hgMock = new Mock<SCM>();
      hgMock.Setup(scm => scm.IsRepository(It.Is<String>(
          s => s.EndsWith(HG_REPO_1) || s.EndsWith(HG_REPO_2)))).Returns(true);

      gitMock = new Mock<SCM>();
      gitMock.Setup(scm => scm.IsRepository(It.Is<String>(
          s => s.EndsWith(GIT_REPO_1) || s.EndsWith(GIT_REPO_2)))).Returns(true);
    }

    private SCM[] CreateArrayOfMockedSCMs() {
      SCM[] result = new SCM[2];
      result[0] = hgMock.Object;
      result[1] = gitMock.Object;

      return result;
    }
    #endregion

    #region Instantiation test cases

    /// <summary>
    /// Confirm the newly created <see cref="RepositoryFinder"/> is
    /// correctly initialized, i.e. SCMs property equals passed SCMs.
    /// </summary>
    [Test]
    public void NewRepositoryFinderIsInitialized() {
      SCM[] scms = CreateArrayOfMockedSCMs();
      RepositoryFinder finder = new RepositoryFinder(scms);

      Assert.IsNotNull(finder);
      Assert.AreEqual(scms, finder.SCMs);
    }

    /// <summary>
    /// Confirm an exception is thrown when trying to create
    /// <see cref="RepositoryFinder"/> without SCMs.
    /// </summary>
    [Test]
    public void InitializingRepositoryFinderWithNoSCMsThrowsExeption() {
      Assert.Throws<ArgumentException>(() => new RepositoryFinder(null));
      Assert.Throws<ArgumentException>(() => new RepositoryFinder(new SCM[0]));
    }

    #endregion

    /// <summary>
    /// Confirm an exception is thrown when passing invalid values to
    /// <see cref="RepositoryFinder.FindRepositories(String)"/> or
    /// <see cref="RepositoryFinder.FindRepositories(DirectoryInfo)"/>.
    /// </summary>
    [Test]
    public void InvalidParametersToFindRepositoriesThrowExeption() {
      String testResPath = TestUtil.TestResourcesPath;
      RepositoryFinder finder = new RepositoryFinder(CreateArrayOfMockedSCMs());

      Assert.Throws<ArgumentNullException>(() =>
          finder.FindRepositories((String) null));
      Assert.Throws<ArgumentException>(() =>
          finder.FindRepositories(""));
      Assert.Throws<DirectoryNotFoundException>(() =>
          finder.FindRepositories(Path.Combine(testResPath, "unknown")));

      Assert.Throws<ArgumentNullException>(() =>
          finder.FindRepositories((DirectoryInfo) null));
    }

    /// <summary>
    /// Confirm the <see cref="RepositoryFinder"/> has found the test repositories.
    /// </summary>
    [Test]
    public void FindRepositoriesOnTestReposReturnsCorrectList() {
      String testResPath = TestUtil.TestResourcesPath;
      RepositoryFinder finder = new RepositoryFinder(CreateArrayOfMockedSCMs());

      IDictionary<String, SCM> repos = finder.FindRepositories(testResPath);

      Assert.AreEqual(2, repos.Count);
      Assert.IsTrue(repos.ContainsKey(Path.Combine(testResPath, HG_REPO_1)));
      Assert.IsTrue(repos.ContainsKey(Path.Combine(testResPath, HG_REPO_2)));
      hgMock.Verify(m => m.IsRepository(It.IsAny<String>()));

      // TODO Add checks for Git: GIT_REPO_1 & GIT_REPO_2
    }

    /// <summary>
    /// Confirm the <see cref="RepositoryFinder"/> has found the repository.
    /// </summary>
    [Test]
    public void FindRepositoriesOnRepoDirReturnsOneItem() {
      String repoPath = Path.Combine(TestUtil.TestResourcesPath, HG_REPO_1);
      RepositoryFinder finder = new RepositoryFinder(CreateArrayOfMockedSCMs());

      IDictionary<String, SCM> repos = finder.FindRepositories(repoPath);

      Assert.AreEqual(1, repos.Count);
      hgMock.Verify(m => m.IsRepository(It.IsAny<String>()));

      // TODO Add checks for Git: GIT_REPO_1
    }
  }
}

{'fraction_non_alphanumeric': 0.13023863231627686, 'fraction_numerical': 0.004392734180220824, 'mean_word_length': 3.243828715365239, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5520095', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2595, 'n_words': 542}