<commit_before>require 'valr'

describe Valr do
  after(:each) do
    clear_fixtures
  end

  describe '#new' do
    context 'without a git repository' do
      before(:each) do
        create_non_repository
      end

      it 'raise an error' do
        expect{Valr::Repo.new repo_path}.to raise_error Valr::RepositoryError, "'#{repo_path}' is not a git repository"
      end
    end

    context 'with an empty repository' do
      before(:each) do
        create_empty_repository
      end

      it 'raise an error' do
        expect{Valr::Repo.new repo_path}.to raise_error Valr::EmptyRepositoryError, "'#{repo_path}' is empty"
      end
    end

    context 'with a valid git repository' do
      before(:each) do
        create_simple_fixtures
      end

      it 'creates an instance of Valr::Repo' do
        expect{Valr::Repo.new repo_path}.not_to raise_error
      end
    end
  end

  describe '#changelog' do
    context 'with a linear git history' do
      before(:each) do
        create_simple_fixtures
      end

      it 'returns the first line of each commit messages in a markdown list' do
        valr = Valr::Repo.new repo_path
        expect(valr.changelog).to eq "- 3rd commit\n- 2nd commit\n- first commit"
      end

      context 'when asked for a commit range' do
        it 'returns only the messages of commits in the range' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog range: 'HEAD~2..HEAD').to eq ["- 3rd commit", "- 2nd commit"].join "\n"
        end

        it 'returns an error if range is not valid' do
          valr = Valr::Repo.new repo_path
          expect{valr.changelog range: 'Plop..Bla'}.to raise_error Valr::NotValidRangeError, "'Plop..Bla' is not a valid range"
        end
      end
    end

    context 'with a git history containing branches and merge' do
      before(:each) do
        create_repo_from 'with_branch'
      end

      context 'when asked for all commits' do
        it 'returns first line of each commit messages in a markdown list' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog).to eq "- commit\n- merge commit\n- feature commit 2\n- feature commit 1\n- first commit"
        end

        it 'returns first line of each commit messages including in the range' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog range: 'c85250a..HEAD').to eq "- commit\n- merge commit\n- feature commit 2\n- feature commit 1"
        end
      end

      context 'when asked for first parent commits' do
        it 'returns only messages for commits written in the branch' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog first_parent: true).to eq "- commit\n- merge commit\n- first commit"
        end

        it 'returns only messages for commits written in the branch and in the range' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog first_parent: true, range: 'HEAD^^..HEAD').to eq ['- commit', '- merge commit'].join "\n"
          expect(valr.changelog first_parent: true, range: 'HEAD~1..HEAD').to eq '- commit'<commit_msg>[#13] Add test for branch switch<commit_after>require 'valr'

describe Valr do
  after(:each) do
    clear_fixtures
  end

  describe '#new' do
    context 'without a git repository' do
      before(:each) do
        create_non_repository
      end

      it 'raise an error' do
        expect{Valr::Repo.new repo_path}.to raise_error Valr::RepositoryError, "'#{repo_path}' is not a git repository"
      end
    end

    context 'with an empty repository' do
      before(:each) do
        create_empty_repository
      end

      it 'raise an error' do
        expect{Valr::Repo.new repo_path}.to raise_error Valr::EmptyRepositoryError, "'#{repo_path}' is empty"
      end
    end

    context 'with a valid git repository' do
      before(:each) do
        create_simple_fixtures
      end

      it 'creates an instance of Valr::Repo' do
        expect{Valr::Repo.new repo_path}.not_to raise_error
      end
    end
  end

  describe '#changelog' do
    context 'with a linear git history' do
      before(:each) do
        create_simple_fixtures
      end

      it 'returns the first line of each commit messages in a markdown list' do
        valr = Valr::Repo.new repo_path
        expect(valr.changelog).to eq "- 3rd commit\n- 2nd commit\n- first commit"
      end

      context 'when asked for a commit range' do
        it 'returns only the messages of commits in the range' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog range: 'HEAD~2..HEAD').to eq ["- 3rd commit", "- 2nd commit"].join "\n"
        end

        it 'returns an error if range is not valid' do
          valr = Valr::Repo.new repo_path
          expect{valr.changelog range: 'Plop..Bla'}.to raise_error Valr::NotValidRangeError, "'Plop..Bla' is not a valid range"
        end
      end
    end

    context 'with a git history containing branches and merge' do
      before(:each) do
        create_repo_from 'with_branch'
      end

      context 'when asked for all commits' do
        it 'returns first line of each commit messages in a markdown list' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog).to eq "- commit\n- merge commit\n- feature commit 2\n- feature commit 1\n- first commit"
        end

        it 'returns first line of each commit messages including in the range' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog range: 'c85250a..HEAD').to eq "- commit\n- merge commit\n- feature commit 2\n- feature commit 1"
        end
      end

      context 'when asked for first parent commits' do
        it 'returns only messages for commits written in the branch' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog first_parent: true).to eq "- commit\n- merge commit\n- first commit"
        end

        it 'returns only messages for commits written in the branch and in the range' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog first_parent: true, range: 'HEAD^^..HEAD').to eq ['- commit', '- merge commit'].join "\n"
          expect(valr.changelog first_parent: true, range: 'HEAD~1..HEAD').to eq '- commit'
        end
      end

      context 'when asked for commits in a branch' do
        it 'returns messages of commits in the branch and parents' do
          valr = Valr::Repo.new repo_path
          expect(valr.changelog branch: 'feature').to eq "- feature commit 2\n- feature commit 1\n- first commit"
{'fraction_non_alphanumeric': 0.08677811550151976, 'fraction_numerical': 0.005167173252279635, 'mean_word_length': 2.35765306122449, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4394382', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1933, 'n_words': 713}